lobzique 0.1.0-x86_64-linux

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 (58) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE-NOKOGIRI.md +15 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +144 -0
  6. data/ext/lobzique/clippy.toml +15 -0
  7. data/lib/lobzique/3.3/lobzique.so +0 -0
  8. data/lib/lobzique/3.4/lobzique.so +0 -0
  9. data/lib/lobzique/4.0/lobzique.so +0 -0
  10. data/lib/lobzique/action_text.rb +29 -0
  11. data/lib/lobzique/class_resolver.rb +33 -0
  12. data/lib/lobzique/css/selector_cache.rb +36 -0
  13. data/lib/lobzique/css/syntax_error.rb +12 -0
  14. data/lib/lobzique/css/xpath_visitor.rb +60 -0
  15. data/lib/lobzique/css.rb +57 -0
  16. data/lib/lobzique/extension.rb +31 -0
  17. data/lib/lobzique/html.rb +14 -0
  18. data/lib/lobzique/html4/document.rb +235 -0
  19. data/lib/lobzique/html4/document_fragment.rb +75 -0
  20. data/lib/lobzique/html4/encoding_reader.rb +374 -0
  21. data/lib/lobzique/html4.rb +35 -0
  22. data/lib/lobzique/html5/document.rb +106 -0
  23. data/lib/lobzique/html5/document_fragment.rb +113 -0
  24. data/lib/lobzique/html5/node.rb +66 -0
  25. data/lib/lobzique/html5.rb +92 -0
  26. data/lib/lobzique/html5ever.rb +14 -0
  27. data/lib/lobzique/rails_dom_testing.rb +95 -0
  28. data/lib/lobzique/sanitizer/concerns.rb +110 -0
  29. data/lib/lobzique/sanitizer/document.rb +14 -0
  30. data/lib/lobzique/sanitizer/document_fragment.rb +12 -0
  31. data/lib/lobzique/sanitizer/escape_raw_text_in_select.rb +31 -0
  32. data/lib/lobzique/sanitizer/scrubbers.rb +46 -0
  33. data/lib/lobzique/sanitizer.rb +96 -0
  34. data/lib/lobzique/syntax_error.rb +8 -0
  35. data/lib/lobzique/version.rb +6 -0
  36. data/lib/lobzique/xml/attr.rb +23 -0
  37. data/lib/lobzique/xml/cdata.rb +13 -0
  38. data/lib/lobzique/xml/character_data.rb +11 -0
  39. data/lib/lobzique/xml/document.rb +149 -0
  40. data/lib/lobzique/xml/document_fragment.rb +109 -0
  41. data/lib/lobzique/xml/dtd.rb +28 -0
  42. data/lib/lobzique/xml/namespace.rb +19 -0
  43. data/lib/lobzique/xml/node/save_options.rb +52 -0
  44. data/lib/lobzique/xml/node.rb +629 -0
  45. data/lib/lobzique/xml/node_set.rb +329 -0
  46. data/lib/lobzique/xml/parse_options.rb +89 -0
  47. data/lib/lobzique/xml/pp/character_data.rb +22 -0
  48. data/lib/lobzique/xml/pp/node.rb +73 -0
  49. data/lib/lobzique/xml/pp.rb +6 -0
  50. data/lib/lobzique/xml/searchable.rb +123 -0
  51. data/lib/lobzique/xml/syntax_error.rb +77 -0
  52. data/lib/lobzique/xml/text.rb +14 -0
  53. data/lib/lobzique/xml/xpath/syntax_error.rb +20 -0
  54. data/lib/lobzique/xml/xpath.rb +16 -0
  55. data/lib/lobzique/xml/xpath_context.rb +27 -0
  56. data/lib/lobzique/xml.rb +22 -0
  57. data/lib/lobzique.rb +15 -0
  58. metadata +106 -0
