mini-pro-gem 0.0.1

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 (181) hide show
  1. checksums.yaml +7 -0
  2. data/mini-pro-gem.gemspec +12 -0
  3. data/nokogiri-1.19.4/Gemfile +40 -0
  4. data/nokogiri-1.19.4/LICENSE-DEPENDENCIES.md +2411 -0
  5. data/nokogiri-1.19.4/LICENSE.md +9 -0
  6. data/nokogiri-1.19.4/README.md +308 -0
  7. data/nokogiri-1.19.4/bin/nokogiri +131 -0
  8. data/nokogiri-1.19.4/dependencies.yml +31 -0
  9. data/nokogiri-1.19.4/ext/nokogiri/depend +38 -0
  10. data/nokogiri-1.19.4/ext/nokogiri/extconf.rb +1165 -0
  11. data/nokogiri-1.19.4/ext/nokogiri/gumbo.c +610 -0
  12. data/nokogiri-1.19.4/ext/nokogiri/html4_document.c +171 -0
  13. data/nokogiri-1.19.4/ext/nokogiri/html4_element_description.c +299 -0
  14. data/nokogiri-1.19.4/ext/nokogiri/html4_entity_lookup.c +37 -0
  15. data/nokogiri-1.19.4/ext/nokogiri/html4_sax_parser.c +40 -0
  16. data/nokogiri-1.19.4/ext/nokogiri/html4_sax_parser_context.c +98 -0
  17. data/nokogiri-1.19.4/ext/nokogiri/html4_sax_push_parser.c +96 -0
  18. data/nokogiri-1.19.4/ext/nokogiri/libxml2_polyfill.c +114 -0
  19. data/nokogiri-1.19.4/ext/nokogiri/nokogiri.c +297 -0
  20. data/nokogiri-1.19.4/ext/nokogiri/nokogiri.h +247 -0
  21. data/nokogiri-1.19.4/ext/nokogiri/test_global_handlers.c +40 -0
  22. data/nokogiri-1.19.4/ext/nokogiri/xml_attr.c +108 -0
  23. data/nokogiri-1.19.4/ext/nokogiri/xml_attribute_decl.c +70 -0
  24. data/nokogiri-1.19.4/ext/nokogiri/xml_cdata.c +62 -0
  25. data/nokogiri-1.19.4/ext/nokogiri/xml_comment.c +57 -0
  26. data/nokogiri-1.19.4/ext/nokogiri/xml_document.c +796 -0
  27. data/nokogiri-1.19.4/ext/nokogiri/xml_document_fragment.c +29 -0
  28. data/nokogiri-1.19.4/ext/nokogiri/xml_dtd.c +208 -0
  29. data/nokogiri-1.19.4/ext/nokogiri/xml_element_content.c +131 -0
  30. data/nokogiri-1.19.4/ext/nokogiri/xml_element_decl.c +69 -0
  31. data/nokogiri-1.19.4/ext/nokogiri/xml_encoding_handler.c +112 -0
  32. data/nokogiri-1.19.4/ext/nokogiri/xml_entity_decl.c +112 -0
  33. data/nokogiri-1.19.4/ext/nokogiri/xml_entity_reference.c +50 -0
  34. data/nokogiri-1.19.4/ext/nokogiri/xml_namespace.c +181 -0
  35. data/nokogiri-1.19.4/ext/nokogiri/xml_node.c +2523 -0
  36. data/nokogiri-1.19.4/ext/nokogiri/xml_node_set.c +518 -0
  37. data/nokogiri-1.19.4/ext/nokogiri/xml_processing_instruction.c +54 -0
  38. data/nokogiri-1.19.4/ext/nokogiri/xml_reader.c +777 -0
  39. data/nokogiri-1.19.4/ext/nokogiri/xml_relax_ng.c +149 -0
  40. data/nokogiri-1.19.4/ext/nokogiri/xml_sax_parser.c +403 -0
  41. data/nokogiri-1.19.4/ext/nokogiri/xml_sax_parser_context.c +396 -0
  42. data/nokogiri-1.19.4/ext/nokogiri/xml_sax_push_parser.c +206 -0
  43. data/nokogiri-1.19.4/ext/nokogiri/xml_schema.c +226 -0
  44. data/nokogiri-1.19.4/ext/nokogiri/xml_syntax_error.c +93 -0
  45. data/nokogiri-1.19.4/ext/nokogiri/xml_text.c +59 -0
  46. data/nokogiri-1.19.4/ext/nokogiri/xml_xpath_context.c +496 -0
  47. data/nokogiri-1.19.4/ext/nokogiri/xslt_stylesheet.c +457 -0
  48. data/nokogiri-1.19.4/gumbo-parser/CHANGES.md +63 -0
  49. data/nokogiri-1.19.4/gumbo-parser/Makefile +129 -0
  50. data/nokogiri-1.19.4/gumbo-parser/THANKS +27 -0
  51. data/nokogiri-1.19.4/gumbo-parser/src/Makefile +34 -0
  52. data/nokogiri-1.19.4/gumbo-parser/src/README.md +41 -0
  53. data/nokogiri-1.19.4/gumbo-parser/src/ascii.c +75 -0
  54. data/nokogiri-1.19.4/gumbo-parser/src/ascii.h +115 -0
  55. data/nokogiri-1.19.4/gumbo-parser/src/attribute.c +42 -0
  56. data/nokogiri-1.19.4/gumbo-parser/src/attribute.h +17 -0
  57. data/nokogiri-1.19.4/gumbo-parser/src/char_ref.c +22225 -0
  58. data/nokogiri-1.19.4/gumbo-parser/src/char_ref.h +29 -0
  59. data/nokogiri-1.19.4/gumbo-parser/src/char_ref.rl +2154 -0
  60. data/nokogiri-1.19.4/gumbo-parser/src/error.c +658 -0
  61. data/nokogiri-1.19.4/gumbo-parser/src/error.h +152 -0
  62. data/nokogiri-1.19.4/gumbo-parser/src/foreign_attrs.c +103 -0
  63. data/nokogiri-1.19.4/gumbo-parser/src/foreign_attrs.gperf +27 -0
  64. data/nokogiri-1.19.4/gumbo-parser/src/insertion_mode.h +33 -0
  65. data/nokogiri-1.19.4/gumbo-parser/src/macros.h +91 -0
  66. data/nokogiri-1.19.4/gumbo-parser/src/nokogiri_gumbo.h +953 -0
  67. data/nokogiri-1.19.4/gumbo-parser/src/parser.c +4932 -0
  68. data/nokogiri-1.19.4/gumbo-parser/src/parser.h +41 -0
  69. data/nokogiri-1.19.4/gumbo-parser/src/replacement.h +33 -0
  70. data/nokogiri-1.19.4/gumbo-parser/src/string_buffer.c +103 -0
  71. data/nokogiri-1.19.4/gumbo-parser/src/string_buffer.h +68 -0
  72. data/nokogiri-1.19.4/gumbo-parser/src/string_piece.c +48 -0
  73. data/nokogiri-1.19.4/gumbo-parser/src/svg_attrs.c +174 -0
  74. data/nokogiri-1.19.4/gumbo-parser/src/svg_attrs.gperf +77 -0
  75. data/nokogiri-1.19.4/gumbo-parser/src/svg_tags.c +137 -0
  76. data/nokogiri-1.19.4/gumbo-parser/src/svg_tags.gperf +55 -0
  77. data/nokogiri-1.19.4/gumbo-parser/src/tag.c +223 -0
  78. data/nokogiri-1.19.4/gumbo-parser/src/tag_lookup.c +382 -0
  79. data/nokogiri-1.19.4/gumbo-parser/src/tag_lookup.gperf +170 -0
  80. data/nokogiri-1.19.4/gumbo-parser/src/tag_lookup.h +13 -0
  81. data/nokogiri-1.19.4/gumbo-parser/src/token_buffer.c +79 -0
  82. data/nokogiri-1.19.4/gumbo-parser/src/token_buffer.h +71 -0
  83. data/nokogiri-1.19.4/gumbo-parser/src/token_type.h +17 -0
  84. data/nokogiri-1.19.4/gumbo-parser/src/tokenizer.c +3464 -0
  85. data/nokogiri-1.19.4/gumbo-parser/src/tokenizer.h +112 -0
  86. data/nokogiri-1.19.4/gumbo-parser/src/tokenizer_states.h +339 -0
  87. data/nokogiri-1.19.4/gumbo-parser/src/utf8.c +245 -0
  88. data/nokogiri-1.19.4/gumbo-parser/src/utf8.h +164 -0
  89. data/nokogiri-1.19.4/gumbo-parser/src/util.c +66 -0
  90. data/nokogiri-1.19.4/gumbo-parser/src/util.h +34 -0
  91. data/nokogiri-1.19.4/gumbo-parser/src/vector.c +111 -0
  92. data/nokogiri-1.19.4/gumbo-parser/src/vector.h +45 -0
  93. data/nokogiri-1.19.4/lib/nokogiri/class_resolver.rb +65 -0
  94. data/nokogiri-1.19.4/lib/nokogiri/css/node.rb +58 -0
  95. data/nokogiri-1.19.4/lib/nokogiri/css/parser.rb +772 -0
  96. data/nokogiri-1.19.4/lib/nokogiri/css/parser.y +277 -0
  97. data/nokogiri-1.19.4/lib/nokogiri/css/parser_extras.rb +36 -0
  98. data/nokogiri-1.19.4/lib/nokogiri/css/selector_cache.rb +38 -0
  99. data/nokogiri-1.19.4/lib/nokogiri/css/syntax_error.rb +9 -0
  100. data/nokogiri-1.19.4/lib/nokogiri/css/tokenizer.rb +155 -0
  101. data/nokogiri-1.19.4/lib/nokogiri/css/tokenizer.rex +57 -0
  102. data/nokogiri-1.19.4/lib/nokogiri/css/xpath_visitor.rb +376 -0
  103. data/nokogiri-1.19.4/lib/nokogiri/css.rb +132 -0
  104. data/nokogiri-1.19.4/lib/nokogiri/decorators/slop.rb +42 -0
  105. data/nokogiri-1.19.4/lib/nokogiri/encoding_handler.rb +57 -0
  106. data/nokogiri-1.19.4/lib/nokogiri/extension.rb +32 -0
  107. data/nokogiri-1.19.4/lib/nokogiri/gumbo.rb +15 -0
  108. data/nokogiri-1.19.4/lib/nokogiri/html.rb +48 -0
  109. data/nokogiri-1.19.4/lib/nokogiri/html4/builder.rb +37 -0
  110. data/nokogiri-1.19.4/lib/nokogiri/html4/document.rb +235 -0
  111. data/nokogiri-1.19.4/lib/nokogiri/html4/document_fragment.rb +166 -0
  112. data/nokogiri-1.19.4/lib/nokogiri/html4/element_description.rb +25 -0
  113. data/nokogiri-1.19.4/lib/nokogiri/html4/element_description_defaults.rb +2040 -0
  114. data/nokogiri-1.19.4/lib/nokogiri/html4/encoding_reader.rb +121 -0
  115. data/nokogiri-1.19.4/lib/nokogiri/html4/entity_lookup.rb +15 -0
  116. data/nokogiri-1.19.4/lib/nokogiri/html4/sax/parser.rb +48 -0
  117. data/nokogiri-1.19.4/lib/nokogiri/html4/sax/parser_context.rb +15 -0
  118. data/nokogiri-1.19.4/lib/nokogiri/html4/sax/push_parser.rb +37 -0
  119. data/nokogiri-1.19.4/lib/nokogiri/html4.rb +42 -0
  120. data/nokogiri-1.19.4/lib/nokogiri/html5/builder.rb +40 -0
  121. data/nokogiri-1.19.4/lib/nokogiri/html5/document.rb +199 -0
  122. data/nokogiri-1.19.4/lib/nokogiri/html5/document_fragment.rb +200 -0
  123. data/nokogiri-1.19.4/lib/nokogiri/html5/node.rb +103 -0
  124. data/nokogiri-1.19.4/lib/nokogiri/html5.rb +368 -0
  125. data/nokogiri-1.19.4/lib/nokogiri/jruby/dependencies.rb +3 -0
  126. data/nokogiri-1.19.4/lib/nokogiri/jruby/nokogiri_jars.rb +48 -0
  127. data/nokogiri-1.19.4/lib/nokogiri/syntax_error.rb +6 -0
  128. data/nokogiri-1.19.4/lib/nokogiri/version/constant.rb +6 -0
  129. data/nokogiri-1.19.4/lib/nokogiri/version/info.rb +234 -0
  130. data/nokogiri-1.19.4/lib/nokogiri/version.rb +4 -0
  131. data/nokogiri-1.19.4/lib/nokogiri/xml/attr.rb +66 -0
  132. data/nokogiri-1.19.4/lib/nokogiri/xml/attribute_decl.rb +22 -0
  133. data/nokogiri-1.19.4/lib/nokogiri/xml/builder.rb +494 -0
  134. data/nokogiri-1.19.4/lib/nokogiri/xml/cdata.rb +13 -0
  135. data/nokogiri-1.19.4/lib/nokogiri/xml/character_data.rb +9 -0
  136. data/nokogiri-1.19.4/lib/nokogiri/xml/document.rb +515 -0
  137. data/nokogiri-1.19.4/lib/nokogiri/xml/document_fragment.rb +276 -0
  138. data/nokogiri-1.19.4/lib/nokogiri/xml/dtd.rb +34 -0
  139. data/nokogiri-1.19.4/lib/nokogiri/xml/element_content.rb +46 -0
  140. data/nokogiri-1.19.4/lib/nokogiri/xml/element_decl.rb +17 -0
  141. data/nokogiri-1.19.4/lib/nokogiri/xml/entity_decl.rb +23 -0
  142. data/nokogiri-1.19.4/lib/nokogiri/xml/entity_reference.rb +20 -0
  143. data/nokogiri-1.19.4/lib/nokogiri/xml/namespace.rb +57 -0
  144. data/nokogiri-1.19.4/lib/nokogiri/xml/node/save_options.rb +76 -0
  145. data/nokogiri-1.19.4/lib/nokogiri/xml/node.rb +1701 -0
  146. data/nokogiri-1.19.4/lib/nokogiri/xml/node_set.rb +449 -0
  147. data/nokogiri-1.19.4/lib/nokogiri/xml/notation.rb +19 -0
  148. data/nokogiri-1.19.4/lib/nokogiri/xml/parse_options.rb +217 -0
  149. data/nokogiri-1.19.4/lib/nokogiri/xml/pp/character_data.rb +21 -0
  150. data/nokogiri-1.19.4/lib/nokogiri/xml/pp/node.rb +73 -0
  151. data/nokogiri-1.19.4/lib/nokogiri/xml/pp.rb +4 -0
  152. data/nokogiri-1.19.4/lib/nokogiri/xml/processing_instruction.rb +11 -0
  153. data/nokogiri-1.19.4/lib/nokogiri/xml/reader.rb +139 -0
  154. data/nokogiri-1.19.4/lib/nokogiri/xml/relax_ng.rb +75 -0
  155. data/nokogiri-1.19.4/lib/nokogiri/xml/sax/document.rb +258 -0
  156. data/nokogiri-1.19.4/lib/nokogiri/xml/sax/parser.rb +199 -0
  157. data/nokogiri-1.19.4/lib/nokogiri/xml/sax/parser_context.rb +129 -0
  158. data/nokogiri-1.19.4/lib/nokogiri/xml/sax/push_parser.rb +64 -0
  159. data/nokogiri-1.19.4/lib/nokogiri/xml/sax.rb +54 -0
  160. data/nokogiri-1.19.4/lib/nokogiri/xml/schema.rb +140 -0
  161. data/nokogiri-1.19.4/lib/nokogiri/xml/searchable.rb +274 -0
  162. data/nokogiri-1.19.4/lib/nokogiri/xml/syntax_error.rb +94 -0
  163. data/nokogiri-1.19.4/lib/nokogiri/xml/text.rb +11 -0
  164. data/nokogiri-1.19.4/lib/nokogiri/xml/xpath/syntax_error.rb +13 -0
  165. data/nokogiri-1.19.4/lib/nokogiri/xml/xpath.rb +21 -0
  166. data/nokogiri-1.19.4/lib/nokogiri/xml/xpath_context.rb +27 -0
  167. data/nokogiri-1.19.4/lib/nokogiri/xml.rb +65 -0
  168. data/nokogiri-1.19.4/lib/nokogiri/xslt/stylesheet.rb +54 -0
  169. data/nokogiri-1.19.4/lib/nokogiri/xslt.rb +138 -0
  170. data/nokogiri-1.19.4/lib/nokogiri.rb +128 -0
  171. data/nokogiri-1.19.4/lib/xsd/xmlparser/nokogiri.rb +105 -0
  172. data/nokogiri-1.19.4/patches/libxml2/0001-Remove-script-macro-support.patch +40 -0
  173. data/nokogiri-1.19.4/patches/libxml2/0002-Update-entities-to-remove-handling-of-ssi.patch +44 -0
  174. data/nokogiri-1.19.4/patches/libxml2/0009-allow-wildcard-namespaces.patch +77 -0
  175. data/nokogiri-1.19.4/patches/libxml2/0010-update-config.guess-and-config.sub-for-libxml2.patch +224 -0
  176. data/nokogiri-1.19.4/patches/libxml2/0011-rip-out-libxml2-s-libc_single_threaded-support.patch +30 -0
  177. data/nokogiri-1.19.4/patches/libxml2/0019-xpath-Use-separate-static-hash-table-for-standard-fu.patch +244 -0
  178. data/nokogiri-1.19.4/patches/libxslt/0001-update-config.guess-and-config.sub-for-libxslt.patch +224 -0
  179. data/nokogiri-1.19.4/ports/archives/libxml2-2.13.9.tar.xz +0 -0
  180. data/nokogiri-1.19.4/ports/archives/libxslt-1.1.43.tar.xz +0 -0
  181. metadata +220 -0
