fixtures/attribute_generators.rb
Lines
100%17 of 17 covered
Branches
—0 of 0 covered
Methods
—0 of 0 covered
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | # Quickdraw generators for attribute hashes, with words that reach the | |
| 4 | # escaping, reference and unsafe-name checks. | |
| 5 | 2 | module AttributeGenerators |
| 6 | 2 | G = Quickdraw::Generators |
| 7 | ||
| 8 | TEXT_WORDS = ( | |
| 9 | 2 | %w[javascript: JavaScript java script : : ∷ : : : j j j �] + |
| 10 | %w[&tab; 	 &newline; 
 	 
 & " & # ; " ' < > / = _ - j J] + | |
| 11 | " \t\n\r\0\fİKſ".chars | |
| 12 | ).freeze | |
| 13 | ||
| 14 | NAME_WORDS = ( | |
| 15 | 2 | %w[on ON click href Href src xlink xlink:href formaction srcdoc sandbox http-equiv http equiv] + |
| 16 | %w[id ID data aria style class title _ - : " ' = < > /] + | |
| 17 | " \t\0İK".chars | |
| 18 | ).freeze | |
| 19 | ||
| 20 | 2 | TEXT = G.one_of(G.string(:ascii, dictionary: TEXT_WORDS), G.string(dictionary: TEXT_WORDS)) |
| 21 | 2 | NAME = G.string(:ascii, length: 0..10, dictionary: NAME_WORDS) |
| 22 | 2 | KEY = G.one_of(NAME.map(&:to_sym), NAME, :_) |
| 23 | ||
| 24 | 2 | SCALAR = G.one_of( |
| 25 | TEXT, | |
| 26 | NAME.map(&:to_sym), | |
| 27 | G.integer, | |
| 28 | G.float, | |
| 29 | true, | |
| 30 | false, | |
| 31 | nil, | |
| 32 | 81 | TEXT.map { |text| Phlex::SGML::SafeValue.new(text) }, |
| 33 | ) | |
| 34 | ||
| 35 | 2 | TOKENS = G.array(G.one_of(SCALAR, G.array(SCALAR, length: 0..3)), length: 0..4) |
| 36 | ||
| 37 | 2 | NESTED = G.hash_of(KEY, G.one_of(SCALAR, TOKENS, G.hash_of(KEY, SCALAR, length: 0..3)), length: 0..3) |
| 38 | ||
| 39 | 113 | VALUE = G.one_of(SCALAR, TOKENS, TOKENS.map { |tokens| Set.new(tokens) }, NESTED) |
| 40 | ||
| 41 | 2 | ATTRIBUTES = G.hash_of(KEY, VALUE, length: 0..4) |
| 42 | ||
| 43 | # Text that isn't valid UTF-8 or isn't ASCII-compatible, which some string | |
| 44 | # operations refuse. | |
| 45 | 2 | ENCODED_TEXT = G.one_of( |
| 46 | 8 | TEXT.map { |text| text.encode(Encoding::UTF_16LE) }, |
| 47 | 8 | TEXT.map { |text| (text.b << "\xFF".b).force_encoding(Encoding::UTF_8) }, |
| 48 | 8 | TEXT.map { |text| text.b << "\xFF".b }, |
| 49 | ) | |
| 50 | end |