lobzique 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/Cargo.lock +520 -0
  4. data/Cargo.toml +11 -0
  5. data/LICENSE-NOKOGIRI.md +15 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +144 -0
  8. data/crates/lobzique-core/Cargo.toml +14 -0
  9. data/crates/lobzique-core/src/css/mod.rs +127 -0
  10. data/crates/lobzique-core/src/css/parser.rs +805 -0
  11. data/crates/lobzique-core/src/css/tables.rs +282 -0
  12. data/crates/lobzique-core/src/css/tests.rs +476 -0
  13. data/crates/lobzique-core/src/css/tokenizer.rs +215 -0
  14. data/crates/lobzique-core/src/css/translate.rs +806 -0
  15. data/crates/lobzique-core/src/css/write.rs +370 -0
  16. data/crates/lobzique-core/src/dom/attribute.rs +144 -0
  17. data/crates/lobzique-core/src/dom/check.rs +160 -0
  18. data/crates/lobzique-core/src/dom/copy.rs +229 -0
  19. data/crates/lobzique-core/src/dom/mod.rs +298 -0
  20. data/crates/lobzique-core/src/dom/namespace.rs +134 -0
  21. data/crates/lobzique-core/src/dom/node.rs +129 -0
  22. data/crates/lobzique-core/src/dom/path.rs +116 -0
  23. data/crates/lobzique-core/src/dom/reparent.rs +308 -0
  24. data/crates/lobzique-core/src/dom/tests.rs +1057 -0
  25. data/crates/lobzique-core/src/dom/text.rs +159 -0
  26. data/crates/lobzique-core/src/dom/tree.rs +446 -0
  27. data/crates/lobzique-core/src/html5/mod.rs +28 -0
  28. data/crates/lobzique-core/src/html5/parse.rs +838 -0
  29. data/crates/lobzique-core/src/html5/serialize.rs +181 -0
  30. data/crates/lobzique-core/src/html5/tests.rs +548 -0
  31. data/crates/lobzique-core/src/lib.rs +12 -0
  32. data/crates/lobzique-core/src/work.rs +26 -0
  33. data/crates/lobzique-core/src/xml/mod.rs +8 -0
  34. data/crates/lobzique-core/src/xml/serialize.rs +681 -0
  35. data/crates/lobzique-core/src/xml/tests.rs +174 -0
  36. data/crates/lobzique-core/src/xpath/ast.rs +415 -0
  37. data/crates/lobzique-core/src/xpath/chars.rs +139 -0
  38. data/crates/lobzique-core/src/xpath/eval.rs +1305 -0
  39. data/crates/lobzique-core/src/xpath/mod.rs +118 -0
  40. data/crates/lobzique-core/src/xpath/parse.rs +729 -0
  41. data/crates/lobzique-core/src/xpath/tests.rs +921 -0
  42. data/ext/lobzique/Cargo.toml +22 -0
  43. data/ext/lobzique/build.rs +5 -0
  44. data/ext/lobzique/clippy.toml +15 -0
  45. data/ext/lobzique/extconf.rb +6 -0
  46. data/ext/lobzique/src/css/mod.rs +9 -0
  47. data/ext/lobzique/src/css/parser.rs +44 -0
  48. data/ext/lobzique/src/css/selector.rs +101 -0
  49. data/ext/lobzique/src/html4/document.rs +31 -0
  50. data/ext/lobzique/src/html4/mod.rs +7 -0
  51. data/ext/lobzique/src/html5/mod.rs +16 -0
  52. data/ext/lobzique/src/html5ever.rs +278 -0
  53. data/ext/lobzique/src/lib.rs +45 -0
  54. data/ext/lobzique/src/support.rs +156 -0
  55. data/ext/lobzique/src/xml/attr.rs +27 -0
  56. data/ext/lobzique/src/xml/cdata.rs +33 -0
  57. data/ext/lobzique/src/xml/comment.rs +34 -0
  58. data/ext/lobzique/src/xml/document.rs +199 -0
  59. data/ext/lobzique/src/xml/document_fragment.rs +27 -0
  60. data/ext/lobzique/src/xml/dtd.rs +37 -0
  61. data/ext/lobzique/src/xml/mod.rs +44 -0
  62. data/ext/lobzique/src/xml/namespace.rs +56 -0
  63. data/ext/lobzique/src/xml/node.rs +944 -0
  64. data/ext/lobzique/src/xml/node_set.rs +20 -0
  65. data/ext/lobzique/src/xml/syntax_error.rs +33 -0
  66. data/ext/lobzique/src/xml/text.rs +44 -0
  67. data/ext/lobzique/src/xml/xpath_context.rs +305 -0
  68. data/lib/lobzique/action_text.rb +29 -0
  69. data/lib/lobzique/class_resolver.rb +33 -0
  70. data/lib/lobzique/css/selector_cache.rb +36 -0
  71. data/lib/lobzique/css/syntax_error.rb +12 -0
  72. data/lib/lobzique/css/xpath_visitor.rb +60 -0
  73. data/lib/lobzique/css.rb +57 -0
  74. data/lib/lobzique/extension.rb +31 -0
  75. data/lib/lobzique/html.rb +14 -0
  76. data/lib/lobzique/html4/document.rb +235 -0
  77. data/lib/lobzique/html4/document_fragment.rb +75 -0
  78. data/lib/lobzique/html4/encoding_reader.rb +374 -0
  79. data/lib/lobzique/html4.rb +35 -0
  80. data/lib/lobzique/html5/document.rb +106 -0
  81. data/lib/lobzique/html5/document_fragment.rb +113 -0
  82. data/lib/lobzique/html5/node.rb +66 -0
  83. data/lib/lobzique/html5.rb +92 -0
  84. data/lib/lobzique/html5ever.rb +14 -0
  85. data/lib/lobzique/rails_dom_testing.rb +95 -0
  86. data/lib/lobzique/sanitizer/concerns.rb +110 -0
  87. data/lib/lobzique/sanitizer/document.rb +14 -0
  88. data/lib/lobzique/sanitizer/document_fragment.rb +12 -0
  89. data/lib/lobzique/sanitizer/escape_raw_text_in_select.rb +31 -0
  90. data/lib/lobzique/sanitizer/scrubbers.rb +46 -0
  91. data/lib/lobzique/sanitizer.rb +96 -0
  92. data/lib/lobzique/syntax_error.rb +8 -0
  93. data/lib/lobzique/version.rb +6 -0
  94. data/lib/lobzique/xml/attr.rb +23 -0
  95. data/lib/lobzique/xml/cdata.rb +13 -0
  96. data/lib/lobzique/xml/character_data.rb +11 -0
  97. data/lib/lobzique/xml/document.rb +149 -0
  98. data/lib/lobzique/xml/document_fragment.rb +109 -0
  99. data/lib/lobzique/xml/dtd.rb +28 -0
  100. data/lib/lobzique/xml/namespace.rb +19 -0
  101. data/lib/lobzique/xml/node/save_options.rb +52 -0
  102. data/lib/lobzique/xml/node.rb +629 -0
  103. data/lib/lobzique/xml/node_set.rb +329 -0
  104. data/lib/lobzique/xml/parse_options.rb +89 -0
  105. data/lib/lobzique/xml/pp/character_data.rb +22 -0
  106. data/lib/lobzique/xml/pp/node.rb +73 -0
  107. data/lib/lobzique/xml/pp.rb +6 -0
  108. data/lib/lobzique/xml/searchable.rb +123 -0
  109. data/lib/lobzique/xml/syntax_error.rb +77 -0
  110. data/lib/lobzique/xml/text.rb +14 -0
  111. data/lib/lobzique/xml/xpath/syntax_error.rb +20 -0
  112. data/lib/lobzique/xml/xpath.rb +16 -0
  113. data/lib/lobzique/xml/xpath_context.rb +27 -0
  114. data/lib/lobzique/xml.rb +22 -0
  115. data/lib/lobzique.rb +15 -0
  116. metadata +177 -0
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/html4.rb (MIT, see
4
+ # LICENSE-NOKOGIRI.md). The HTML4 names keep the hierarchy of Nokogiri but parse
5
+ # and serialize with the HTML5 parser (plan.md decision 3).
6
+
7
+ module Lobzique
8
+ class << self
9
+ # Convenience method for Nokogiri::HTML4::Document.parse
10
+ def HTML4(...)
11
+ HTML4::Document.parse(...)
12
+ end
13
+ end
14
+
15
+ module HTML4
16
+ class << self
17
+ # Convenience method for Nokogiri::HTML4::Document.parse
18
+ def parse(...)
19
+ Document.parse(...)
20
+ end
21
+
22
+ # Convenience method for Nokogiri::HTML4::DocumentFragment.parse
23
+ def fragment(...)
24
+ HTML4::DocumentFragment.parse(...)
25
+ end
26
+ end
27
+
28
+ # lobzique: NamedCharacters (EntityLookup), SAX and ElementDescription
29
+ # are left out: they read the HTML4 tables of libxml2, or are SAX.
30
+ end
31
+ end
32
+
33
+ require_relative "html4/document"
34
+ require_relative "html4/document_fragment"
35
+ require_relative "html4/encoding_reader"
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/html5/document.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ require_relative "../html4/document"
6
+
7
+ module Lobzique
8
+ module HTML5
9
+ module QuirksMode
10
+ NO_QUIRKS = 0
11
+ QUIRKS = 1
12
+ LIMITED_QUIRKS = 2
13
+ end
14
+
15
+ class Document < HTML4::Document
16
+ attr_reader :url
17
+ attr_reader :quirks_mode
18
+
19
+ # Whether the parser treated the content of +noscript+ elements as text, or +nil+ before the
20
+ # first parse. Fragments parsed into this document must match it, because the serializer emits
21
+ # +noscript+ text literally only when this is true.
22
+ attr_accessor :parse_noscript_content_as_text # :nodoc:
23
+
24
+ class << self
25
+ # Parses with the algorithm of the HTML standard. The block, if given,
26
+ # gets the options Hash. `url:` and `encoding:` cannot be set there.
27
+ def parse(
28
+ string_or_io,
29
+ url_ = nil, encoding_ = nil,
30
+ url: url_, encoding: encoding_,
31
+ **options, &block
32
+ )
33
+ yield options if block
34
+ string_or_io ||= ""
35
+
36
+ if string_or_io.respond_to?(:encoding) && string_or_io.encoding != Encoding::ASCII_8BIT
37
+ encoding ||= string_or_io.encoding.name
38
+ end
39
+
40
+ if string_or_io.respond_to?(:read) && string_or_io.respond_to?(:path)
41
+ url ||= string_or_io.path
42
+ end
43
+ unless string_or_io.respond_to?(:read) || string_or_io.respond_to?(:to_str)
44
+ raise ArgumentError, "not a string or IO object"
45
+ end
46
+
47
+ do_parse(string_or_io, url, encoding, **options)
48
+ end
49
+
50
+ def read_io(io, url_ = nil, encoding_ = nil, url: url_, encoding: encoding_, **options)
51
+ raise ArgumentError, "io object doesn't respond to :read" unless io.respond_to?(:read)
52
+
53
+ do_parse(io, url, encoding, **options)
54
+ end
55
+
56
+ def read_memory(string, url_ = nil, encoding_ = nil, url: url_, encoding: encoding_, **options)
57
+ raise ArgumentError, "string object doesn't respond to :to_str" unless string.respond_to?(:to_str)
58
+
59
+ do_parse(string, url, encoding, **options)
60
+ end
61
+
62
+ private
63
+
64
+ def do_parse(string_or_io, url, encoding, **options)
65
+ string = HTML5.read_and_encode(string_or_io, encoding)
66
+
67
+ # lobzique: Html5ever is the Gumbo of Nokogiri, renamed after
68
+ # the engine.
69
+ options[:max_attributes] ||= Html5ever::DEFAULT_MAX_ATTRIBUTES
70
+ options[:max_errors] ||= options.delete(:max_parse_errors) || Html5ever::DEFAULT_MAX_ERRORS
71
+ options[:max_tree_depth] ||= Html5ever::DEFAULT_MAX_TREE_DEPTH
72
+
73
+ noscript_content_as_text = options[:parse_noscript_content_as_text] ? true : false
74
+
75
+ doc = Html5ever.parse(string, url, self, **options)
76
+ doc.encoding = "UTF-8"
77
+ doc.parse_noscript_content_as_text = noscript_content_as_text
78
+ doc
79
+ end
80
+ end
81
+
82
+ def initialize(*args) # :nodoc:
83
+ super
84
+ @url = nil
85
+ @quirks_mode = nil
86
+ @parse_noscript_content_as_text = nil
87
+ end
88
+
89
+ # Parses markup in the context of `<body>`.
90
+ def fragment(markup = nil)
91
+ DocumentFragment.new(self, markup)
92
+ end
93
+
94
+ def to_xml(options = {}, &block) # :nodoc:
95
+ # Bypass XML::Document#to_xml which doesn't add
96
+ # XML::Node::SaveOptions::AS_XML like XML::Node#to_xml does.
97
+ XML::Node.instance_method(:to_xml).bind_call(self, options, &block)
98
+ end
99
+
100
+ # lobzique: CSS::XPathVisitor::DoctypeConfig::HTML5 in Nokogiri.
101
+ def xpath_doctype
102
+ :html5
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/html5/document_fragment.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ require_relative "../html4/document_fragment"
6
+
7
+ module Lobzique
8
+ module HTML5
9
+ class DocumentFragment < HTML4::DocumentFragment
10
+ class << self
11
+ # Parses in a new HTML5::Document. Takes `context:` among the options.
12
+ def parse(
13
+ input,
14
+ encoding_ = nil, positional_options_hash = nil,
15
+ encoding: encoding_, **options
16
+ )
17
+ unless positional_options_hash.nil? || positional_options_hash.empty?
18
+ options.merge!(positional_options_hash)
19
+ end
20
+
21
+ context = options.delete(:context)
22
+
23
+ document = HTML5::Document.new
24
+ document.encoding = "UTF-8"
25
+ input = HTML5.read_and_encode(input, encoding)
26
+
27
+ new(document, input, context, options)
28
+ end
29
+ end
30
+
31
+ # Ruby attributes, not the native `Node#document`: Loofah and others
32
+ # build a fragment with `new(document, input)` and read them back.
33
+ attr_accessor :document
34
+ attr_accessor :errors
35
+
36
+ attr_reader :quirks_mode
37
+
38
+ def initialize(
39
+ doc, input = nil,
40
+ context_ = nil, positional_options_hash = nil,
41
+ context: context_,
42
+ **options
43
+ ) # rubocop:disable Lint/MissingSuper
44
+ unless positional_options_hash.nil? || positional_options_hash.empty?
45
+ options.merge!(positional_options_hash)
46
+ end
47
+
48
+ @document = doc
49
+ @errors = []
50
+ return self unless input # rubocop:disable Lint/ReturnInVoidContext
51
+
52
+ input = HTML5.read_and_encode(input, nil)
53
+
54
+ context = options.delete(:context) if options.key?(:context)
55
+
56
+ # lobzique: Html5ever is the Gumbo of Nokogiri, renamed after
57
+ # the engine.
58
+ options[:max_attributes] ||= Html5ever::DEFAULT_MAX_ATTRIBUTES
59
+ options[:max_errors] ||= options.delete(:max_parse_errors) || Html5ever::DEFAULT_MAX_ERRORS
60
+ options[:max_tree_depth] ||= Html5ever::DEFAULT_MAX_TREE_DEPTH
61
+ noscript_content_as_text = options[:parse_noscript_content_as_text] = noscript_parsing_mode(doc, options)
62
+
63
+ Html5ever.fragment(self, input, context, **options)
64
+ if doc.is_a?(HTML5::Document) && doc.parse_noscript_content_as_text.nil? # lobzique: as below
65
+ doc.parse_noscript_content_as_text = noscript_content_as_text
66
+ end
67
+ end
68
+
69
+ private def noscript_parsing_mode(doc, options)
70
+ # lobzique: HTML4 documents reach here too. They serialize noscript
71
+ # content as markup.
72
+ doc_mode = doc.is_a?(HTML5::Document) ? doc.parse_noscript_content_as_text : false
73
+ mode = options.fetch(:parse_noscript_content_as_text, doc_mode) ? true : false
74
+
75
+ if !doc_mode.nil? && mode != doc_mode
76
+ raise ArgumentError, "parse_noscript_content_as_text: #{mode} does not match the document's value #{doc_mode}"
77
+ end
78
+
79
+ mode
80
+ end
81
+
82
+ def serialize(options = {}, &block) # :nodoc:
83
+ # Bypass XML::Document.serialize which doesn't support options even
84
+ # though we might be passed options (like :preserve_newline)
85
+ XML::Node.instance_method(:serialize).bind_call(self, options, &block)
86
+ end
87
+
88
+ def extract_params(params) # :nodoc:
89
+ handler = params.find do |param|
90
+ # lobzique: as in Searchable#extract_params
91
+ ![Hash, String, Symbol, CSS::Selector].include?(param.class)
92
+ end
93
+ params -= [handler] if handler
94
+
95
+ hashes = []
96
+ while Hash === params.last || params.last.nil?
97
+ hashes << params.pop
98
+ break if params.empty?
99
+ end
100
+ ns, binds = hashes.reverse
101
+
102
+ ns ||=
103
+ begin
104
+ ns = {}
105
+ children.each { |child| ns.merge!(child.namespaces) }
106
+ ns
107
+ end
108
+
109
+ [params, handler, ns, binds]
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/html5/node.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ require_relative "../xml/node"
6
+
7
+ module Lobzique
8
+ module HTML5
9
+ # Overrides prepended into every node class, active in HTML documents.
10
+ module Node
11
+ def inner_html(options = {})
12
+ # lobzique: Nokogiri checks for HTML5::Document. HTML4 documents use
13
+ # the HTML5 serializer too (plan.md decision 3).
14
+ return super unless document.is_a?(HTML4::Document)
15
+
16
+ result = (options[:preserve_newline] && prepend_newline?) ? +"\n" : +""
17
+ result << children.map { |child| child.to_html(options) }.join
18
+ result
19
+ end
20
+
21
+ def write_to(io, *options)
22
+ return super unless document.is_a?(HTML4::Document) # lobzique: as in #inner_html
23
+
24
+ options = options.first.is_a?(Hash) ? options.shift : {}
25
+ encoding = options[:encoding] || options[0]
26
+ save_options = options[:save_with] || options[1] || XML::Node::SaveOptions::FORMAT
27
+ indent_times = options[:indent] || 2
28
+ indent_string = (options[:indent_text] || " ") * indent_times
29
+
30
+ config = XML::Node::SaveOptions.new(save_options.to_i)
31
+ yield config if block_given?
32
+
33
+ encoding = encoding.is_a?(Encoding) ? encoding.name : encoding
34
+
35
+ config_options = config.options
36
+ if config_options & (XML::Node::SaveOptions::AS_XML | XML::Node::SaveOptions::AS_XHTML) != 0
37
+ native_write_to(io, encoding, indent_string, config_options)
38
+ else
39
+ # lobzique: HTML4 documents have no parse_noscript_content_as_text.
40
+ # They parse noscript content as markup.
41
+ scripting_enabled = document.is_a?(HTML5::Document) && document.parse_noscript_content_as_text
42
+ html = html_standard_serialize(options[:preserve_newline] || false, scripting_enabled)
43
+ encoding ||= document.encoding || Encoding::UTF_8
44
+ io << html.encode(encoding, fallback: lambda { |c| "&#x#{c.ord.to_s(16)};" })
45
+ end
46
+ end
47
+
48
+ def fragment(tags)
49
+ return super unless document.is_a?(HTML5::Document)
50
+
51
+ DocumentFragment.new(document, tags, self)
52
+ end
53
+
54
+ private
55
+
56
+ # HTML5 documents keep a prefixed attribute's name as parsed.
57
+ def add_child_node_and_reparent_attrs(node)
58
+ return super unless document.is_a?(HTML4::Document) # lobzique: as in #inner_html
59
+
60
+ add_child_node(node)
61
+ end
62
+ end
63
+
64
+ XML::Node.prepend(HTML5::Node)
65
+ end
66
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/html5.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ require_relative "html5/document"
6
+ require_relative "html5/document_fragment"
7
+ require_relative "html5/node"
8
+
9
+ module Lobzique
10
+ def self.HTML5(...)
11
+ HTML5::Document.parse(...)
12
+ end
13
+
14
+ module HTML5
15
+ class << self
16
+ def parse(...)
17
+ Document.parse(...)
18
+ end
19
+
20
+ def fragment(...)
21
+ DocumentFragment.parse(...)
22
+ end
23
+
24
+ # Reads an IO or String and returns it as UTF-8, which the parser takes.
25
+ def read_and_encode(string, encoding)
26
+ if string.respond_to?(:read)
27
+ string = if encoding.nil?
28
+ string.read
29
+ else
30
+ string.read(encoding: encoding)
31
+ end
32
+ else
33
+ string = string.to_s
34
+ if encoding
35
+ string = string.dup
36
+ string.force_encoding(encoding)
37
+ end
38
+ end
39
+
40
+ if string.encoding != Encoding::UTF_8
41
+ string = reencode(string)
42
+ end
43
+ string
44
+ end
45
+
46
+ private
47
+
48
+ # The encoding of a binary string comes from its byte order mark,
49
+ # the content type, or a `<meta charset>` in its first 1024 bytes.
50
+ # The fallback is ISO-8859-1.
51
+ def reencode(body, content_type = nil)
52
+ if body.encoding == Encoding::ASCII_8BIT
53
+ encoding = nil
54
+
55
+ initial_bytes = body[0..2].bytes
56
+ if initial_bytes[0..2] == [0xEF, 0xBB, 0xBF]
57
+ encoding = Encoding::UTF_8
58
+ elsif initial_bytes[0..1] == [0xFE, 0xFF]
59
+ encoding = Encoding::UTF_16BE
60
+ elsif initial_bytes[0..1] == [0xFF, 0xFE]
61
+ encoding = Encoding::UTF_16LE
62
+ end
63
+
64
+ if content_type
65
+ encoding ||= content_type[/charset=["']?(.*?)($|["';\s])/i, 1]
66
+ end
67
+
68
+ unless encoding
69
+ data = body[0..1023].gsub(/<!--.*?(-->|\Z)/m, "")
70
+ data.scan(/<meta.*?>/im).each do |meta|
71
+ encoding ||= meta[/charset=["']?([^>]*?)($|["'\s>])/im, 1]
72
+ end
73
+ end
74
+
75
+ encoding ||= Encoding::ISO_8859_1
76
+
77
+ body = body.dup
78
+ begin
79
+ body.force_encoding(encoding)
80
+ rescue ArgumentError
81
+ body.force_encoding(Encoding::ISO_8859_1)
82
+ end
83
+ end
84
+
85
+ # lobzique: Nokogiri raises on a byte that the encoding does not have.
86
+ body.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ require_relative "html5ever" # lobzique: Nokogiri's "gumbo"
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/gumbo.rb (MIT, see
4
+ # LICENSE-NOKOGIRI.md). lobzique: renamed from Gumbo after the engine
5
+ # that it binds (ARCHITECTURE.md, departures).
6
+
7
+ module Lobzique
8
+ module Html5ever
9
+ DEFAULT_MAX_ATTRIBUTES = 400
10
+ # Nokogiri::HTML5 keeps no parse errors unless asked to
11
+ DEFAULT_MAX_ERRORS = 0
12
+ DEFAULT_MAX_TREE_DEPTH = 400
13
+ end
14
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rails-dom-testing on lobzique (plan.md decision 1, seam mode):
4
+ #
5
+ # require "lobzique/rails_dom_testing"
6
+ #
7
+ # Wherever rails-dom-testing picks the classes of Nokogiri::HTML5, it gets
8
+ # the classes of lobzique instead: assert_dom, assert_dom_equal, `rendered`
9
+ # of Action View, and html_document and parsed_body of Action Dispatch. :html4
10
+ # stays on Nokogiri (decision 3).
11
+ #
12
+ # HTMLSelector#filter and SelectorAssertions#nodeset build Nokogiri node sets.
13
+ # For lobzique nodes they build lobzique node sets (filter is ported
14
+ # from rails-dom-testing 2.3.0). The node sets of Nokogiri still use
15
+ # the original code.
16
+
17
+ require "lobzique"
18
+ require "rails-dom-testing"
19
+ require "rails/dom/testing/version"
20
+
21
+ module Lobzique
22
+ module RailsDomTesting
23
+ # filter is a private method whose code changed in 2.3.0.
24
+ REQUIREMENT = Gem::Requirement.new(">= 2.3.0", "< 2.4")
25
+
26
+ unless REQUIREMENT.satisfied_by?(Gem::Version.new(Rails::Dom::Testing::VERSION))
27
+ raise "#{self} needs rails-dom-testing #{REQUIREMENT}, not #{Rails::Dom::Testing::VERSION}"
28
+ end
29
+
30
+ module ParserSelection
31
+ def html_document(...)
32
+ parser = super
33
+ parser.equal?(::Nokogiri::HTML5::Document) ? HTML5::Document : parser
34
+ end
35
+
36
+ def html_document_fragment(...)
37
+ parser = super
38
+ parser.equal?(::Nokogiri::HTML5::DocumentFragment) ? HTML5::DocumentFragment : parser
39
+ end
40
+ end
41
+
42
+ module SelectorAssertions
43
+ private
44
+
45
+ def nodeset(node)
46
+ case node
47
+ when XML::NodeSet then node
48
+ when XML::Node then XML::NodeSet.new(node.document, [node])
49
+ else super
50
+ end
51
+ end
52
+ end
53
+
54
+ module HTMLSelector
55
+ NO_STRIP = Rails::Dom::Testing::Assertions::SelectorAssertions::HTMLSelector::NO_STRIP
56
+
57
+ private
58
+
59
+ # Ported from rails-dom-testing 2.3.0,
60
+ # lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb
61
+ def filter(matches)
62
+ return super unless matches.is_a?(XML::NodeSet)
63
+
64
+ match_with = tests[:text] || tests[:html]
65
+ return matches if matches.empty? || !match_with
66
+
67
+ content_mismatch = nil
68
+ text_matches = tests.has_key?(:text)
69
+ html_matches = tests.has_key?(:html)
70
+ regex_matching = match_with.is_a?(Regexp)
71
+
72
+ remaining = matches.reject do |match|
73
+ # Preserve markup with to_s for html elements
74
+ content = text_matches ? match.text : match.inner_html
75
+
76
+ content.strip! unless NO_STRIP.include?(match.name)
77
+ content.delete_prefix!("\n") if text_matches && match.name == "textarea"
78
+ collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || html_matches
79
+
80
+ next if regex_matching ? (content =~ match_with) : (content == match_with)
81
+ content_mismatch ||= diff(match_with, content)
82
+ true
83
+ end
84
+
85
+ @message ||= content_mismatch if remaining.empty?
86
+ # lobzique: the NodeSet of lobzique, not Nokogiri::XML::NodeSet.
87
+ XML::NodeSet.new(matches.document, remaining)
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ Rails::Dom::Testing.singleton_class.prepend(Lobzique::RailsDomTesting::ParserSelection)
94
+ Rails::Dom::Testing::Assertions::SelectorAssertions.prepend(Lobzique::RailsDomTesting::SelectorAssertions)
95
+ Rails::Dom::Testing::Assertions::SelectorAssertions::HTMLSelector.prepend(Lobzique::RailsDomTesting::HTMLSelector)
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Loofah 2.25.3, lib/loofah/concerns.rb (MIT).
4
+
5
+ module Lobzique
6
+ module Sanitizer
7
+ module ScrubBehavior
8
+ module Node # :nodoc:
9
+ def scrub!(scrubber)
10
+ scrubber = ScrubBehavior.resolve_scrubber(scrubber)
11
+ # lobzique: the case of Loofah names the classes of Nokogiri,
12
+ # so lobzique documents and fragments would fall through to else.
13
+ case self
14
+ when XML::Document
15
+ scrubber.traverse(root) if root
16
+ when XML::DocumentFragment
17
+ # lobzique: traversed here, not by children.scrub!, so the fragment
18
+ # is escaped once.
19
+ children.each { |node| scrubber.traverse(node) }
20
+ else
21
+ scrubber.traverse(self)
22
+ end
23
+ # lobzique: escape_raw_text_in_select.rb
24
+ EscapeRawTextInSelect.call(self)
25
+ self
26
+ end
27
+ end
28
+
29
+ module NodeSet # :nodoc:
30
+ def scrub!(scrubber)
31
+ each { |node| node.scrub!(scrubber) }
32
+ self
33
+ end
34
+ end
35
+
36
+ class << self
37
+ def resolve_scrubber(scrubber) # :nodoc:
38
+ # lobzique: the Loofah scrubbers that build Nokogiri nodes, by name
39
+ # or by instance, resolve to ports (scrubbers.rb).
40
+ klass = Scrubbers::MAP[scrubber] || Loofah::Scrubbers::MAP[scrubber] || Scrubbers::PORTS[scrubber.class]
41
+ scrubber = klass.new if klass
42
+ unless scrubber.is_a?(Loofah::Scrubber)
43
+ raise Loofah::ScrubberNotFound, "not a Scrubber or a scrubber name: #{scrubber.inspect}"
44
+ end
45
+
46
+ scrubber
47
+ end
48
+ end
49
+ end
50
+
51
+ # lobzique: TextBehavior (text, to_text) is not ported yet. The sanitizers
52
+ # of Rails do not call it.
53
+
54
+ module DocumentDecorator # :nodoc:
55
+ def initialize(*args, &block)
56
+ super
57
+ decorators(XML::Node) << ScrubBehavior::Node
58
+ decorators(XML::NodeSet) << ScrubBehavior::NodeSet
59
+ end
60
+ end
61
+
62
+ module HtmlDocumentBehavior # :nodoc:
63
+ # lobzique: ClassMethods#parse, which removes comments before the html
64
+ # element, is not ported yet. The sanitizers of Rails parse only
65
+ # fragments.
66
+
67
+ def serialize_root
68
+ at_xpath("/html/body")
69
+ end
70
+ end
71
+
72
+ module HtmlFragmentBehavior # :nodoc:
73
+ module ClassMethods
74
+ def parse(tags, encoding = nil)
75
+ doc = document_klass.new
76
+
77
+ encoding ||= tags.respond_to?(:encoding) ? tags.encoding.name : "UTF-8"
78
+ doc.encoding = encoding
79
+
80
+ new(doc, tags)
81
+ end
82
+
83
+ def document_klass
84
+ # lobzique: one class, HTML5 only
85
+ @document_klass ||= if self == Sanitizer::DocumentFragment
86
+ Sanitizer::Document
87
+ else
88
+ raise ArgumentError, "unexpected class: #{self}"
89
+ end
90
+ end
91
+ end
92
+
93
+ class << self
94
+ def included(base)
95
+ base.extend(ClassMethods)
96
+ end
97
+ end
98
+
99
+ def to_s
100
+ serialize_root.children.to_s
101
+ end
102
+
103
+ alias_method :serialize, :to_s
104
+
105
+ def serialize_root
106
+ at_xpath("./body") || self
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Loofah 2.25.3, lib/loofah/html5/document.rb (MIT).
4
+
5
+ module Lobzique
6
+ module Sanitizer
7
+ class Document < HTML5::Document
8
+ include ScrubBehavior::Node
9
+ include DocumentDecorator
10
+ # lobzique: TextBehavior isn't ported yet (concerns.rb)
11
+ include HtmlDocumentBehavior
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Loofah 2.25.3, lib/loofah/html5/document_fragment.rb (MIT).
4
+
5
+ module Lobzique
6
+ module Sanitizer
7
+ class DocumentFragment < HTML5::DocumentFragment
8
+ # lobzique: TextBehavior isn't ported yet (concerns.rb)
9
+ include HtmlFragmentBehavior
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lobzique
4
+ module Sanitizer
5
+ # html5ever parses <select> as the HTML standard has since 2025
6
+ # (customizable select). It keeps raw-text elements inside
7
+ # it, and serializes their text unescaped. Browsers on the older rules,
8
+ # and gumbo, ignore those start tags and parse the text as markup. So scrub!
9
+ # escapes it, as Loofah escapes the CDATA of libxml2. This lasts
10
+ # until Nokogiri adopts the new rules (sparklemotion/nokogiri#3317)
11
+ # and Loofah handles them.
12
+ module EscapeRawTextInSelect
13
+ RAW_TEXT = %w[iframe noembed noframes plaintext style xmp].freeze
14
+
15
+ # Unprefixed names match only HTML elements. Escaped text has no
16
+ # "<" or ">" left, so scrubbing a tree again changes nothing
17
+ # (the fragments of Loofah scrub each child, and callers chain scrub!).
18
+ XPATH = "descendant-or-self::select//*[#{RAW_TEXT.map { |name| "self::#{name}" }.join(" or ")}]" \
19
+ "/text()[contains(., '<') or contains(., '>')]"
20
+
21
+ def self.call(root)
22
+ # Most trees have no select, and the short query costs a third of XPATH.
23
+ return if root.text? || !root.at_xpath("descendant-or-self::select")
24
+
25
+ root.xpath(XPATH).each do |text|
26
+ text.replace(text.document.create_text_node(Loofah::HTML5::Scrub.escape_tags(text.content)))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end