@@ -0,0 +1,515 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "pathname"
5
+
6
+ module Nokogiri
7
+ module XML
8
+ # Nokogiri::XML::Document is the main entry point for dealing with \XML documents. The Document
9
+ # is created by parsing \XML content from a String or an IO object. See
10
+ # Nokogiri::XML::Document.parse for more information on parsing.
11
+ #
12
+ # Document inherits a great deal of functionality from its superclass Nokogiri::XML::Node, so
13
+ # please read that class's documentation as well.
14
+ class Document < Nokogiri::XML::Node
15
+ # See http://www.w3.org/TR/REC-xml-names/#ns-decl for more details. Note that we're not
16
+ # attempting to handle unicode characters partly because libxml2 doesn't handle unicode
17
+ # characters in NCNAMEs.
18
+ NCNAME_START_CHAR = "A-Za-z_"
19
+ NCNAME_CHAR = NCNAME_START_CHAR + "\\-\\.0-9"
20
+ NCNAME_RE = /^xmlns(?::([#{NCNAME_START_CHAR}][#{NCNAME_CHAR}]*))?$/
21
+
22
+ OBJECT_DUP_METHOD = Object.instance_method(:dup)
23
+ OBJECT_CLONE_METHOD = Object.instance_method(:clone)
24
+ private_constant :OBJECT_DUP_METHOD, :OBJECT_CLONE_METHOD
25
+
26
+ class << self
27
+ # call-seq:
28
+ # parse(input) { |options| ... } => Nokogiri::XML::Document
29
+ # parse(input, url:, encoding:, options:) => Nokogiri::XML::Document
30
+ #
31
+ # Parse \XML input from a String or IO object, and return a new XML::Document.
32
+ #
33
+ # 🛡 By default, Nokogiri treats documents as untrusted, and so does not attempt to load DTDs
34
+ # or access the network. See Nokogiri::XML::ParseOptions for a complete list of options; and
35
+ # that module's DEFAULT_XML constant for what's set (and not set) by default.
36
+ #
37
+ # [Required Parameters]
38
+ # - +input+ (String | IO) The content to be parsed.
39
+ #
40
+ # [Optional Keyword Arguments]
41
+ # - +url:+ (String) The base URI for this document.
42
+ #
43
+ # - +encoding:+ (String) The name of the encoding that should be used when processing the
44
+ # document. When not provided, the encoding will be determined based on the document
45
+ # content.
46
+ #
47
+ # - +options:+ (Nokogiri::XML::ParseOptions) Configuration object that determines some
48
+ # behaviors during parsing. See ParseOptions for more information. The default value is
49
+ # +ParseOptions::DEFAULT_XML+.
50
+ #
51
+ # [Yields]
52
+ # If a block is given, a Nokogiri::XML::ParseOptions object is yielded to the block which
53
+ # can be configured before parsing. See Nokogiri::XML::ParseOptions for more information.
54
+ #
55
+ # [Returns] Nokogiri::XML::Document
56
+ def parse(
57
+ string_or_io,
58
+ url_ = nil, encoding_ = nil, options_ = XML::ParseOptions::DEFAULT_XML,
59
+ url: url_, encoding: encoding_, options: options_
60
+ )
61
+ options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
62
+ yield options if block_given?
63
+
64
+ url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
65
+
66
+ if empty_doc?(string_or_io)
67
+ if options.strict?
68
+ raise Nokogiri::XML::SyntaxError, "Empty document"
69
+ else
70
+ return encoding ? new.tap { |i| i.encoding = encoding } : new
71
+ end
72
+ end
73
+
74
+ doc = if string_or_io.respond_to?(:read)
75
+ # TODO: should we instead check for respond_to?(:to_path) ?
76
+ if string_or_io.is_a?(Pathname)
77
+ # resolve the Pathname to the file and open it as an IO object, see #2110
78
+ string_or_io = string_or_io.expand_path.open
79
+ url ||= string_or_io.path
80
+ end
81
+
82
+ read_io(string_or_io, url, encoding, options.to_i)
83
+ else
84
+ # read_memory pukes on empty docs
85
+ read_memory(string_or_io, url, encoding, options.to_i)
86
+ end
87
+
88
+ # do xinclude processing; the document is freshly parsed and unexposed to Ruby, so the
89
+ # defensive copy is unnecessary
90
+ doc.do_xinclude(options, safe_copy: false) if options.xinclude?
91
+
92
+ doc
93
+ end
94
+
95
+ private
96
+
97
+ def empty_doc?(string_or_io)
98
+ string_or_io.nil? ||
99
+ (string_or_io.respond_to?(:empty?) && string_or_io.empty?) ||
100
+ (string_or_io.respond_to?(:eof?) && string_or_io.eof?)
101
+ end
102
+ end
103
+
104
+ ##
105
+ # :singleton-method: wrap
106
+ # :call-seq: wrap(java_document) → Nokogiri::XML::Document
107
+ #
108
+ # ⚠ This method is only available when running JRuby.
109
+ #
110
+ # Create a Document using an existing Java DOM document object.
111
+ #
112
+ # The returned Document shares the same underlying data structure as the Java object, so
113
+ # changes in one are reflected in the other.
114
+ #
115
+ # [Parameters]
116
+ # - `java_document` (Java::OrgW3cDom::Document)
117
+ # (The class `Java::OrgW3cDom::Document` is also accessible as `org.w3c.dom.Document`.)
118
+ #
119
+ # [Returns] Nokogiri::XML::Document
120
+ #
121
+ # See also \#to_java
122
+
123
+ # :method: to_java
124
+ # :call-seq: to_java() → Java::OrgW3cDom::Document
125
+ #
126
+ # ⚠ This method is only available when running JRuby.
127
+ #
128
+ # Returns the underlying Java DOM document object for this document.
129
+ #
130
+ # The returned Java object shares the same underlying data structure as this document, so
131
+ # changes in one are reflected in the other.
132
+ #
133
+ # [Returns]
134
+ # Java::OrgW3cDom::Document
135
+ # (The class `Java::OrgW3cDom::Document` is also accessible as `org.w3c.dom.Document`.)
136
+ #
137
+ # See also Document.wrap
138
+
139
+ # The errors found while parsing a document.
140
+ #
141
+ # [Returns] Array<Nokogiri::XML::SyntaxError>
142
+ attr_accessor :errors
143
+
144
+ # When `true`, reparented elements without a namespace will inherit their new parent's
145
+ # namespace (if one exists). Defaults to `false`.
146
+ #
147
+ # [Returns] Boolean
148
+ #
149
+ # *Example:* Default behavior of namespace inheritance
150
+ #
151
+ # xml = <<~EOF
152
+ # <root xmlns:foo="http://nokogiri.org/default_ns/test/foo">
153
+ # <foo:parent>
154
+ # </foo:parent>
155
+ # </root>
156
+ # EOF
157
+ # doc = Nokogiri::XML(xml)
158
+ # parent = doc.at_xpath("//foo:parent", "foo" => "http://nokogiri.org/default_ns/test/foo")
159
+ # parent.add_child("<child></child>")
160
+ # doc.to_xml
161
+ # # => <?xml version="1.0"?>
162
+ # # <root xmlns:foo="http://nokogiri.org/default_ns/test/foo">
163
+ # # <foo:parent>
164
+ # # <child/>
165
+ # # </foo:parent>
166
+ # # </root>
167
+ #
168
+ # *Example:* Setting namespace inheritance to `true`
169
+ #
170
+ # xml = <<~EOF
171
+ # <root xmlns:foo="http://nokogiri.org/default_ns/test/foo">
172
+ # <foo:parent>
173
+ # </foo:parent>
174
+ # </root>
175
+ # EOF
176
+ # doc = Nokogiri::XML(xml)
177
+ # doc.namespace_inheritance = true
178
+ # parent = doc.at_xpath("//foo:parent", "foo" => "http://nokogiri.org/default_ns/test/foo")
179
+ # parent.add_child("<child></child>")
180
+ # doc.to_xml
181
+ # # => <?xml version="1.0"?>
182
+ # # <root xmlns:foo="http://nokogiri.org/default_ns/test/foo">
183
+ # # <foo:parent>
184
+ # # <foo:child/>
185
+ # # </foo:parent>
186
+ # # </root>
187
+ #
188
+ # Since v1.12.4
189
+ attr_accessor :namespace_inheritance
190
+
191
+ def initialize(*args) # :nodoc: # rubocop:disable Lint/MissingSuper
192
+ @errors = []
193
+ @decorators = nil
194
+ @namespace_inheritance = false
195
+ end
196
+
197
+ #
198
+ # :call-seq:
199
+ # dup → Nokogiri::XML::Document
200
+ # dup(level) → Nokogiri::XML::Document
201
+ #
202
+ # Duplicate this node.
203
+ #
204
+ # [Parameters]
205
+ # - +level+ (optional Integer). 0 is a shallow copy, 1 (the default) is a deep copy.
206
+ # [Returns] The new Nokogiri::XML::Document
207
+ #
208
+ def dup(level = 1)
209
+ copy = OBJECT_DUP_METHOD.bind_call(self)
210
+ copy.initialize_copy_with_args(self, level)
211
+ end
212
+
213
+ #
214
+ # :call-seq:
215
+ # clone → Nokogiri::XML::Document
216
+ # clone(level) → Nokogiri::XML::Document
217
+ #
218
+ # Clone this node.
219
+ #
220
+ # [Parameters]
221
+ # - +level+ (optional Integer). 0 is a shallow copy, 1 (the default) is a deep copy.
222
+ # [Returns] The new Nokogiri::XML::Document
223
+ #
224
+ def clone(level = 1)
225
+ copy = OBJECT_CLONE_METHOD.bind_call(self)
226
+ copy.initialize_copy_with_args(self, level)
227
+ end
228
+
229
+ # :call-seq:
230
+ # create_element(name, *contents_or_attrs, &block) → Nokogiri::XML::Element
231
+ #
232
+ # Create a new Element with `name` belonging to this document, optionally setting contents or
233
+ # attributes.
234
+ #
235
+ # This method is _not_ the most user-friendly option if your intention is to add a node to the
236
+ # document tree. Prefer one of the Nokogiri::XML::Node methods like Node#add_child,
237
+ # Node#add_next_sibling, Node#replace, etc. which will both create an element (or subtree) and
238
+ # place it in the document tree.
239
+ #
240
+ # Arguments may be passed to initialize the element:
241
+ #
242
+ # - a Hash argument will be used to set attributes
243
+ # - a non-Hash object that responds to \#to_s will be used to set the new node's contents
244
+ #
245
+ # A block may be passed to mutate the node.
246
+ #
247
+ # [Parameters]
248
+ # - `name` (String)
249
+ # - `contents_or_attrs` (\#to_s, Hash)
250
+ # [Yields] `node` (Nokogiri::XML::Element)
251
+ # [Returns] Nokogiri::XML::Element
252
+ #
253
+ # *Example:* An empty element without attributes
254
+ #
255
+ # doc.create_element("div")
256
+ # # => <div></div>
257
+ #
258
+ # *Example:* An element with contents
259
+ #
260
+ # doc.create_element("div", "contents")
261
+ # # => <div>contents</div>
262
+ #
263
+ # *Example:* An element with attributes
264
+ #
265
+ # doc.create_element("div", {"class" => "container"})
266
+ # # => <div class='container'></div>
267
+ #
268
+ # *Example:* An element with contents and attributes
269
+ #
270
+ # doc.create_element("div", "contents", {"class" => "container"})
271
+ # # => <div class='container'>contents</div>
272
+ #
273
+ # *Example:* Passing a block to mutate the element
274
+ #
275
+ # doc.create_element("div") { |node| node["class"] = "blue" if before_noon? }
276
+ #
277
+ def create_element(name, *contents_or_attrs, &block)
278
+ elm = Nokogiri::XML::Element.new(name, self, &block)
279
+ contents_or_attrs.each do |arg|
280
+ case arg
281
+ when Hash
282
+ arg.each do |k, v|
283
+ key = k.to_s
284
+ if key =~ NCNAME_RE
285
+ ns_name = Regexp.last_match(1)
286
+ elm.add_namespace_definition(ns_name, v)
287
+ else
288
+ elm[k.to_s] = v.to_s
289
+ end
290
+ end
291
+ else
292
+ elm.content = arg
293
+ end
294
+ end
295
+ if (ns = elm.namespace_definitions.find { |n| n.prefix.nil? || (n.prefix == "") })
296
+ elm.namespace = ns
297
+ end
298
+ elm
299
+ end
300
+
301
+ # Create a Text Node with +string+
302
+ def create_text_node(string, &block)
303
+ Nokogiri::XML::Text.new(string.to_s, self, &block)
304
+ end
305
+
306
+ # Create a CDATA Node containing +string+
307
+ def create_cdata(string, &block)
308
+ Nokogiri::XML::CDATA.new(self, string.to_s, &block)
309
+ end
310
+
311
+ # Create a Comment Node containing +string+
312
+ def create_comment(string, &block)
313
+ Nokogiri::XML::Comment.new(self, string.to_s, &block)
314
+ end
315
+
316
+ # The name of this document. Always returns "document"
317
+ def name
318
+ "document"
319
+ end
320
+
321
+ # A reference to +self+
322
+ def document
323
+ self
324
+ end
325
+
326
+ # :call-seq:
327
+ # collect_namespaces() → Hash<String(Namespace#prefix) ⇒ String(Namespace#href)>
328
+ #
329
+ # Recursively get all namespaces from this node and its subtree and return them as a
330
+ # hash.
331
+ #
332
+ # ⚠ This method will not handle duplicate namespace prefixes, since the return value is a hash.
333
+ #
334
+ # Note that this method does an xpath lookup for nodes with namespaces, and as a result the
335
+ # order (and which duplicate prefix "wins") may be dependent on the implementation of the
336
+ # underlying XML library.
337
+ #
338
+ # *Example:* Basic usage
339
+ #
340
+ # Given this document:
341
+ #
342
+ # <root xmlns="default" xmlns:foo="bar">
343
+ # <bar xmlns:hello="world" />
344
+ # </root>
345
+ #
346
+ # This method will return:
347
+ #
348
+ # {"xmlns:foo"=>"bar", "xmlns"=>"default", "xmlns:hello"=>"world"}
349
+ #
350
+ # *Example:* Duplicate prefixes
351
+ #
352
+ # Given this document:
353
+ #
354
+ # <root xmlns:foo="bar">
355
+ # <bar xmlns:foo="baz" />
356
+ # </root>
357
+ #
358
+ # The hash returned will be something like:
359
+ #
360
+ # {"xmlns:foo" => "baz"}
361
+ #
362
+ def collect_namespaces
363
+ xpath("//namespace::*").each_with_object({}) do |ns, hash|
364
+ hash[["xmlns", ns.prefix].compact.join(":")] = ns.href if ns.prefix != "xml"
365
+ end
366
+ end
367
+
368
+ # Get the list of decorators given +key+
369
+ def decorators(key)
370
+ @decorators ||= {}
371
+ @decorators[key] ||= []
372
+ end
373
+
374
+ ##
375
+ # Validate this Document against its DTD. Returns a list of errors on
376
+ # the document or +nil+ when there is no DTD.
377
+ def validate
378
+ return unless internal_subset
379
+
380
+ internal_subset.validate(self)
381
+ end
382
+
383
+ ##
384
+ # Explore a document with shortcut methods. See Nokogiri::Slop for details.
385
+ #
386
+ # Note that any nodes that have been instantiated before #slop!
387
+ # is called will not be decorated with sloppy behavior. So, if you're in
388
+ # irb, the preferred idiom is:
389
+ #
390
+ # irb> doc = Nokogiri::Slop my_markup
391
+ #
392
+ # and not
393
+ #
394
+ # irb> doc = Nokogiri::HTML my_markup
395
+ # ... followed by irb's implicit inspect (and therefore instantiation of every node) ...
396
+ # irb> doc.slop!
397
+ # ... which does absolutely nothing.
398
+ #
399
+ def slop!
400
+ unless decorators(XML::Node).include?(Nokogiri::Decorators::Slop)
401
+ decorators(XML::Node) << Nokogiri::Decorators::Slop
402
+ decorate!
403
+ end
404
+
405
+ self
406
+ end
407
+
408
+ ##
409
+ # Apply any decorators to +node+
410
+ def decorate(node)
411
+ return unless @decorators
412
+
413
+ @decorators.each do |klass, list|
414
+ next unless node.is_a?(klass)
415
+
416
+ list.each { |mod| node.extend(mod) }
417
+ end
418
+ end
419
+
420
+ alias_method :to_xml, :serialize
421
+
422
+ # Get the hash of namespaces on the root Nokogiri::XML::Node
423
+ def namespaces
424
+ root ? root.namespaces : {}
425
+ end
426
+
427
+ ##
428
+ # Create a Nokogiri::XML::DocumentFragment from +tags+
429
+ # Returns an empty fragment if +tags+ is nil.
430
+ def fragment(tags = nil)
431
+ DocumentFragment.new(self, tags, root)
432
+ end
433
+
434
+ undef_method :swap, :parent, :namespace, :default_namespace=
435
+ undef_method :add_namespace_definition, :attributes
436
+ undef_method :namespace_definitions, :line, :add_namespace
437
+
438
+ def add_child(node_or_tags)
439
+ raise "A document may not have multiple root nodes." if (root && root.name != "nokogiri_text_wrapper") && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
440
+
441
+ node_or_tags = coerce(node_or_tags)
442
+ if node_or_tags.is_a?(XML::NodeSet)
443
+ raise "A document may not have multiple root nodes." if node_or_tags.size > 1
444
+
445
+ super(node_or_tags.first)
446
+ else
447
+ super
448
+ end
449
+ end
450
+ alias_method :<<, :add_child
451
+
452
+ # :call-seq:
453
+ # xpath_doctype() → Nokogiri::CSS::XPathVisitor::DoctypeConfig
454
+ #
455
+ # [Returns] The document type which determines CSS-to-XPath translation.
456
+ #
457
+ # See XPathVisitor for more information.
458
+ def xpath_doctype
459
+ Nokogiri::CSS::XPathVisitor::DoctypeConfig::XML
460
+ end
461
+
462
+ #
463
+ # :call-seq: deconstruct_keys(array_of_names) → Hash
464
+ #
465
+ # Returns a hash describing the Document, to use in pattern matching.
466
+ #
467
+ # Valid keys and their values:
468
+ # - +root+ → (Node, nil) The root node of the Document, or +nil+ if the document is empty.
469
+ #
470
+ # In the future, other keys may allow accessing things like doctype and processing
471
+ # instructions. If you have a use case and would like this functionality, please let us know
472
+ # by opening an issue or a discussion on the github project.
473
+ #
474
+ # *Example*
475
+ #
476
+ # doc = Nokogiri::XML.parse(<<~XML)
477
+ # <?xml version="1.0"?>
478
+ # <root>
479
+ # <child>
480
+ # </root>
481
+ # XML
482
+ #
483
+ # doc.deconstruct_keys([:root])
484
+ # # => {:root=>
485
+ # # #(Element:0x35c {
486
+ # # name = "root",
487
+ # # children = [
488
+ # # #(Text "\n" + " "),
489
+ # # #(Element:0x370 { name = "child", children = [ #(Text "\n")] }),
490
+ # # #(Text "\n")]
491
+ # # })}
492
+ #
493
+ # *Example* of an empty document
494
+ #
495
+ # doc = Nokogiri::XML::Document.new
496
+ #
497
+ # doc.deconstruct_keys([:root])
498
+ # # => {:root=>nil}
499
+ #
500
+ # Since v1.14.0
501
+ #
502
+ def deconstruct_keys(keys)
503
+ { root: root }
504
+ end
505
+
506
+ private
507
+
508
+ IMPLIED_XPATH_CONTEXTS = ["//"].freeze # :nodoc:
509
+
510
+ def inspect_attributes
511
+ [:name, :children]
512
+ end
513
+ end
514
+ end
515
+ end