nokogiri 1.11.0-arm64-darwin

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (219) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE-DEPENDENCIES.md +1682 -0
  4. data/LICENSE.md +9 -0
  5. data/README.md +272 -0
  6. data/bin/nokogiri +118 -0
  7. data/dependencies.yml +74 -0
  8. data/ext/nokogiri/depend +477 -0
  9. data/ext/nokogiri/extconf.rb +836 -0
  10. data/ext/nokogiri/html_document.c +171 -0
  11. data/ext/nokogiri/html_document.h +10 -0
  12. data/ext/nokogiri/html_element_description.c +279 -0
  13. data/ext/nokogiri/html_element_description.h +10 -0
  14. data/ext/nokogiri/html_entity_lookup.c +32 -0
  15. data/ext/nokogiri/html_entity_lookup.h +8 -0
  16. data/ext/nokogiri/html_sax_parser_context.c +116 -0
  17. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  18. data/ext/nokogiri/html_sax_push_parser.c +87 -0
  19. data/ext/nokogiri/html_sax_push_parser.h +9 -0
  20. data/ext/nokogiri/include/libexslt/exslt.h +102 -0
  21. data/ext/nokogiri/include/libexslt/exsltconfig.h +70 -0
  22. data/ext/nokogiri/include/libexslt/exsltexports.h +140 -0
  23. data/ext/nokogiri/include/libxml2/libxml/DOCBparser.h +96 -0
  24. data/ext/nokogiri/include/libxml2/libxml/HTMLparser.h +306 -0
  25. data/ext/nokogiri/include/libxml2/libxml/HTMLtree.h +147 -0
  26. data/ext/nokogiri/include/libxml2/libxml/SAX.h +173 -0
  27. data/ext/nokogiri/include/libxml2/libxml/SAX2.h +178 -0
  28. data/ext/nokogiri/include/libxml2/libxml/c14n.h +126 -0
  29. data/ext/nokogiri/include/libxml2/libxml/catalog.h +182 -0
  30. data/ext/nokogiri/include/libxml2/libxml/chvalid.h +230 -0
  31. data/ext/nokogiri/include/libxml2/libxml/debugXML.h +217 -0
  32. data/ext/nokogiri/include/libxml2/libxml/dict.h +79 -0
  33. data/ext/nokogiri/include/libxml2/libxml/encoding.h +245 -0
  34. data/ext/nokogiri/include/libxml2/libxml/entities.h +151 -0
  35. data/ext/nokogiri/include/libxml2/libxml/globals.h +508 -0
  36. data/ext/nokogiri/include/libxml2/libxml/hash.h +236 -0
  37. data/ext/nokogiri/include/libxml2/libxml/list.h +137 -0
  38. data/ext/nokogiri/include/libxml2/libxml/nanoftp.h +163 -0
  39. data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +81 -0
  40. data/ext/nokogiri/include/libxml2/libxml/parser.h +1241 -0
  41. data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +644 -0
  42. data/ext/nokogiri/include/libxml2/libxml/pattern.h +100 -0
  43. data/ext/nokogiri/include/libxml2/libxml/relaxng.h +217 -0
  44. data/ext/nokogiri/include/libxml2/libxml/schemasInternals.h +958 -0
  45. data/ext/nokogiri/include/libxml2/libxml/schematron.h +142 -0
  46. data/ext/nokogiri/include/libxml2/libxml/threads.h +89 -0
  47. data/ext/nokogiri/include/libxml2/libxml/tree.h +1311 -0
  48. data/ext/nokogiri/include/libxml2/libxml/uri.h +94 -0
  49. data/ext/nokogiri/include/libxml2/libxml/valid.h +458 -0
  50. data/ext/nokogiri/include/libxml2/libxml/xinclude.h +129 -0
  51. data/ext/nokogiri/include/libxml2/libxml/xlink.h +189 -0
  52. data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +366 -0
  53. data/ext/nokogiri/include/libxml2/libxml/xmlautomata.h +146 -0
  54. data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +945 -0
  55. data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +153 -0
  56. data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +224 -0
  57. data/ext/nokogiri/include/libxml2/libxml/xmlmodule.h +57 -0
  58. data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +428 -0
  59. data/ext/nokogiri/include/libxml2/libxml/xmlregexp.h +222 -0
  60. data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +88 -0
  61. data/ext/nokogiri/include/libxml2/libxml/xmlschemas.h +246 -0
  62. data/ext/nokogiri/include/libxml2/libxml/xmlschemastypes.h +151 -0
  63. data/ext/nokogiri/include/libxml2/libxml/xmlstring.h +140 -0
  64. data/ext/nokogiri/include/libxml2/libxml/xmlunicode.h +202 -0
  65. data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +485 -0
  66. data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +488 -0
  67. data/ext/nokogiri/include/libxml2/libxml/xpath.h +566 -0
  68. data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +632 -0
  69. data/ext/nokogiri/include/libxml2/libxml/xpointer.h +114 -0
  70. data/ext/nokogiri/include/libxslt/attributes.h +38 -0
  71. data/ext/nokogiri/include/libxslt/documents.h +93 -0
  72. data/ext/nokogiri/include/libxslt/extensions.h +262 -0
  73. data/ext/nokogiri/include/libxslt/extra.h +72 -0
  74. data/ext/nokogiri/include/libxslt/functions.h +78 -0
  75. data/ext/nokogiri/include/libxslt/imports.h +75 -0
  76. data/ext/nokogiri/include/libxslt/keys.h +53 -0
  77. data/ext/nokogiri/include/libxslt/namespaces.h +68 -0
  78. data/ext/nokogiri/include/libxslt/numbersInternals.h +73 -0
  79. data/ext/nokogiri/include/libxslt/pattern.h +84 -0
  80. data/ext/nokogiri/include/libxslt/preproc.h +43 -0
  81. data/ext/nokogiri/include/libxslt/security.h +104 -0
  82. data/ext/nokogiri/include/libxslt/templates.h +77 -0
  83. data/ext/nokogiri/include/libxslt/transform.h +207 -0
  84. data/ext/nokogiri/include/libxslt/variables.h +118 -0
  85. data/ext/nokogiri/include/libxslt/xslt.h +110 -0
  86. data/ext/nokogiri/include/libxslt/xsltInternals.h +1978 -0
  87. data/ext/nokogiri/include/libxslt/xsltconfig.h +180 -0
  88. data/ext/nokogiri/include/libxslt/xsltexports.h +142 -0
  89. data/ext/nokogiri/include/libxslt/xsltlocale.h +76 -0
  90. data/ext/nokogiri/include/libxslt/xsltutils.h +313 -0
  91. data/ext/nokogiri/nokogiri.c +135 -0
  92. data/ext/nokogiri/nokogiri.h +130 -0
  93. data/ext/nokogiri/xml_attr.c +103 -0
  94. data/ext/nokogiri/xml_attr.h +9 -0
  95. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  96. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  97. data/ext/nokogiri/xml_cdata.c +62 -0
  98. data/ext/nokogiri/xml_cdata.h +9 -0
  99. data/ext/nokogiri/xml_comment.c +69 -0
  100. data/ext/nokogiri/xml_comment.h +9 -0
  101. data/ext/nokogiri/xml_document.c +622 -0
  102. data/ext/nokogiri/xml_document.h +23 -0
  103. data/ext/nokogiri/xml_document_fragment.c +48 -0
  104. data/ext/nokogiri/xml_document_fragment.h +10 -0
  105. data/ext/nokogiri/xml_dtd.c +202 -0
  106. data/ext/nokogiri/xml_dtd.h +10 -0
  107. data/ext/nokogiri/xml_element_content.c +123 -0
  108. data/ext/nokogiri/xml_element_content.h +10 -0
  109. data/ext/nokogiri/xml_element_decl.c +69 -0
  110. data/ext/nokogiri/xml_element_decl.h +9 -0
  111. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  112. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  113. data/ext/nokogiri/xml_entity_decl.c +110 -0
  114. data/ext/nokogiri/xml_entity_decl.h +10 -0
  115. data/ext/nokogiri/xml_entity_reference.c +52 -0
  116. data/ext/nokogiri/xml_entity_reference.h +9 -0
  117. data/ext/nokogiri/xml_io.c +63 -0
  118. data/ext/nokogiri/xml_io.h +11 -0
  119. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  120. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  121. data/ext/nokogiri/xml_namespace.c +111 -0
  122. data/ext/nokogiri/xml_namespace.h +14 -0
  123. data/ext/nokogiri/xml_node.c +1773 -0
  124. data/ext/nokogiri/xml_node.h +13 -0
  125. data/ext/nokogiri/xml_node_set.c +486 -0
  126. data/ext/nokogiri/xml_node_set.h +12 -0
  127. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  128. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  129. data/ext/nokogiri/xml_reader.c +657 -0
  130. data/ext/nokogiri/xml_reader.h +10 -0
  131. data/ext/nokogiri/xml_relax_ng.c +179 -0
  132. data/ext/nokogiri/xml_relax_ng.h +9 -0
  133. data/ext/nokogiri/xml_sax_parser.c +305 -0
  134. data/ext/nokogiri/xml_sax_parser.h +39 -0
  135. data/ext/nokogiri/xml_sax_parser_context.c +262 -0
  136. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  137. data/ext/nokogiri/xml_sax_push_parser.c +159 -0
  138. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  139. data/ext/nokogiri/xml_schema.c +276 -0
  140. data/ext/nokogiri/xml_schema.h +9 -0
  141. data/ext/nokogiri/xml_syntax_error.c +64 -0
  142. data/ext/nokogiri/xml_syntax_error.h +13 -0
  143. data/ext/nokogiri/xml_text.c +52 -0
  144. data/ext/nokogiri/xml_text.h +9 -0
  145. data/ext/nokogiri/xml_xpath_context.c +374 -0
  146. data/ext/nokogiri/xml_xpath_context.h +10 -0
  147. data/ext/nokogiri/xslt_stylesheet.c +263 -0
  148. data/ext/nokogiri/xslt_stylesheet.h +14 -0
  149. data/lib/nokogiri.rb +144 -0
  150. data/lib/nokogiri/2.5/nokogiri.bundle +0 -0
  151. data/lib/nokogiri/2.6/nokogiri.bundle +0 -0
  152. data/lib/nokogiri/2.7/nokogiri.bundle +0 -0
  153. data/lib/nokogiri/3.0/nokogiri.bundle +0 -0
  154. data/lib/nokogiri/css.rb +28 -0
  155. data/lib/nokogiri/css/node.rb +53 -0
  156. data/lib/nokogiri/css/parser.rb +751 -0
  157. data/lib/nokogiri/css/parser.y +272 -0
  158. data/lib/nokogiri/css/parser_extras.rb +94 -0
  159. data/lib/nokogiri/css/syntax_error.rb +8 -0
  160. data/lib/nokogiri/css/tokenizer.rb +154 -0
  161. data/lib/nokogiri/css/tokenizer.rex +55 -0
  162. data/lib/nokogiri/css/xpath_visitor.rb +260 -0
  163. data/lib/nokogiri/decorators/slop.rb +43 -0
  164. data/lib/nokogiri/html.rb +38 -0
  165. data/lib/nokogiri/html/builder.rb +36 -0
  166. data/lib/nokogiri/html/document.rb +322 -0
  167. data/lib/nokogiri/html/document_fragment.rb +50 -0
  168. data/lib/nokogiri/html/element_description.rb +24 -0
  169. data/lib/nokogiri/html/element_description_defaults.rb +672 -0
  170. data/lib/nokogiri/html/entity_lookup.rb +14 -0
  171. data/lib/nokogiri/html/sax/parser.rb +63 -0
  172. data/lib/nokogiri/html/sax/parser_context.rb +17 -0
  173. data/lib/nokogiri/html/sax/push_parser.rb +37 -0
  174. data/lib/nokogiri/jruby/dependencies.rb +20 -0
  175. data/lib/nokogiri/syntax_error.rb +5 -0
  176. data/lib/nokogiri/version.rb +3 -0
  177. data/lib/nokogiri/version/constant.rb +5 -0
  178. data/lib/nokogiri/version/info.rb +182 -0
  179. data/lib/nokogiri/xml.rb +76 -0
  180. data/lib/nokogiri/xml/attr.rb +15 -0
  181. data/lib/nokogiri/xml/attribute_decl.rb +19 -0
  182. data/lib/nokogiri/xml/builder.rb +447 -0
  183. data/lib/nokogiri/xml/cdata.rb +12 -0
  184. data/lib/nokogiri/xml/character_data.rb +8 -0
  185. data/lib/nokogiri/xml/document.rb +290 -0
  186. data/lib/nokogiri/xml/document_fragment.rb +159 -0
  187. data/lib/nokogiri/xml/dtd.rb +33 -0
  188. data/lib/nokogiri/xml/element_content.rb +37 -0
  189. data/lib/nokogiri/xml/element_decl.rb +14 -0
  190. data/lib/nokogiri/xml/entity_decl.rb +20 -0
  191. data/lib/nokogiri/xml/entity_reference.rb +19 -0
  192. data/lib/nokogiri/xml/namespace.rb +14 -0
  193. data/lib/nokogiri/xml/node.rb +1240 -0
  194. data/lib/nokogiri/xml/node/save_options.rb +62 -0
  195. data/lib/nokogiri/xml/node_set.rb +372 -0
  196. data/lib/nokogiri/xml/notation.rb +7 -0
  197. data/lib/nokogiri/xml/parse_options.rb +127 -0
  198. data/lib/nokogiri/xml/pp.rb +3 -0
  199. data/lib/nokogiri/xml/pp/character_data.rb +19 -0
  200. data/lib/nokogiri/xml/pp/node.rb +57 -0
  201. data/lib/nokogiri/xml/processing_instruction.rb +9 -0
  202. data/lib/nokogiri/xml/reader.rb +116 -0
  203. data/lib/nokogiri/xml/relax_ng.rb +37 -0
  204. data/lib/nokogiri/xml/sax.rb +5 -0
  205. data/lib/nokogiri/xml/sax/document.rb +172 -0
  206. data/lib/nokogiri/xml/sax/parser.rb +123 -0
  207. data/lib/nokogiri/xml/sax/parser_context.rb +17 -0
  208. data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
  209. data/lib/nokogiri/xml/schema.rb +72 -0
  210. data/lib/nokogiri/xml/searchable.rb +239 -0
  211. data/lib/nokogiri/xml/syntax_error.rb +71 -0
  212. data/lib/nokogiri/xml/text.rb +10 -0
  213. data/lib/nokogiri/xml/xpath.rb +11 -0
  214. data/lib/nokogiri/xml/xpath/syntax_error.rb +12 -0
  215. data/lib/nokogiri/xml/xpath_context.rb +17 -0
  216. data/lib/nokogiri/xslt.rb +57 -0
  217. data/lib/nokogiri/xslt/stylesheet.rb +26 -0
  218. data/lib/xsd/xmlparser/nokogiri.rb +103 -0
  219. metadata +499 -0
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ module Nokogiri
3
+ module XML
4
+ module SAX
5
+ ###
6
+ # Context for XML SAX parsers. This class is usually not instantiated
7
+ # by the user. Instead, you should be looking at
8
+ # Nokogiri::XML::SAX::Parser
9
+ class ParserContext
10
+ def self.new thing, encoding = 'UTF-8'
11
+ [:read, :close].all? { |x| thing.respond_to?(x) } ?
12
+ io(thing, Parser::ENCODINGS[encoding]) : memory(thing)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+ module Nokogiri
3
+ module XML
4
+ module SAX
5
+ ###
6
+ # PushParser can parse a document that is fed to it manually. It
7
+ # must be given a SAX::Document object which will be called with
8
+ # SAX events as the document is being parsed.
9
+ #
10
+ # Calling PushParser#<< writes XML to the parser, calling any SAX
11
+ # callbacks it can.
12
+ #
13
+ # PushParser#finish tells the parser that the document is finished
14
+ # and calls the end_document SAX method.
15
+ #
16
+ # Example:
17
+ #
18
+ # parser = PushParser.new(Class.new(XML::SAX::Document) {
19
+ # def start_document
20
+ # puts "start document called"
21
+ # end
22
+ # }.new)
23
+ # parser << "<div>hello<"
24
+ # parser << "/div>"
25
+ # parser.finish
26
+ class PushParser
27
+
28
+ # The Nokogiri::XML::SAX::Document on which the PushParser will be
29
+ # operating
30
+ attr_accessor :document
31
+
32
+ ###
33
+ # Create a new PushParser with +doc+ as the SAX Document, providing
34
+ # an optional +file_name+ and +encoding+
35
+ def initialize(doc = XML::SAX::Document.new, file_name = nil, encoding = 'UTF-8')
36
+ @document = doc
37
+ @encoding = encoding
38
+ @sax_parser = XML::SAX::Parser.new(doc)
39
+
40
+ ## Create our push parser context
41
+ initialize_native(@sax_parser, file_name)
42
+ end
43
+
44
+ ###
45
+ # Write a +chunk+ of XML to the PushParser. Any callback methods
46
+ # that can be called will be called immediately.
47
+ def write chunk, last_chunk = false
48
+ native_write(chunk, last_chunk)
49
+ end
50
+ alias :<< :write
51
+
52
+ ###
53
+ # Finish the parsing. This method is only necessary for
54
+ # Nokogiri::XML::SAX::Document#end_document to be called.
55
+ def finish
56
+ write '', true
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+ module Nokogiri
3
+ module XML
4
+ class << self
5
+ ###
6
+ # Create a new Nokogiri::XML::Schema object using a +string_or_io+
7
+ # object.
8
+ def Schema(string_or_io, options = ParseOptions::DEFAULT_SCHEMA)
9
+ Schema.new(string_or_io, options)
10
+ end
11
+ end
12
+
13
+ ###
14
+ # Nokogiri::XML::Schema is used for validating XML against a schema
15
+ # (usually from an xsd file).
16
+ #
17
+ # == Synopsis
18
+ #
19
+ # Validate an XML document against a Schema. Loop over the errors that
20
+ # are returned and print them out:
21
+ #
22
+ # xsd = Nokogiri::XML::Schema(File.read(PO_SCHEMA_FILE))
23
+ # doc = Nokogiri::XML(File.read(PO_XML_FILE))
24
+ #
25
+ # xsd.validate(doc).each do |error|
26
+ # puts error.message
27
+ # end
28
+ #
29
+ # The list of errors are Nokogiri::XML::SyntaxError objects.
30
+ #
31
+ # NOTE: As of v1.11.0, Schema treats inputs as UNTRUSTED by default, and so external entities
32
+ # are not resolved from the network (`http://` or `ftp://`). Previously, parsing treated
33
+ # documents as "trusted" by default which was counter to Nokogiri's "untrusted by default"
34
+ # security policy. If a document is trusted, then the caller may turn off the NONET option via
35
+ # the ParseOptions to re-enable external entity resolution over a network connection.
36
+ class Schema
37
+ # Errors while parsing the schema file
38
+ attr_accessor :errors
39
+ # The Nokogiri::XML::ParseOptions used to parse the schema
40
+ attr_accessor :parse_options
41
+
42
+ ###
43
+ # Create a new Nokogiri::XML::Schema object using a +string_or_io+
44
+ # object.
45
+ def self.new string_or_io, options = ParseOptions::DEFAULT_SCHEMA
46
+ from_document(Nokogiri::XML(string_or_io), options)
47
+ end
48
+
49
+ ###
50
+ # Validate +thing+ against this schema. +thing+ can be a
51
+ # Nokogiri::XML::Document object, or a filename. An Array of
52
+ # Nokogiri::XML::SyntaxError objects found while validating the
53
+ # +thing+ is returned.
54
+ def validate thing
55
+ if thing.is_a?(Nokogiri::XML::Document)
56
+ validate_document(thing)
57
+ elsif File.file?(thing)
58
+ validate_file(thing)
59
+ else
60
+ raise ArgumentError, "Must provide Nokogiri::Xml::Document or the name of an existing file"
61
+ end
62
+ end
63
+
64
+ ###
65
+ # Returns true if +thing+ is a valid Nokogiri::XML::Document or
66
+ # file.
67
+ def valid? thing
68
+ validate(thing).length == 0
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,239 @@
1
+ # frozen_string_literal: true
2
+ module Nokogiri
3
+ module XML
4
+ #
5
+ # The Searchable module declares the interface used for searching your DOM.
6
+ #
7
+ # It implements the public methods `search`, `css`, and `xpath`,
8
+ # as well as allowing specific implementations to specialize some
9
+ # of the important behaviors.
10
+ #
11
+ module Searchable
12
+ # Regular expression used by Searchable#search to determine if a query
13
+ # string is CSS or XPath
14
+ LOOKS_LIKE_XPATH = /^(\.\/|\/|\.\.|\.$)/
15
+
16
+ # @!group Searching via XPath or CSS Queries
17
+
18
+ ###
19
+ # call-seq: search *paths, [namespace-bindings, xpath-variable-bindings, custom-handler-class]
20
+ #
21
+ # Search this object for +paths+. +paths+ must be one or more XPath or CSS queries:
22
+ #
23
+ # node.search("div.employee", ".//title")
24
+ #
25
+ # A hash of namespace bindings may be appended:
26
+ #
27
+ # node.search('.//bike:tire', {'bike' => 'http://schwinn.com/'})
28
+ # node.search('bike|tire', {'bike' => 'http://schwinn.com/'})
29
+ #
30
+ # For XPath queries, a hash of variable bindings may also be
31
+ # appended to the namespace bindings. For example:
32
+ #
33
+ # node.search('.//address[@domestic=$value]', nil, {:value => 'Yes'})
34
+ #
35
+ # Custom XPath functions and CSS pseudo-selectors may also be
36
+ # defined. To define custom functions create a class and
37
+ # implement the function you want to define. The first argument
38
+ # to the method will be the current matching NodeSet. Any other
39
+ # arguments are ones that you pass in. Note that this class may
40
+ # appear anywhere in the argument list. For example:
41
+ #
42
+ # node.search('.//title[regex(., "\w+")]', 'div.employee:regex("[0-9]+")'
43
+ # Class.new {
44
+ # def regex node_set, regex
45
+ # node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
46
+ # end
47
+ # }.new
48
+ # )
49
+ #
50
+ # See Searchable#xpath and Searchable#css for further usage help.
51
+ def search(*args)
52
+ paths, handler, ns, binds = extract_params(args)
53
+
54
+ xpaths = paths.map(&:to_s).map do |path|
55
+ (path =~ LOOKS_LIKE_XPATH) ? path : xpath_query_from_css_rule(path, ns)
56
+ end.flatten.uniq
57
+
58
+ xpath(*(xpaths + [ns, handler, binds].compact))
59
+ end
60
+
61
+ alias :/ :search
62
+
63
+ ###
64
+ # call-seq: search *paths, [namespace-bindings, xpath-variable-bindings, custom-handler-class]
65
+ #
66
+ # Search this object for +paths+, and return only the first
67
+ # result. +paths+ must be one or more XPath or CSS queries.
68
+ #
69
+ # See Searchable#search for more information.
70
+ def at(*args)
71
+ search(*args).first
72
+ end
73
+
74
+ alias :% :at
75
+
76
+ ###
77
+ # call-seq: css *rules, [namespace-bindings, custom-pseudo-class]
78
+ #
79
+ # Search this object for CSS +rules+. +rules+ must be one or more CSS
80
+ # selectors. For example:
81
+ #
82
+ # node.css('title')
83
+ # node.css('body h1.bold')
84
+ # node.css('div + p.green', 'div#one')
85
+ #
86
+ # A hash of namespace bindings may be appended. For example:
87
+ #
88
+ # node.css('bike|tire', {'bike' => 'http://schwinn.com/'})
89
+ #
90
+ # Custom CSS pseudo classes may also be defined. To define
91
+ # custom pseudo classes, create a class and implement the custom
92
+ # pseudo class you want defined. The first argument to the
93
+ # method will be the current matching NodeSet. Any other
94
+ # arguments are ones that you pass in. For example:
95
+ #
96
+ # node.css('title:regex("\w+")', Class.new {
97
+ # def regex node_set, regex
98
+ # node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
99
+ # end
100
+ # }.new)
101
+ #
102
+ # Note that the CSS query string is case-sensitive with regards
103
+ # to your document type. That is, if you're looking for "H1" in
104
+ # an HTML document, you'll never find anything, since HTML tags
105
+ # will match only lowercase CSS queries. However, "H1" might be
106
+ # found in an XML document, where tags names are case-sensitive
107
+ # (e.g., "H1" is distinct from "h1").
108
+ #
109
+ def css(*args)
110
+ rules, handler, ns, _ = extract_params(args)
111
+
112
+ css_internal self, rules, handler, ns
113
+ end
114
+
115
+ ##
116
+ # call-seq: css *rules, [namespace-bindings, custom-pseudo-class]
117
+ #
118
+ # Search this object for CSS +rules+, and return only the first
119
+ # match. +rules+ must be one or more CSS selectors.
120
+ #
121
+ # See Searchable#css for more information.
122
+ def at_css(*args)
123
+ css(*args).first
124
+ end
125
+
126
+ ###
127
+ # call-seq: xpath *paths, [namespace-bindings, variable-bindings, custom-handler-class]
128
+ #
129
+ # Search this node for XPath +paths+. +paths+ must be one or more XPath
130
+ # queries.
131
+ #
132
+ # node.xpath('.//title')
133
+ #
134
+ # A hash of namespace bindings may be appended. For example:
135
+ #
136
+ # node.xpath('.//foo:name', {'foo' => 'http://example.org/'})
137
+ # node.xpath('.//xmlns:name', node.root.namespaces)
138
+ #
139
+ # A hash of variable bindings may also be appended to the namespace bindings. For example:
140
+ #
141
+ # node.xpath('.//address[@domestic=$value]', nil, {:value => 'Yes'})
142
+ #
143
+ # Custom XPath functions may also be defined. To define custom
144
+ # functions create a class and implement the function you want
145
+ # to define. The first argument to the method will be the
146
+ # current matching NodeSet. Any other arguments are ones that
147
+ # you pass in. Note that this class may appear anywhere in the
148
+ # argument list. For example:
149
+ #
150
+ # node.xpath('.//title[regex(., "\w+")]', Class.new {
151
+ # def regex node_set, regex
152
+ # node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
153
+ # end
154
+ # }.new)
155
+ #
156
+ def xpath(*args)
157
+ paths, handler, ns, binds = extract_params(args)
158
+
159
+ xpath_internal self, paths, handler, ns, binds
160
+ end
161
+
162
+ ##
163
+ # call-seq: xpath *paths, [namespace-bindings, variable-bindings, custom-handler-class]
164
+ #
165
+ # Search this node for XPath +paths+, and return only the first
166
+ # match. +paths+ must be one or more XPath queries.
167
+ #
168
+ # See Searchable#xpath for more information.
169
+ def at_xpath(*args)
170
+ xpath(*args).first
171
+ end
172
+
173
+ # @!endgroup
174
+
175
+ private
176
+
177
+ def css_internal(node, rules, handler, ns)
178
+ xpath_internal node, css_rules_to_xpath(rules, ns), handler, ns, nil
179
+ end
180
+
181
+ def xpath_internal(node, paths, handler, ns, binds)
182
+ document = node.document
183
+ return NodeSet.new(document) unless document
184
+
185
+ if paths.length == 1
186
+ return xpath_impl(node, paths.first, handler, ns, binds)
187
+ end
188
+
189
+ NodeSet.new(document) do |combined|
190
+ paths.each do |path|
191
+ xpath_impl(node, path, handler, ns, binds).each { |set| combined << set }
192
+ end
193
+ end
194
+ end
195
+
196
+ def xpath_impl(node, path, handler, ns, binds)
197
+ ctx = XPathContext.new(node)
198
+ ctx.register_namespaces(ns)
199
+ path = path.gsub(/xmlns:/, " :") unless Nokogiri.uses_libxml?
200
+
201
+ binds.each do |key, value|
202
+ ctx.register_variable key.to_s, value
203
+ end if binds
204
+
205
+ ctx.evaluate(path, handler)
206
+ end
207
+
208
+ def css_rules_to_xpath(rules, ns)
209
+ rules.map { |rule| xpath_query_from_css_rule(rule, ns) }
210
+ end
211
+
212
+ def xpath_query_from_css_rule(rule, ns)
213
+ visitor = Nokogiri::CSS::XPathVisitorOptimallyUseBuiltins.new
214
+ self.class::IMPLIED_XPATH_CONTEXTS.map do |implied_xpath_context|
215
+ CSS.xpath_for(rule.to_s, {:prefix => implied_xpath_context, :ns => ns,
216
+ :visitor => visitor})
217
+ end.join(" | ")
218
+ end
219
+
220
+ def extract_params(params) # :nodoc:
221
+ handler = params.find do |param|
222
+ ![Hash, String, Symbol].include?(param.class)
223
+ end
224
+ params -= [handler] if handler
225
+
226
+ hashes = []
227
+ while Hash === params.last || params.last.nil?
228
+ hashes << params.pop
229
+ break if params.empty?
230
+ end
231
+ ns, binds = hashes.reverse
232
+
233
+ ns ||= document.root ? document.root.namespaces : {}
234
+
235
+ [params, handler, ns, binds]
236
+ end
237
+ end
238
+ end
239
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+ module Nokogiri
3
+ module XML
4
+ ###
5
+ # This class provides information about XML SyntaxErrors. These
6
+ # exceptions are typically stored on Nokogiri::XML::Document#errors.
7
+ class SyntaxError < ::Nokogiri::SyntaxError
8
+ attr_reader :domain
9
+ attr_reader :code
10
+ attr_reader :level
11
+ attr_reader :file
12
+ attr_reader :line
13
+ attr_reader :str1
14
+ attr_reader :str2
15
+ attr_reader :str3
16
+ attr_reader :int1
17
+ attr_reader :column
18
+
19
+ ###
20
+ # return true if this is a non error
21
+ def none?
22
+ level == 0
23
+ end
24
+
25
+ ###
26
+ # return true if this is a warning
27
+ def warning?
28
+ level == 1
29
+ end
30
+
31
+ ###
32
+ # return true if this is an error
33
+ def error?
34
+ level == 2
35
+ end
36
+
37
+ ###
38
+ # return true if this error is fatal
39
+ def fatal?
40
+ level == 3
41
+ end
42
+
43
+ def to_s
44
+ message = super.chomp
45
+ [location_to_s, level_to_s, message].
46
+ compact.join(": ").
47
+ force_encoding(message.encoding)
48
+ end
49
+
50
+ private
51
+
52
+ def level_to_s
53
+ case level
54
+ when 3 then "FATAL"
55
+ when 2 then "ERROR"
56
+ when 1 then "WARNING"
57
+ else nil
58
+ end
59
+ end
60
+
61
+ def nil_or_zero?(attribute)
62
+ attribute.nil? || attribute.zero?
63
+ end
64
+
65
+ def location_to_s
66
+ return nil if nil_or_zero?(line) && nil_or_zero?(column)
67
+ "#{line}:#{column}"
68
+ end
69
+ end
70
+ end
71
+ end