@@ -0,0 +1,329 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/node_set.rb and the native
4
+ # methods of ext/nokogiri/xml_node_set.c (MIT, see LICENSE-NOKOGIRI.md).
5
+
6
+ module Lobzique
7
+ module XML
8
+ class NodeSet
9
+ include Searchable
10
+ include Enumerable
11
+
12
+ attr_accessor :document
13
+
14
+ # lobzique: Nokogiri implements the methods from here to #initialize in C
15
+ # over the xmlNodeSet of libxml2. Here they work on an Array, which the GC
16
+ # of Ruby already marks.
17
+
18
+ # Port of the C noko_xml_node_set_wrap of Nokogiri: a decorated set
19
+ # of +nodes+, which must be distinct, without #initialize or a duplicate
20
+ # check for each node. Native search results come through here.
21
+ def self.wrap(document, nodes) # :nodoc:
22
+ allocate.send(:adopt, document, nodes)
23
+ end
24
+
25
+ def length
26
+ @nodes.length
27
+ end
28
+
29
+ def push(node)
30
+ check_member(node)
31
+ @nodes << node unless @nodes.any? { |member| member.equal?(node) }
32
+ self
33
+ end
34
+
35
+ def delete(node)
36
+ check_member(node)
37
+ index = @nodes.index { |member| member.equal?(node) }
38
+ @nodes.delete_at(index) if index
39
+ end
40
+
41
+ def include?(node)
42
+ check_member(node)
43
+ @nodes.any? { |member| member.equal?(node) }
44
+ end
45
+
46
+ def [](*args)
47
+ result = @nodes[*args]
48
+ result.is_a?(Array) ? NodeSet.wrap(document, result) : result
49
+ end
50
+ alias_method :slice, :[]
51
+
52
+ def to_a
53
+ @nodes.dup
54
+ end
55
+
56
+ def &(other)
57
+ check_node_set(other)
58
+ members = identity_set(other.to_a)
59
+ NodeSet.wrap(document, @nodes.select { |node| members.key?(node) })
60
+ end
61
+
62
+ def -(other)
63
+ check_node_set(other)
64
+ members = identity_set(other.to_a)
65
+ NodeSet.wrap(document, @nodes.reject { |node| members.key?(node) })
66
+ end
67
+
68
+ def |(other)
69
+ check_node_set(other)
70
+ members = identity_set(@nodes)
71
+ NodeSet.wrap(document, @nodes + other.to_a.reject { |node| members.key?(node) })
72
+ end
73
+
74
+ def initialize_copy(other)
75
+ super
76
+ @nodes = other.to_a
77
+ document&.decorate(self)
78
+ end
79
+
80
+ def initialize(document, list = [])
81
+ @nodes = [] # lobzique: what libxml2's xmlNodeSet is in Nokogiri
82
+ @document = document
83
+ document.decorate(self)
84
+ list.each { |x| self << x }
85
+ yield self if block_given?
86
+ end
87
+
88
+ def first(n = nil)
89
+ return self[0] unless n
90
+
91
+ list = []
92
+ [n, length].min.times { |i| list << self[i] }
93
+ list
94
+ end
95
+
96
+ def last
97
+ self[-1]
98
+ end
99
+
100
+ def empty?
101
+ length == 0
102
+ end
103
+
104
+ def index(node = nil)
105
+ if node
106
+ warn("given block not used") if block_given?
107
+ each_with_index { |member, j| return j if member == node }
108
+ elsif block_given?
109
+ each_with_index { |member, j| return j if yield(member) }
110
+ end
111
+ nil
112
+ end
113
+
114
+ def before(datum)
115
+ first.before(datum)
116
+ end
117
+
118
+ def after(datum)
119
+ last.after(datum)
120
+ end
121
+
122
+ alias_method :<<, :push
123
+
124
+ def css(*args)
125
+ rules, handler, ns, _ = extract_params(args)
126
+ paths = css_rules_to_xpath(rules, ns)
127
+
128
+ inject(NodeSet.new(document)) do |set, node|
129
+ set + xpath_internal(node, paths, handler, ns, nil)
130
+ end
131
+ end
132
+
133
+ def xpath(*args)
134
+ paths, handler, ns, binds = extract_params(args)
135
+
136
+ inject(NodeSet.new(document)) do |set, node|
137
+ set + xpath_internal(node, paths, handler, ns, binds)
138
+ end
139
+ end
140
+
141
+ def at(*args)
142
+ if args.length == 1 && args.first.is_a?(Numeric)
143
+ return self[args.first]
144
+ end
145
+
146
+ super
147
+ end
148
+ alias_method :%, :at
149
+
150
+ def filter(expr)
151
+ find_all { |node| node.matches?(expr) }
152
+ end
153
+
154
+ # lobzique: native in Nokogiri, which also calls the #unlink of each node.
155
+ def unlink
156
+ each { |node| node.unlink unless node.is_a?(XML::Namespace) }
157
+ self
158
+ end
159
+ alias_method :remove, :unlink
160
+
161
+ def add_class(name)
162
+ each do |el|
163
+ el.add_class(name)
164
+ end
165
+ self
166
+ end
167
+
168
+ def append_class(name)
169
+ each do |el|
170
+ el.append_class(name)
171
+ end
172
+ self
173
+ end
174
+
175
+ def remove_class(name = nil)
176
+ each do |el|
177
+ el.remove_class(name)
178
+ end
179
+ self
180
+ end
181
+
182
+ # With a value, a Hash or a block, sets attributes on every node;
183
+ # otherwise the first node's attribute named +key+.
184
+ def attr(key, value = nil, &block)
185
+ setting = key.is_a?(Hash) || (key && (value || block))
186
+ return first&.attribute(key) unless setting
187
+
188
+ hash = key.is_a?(Hash) ? key : {key => value}
189
+
190
+ hash.each do |k, v|
191
+ each do |node|
192
+ node[k] = v || yield(node)
193
+ end
194
+ end
195
+
196
+ self
197
+ end
198
+ alias_method :set, :attr
199
+ alias_method :attribute, :attr
200
+
201
+ def remove_attr(name)
202
+ each { |el| el.delete(name) }
203
+ self
204
+ end
205
+ alias_method :remove_attribute, :remove_attr
206
+
207
+ def each
208
+ return to_enum unless block_given?
209
+
210
+ 0.upto(length - 1) do |x|
211
+ yield self[x]
212
+ end
213
+ self
214
+ end
215
+
216
+ alias_method :size, :length
217
+ alias_method :to_ary, :to_a
218
+
219
+ def pop
220
+ return if length == 0
221
+
222
+ delete(last)
223
+ end
224
+
225
+ def shift
226
+ return if length == 0
227
+
228
+ delete(first)
229
+ end
230
+
231
+ def ==(other)
232
+ return false unless other.is_a?(XML::NodeSet)
233
+ return false unless length == other.length
234
+
235
+ each_with_index do |node, i|
236
+ return false unless node == other[i]
237
+ end
238
+ true
239
+ end
240
+
241
+ def children
242
+ node_set = NodeSet.new(document)
243
+ each do |node|
244
+ node.children.each { |n| node_set.push(n) }
245
+ end
246
+ node_set
247
+ end
248
+
249
+ def reverse
250
+ node_set = NodeSet.new(document)
251
+ (length - 1).downto(0) do |x|
252
+ node_set.push(self[x])
253
+ end
254
+ node_set
255
+ end
256
+
257
+ def inspect
258
+ "[#{map(&:inspect).join(", ")}]"
259
+ end
260
+
261
+ alias_method :+, :|
262
+
263
+ def inner_text
264
+ collect(&:inner_text).join("")
265
+ end
266
+ alias_method :text, :inner_text
267
+
268
+ def inner_html(*args)
269
+ collect { |j| j.inner_html(*args) }.join("")
270
+ end
271
+
272
+ def wrap(node_or_tags)
273
+ map { |node| node.wrap(node_or_tags) }
274
+ self
275
+ end
276
+
277
+ def to_s
278
+ map(&:to_s).join # rubocop:disable Style/MapJoin -- a NodeSet has no #join
279
+ end
280
+
281
+ def to_html(*args)
282
+ if empty?
283
+ encoding = (args.first.is_a?(Hash) ? args.first[:encoding] : nil)
284
+ encoding ||= document.encoding
285
+ encoding.nil? ? "" : "".encode(encoding)
286
+ else
287
+ map { |x| x.to_html(*args) }.join
288
+ end
289
+ end
290
+
291
+ def to_xhtml(*args)
292
+ map { |x| x.to_xhtml(*args) }.join
293
+ end
294
+
295
+ def to_xml(*args)
296
+ map { |x| x.to_xml(*args) }.join
297
+ end
298
+
299
+ def deconstruct
300
+ to_a
301
+ end
302
+
303
+ IMPLIED_XPATH_CONTEXTS = [".//", "self::"].freeze # :nodoc:
304
+
305
+ private
306
+
307
+ def adopt(document, nodes)
308
+ @nodes = nodes
309
+ @document = document
310
+ document&.decorate(self)
311
+ self
312
+ end
313
+
314
+ def identity_set(nodes)
315
+ nodes.each_with_object({}.compare_by_identity) { |node, set| set[node] = true }
316
+ end
317
+
318
+ def check_member(node)
319
+ return if node.is_a?(XML::Node) || node.is_a?(XML::Namespace)
320
+
321
+ raise ArgumentError, "node must be a #{XML::Node} or #{XML::Namespace}"
322
+ end
323
+
324
+ def check_node_set(other)
325
+ raise ArgumentError, "node_set must be a #{XML::NodeSet}" unless other.is_a?(XML::NodeSet)
326
+ end
327
+ end
328
+ end
329
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/parse_options.rb (MIT, see
4
+ # LICENSE-NOKOGIRI.md). The parser flags of libxml2. The HTML5 parser ignores
5
+ # them, but `Node#parse` and the fragment initializers accept them and pass them
6
+ # on, as in Nokogiri.
7
+
8
+ module Lobzique
9
+ module XML
10
+ class ParseOptions
11
+ STRICT = 0
12
+ RECOVER = 1 << 0
13
+ NOENT = 1 << 1
14
+ DTDLOAD = 1 << 2
15
+ DTDATTR = 1 << 3
16
+ DTDVALID = 1 << 4
17
+ NOERROR = 1 << 5
18
+ NOWARNING = 1 << 6
19
+ PEDANTIC = 1 << 7
20
+ NOBLANKS = 1 << 8
21
+ SAX1 = 1 << 9
22
+ XINCLUDE = 1 << 10
23
+ NONET = 1 << 11
24
+ NODICT = 1 << 12
25
+ NSCLEAN = 1 << 13
26
+ NOCDATA = 1 << 14
27
+ NOXINCNODE = 1 << 15
28
+ COMPACT = 1 << 16
29
+ OLD10 = 1 << 17
30
+ NOBASEFIX = 1 << 18
31
+ HUGE = 1 << 19
32
+ BIG_LINES = 1 << 22
33
+
34
+ DEFAULT_XML = RECOVER | NONET | BIG_LINES
35
+ DEFAULT_XSLT = RECOVER | NONET | NOENT | DTDLOAD | DTDATTR | NOCDATA | BIG_LINES
36
+ DEFAULT_HTML = RECOVER | NOERROR | NOWARNING | NONET | BIG_LINES
37
+ DEFAULT_SCHEMA = NONET | BIG_LINES
38
+
39
+ attr_accessor :options
40
+
41
+ def initialize(options = STRICT)
42
+ @options = options
43
+ end
44
+
45
+ constants.each do |constant|
46
+ next if constant.to_sym == :STRICT
47
+
48
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
49
+ def #{constant.downcase}
50
+ @options |= #{constant}
51
+ self
52
+ end
53
+
54
+ def no#{constant.downcase}
55
+ @options &= ~#{constant}
56
+ self
57
+ end
58
+
59
+ def #{constant.downcase}?
60
+ #{constant} & @options == #{constant}
61
+ end
62
+ RUBY
63
+ end
64
+
65
+ def strict
66
+ @options &= ~RECOVER
67
+ self
68
+ end
69
+
70
+ def strict?
71
+ @options & RECOVER == STRICT
72
+ end
73
+
74
+ def ==(other)
75
+ other.to_i == to_i
76
+ end
77
+
78
+ alias_method :to_i, :options
79
+
80
+ def inspect
81
+ options = []
82
+ self.class.constants.each do |k|
83
+ options << k.downcase if send(:"#{k.downcase}?")
84
+ end
85
+ super.sub(/>$/, " " + options.join(", ") + ">")
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/pp/character_data.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ module Lobzique
6
+ module XML
7
+ module PP
8
+ module CharacterData
9
+ def pretty_print(pp)
10
+ nice_name = self.class.name.split("::").last
11
+ pp.group(2, "#(#{nice_name} ", ")") do
12
+ pp.pp(text)
13
+ end
14
+ end
15
+
16
+ def inspect
17
+ "#<#{self.class.name}:#{format("0x%x", object_id)} #{text.inspect}>"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/pp/node.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ module Lobzique
6
+ module XML
7
+ module PP
8
+ module Node
9
+ COLLECTIONS = [:attribute_nodes, :children]
10
+
11
+ def inspect
12
+ if respond_to?(:data_ptr?) && !data_ptr?
13
+ return "#<#{self.class}:#{format("0x%x", object_id)} (no data)>"
14
+ end
15
+
16
+ attributes = inspect_attributes.reject do |x|
17
+ attribute = send(x)
18
+ !attribute || (attribute.respond_to?(:empty?) && attribute.empty?)
19
+ rescue NoMethodError
20
+ true
21
+ end
22
+ attributes = if inspect_attributes.length == 1
23
+ send(attributes.first).inspect
24
+ else
25
+ attributes.map do |attribute|
26
+ "#{attribute}=#{send(attribute).inspect}"
27
+ end.join(" ")
28
+ end
29
+ "#<#{self.class}:#{format("0x%x", object_id)} #{attributes}>"
30
+ end
31
+
32
+ def pretty_print(pp)
33
+ nice_name = self.class.name.split("::").last
34
+ pp.group(2, "#(#{nice_name}:#{format("0x%x", object_id)} {", "})") do
35
+ pp.breakable
36
+
37
+ attrs = inspect_attributes.filter_map do |t|
38
+ [t, send(t)] if respond_to?(t)
39
+ end.find_all do |x|
40
+ if x.last
41
+ if COLLECTIONS.include?(x.first)
42
+ !x.last.empty?
43
+ else
44
+ true
45
+ end
46
+ end
47
+ end
48
+
49
+ if inspect_attributes.length == 1
50
+ pp.pp(attrs.first.last)
51
+ else
52
+ pp.seplist(attrs) do |v|
53
+ if COLLECTIONS.include?(v.first)
54
+ pp.group(2, "#{v.first} = [", "]") do
55
+ pp.breakable
56
+ pp.seplist(v.last) do |item|
57
+ pp.pp(item)
58
+ end
59
+ end
60
+ else
61
+ pp.text("#{v.first} = ")
62
+ pp.pp(v.last)
63
+ end
64
+ end
65
+ end
66
+
67
+ pp.breakable
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/pp.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ require_relative "pp/node"
6
+ require_relative "pp/character_data"
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/searchable.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ module Lobzique
6
+ module XML
7
+ # The search methods that Node and NodeSet share. CSS selectors compile
8
+ # to CSS::Selector where Nokogiri translates them to XPath strings. They go
9
+ # through #xpath in the same way (ARCHITECTURE.md).
10
+ module Searchable
11
+ LOOKS_LIKE_XPATH = %r{^(\./|/|\.\.|\.$)}
12
+
13
+ def search(*args)
14
+ paths, handler, ns, binds = extract_params(args)
15
+
16
+ xpaths = paths.map(&:to_s).map do |path|
17
+ LOOKS_LIKE_XPATH.match?(path) ? path : xpath_query_from_css_rule(path, ns)
18
+ end.flatten.uniq
19
+
20
+ xpath(*(xpaths + [ns, handler, binds].compact))
21
+ end
22
+
23
+ alias_method :/, :search
24
+
25
+ def at(*args)
26
+ search(*args).first
27
+ end
28
+
29
+ alias_method :%, :at
30
+
31
+ def css(*args)
32
+ rules, handler, ns, _ = extract_params(args)
33
+
34
+ css_internal(self, rules, handler, ns)
35
+ end
36
+
37
+ def at_css(*args)
38
+ css(*args).first
39
+ end
40
+
41
+ def xpath(*args)
42
+ paths, handler, ns, binds = extract_params(args)
43
+
44
+ xpath_internal(self, paths, handler, ns, binds)
45
+ end
46
+
47
+ def at_xpath(*args)
48
+ xpath(*args).first
49
+ end
50
+
51
+ def >(selector) # rubocop:disable Naming/BinaryOperatorParameterName
52
+ ns = document.root&.namespaces || {}
53
+ # lobzique: CSS.xpath_for(selector, prefix: "./", ns: ns).first
54
+ query = CSS::Selector.new(
55
+ selector, prefixes: [XPath::CURRENT_SEARCH_PREFIX], doctype: :xml, namespaces: ns,
56
+ builtins: false, first: true
57
+ )
58
+ xpath(query)
59
+ end
60
+
61
+ private
62
+
63
+ def extract_params(params) # :nodoc:
64
+ handler = params.find do |param|
65
+ # lobzique: compiled CSS is a query too, where Nokogiri has a String.
66
+ ![Hash, String, Symbol, CSS::Selector].include?(param.class)
67
+ end
68
+ params -= [handler] if handler
69
+
70
+ hashes = []
71
+ while Hash === params.last || params.last.nil?
72
+ hashes << params.pop
73
+ break if params.empty?
74
+ end
75
+ ns, binds = hashes.reverse
76
+
77
+ ns ||= document.root&.namespaces || {}
78
+
79
+ [params, handler, ns, binds]
80
+ end
81
+
82
+ def css_internal(node, rules, handler, ns)
83
+ xpath_internal(node, css_rules_to_xpath(rules, ns), handler, ns, nil)
84
+ end
85
+
86
+ def css_rules_to_xpath(rules, ns)
87
+ rules.map { |rule| xpath_query_from_css_rule(rule, ns) }
88
+ end
89
+
90
+ # lobzique: one CSS::Selector for every implied context, where Nokogiri
91
+ # joins an XPath string for each context with " | ".
92
+ def xpath_query_from_css_rule(rule, ns)
93
+ CSS::Selector.new(
94
+ rule.to_s, prefixes: self.class::IMPLIED_XPATH_CONTEXTS, doctype: document.xpath_doctype,
95
+ namespaces: ns
96
+ )
97
+ end
98
+
99
+ def xpath_internal(node, paths, handler, ns, binds)
100
+ document = node.document
101
+ return NodeSet.new(document) unless document
102
+
103
+ if paths.length == 1
104
+ return xpath_impl(node, paths.first, handler, ns, binds)
105
+ end
106
+
107
+ NodeSet.new(document) do |combined|
108
+ paths.each do |path|
109
+ xpath_impl(node, path, handler, ns, binds).each { |set| combined << set }
110
+ end
111
+ end
112
+ end
113
+
114
+ def xpath_impl(node, path, handler, ns, binds)
115
+ context = XPathContext.new(node)
116
+ context.register_namespaces(ns)
117
+ context.register_variables(binds)
118
+
119
+ context.evaluate(path, handler)
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/syntax_error.rb (MIT, see LICENSE-NOKOGIRI.md).
4
+
5
+ module Lobzique
6
+ module XML
7
+ class SyntaxError < Lobzique::SyntaxError
8
+ class << self
9
+ def aggregate(errors)
10
+ return nil if errors.empty?
11
+ return errors.first if errors.length == 1
12
+
13
+ messages = ["Multiple errors encountered:"]
14
+ errors.each do |error|
15
+ messages << error.to_s
16
+ end
17
+ new(messages.join("\n"))
18
+ end
19
+ end
20
+
21
+ attr_reader :domain
22
+ attr_reader :code
23
+ attr_reader :level
24
+ attr_reader :file
25
+ attr_reader :line
26
+ attr_reader :path
27
+ attr_reader :str1
28
+ attr_reader :str2
29
+ attr_reader :str3
30
+ attr_reader :int1
31
+ attr_reader :column
32
+
33
+ def none?
34
+ level == 0
35
+ end
36
+
37
+ def warning?
38
+ level == 1
39
+ end
40
+
41
+ def error?
42
+ level == 2
43
+ end
44
+
45
+ def fatal?
46
+ level == 3
47
+ end
48
+
49
+ def to_s
50
+ message = super.chomp
51
+ [location_to_s, level_to_s, message]
52
+ .compact.join(": ")
53
+ .force_encoding(message.encoding)
54
+ end
55
+
56
+ private
57
+
58
+ def level_to_s
59
+ case level
60
+ when 3 then "FATAL"
61
+ when 2 then "ERROR"
62
+ when 1 then "WARNING"
63
+ end
64
+ end
65
+
66
+ def nil_or_zero?(attribute)
67
+ attribute.nil? || attribute.zero?
68
+ end
69
+
70
+ def location_to_s
71
+ return if nil_or_zero?(line) && nil_or_zero?(column)
72
+
73
+ "#{line}:#{column}"
74
+ end
75
+ end
76
+ end
77
+ end