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,1701 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "stringio"
5
+
6
+ module Nokogiri
7
+ module XML
8
+ # Nokogiri::XML::Node is the primary API you'll use to interact with your Document.
9
+ #
10
+ # == Attributes
11
+ #
12
+ # A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes. For
13
+ # example:
14
+ #
15
+ # node = Nokogiri::XML::DocumentFragment.parse("<a href='#foo' id='link'>link</a>").at_css("a")
16
+ # node.to_html # => "<a href=\"#foo\" id=\"link\">link</a>"
17
+ # node['href'] # => "#foo"
18
+ # node.keys # => ["href", "id"]
19
+ # node.values # => ["#foo", "link"]
20
+ # node['class'] = 'green' # => "green"
21
+ # node.to_html # => "<a href=\"#foo\" id=\"link\" class=\"green\">link</a>"
22
+ #
23
+ # See the method group entitled Node@Working+With+Node+Attributes for the full set of methods.
24
+ #
25
+ # == Navigation
26
+ #
27
+ # Nokogiri::XML::Node also has methods that let you move around your tree:
28
+ #
29
+ # [#parent, #children, #next, #previous]
30
+ # Navigate up, down, or through siblings.
31
+ #
32
+ # See the method group entitled Node@Traversing+Document+Structure for the full set of methods.
33
+ #
34
+ # == Serialization
35
+ #
36
+ # When printing or otherwise emitting a document or a node (and its subtree), there are a few
37
+ # methods you might want to use:
38
+ #
39
+ # [#content, #text, #inner_text, #to_str]
40
+ # These methods will all **emit plaintext**,
41
+ # meaning that entities will be replaced (e.g., +&lt;+ will be replaced with +<+), meaning
42
+ # that any sanitizing will likely be un-done in the output.
43
+ #
44
+ # [#to_s, #to_xml, #to_html, #inner_html]
45
+ # These methods will all **emit properly-escaped markup**, meaning that it's suitable for
46
+ # consumption by browsers, parsers, etc.
47
+ #
48
+ # See the method group entitled Node@Serialization+and+Generating+Output for the full set of methods.
49
+ #
50
+ # == Searching
51
+ #
52
+ # You may search this node's subtree using methods like #xpath and #css.
53
+ #
54
+ # See the method group entitled Node@Searching+via+XPath+or+CSS+Queries for the full set of methods.
55
+ #
56
+ class Node
57
+ include Nokogiri::XML::PP::Node
58
+ include Nokogiri::XML::Searchable
59
+ include Nokogiri::ClassResolver
60
+ include Enumerable
61
+
62
+ # Element node type, see Nokogiri::XML::Node#element?
63
+ ELEMENT_NODE = 1
64
+ # Attribute node type
65
+ ATTRIBUTE_NODE = 2
66
+ # Text node type, see Nokogiri::XML::Node#text?
67
+ TEXT_NODE = 3
68
+ # CDATA node type, see Nokogiri::XML::Node#cdata?
69
+ CDATA_SECTION_NODE = 4
70
+ # Entity reference node type
71
+ ENTITY_REF_NODE = 5
72
+ # Entity node type
73
+ ENTITY_NODE = 6
74
+ # PI node type
75
+ PI_NODE = 7
76
+ # Comment node type, see Nokogiri::XML::Node#comment?
77
+ COMMENT_NODE = 8
78
+ # Document node type, see Nokogiri::XML::Node#xml?
79
+ DOCUMENT_NODE = 9
80
+ # Document type node type
81
+ DOCUMENT_TYPE_NODE = 10
82
+ # Document fragment node type
83
+ DOCUMENT_FRAG_NODE = 11
84
+ # Notation node type
85
+ NOTATION_NODE = 12
86
+ # HTML document node type, see Nokogiri::XML::Node#html?
87
+ HTML_DOCUMENT_NODE = 13
88
+ # DTD node type
89
+ DTD_NODE = 14
90
+ # Element declaration type
91
+ ELEMENT_DECL = 15
92
+ # Attribute declaration type
93
+ ATTRIBUTE_DECL = 16
94
+ # Entity declaration type
95
+ ENTITY_DECL = 17
96
+ # Namespace declaration type
97
+ NAMESPACE_DECL = 18
98
+ # XInclude start type
99
+ XINCLUDE_START = 19
100
+ # XInclude end type
101
+ XINCLUDE_END = 20
102
+ # DOCB document node type
103
+ DOCB_DOCUMENT_NODE = 21
104
+
105
+ #
106
+ # :call-seq:
107
+ # new(name, document) -> Nokogiri::XML::Node
108
+ # new(name, document) { |node| ... } -> Nokogiri::XML::Node
109
+ #
110
+ # Create a new node with +name+ that belongs to +document+.
111
+ #
112
+ # If you intend to add a node to a document tree, it's likely that you will prefer one of the
113
+ # Nokogiri::XML::Node methods like #add_child, #add_next_sibling, #replace, etc. which will
114
+ # both create an element (or subtree) and place it in the document tree.
115
+ #
116
+ # Another alternative, if you are concerned about performance, is
117
+ # Nokogiri::XML::Document#create_element which accepts additional arguments for contents or
118
+ # attributes but (like this method) avoids parsing markup.
119
+ #
120
+ # [Parameters]
121
+ # - +name+ (String)
122
+ # - +document+ (Nokogiri::XML::Document) The document to which the the returned node will belong.
123
+ # [Yields] Nokogiri::XML::Node
124
+ # [Returns] Nokogiri::XML::Node
125
+ #
126
+ def initialize(name, document)
127
+ # This is intentionally empty, and sets the method signature for subclasses.
128
+ end
129
+
130
+ #
131
+ # :call-seq:
132
+ # dup → Nokogiri::XML::Node
133
+ # dup(level) → Nokogiri::XML::Node
134
+ # dup(level, new_parent_doc) → Nokogiri::XML::Node
135
+ #
136
+ # Duplicate this node.
137
+ #
138
+ # [Parameters]
139
+ # - +level+ (optional Integer). 0 is a shallow copy, 1 (the default) is a deep copy.
140
+ # - +new_parent_doc+ (optional Nokogiri::XML::Document)
141
+ # The new node's parent Document. Defaults to the the Document of the current node.
142
+ # [Returns] The new Nokogiri::XML::Node
143
+ #
144
+ def dup(level = 1, new_parent_doc = document)
145
+ super().initialize_copy_with_args(self, level, new_parent_doc)
146
+ end
147
+
148
+ #
149
+ # :call-seq:
150
+ # clone → Nokogiri::XML::Node
151
+ # clone(level) → Nokogiri::XML::Node
152
+ # clone(level, new_parent_doc) → Nokogiri::XML::Node
153
+ #
154
+ # Clone this node.
155
+ #
156
+ # [Parameters]
157
+ # - +level+ (optional Integer). 0 is a shallow copy, 1 (the default) is a deep copy.
158
+ # - +new_parent_doc+
159
+ # The new node's parent Document. Defaults to the the Document of the current node.
160
+ # [Returns] The new Nokogiri::XML::Node
161
+ #
162
+ def clone(level = 1, new_parent_doc = document)
163
+ super().initialize_copy_with_args(self, level, new_parent_doc)
164
+ end
165
+
166
+ ###
167
+ # Decorate this node with the decorators set up in this node's Document
168
+ def decorate!
169
+ document.decorate(self)
170
+ end
171
+
172
+ # :section: Manipulating Document Structure
173
+
174
+ ###
175
+ # Add +node_or_tags+ as a child of this Node.
176
+ #
177
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
178
+ # containing markup.
179
+ #
180
+ # Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is
181
+ # a DocumentFragment, NodeSet, or String).
182
+ #
183
+ # Also see related method +<<+.
184
+ def add_child(node_or_tags)
185
+ node_or_tags = coerce(node_or_tags)
186
+ if node_or_tags.is_a?(XML::NodeSet)
187
+ node_or_tags.each { |n| add_child_node_and_reparent_attrs(n) }
188
+ else
189
+ add_child_node_and_reparent_attrs(node_or_tags)
190
+ end
191
+ node_or_tags
192
+ end
193
+
194
+ ###
195
+ # Add +node_or_tags+ as the first child of this Node.
196
+ #
197
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
198
+ # containing markup.
199
+ #
200
+ # Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is
201
+ # a DocumentFragment, NodeSet, or String).
202
+ #
203
+ # Also see related method +add_child+.
204
+ def prepend_child(node_or_tags)
205
+ if (first = children.first)
206
+ # Mimic the error add_child would raise.
207
+ raise "Document already has a root node" if document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
208
+
209
+ first.__send__(:add_sibling, :previous, node_or_tags)
210
+ else
211
+ add_child(node_or_tags)
212
+ end
213
+ end
214
+
215
+ # :call-seq:
216
+ # wrap(markup) -> self
217
+ # wrap(node) -> self
218
+ #
219
+ # Wrap this Node with the node parsed from +markup+ or a dup of the +node+.
220
+ #
221
+ # [Parameters]
222
+ # - *markup* (String)
223
+ # Markup that is parsed and used as the wrapper. This node's parent, if it exists, is used
224
+ # as the context node for parsing; otherwise the associated document is used. If the parsed
225
+ # fragment has multiple roots, the first root node is used as the wrapper.
226
+ # - *node* (Nokogiri::XML::Node)
227
+ # An element that is `#dup`ed and used as the wrapper.
228
+ #
229
+ # [Returns] +self+, to support chaining.
230
+ #
231
+ # Also see NodeSet#wrap
232
+ #
233
+ # *Example* with a +String+ argument:
234
+ #
235
+ # doc = Nokogiri::HTML5(<<~HTML)
236
+ # <html><body>
237
+ # <a>asdf</a>
238
+ # </body></html>
239
+ # HTML
240
+ # doc.at_css("a").wrap("<div></div>")
241
+ # doc.to_html
242
+ # # => <html><head></head><body>
243
+ # # <div><a>asdf</a></div>
244
+ # # </body></html>
245
+ #
246
+ # *Example* with a +Node+ argument:
247
+ #
248
+ # doc = Nokogiri::HTML5(<<~HTML)
249
+ # <html><body>
250
+ # <a>asdf</a>
251
+ # </body></html>
252
+ # HTML
253
+ # doc.at_css("a").wrap(doc.create_element("div"))
254
+ # doc.to_html
255
+ # # <html><head></head><body>
256
+ # # <div><a>asdf</a></div>
257
+ # # </body></html>
258
+ #
259
+ def wrap(node_or_tags)
260
+ case node_or_tags
261
+ when String
262
+ context_node = parent || document
263
+ new_parent = context_node.coerce(node_or_tags).first
264
+ if new_parent.nil?
265
+ raise "Failed to parse '#{node_or_tags}' in the context of a '#{context_node.name}' element"
266
+ end
267
+ when Node
268
+ new_parent = node_or_tags.dup
269
+ else
270
+ raise ArgumentError, "Requires a String or Node argument, and cannot accept a #{node_or_tags.class}"
271
+ end
272
+
273
+ if parent
274
+ add_next_sibling(new_parent)
275
+ else
276
+ new_parent.unlink
277
+ end
278
+ new_parent.add_child(self)
279
+
280
+ self
281
+ end
282
+
283
+ ###
284
+ # Add +node_or_tags+ as a child of this Node.
285
+ #
286
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
287
+ # containing markup.
288
+ #
289
+ # Returns +self+, to support chaining of calls (e.g., root << child1 << child2)
290
+ #
291
+ # Also see related method +add_child+.
292
+ def <<(node_or_tags)
293
+ add_child(node_or_tags)
294
+ self
295
+ end
296
+
297
+ ###
298
+ # Insert +node_or_tags+ before this Node (as a sibling).
299
+ #
300
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
301
+ # containing markup.
302
+ #
303
+ # Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is
304
+ # a DocumentFragment, NodeSet, or String).
305
+ #
306
+ # Also see related method +before+.
307
+ def add_previous_sibling(node_or_tags)
308
+ raise ArgumentError,
309
+ "A document may not have multiple root nodes." if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
310
+
311
+ add_sibling(:previous, node_or_tags)
312
+ end
313
+
314
+ ###
315
+ # Insert +node_or_tags+ after this Node (as a sibling).
316
+ #
317
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
318
+ # containing markup.
319
+ #
320
+ # Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is
321
+ # a DocumentFragment, NodeSet, or String).
322
+ #
323
+ # Also see related method +after+.
324
+ def add_next_sibling(node_or_tags)
325
+ raise ArgumentError,
326
+ "A document may not have multiple root nodes." if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
327
+
328
+ add_sibling(:next, node_or_tags)
329
+ end
330
+
331
+ ####
332
+ # Insert +node_or_tags+ before this node (as a sibling).
333
+ #
334
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
335
+ # containing markup.
336
+ #
337
+ # Returns +self+, to support chaining of calls.
338
+ #
339
+ # Also see related method +add_previous_sibling+.
340
+ def before(node_or_tags)
341
+ add_previous_sibling(node_or_tags)
342
+ self
343
+ end
344
+
345
+ ####
346
+ # Insert +node_or_tags+ after this node (as a sibling).
347
+ #
348
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a String
349
+ # containing markup.
350
+ #
351
+ # Returns +self+, to support chaining of calls.
352
+ #
353
+ # Also see related method +add_next_sibling+.
354
+ def after(node_or_tags)
355
+ add_next_sibling(node_or_tags)
356
+ self
357
+ end
358
+
359
+ ####
360
+ # Set the content for this Node to +node_or_tags+.
361
+ #
362
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a String
363
+ # containing markup.
364
+ #
365
+ # ⚠ Please note that despite the name, this method will *not* always parse a String argument
366
+ # as HTML. A String argument will be parsed with the +DocumentFragment+ parser related to this
367
+ # node's document.
368
+ #
369
+ # For example, if the document is an HTML4::Document then the string will be parsed as HTML4
370
+ # using HTML4::DocumentFragment; but if the document is an XML::Document then it will
371
+ # parse the string as XML using XML::DocumentFragment.
372
+ #
373
+ # Also see related method +children=+
374
+ def inner_html=(node_or_tags)
375
+ self.children = node_or_tags
376
+ end
377
+
378
+ ####
379
+ # Set the content for this Node +node_or_tags+
380
+ #
381
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a String
382
+ # containing markup.
383
+ #
384
+ # Also see related method +inner_html=+
385
+ def children=(node_or_tags)
386
+ node_or_tags = coerce(node_or_tags)
387
+ children.unlink
388
+ if node_or_tags.is_a?(XML::NodeSet)
389
+ node_or_tags.each { |n| add_child_node_and_reparent_attrs(n) }
390
+ else
391
+ add_child_node_and_reparent_attrs(node_or_tags)
392
+ end
393
+ end
394
+
395
+ ####
396
+ # Replace this Node with +node_or_tags+.
397
+ #
398
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
399
+ # containing markup.
400
+ #
401
+ # Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is
402
+ # a DocumentFragment, NodeSet, or String).
403
+ #
404
+ # Also see related method +swap+.
405
+ def replace(node_or_tags)
406
+ raise("Cannot replace a node with no parent") unless parent
407
+
408
+ # We cannot replace a text node directly, otherwise libxml will return
409
+ # an internal error at parser.c:13031, I don't know exactly why
410
+ # libxml is trying to find a parent node that is an element or document
411
+ # so I can't tell if this is bug in libxml or not. issue #775.
412
+ if text?
413
+ replacee = Nokogiri::XML::Node.new("dummy", document)
414
+ add_previous_sibling_node(replacee)
415
+ unlink
416
+ return replacee.replace(node_or_tags)
417
+ end
418
+
419
+ node_or_tags = parent.coerce(node_or_tags)
420
+
421
+ if node_or_tags.is_a?(XML::NodeSet)
422
+ node_or_tags.each { |n| add_previous_sibling(n) }
423
+ unlink
424
+ else
425
+ replace_node(node_or_tags)
426
+ end
427
+ node_or_tags
428
+ end
429
+
430
+ ####
431
+ # Swap this Node for +node_or_tags+
432
+ #
433
+ # +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
434
+ # Containing markup.
435
+ #
436
+ # Returns self, to support chaining of calls.
437
+ #
438
+ # Also see related method +replace+.
439
+ def swap(node_or_tags)
440
+ replace(node_or_tags)
441
+ self
442
+ end
443
+
444
+ ####
445
+ # call-seq:
446
+ # content=(input)
447
+ #
448
+ # Set the content of this node to +input+.
449
+ #
450
+ # [Parameters]
451
+ # - +input+ (String) The new content for this node. Input is considered to be raw content, and
452
+ # so will be entity-escaped in the final DOM string.
453
+ #
454
+ # [Example]
455
+ # Note how entities are handled:
456
+ #
457
+ # doc = Nokogiri::HTML::Document.parse(<<~HTML)
458
+ # <html>
459
+ # <body>
460
+ # <div id="first">asdf</div>
461
+ # <div id="second">asdf</div>
462
+ # HTML
463
+ #
464
+ # text_node = doc.at_css("div#first").children.first
465
+ # div_node = doc.at_css("div#second")
466
+ #
467
+ # value = "You &amp; Me"
468
+ #
469
+ # text_node.content = value
470
+ # div_node.content = value
471
+ #
472
+ # doc.css("div").to_html
473
+ # # => "<div id=\"first\">You &amp;amp; Me</div>
474
+ # # <div id=\"second\">You &amp;amp; Me</div>"
475
+ #
476
+ # For content that is already entity-escaped, use CGI::unescapeHTML to decode it:
477
+ #
478
+ # text_node.content = CGI::unescapeHTML(value)
479
+ # div_node.content = CGI::unescapeHTML(value)
480
+ #
481
+ # doc.css("div").to_html
482
+ # # => "<div id=\"first\">You &amp; Me</div>
483
+ # # <div id=\"second\">You &amp; Me</div>"
484
+ #
485
+ # See also: #native_content=
486
+ #
487
+ def content=(string)
488
+ self.native_content = encode_special_chars(string.to_s)
489
+ end
490
+
491
+ ###
492
+ # Set the parent Node for this Node
493
+ def parent=(parent_node)
494
+ parent_node.add_child(self)
495
+ end
496
+
497
+ ###
498
+ # Adds a default namespace supplied as a string +url+ href, to self.
499
+ # The consequence is as an xmlns attribute with supplied argument were
500
+ # present in parsed XML. A default namespace set with this method will
501
+ # now show up in #attributes, but when this node is serialized to XML an
502
+ # "xmlns" attribute will appear. See also #namespace and #namespace=
503
+ def default_namespace=(url)
504
+ add_namespace_definition(nil, url)
505
+ end
506
+
507
+ ###
508
+ # Set the default namespace on this node (as would be defined with an
509
+ # "xmlns=" attribute in XML source), as a Namespace object +ns+. Note that
510
+ # a Namespace added this way will NOT be serialized as an xmlns attribute
511
+ # for this node. You probably want #default_namespace= instead, or perhaps
512
+ # #add_namespace_definition with a nil prefix argument.
513
+ def namespace=(ns)
514
+ return set_namespace(ns) unless ns
515
+
516
+ unless Nokogiri::XML::Namespace === ns
517
+ raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace"
518
+ end
519
+ if ns.document != document
520
+ raise ArgumentError, "namespace must be declared on the same document"
521
+ end
522
+
523
+ set_namespace(ns)
524
+ end
525
+
526
+ XINCLUDE_NAMESPACES = {
527
+ "xi2001" => "http://www.w3.org/2001/XInclude",
528
+ "xi2003" => "http://www.w3.org/2003/XInclude",
529
+ }.freeze
530
+ private_constant :XINCLUDE_NAMESPACES
531
+
532
+ # Every top-level <xi:include> in the subtree, in either XInclude namespace, excluding
533
+ # includes nested inside another include's fallback (libxml2 only expands those if the
534
+ # parent include fails).
535
+ XINCLUDE_QUERY =
536
+ "descendant-or-self::xi2001:include[not(ancestor::xi2001:include) and not(ancestor::xi2003:include)] | " \
537
+ "descendant-or-self::xi2003:include[not(ancestor::xi2001:include) and not(ancestor::xi2003:include)]"
538
+ private_constant :XINCLUDE_QUERY
539
+
540
+ ###
541
+ # :call-seq:
542
+ # do_xinclude(options = ParseOptions::DEFAULT_XML, safe_copy: true) → self
543
+ # do_xinclude(options = ParseOptions::DEFAULT_XML, safe_copy: true) { |options| ... } → self
544
+ #
545
+ # Do XInclude substitution on the subtree below this node, replacing each +<xi:include>+ with
546
+ # the content it references.
547
+ #
548
+ # [Parameters]
549
+ # - +options+ (Nokogiri::XML::ParseOptions) The parser options for the substitution. (default
550
+ # +ParseOptions::DEFAULT_XML+)
551
+ #
552
+ # [Optional Keyword Arguments]
553
+ # - +safe_copy:+ (Boolean) Operate on a defensive copy of each +<xi:include>+ element, to
554
+ # prevent libxml2 from freeing memory that is bound to live Ruby objects. (default +true+)
555
+ #
556
+ # When +true+, each +<xi:include>+ is processed on an unwrapped copy of itself, so libxml2
557
+ # frees the copy while the original node is unlinked from the document and kept alive. This
558
+ # prevents a use-after-free when the +<xi:include>+ node, or any of its descendants or
559
+ # namespaces, has already been exposed to Ruby; as a consequence such a wrapped node ends up
560
+ # detached from the document rather than removed or converted in place.
561
+ #
562
+ # When +false+, the document is processed in place. This is faster but only safe when nothing
563
+ # in the subtree has been exposed to Ruby (for example, immediately after parsing), which is
564
+ # why Document.parse uses it.
565
+ #
566
+ # This option has no effect on the pure-Java backend, which performs XInclude substitution
567
+ # during parsing.
568
+ #
569
+ # [Yields]
570
+ # If a block is given, a Nokogiri::XML::ParseOptions object initialized from +options+ is
571
+ # yielded to it, which can be configured before substitution.
572
+ #
573
+ # [Returns] +self+ (Nokogiri::XML::Node)
574
+ def do_xinclude(options = XML::ParseOptions::DEFAULT_XML, safe_copy: true)
575
+ options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
576
+ yield options if block_given?
577
+
578
+ if safe_copy && Nokogiri.uses_libxml?
579
+ xpath(XINCLUDE_QUERY, XINCLUDE_NAMESPACES).each do |include_node|
580
+ include_node.safe_process_xinclude(options.to_i)
581
+ end
582
+ else
583
+ process_xincludes(options.to_i)
584
+ end
585
+
586
+ self
587
+ end
588
+
589
+ alias_method :next, :next_sibling
590
+ alias_method :previous, :previous_sibling
591
+ alias_method :next=, :add_next_sibling
592
+ alias_method :previous=, :add_previous_sibling
593
+ alias_method :remove, :unlink
594
+ alias_method :name=, :node_name=
595
+ alias_method :add_namespace, :add_namespace_definition
596
+
597
+ # :section:
598
+
599
+ alias_method :inner_text, :content
600
+ alias_method :text, :content
601
+ alias_method :to_str, :content
602
+ alias_method :name, :node_name
603
+ alias_method :type, :node_type
604
+ alias_method :elements, :element_children
605
+
606
+ # :section: Working With Node Attributes
607
+
608
+ # :call-seq: [](name) → (String, nil)
609
+ #
610
+ # Fetch an attribute from this node.
611
+ #
612
+ # ⚠ Note that attributes with namespaces cannot be accessed with this method. To access
613
+ # namespaced attributes, use #attribute_with_ns.
614
+ #
615
+ # [Returns] (String, nil) value of the attribute +name+, or +nil+ if no matching attribute exists
616
+ #
617
+ # *Example*
618
+ #
619
+ # doc = Nokogiri::XML("<root><child size='large' class='big wide tall'/></root>")
620
+ # child = doc.at_css("child")
621
+ # child["size"] # => "large"
622
+ # child["class"] # => "big wide tall"
623
+ #
624
+ # *Example:* Namespaced attributes will not be returned.
625
+ #
626
+ # ⚠ Note namespaced attributes may be accessed with #attribute or #attribute_with_ns
627
+ #
628
+ # doc = Nokogiri::XML(<<~EOF)
629
+ # <root xmlns:width='http://example.com/widths'>
630
+ # <child width:size='broad'/>
631
+ # </root>
632
+ # EOF
633
+ # doc.at_css("child")["size"] # => nil
634
+ # doc.at_css("child").attribute("size").value # => "broad"
635
+ # doc.at_css("child").attribute_with_ns("size", "http://example.com/widths").value
636
+ # # => "broad"
637
+ #
638
+ def [](name)
639
+ get(name.to_s)
640
+ end
641
+
642
+ # :call-seq: []=(name, value) → value
643
+ #
644
+ # Update the attribute +name+ to +value+, or create the attribute if it does not exist.
645
+ #
646
+ # ⚠ Note that attributes with namespaces cannot be accessed with this method. To access
647
+ # namespaced attributes for update, use #attribute_with_ns. To add a namespaced attribute,
648
+ # see the example below.
649
+ #
650
+ # [Returns] +value+
651
+ #
652
+ # *Example*
653
+ #
654
+ # doc = Nokogiri::XML("<root><child/></root>")
655
+ # child = doc.at_css("child")
656
+ # child["size"] = "broad"
657
+ # child.to_html
658
+ # # => "<child size=\"broad\"></child>"
659
+ #
660
+ # *Example:* Add a namespaced attribute.
661
+ #
662
+ # doc = Nokogiri::XML(<<~EOF)
663
+ # <root xmlns:width='http://example.com/widths'>
664
+ # <child/>
665
+ # </root>
666
+ # EOF
667
+ # child = doc.at_css("child")
668
+ # child["size"] = "broad"
669
+ # ns = doc.root.namespace_definitions.find { |ns| ns.prefix == "width" }
670
+ # child.attribute("size").namespace = ns
671
+ # doc.to_html
672
+ # # => "<root xmlns:width=\"http://example.com/widths\">\n" +
673
+ # # " <child width:size=\"broad\"></child>\n" +
674
+ # # "</root>\n"
675
+ #
676
+ def []=(name, value)
677
+ set(name.to_s, value.to_s)
678
+ end
679
+
680
+ #
681
+ # :call-seq: attributes() → Hash<String ⇒ Nokogiri::XML::Attr>
682
+ #
683
+ # Fetch this node's attributes.
684
+ #
685
+ # ⚠ Because the keys do not include any namespace information for the attribute, in case of a
686
+ # simple name collision, not all attributes will be returned. In this case, you will need to
687
+ # use #attribute_nodes.
688
+ #
689
+ # [Returns]
690
+ # Hash containing attributes belonging to +self+. The hash keys are String attribute
691
+ # names (without the namespace), and the hash values are Nokogiri::XML::Attr.
692
+ #
693
+ # *Example* with no namespaces:
694
+ #
695
+ # doc = Nokogiri::XML("<root><child size='large' class='big wide tall'/></root>")
696
+ # doc.at_css("child").attributes
697
+ # # => {"size"=>#(Attr:0x550 { name = "size", value = "large" }),
698
+ # # "class"=>#(Attr:0x564 { name = "class", value = "big wide tall" })}
699
+ #
700
+ # *Example* with a namespace:
701
+ #
702
+ # doc = Nokogiri::XML("<root xmlns:desc='http://example.com/sizes'><child desc:size='large'/></root>")
703
+ # doc.at_css("child").attributes
704
+ # # => {"size"=>
705
+ # # #(Attr:0x550 {
706
+ # # name = "size",
707
+ # # namespace = #(Namespace:0x564 {
708
+ # # prefix = "desc",
709
+ # # href = "http://example.com/sizes"
710
+ # # }),
711
+ # # value = "large"
712
+ # # })}
713
+ #
714
+ # *Example* with an attribute name collision:
715
+ #
716
+ # ⚠ Note that only one of the attributes is returned in the Hash.
717
+ #
718
+ # doc = Nokogiri::XML(<<~EOF)
719
+ # <root xmlns:width='http://example.com/widths'
720
+ # xmlns:height='http://example.com/heights'>
721
+ # <child width:size='broad' height:size='tall'/>
722
+ # </root>
723
+ # EOF
724
+ # doc.at_css("child").attributes
725
+ # # => {"size"=>
726
+ # # #(Attr:0x550 {
727
+ # # name = "size",
728
+ # # namespace = #(Namespace:0x564 {
729
+ # # prefix = "height",
730
+ # # href = "http://example.com/heights"
731
+ # # }),
732
+ # # value = "tall"
733
+ # # })}
734
+ #
735
+ def attributes
736
+ attribute_nodes.each_with_object({}) do |node, hash|
737
+ hash[node.node_name] = node
738
+ end
739
+ end
740
+
741
+ ###
742
+ # Get the attribute values for this Node.
743
+ def values
744
+ attribute_nodes.map(&:value)
745
+ end
746
+
747
+ ###
748
+ # Does this Node's attributes include <value>
749
+ def value?(value)
750
+ values.include?(value)
751
+ end
752
+
753
+ ###
754
+ # Get the attribute names for this Node.
755
+ def keys
756
+ attribute_nodes.map(&:node_name)
757
+ end
758
+
759
+ ###
760
+ # Iterate over each attribute name and value pair for this Node.
761
+ def each
762
+ attribute_nodes.each do |node|
763
+ yield [node.node_name, node.value]
764
+ end
765
+ end
766
+
767
+ ###
768
+ # Remove the attribute named +name+
769
+ def remove_attribute(name)
770
+ attr = attributes[name].remove if key?(name)
771
+ clear_xpath_context if Nokogiri.jruby?
772
+ attr
773
+ end
774
+
775
+ #
776
+ # :call-seq: classes() → Array<String>
777
+ #
778
+ # Fetch CSS class names of a Node.
779
+ #
780
+ # This is a convenience function and is equivalent to:
781
+ #
782
+ # node.kwattr_values("class")
783
+ #
784
+ # See related: #kwattr_values, #add_class, #append_class, #remove_class
785
+ #
786
+ # [Returns]
787
+ # The CSS classes (Array of String) present in the Node's "class" attribute. If the
788
+ # attribute is empty or non-existent, the return value is an empty array.
789
+ #
790
+ # *Example*
791
+ #
792
+ # node # => <div class="section title header"></div>
793
+ # node.classes # => ["section", "title", "header"]
794
+ #
795
+ def classes
796
+ kwattr_values("class")
797
+ end
798
+
799
+ #
800
+ # :call-seq: add_class(names) → self
801
+ #
802
+ # Ensure HTML CSS classes are present on +self+. Any CSS classes in +names+ that already exist
803
+ # in the "class" attribute are _not_ added. Note that any existing duplicates in the
804
+ # "class" attribute are not removed. Compare with #append_class.
805
+ #
806
+ # This is a convenience function and is equivalent to:
807
+ #
808
+ # node.kwattr_add("class", names)
809
+ #
810
+ # See related: #kwattr_add, #classes, #append_class, #remove_class
811
+ #
812
+ # [Parameters]
813
+ # - +names+ (String, Array<String>)
814
+ #
815
+ # CSS class names to be added to the Node's "class" attribute. May be a string containing
816
+ # whitespace-delimited names, or an Array of String names. Any class names already present
817
+ # will not be added. Any class names not present will be added. If no "class" attribute
818
+ # exists, one is created.
819
+ #
820
+ # [Returns] +self+ (Node) for ease of chaining method calls.
821
+ #
822
+ # *Example:* Ensure that the node has CSS class "section"
823
+ #
824
+ # node # => <div></div>
825
+ # node.add_class("section") # => <div class="section"></div>
826
+ # node.add_class("section") # => <div class="section"></div> # duplicate not added
827
+ #
828
+ # *Example:* Ensure that the node has CSS classes "section" and "header", via a String argument
829
+ #
830
+ # Note that the CSS class "section" is not added because it is already present.
831
+ # Note also that the pre-existing duplicate CSS class "section" is not removed.
832
+ #
833
+ # node # => <div class="section section"></div>
834
+ # node.add_class("section header") # => <div class="section section header"></div>
835
+ #
836
+ # *Example:* Ensure that the node has CSS classes "section" and "header", via an Array argument
837
+ #
838
+ # node # => <div></div>
839
+ # node.add_class(["section", "header"]) # => <div class="section header"></div>
840
+ #
841
+ def add_class(names)
842
+ kwattr_add("class", names)
843
+ end
844
+
845
+ #
846
+ # :call-seq: append_class(names) → self
847
+ #
848
+ # Add HTML CSS classes to +self+, regardless of duplication. Compare with #add_class.
849
+ #
850
+ # This is a convenience function and is equivalent to:
851
+ #
852
+ # node.kwattr_append("class", names)
853
+ #
854
+ # See related: #kwattr_append, #classes, #add_class, #remove_class
855
+ #
856
+ # [Parameters]
857
+ # - +names+ (String, Array<String>)
858
+ #
859
+ # CSS class names to be appended to the Node's "class" attribute. May be a string containing
860
+ # whitespace-delimited names, or an Array of String names. All class names passed in will be
861
+ # appended to the "class" attribute even if they are already present in the attribute
862
+ # value. If no "class" attribute exists, one is created.
863
+ #
864
+ # [Returns] +self+ (Node) for ease of chaining method calls.
865
+ #
866
+ # *Example:* Append "section" to the node's CSS "class" attribute
867
+ #
868
+ # node # => <div></div>
869
+ # node.append_class("section") # => <div class="section"></div>
870
+ # node.append_class("section") # => <div class="section section"></div> # duplicate added!
871
+ #
872
+ # *Example:* Append "section" and "header" to the noded's CSS "class" attribute, via a String argument
873
+ #
874
+ # Note that the CSS class "section" is appended even though it is already present
875
+ #
876
+ # node # => <div class="section section"></div>
877
+ # node.append_class("section header") # => <div class="section section section header"></div>
878
+ #
879
+ # *Example:* Append "section" and "header" to the node's CSS "class" attribute, via an Array argument
880
+ #
881
+ # node # => <div></div>
882
+ # node.append_class(["section", "header"]) # => <div class="section header"></div>
883
+ # node.append_class(["section", "header"]) # => <div class="section header section header"></div>
884
+ #
885
+ def append_class(names)
886
+ kwattr_append("class", names)
887
+ end
888
+
889
+ # :call-seq:
890
+ # remove_class(css_classes) → self
891
+ #
892
+ # Remove HTML CSS classes from this node. Any CSS class names in +css_classes+ that exist in
893
+ # this node's "class" attribute are removed, including any multiple entries.
894
+ #
895
+ # If no CSS classes remain after this operation, or if +css_classes+ is +nil+, the "class"
896
+ # attribute is deleted from the node.
897
+ #
898
+ # This is a convenience function and is equivalent to:
899
+ #
900
+ # node.kwattr_remove("class", css_classes)
901
+ #
902
+ # Also see #kwattr_remove, #classes, #add_class, #append_class
903
+ #
904
+ # [Parameters]
905
+ # - +css_classes+ (String, Array<String>)
906
+ #
907
+ # CSS class names to be removed from the Node's
908
+ # "class" attribute. May be a string containing whitespace-delimited names, or an Array of
909
+ # String names. Any class names already present will be removed. If no CSS classes remain,
910
+ # the "class" attribute is deleted.
911
+ #
912
+ # [Returns] +self+ (Nokogiri::XML::Node) for ease of chaining method calls.
913
+ #
914
+ # *Example*: Deleting a CSS class
915
+ #
916
+ # Note that all instances of the class "section" are removed from the "class" attribute.
917
+ #
918
+ # node # => <div class="section header section"></div>
919
+ # node.remove_class("section") # => <div class="header"></div>
920
+ #
921
+ # *Example*: Deleting the only remaining CSS class
922
+ #
923
+ # Note that the attribute is removed once there are no remaining classes.
924
+ #
925
+ # node # => <div class="section"></div>
926
+ # node.remove_class("section") # => <div></div>
927
+ #
928
+ # *Example*: Deleting multiple CSS classes
929
+ #
930
+ # Note that the "class" attribute is deleted once it's empty.
931
+ #
932
+ # node # => <div class="section header float"></div>
933
+ # node.remove_class(["section", "float"]) # => <div class="header"></div>
934
+ #
935
+ def remove_class(names = nil)
936
+ kwattr_remove("class", names)
937
+ end
938
+
939
+ # :call-seq:
940
+ # kwattr_values(attribute_name) → Array<String>
941
+ #
942
+ # Fetch values from a keyword attribute of a Node.
943
+ #
944
+ # A "keyword attribute" is a node attribute that contains a set of space-delimited
945
+ # values. Perhaps the most familiar example of this is the HTML "class" attribute used to
946
+ # contain CSS classes. But other keyword attributes exist, for instance
947
+ # {the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
948
+ #
949
+ # See also #classes, #kwattr_add, #kwattr_append, #kwattr_remove
950
+ #
951
+ # [Parameters]
952
+ # - +attribute_name+ (String) The name of the keyword attribute to be inspected.
953
+ #
954
+ # [Returns]
955
+ # (Array<String>) The values present in the Node's +attribute_name+ attribute. If the
956
+ # attribute is empty or non-existent, the return value is an empty array.
957
+ #
958
+ # *Example:*
959
+ #
960
+ # node # => <a rel="nofollow noopener external">link</a>
961
+ # node.kwattr_values("rel") # => ["nofollow", "noopener", "external"]
962
+ #
963
+ # Since v1.11.0
964
+ def kwattr_values(attribute_name)
965
+ keywordify(get_attribute(attribute_name) || [])
966
+ end
967
+
968
+ # :call-seq:
969
+ # kwattr_add(attribute_name, keywords) → self
970
+ #
971
+ # Ensure that values are present in a keyword attribute.
972
+ #
973
+ # Any values in +keywords+ that already exist in the Node's attribute values are _not_
974
+ # added. Note that any existing duplicates in the attribute values are not removed. Compare
975
+ # with #kwattr_append.
976
+ #
977
+ # A "keyword attribute" is a node attribute that contains a set of space-delimited
978
+ # values. Perhaps the most familiar example of this is the HTML "class" attribute used to
979
+ # contain CSS classes. But other keyword attributes exist, for instance
980
+ # {the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
981
+ #
982
+ # See also #add_class, #kwattr_values, #kwattr_append, #kwattr_remove
983
+ #
984
+ # [Parameters]
985
+ # - +attribute_name+ (String) The name of the keyword attribute to be modified.
986
+ # - +keywords+ (String, Array<String>)
987
+ # Keywords to be added to the attribute named +attribute_name+. May be a string containing
988
+ # whitespace-delimited values, or an Array of String values. Any values already present will
989
+ # not be added. Any values not present will be added. If the named attribute does not exist,
990
+ # it is created.
991
+ #
992
+ # [Returns] +self+ (Nokogiri::XML::Node) for ease of chaining method calls.
993
+ #
994
+ # *Example:* Ensure that a +Node+ has "nofollow" in its +rel+ attribute.
995
+ #
996
+ # Note that duplicates are not added.
997
+ #
998
+ # node # => <a></a>
999
+ # node.kwattr_add("rel", "nofollow") # => <a rel="nofollow"></a>
1000
+ # node.kwattr_add("rel", "nofollow") # => <a rel="nofollow"></a>
1001
+ #
1002
+ # *Example:* Ensure that a +Node+ has "nofollow" and "noreferrer" in its +rel+ attribute, via a
1003
+ # String argument.
1004
+ #
1005
+ # Note that "nofollow" is not added because it is already present. Note also that the
1006
+ # pre-existing duplicate "nofollow" is not removed.
1007
+ #
1008
+ # node # => <a rel="nofollow nofollow"></a>
1009
+ # node.kwattr_add("rel", "nofollow noreferrer") # => <a rel="nofollow nofollow noreferrer"></a>
1010
+ #
1011
+ # *Example:* Ensure that a +Node+ has "nofollow" and "noreferrer" in its +rel+ attribute, via
1012
+ # an Array argument.
1013
+ #
1014
+ # node # => <a></a>
1015
+ # node.kwattr_add("rel", ["nofollow", "noreferrer"]) # => <a rel="nofollow noreferrer"></a>
1016
+ #
1017
+ # Since v1.11.0
1018
+ def kwattr_add(attribute_name, keywords)
1019
+ keywords = keywordify(keywords)
1020
+ current_kws = kwattr_values(attribute_name)
1021
+ new_kws = (current_kws + (keywords - current_kws)).join(" ")
1022
+ set_attribute(attribute_name, new_kws)
1023
+ self
1024
+ end
1025
+
1026
+ # :call-seq:
1027
+ # kwattr_append(attribute_name, keywords) → self
1028
+ #
1029
+ # Add keywords to a Node's keyword attribute, regardless of duplication. Compare with
1030
+ # #kwattr_add.
1031
+ #
1032
+ # A "keyword attribute" is a node attribute that contains a set of space-delimited
1033
+ # values. Perhaps the most familiar example of this is the HTML "class" attribute used to
1034
+ # contain CSS classes. But other keyword attributes exist, for instance
1035
+ # {the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
1036
+ #
1037
+ # See also #append_class, #kwattr_values, #kwattr_add, #kwattr_remove
1038
+ #
1039
+ # [Parameters]
1040
+ # - +attribute_name+ (String) The name of the keyword attribute to be modified.
1041
+ # - +keywords+ (String, Array<String>)
1042
+ # Keywords to be added to the attribute named +attribute_name+. May be a string containing
1043
+ # whitespace-delimited values, or an Array of String values. All values passed in will be
1044
+ # appended to the named attribute even if they are already present in the attribute. If the
1045
+ # named attribute does not exist, it is created.
1046
+ #
1047
+ # [Returns] +self+ (Node) for ease of chaining method calls.
1048
+ #
1049
+ # *Example:* Append "nofollow" to the +rel+ attribute.
1050
+ #
1051
+ # Note that duplicates are added.
1052
+ #
1053
+ # node # => <a></a>
1054
+ # node.kwattr_append("rel", "nofollow") # => <a rel="nofollow"></a>
1055
+ # node.kwattr_append("rel", "nofollow") # => <a rel="nofollow nofollow"></a>
1056
+ #
1057
+ # *Example:* Append "nofollow" and "noreferrer" to the +rel+ attribute, via a String argument.
1058
+ #
1059
+ # Note that "nofollow" is appended even though it is already present.
1060
+ #
1061
+ # node # => <a rel="nofollow"></a>
1062
+ # node.kwattr_append("rel", "nofollow noreferrer") # => <a rel="nofollow nofollow noreferrer"></a>
1063
+ #
1064
+ #
1065
+ # *Example:* Append "nofollow" and "noreferrer" to the +rel+ attribute, via an Array argument.
1066
+ #
1067
+ # node # => <a></a>
1068
+ # node.kwattr_append("rel", ["nofollow", "noreferrer"]) # => <a rel="nofollow noreferrer"></a>
1069
+ #
1070
+ # Since v1.11.0
1071
+ def kwattr_append(attribute_name, keywords)
1072
+ keywords = keywordify(keywords)
1073
+ current_kws = kwattr_values(attribute_name)
1074
+ new_kws = (current_kws + keywords).join(" ")
1075
+ set_attribute(attribute_name, new_kws)
1076
+ self
1077
+ end
1078
+
1079
+ # :call-seq:
1080
+ # kwattr_remove(attribute_name, keywords) → self
1081
+ #
1082
+ # Remove keywords from a keyword attribute. Any matching keywords that exist in the named
1083
+ # attribute are removed, including any multiple entries.
1084
+ #
1085
+ # If no keywords remain after this operation, or if +keywords+ is +nil+, the attribute is
1086
+ # deleted from the node.
1087
+ #
1088
+ # A "keyword attribute" is a node attribute that contains a set of space-delimited
1089
+ # values. Perhaps the most familiar example of this is the HTML "class" attribute used to
1090
+ # contain CSS classes. But other keyword attributes exist, for instance
1091
+ # {the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
1092
+ #
1093
+ # See also #remove_class, #kwattr_values, #kwattr_add, #kwattr_append
1094
+ #
1095
+ # [Parameters]
1096
+ # - +attribute_name+ (String) The name of the keyword attribute to be modified.
1097
+ # - +keywords+ (String, Array<String>)
1098
+ # Keywords to be removed from the attribute named +attribute_name+. May be a string
1099
+ # containing whitespace-delimited values, or an Array of String values. Any keywords present
1100
+ # in the named attribute will be removed. If no keywords remain, or if +keywords+ is nil,
1101
+ # the attribute is deleted.
1102
+ #
1103
+ # [Returns] +self+ (Node) for ease of chaining method calls.
1104
+ #
1105
+ # *Example:*
1106
+ #
1107
+ # Note that the +rel+ attribute is deleted when empty.
1108
+ #
1109
+ # node # => <a rel="nofollow noreferrer">link</a>
1110
+ # node.kwattr_remove("rel", "nofollow") # => <a rel="noreferrer">link</a>
1111
+ # node.kwattr_remove("rel", "noreferrer") # => <a>link</a>
1112
+ #
1113
+ # Since v1.11.0
1114
+ def kwattr_remove(attribute_name, keywords)
1115
+ if keywords.nil?
1116
+ remove_attribute(attribute_name)
1117
+ return self
1118
+ end
1119
+
1120
+ keywords = keywordify(keywords)
1121
+ current_kws = kwattr_values(attribute_name)
1122
+ new_kws = current_kws - keywords
1123
+ if new_kws.empty?
1124
+ remove_attribute(attribute_name)
1125
+ else
1126
+ set_attribute(attribute_name, new_kws.join(" "))
1127
+ end
1128
+ self
1129
+ end
1130
+
1131
+ alias_method :delete, :remove_attribute
1132
+ alias_method :get_attribute, :[]
1133
+ alias_method :attr, :[]
1134
+ alias_method :set_attribute, :[]=
1135
+ alias_method :has_attribute?, :key?
1136
+
1137
+ # :section:
1138
+
1139
+ ###
1140
+ # Returns true if this Node matches +selector+
1141
+ def matches?(selector)
1142
+ ancestors.last.search(selector).include?(self)
1143
+ end
1144
+
1145
+ ###
1146
+ # Create a DocumentFragment containing +tags+ that is relative to _this_
1147
+ # context node.
1148
+ def fragment(tags)
1149
+ document.related_class("DocumentFragment").new(document, tags, self)
1150
+ end
1151
+
1152
+ ###
1153
+ # Parse +string_or_io+ as a document fragment within the context of
1154
+ # *this* node. Returns a XML::NodeSet containing the nodes parsed from
1155
+ # +string_or_io+.
1156
+ def parse(string_or_io, options = nil)
1157
+ ##
1158
+ # When the current node is unparented and not an element node, use the
1159
+ # document as the parsing context instead. Otherwise, the in-context
1160
+ # parser cannot find an element or a document node.
1161
+ # Document Fragments are also not usable by the in-context parser.
1162
+ if !element? && !document? && (!parent || parent.fragment?)
1163
+ return document.parse(string_or_io, options)
1164
+ end
1165
+
1166
+ options ||= (document.html? ? ParseOptions::DEFAULT_HTML : ParseOptions::DEFAULT_XML)
1167
+ options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
1168
+ yield options if block_given?
1169
+
1170
+ contents = if string_or_io.respond_to?(:read)
1171
+ string_or_io.read
1172
+ else
1173
+ string_or_io
1174
+ end
1175
+
1176
+ return Nokogiri::XML::NodeSet.new(document) if contents.empty?
1177
+
1178
+ error_count = document.errors.length
1179
+ node_set = in_context(contents, options.to_i)
1180
+
1181
+ if document.errors.length > error_count
1182
+ raise document.errors[error_count] unless options.recover?
1183
+
1184
+ # TODO: remove this block when libxml2 < 2.13 is no longer supported
1185
+ if node_set.empty?
1186
+ # libxml2 < 2.13 does not obey the +recover+ option after encountering errors during
1187
+ # +in_context+ parsing, and so this horrible hack is here to try to emulate recovery
1188
+ # behavior.
1189
+ #
1190
+ # (Note that HTML4 fragment parsing seems to have been fixed in abd74186, and XML
1191
+ # fragment parsing is fixed in 1c106edf. Both are in 2.13.)
1192
+ #
1193
+ # Unfortunately, this means we're no longer parsing "in context" and so namespaces that
1194
+ # would have been inherited from the context node won't be handled correctly. This hack
1195
+ # was written in 2010, and I regret it, because it's silently degrading functionality in
1196
+ # a way that's not easily prevented (or even detected).
1197
+ #
1198
+ # I think preferable behavior would be to either:
1199
+ #
1200
+ # a. add an error noting that we "fell back" and pointing the user to turning off the
1201
+ # +recover+ option
1202
+ # b. don't recover, but raise a sensible exception
1203
+ #
1204
+ # For context and background:
1205
+ # - https://github.com/sparklemotion/nokogiri/issues/313
1206
+ # - https://github.com/sparklemotion/nokogiri/issues/2092
1207
+ fragment = document.related_class("DocumentFragment").parse(contents)
1208
+ node_set = fragment.children
1209
+ end
1210
+ end
1211
+
1212
+ node_set
1213
+ end
1214
+
1215
+ # :call-seq:
1216
+ # namespaces() → Hash<String(Namespace#prefix) ⇒ String(Namespace#href)>
1217
+ #
1218
+ # Fetch all the namespaces on this node and its ancestors.
1219
+ #
1220
+ # Note that the keys in this hash XML attributes that would be used to define this namespace,
1221
+ # such as "xmlns:prefix", not just the prefix.
1222
+ #
1223
+ # The default namespace for this node will be included with key "xmlns".
1224
+ #
1225
+ # See also #namespace_scopes
1226
+ #
1227
+ # [Returns]
1228
+ # Hash containing all the namespaces on this node and its ancestors. The hash keys are the
1229
+ # namespace prefix, and the hash value for each key is the namespace URI.
1230
+ #
1231
+ # *Example:*
1232
+ #
1233
+ # doc = Nokogiri::XML(<<~EOF)
1234
+ # <root xmlns="http://example.com/root" xmlns:in_scope="http://example.com/in_scope">
1235
+ # <first/>
1236
+ # <second xmlns="http://example.com/child"/>
1237
+ # <third xmlns:foo="http://example.com/foo"/>
1238
+ # </root>
1239
+ # EOF
1240
+ # doc.at_xpath("//root:first", "root" => "http://example.com/root").namespaces
1241
+ # # => {"xmlns"=>"http://example.com/root",
1242
+ # # "xmlns:in_scope"=>"http://example.com/in_scope"}
1243
+ # doc.at_xpath("//child:second", "child" => "http://example.com/child").namespaces
1244
+ # # => {"xmlns"=>"http://example.com/child",
1245
+ # # "xmlns:in_scope"=>"http://example.com/in_scope"}
1246
+ # doc.at_xpath("//root:third", "root" => "http://example.com/root").namespaces
1247
+ # # => {"xmlns:foo"=>"http://example.com/foo",
1248
+ # # "xmlns"=>"http://example.com/root",
1249
+ # # "xmlns:in_scope"=>"http://example.com/in_scope"}
1250
+ #
1251
+ def namespaces
1252
+ namespace_scopes.each_with_object({}) do |ns, hash|
1253
+ prefix = ns.prefix
1254
+ key = prefix ? "xmlns:#{prefix}" : "xmlns"
1255
+ hash[key] = ns.href
1256
+ end
1257
+ end
1258
+
1259
+ # Returns true if this is a Comment
1260
+ def comment?
1261
+ type == COMMENT_NODE
1262
+ end
1263
+
1264
+ # Returns true if this is a CDATA
1265
+ def cdata?
1266
+ type == CDATA_SECTION_NODE
1267
+ end
1268
+
1269
+ # Returns true if this is an XML::Document node
1270
+ def xml?
1271
+ type == DOCUMENT_NODE
1272
+ end
1273
+
1274
+ # Returns true if this is an HTML4::Document or HTML5::Document node
1275
+ def html?
1276
+ type == HTML_DOCUMENT_NODE
1277
+ end
1278
+
1279
+ # Returns true if this is a Document
1280
+ def document?
1281
+ is_a?(XML::Document)
1282
+ end
1283
+
1284
+ # Returns true if this is a ProcessingInstruction node
1285
+ def processing_instruction?
1286
+ type == PI_NODE
1287
+ end
1288
+
1289
+ # Returns true if this is a Text node
1290
+ def text?
1291
+ type == TEXT_NODE
1292
+ end
1293
+
1294
+ # Returns true if this is a DocumentFragment
1295
+ def fragment?
1296
+ type == DOCUMENT_FRAG_NODE
1297
+ end
1298
+
1299
+ ###
1300
+ # Fetch the Nokogiri::HTML4::ElementDescription for this node. Returns
1301
+ # nil on XML documents and on unknown tags.
1302
+ def description
1303
+ return if document.xml?
1304
+
1305
+ Nokogiri::HTML4::ElementDescription[name]
1306
+ end
1307
+
1308
+ ###
1309
+ # Is this a read only node?
1310
+ def read_only?
1311
+ # According to gdome2, these are read-only node types
1312
+ [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
1313
+ end
1314
+
1315
+ # Returns true if this is an Element node
1316
+ def element?
1317
+ type == ELEMENT_NODE
1318
+ end
1319
+
1320
+ alias_method :elem?, :element?
1321
+
1322
+ ###
1323
+ # Turn this node in to a string. If the document is HTML, this method
1324
+ # returns html. If the document is XML, this method returns XML.
1325
+ def to_s
1326
+ document.xml? ? to_xml : to_html
1327
+ end
1328
+
1329
+ # Get the inner_html for this node's Node#children
1330
+ def inner_html(*args)
1331
+ children.map { |x| x.to_html(*args) }.join
1332
+ end
1333
+
1334
+ # Get the path to this node as a CSS expression
1335
+ def css_path
1336
+ path.split(%r{/}).filter_map do |part|
1337
+ part.empty? ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
1338
+ end.join(" > ")
1339
+ end
1340
+
1341
+ ###
1342
+ # Get a list of ancestor Node for this Node. If +selector+ is given,
1343
+ # the ancestors must match +selector+
1344
+ def ancestors(selector = nil)
1345
+ return NodeSet.new(document) unless respond_to?(:parent)
1346
+ return NodeSet.new(document) unless parent
1347
+
1348
+ parents = [parent]
1349
+
1350
+ while parents.last.respond_to?(:parent)
1351
+ break unless (ctx_parent = parents.last.parent)
1352
+
1353
+ parents << ctx_parent
1354
+ end
1355
+
1356
+ return NodeSet.new(document, parents) unless selector
1357
+
1358
+ root = parents.last
1359
+ search_results = root.search(selector)
1360
+
1361
+ NodeSet.new(document, parents.find_all do |parent|
1362
+ search_results.include?(parent)
1363
+ end)
1364
+ end
1365
+
1366
+ ####
1367
+ # Yields self and all children to +block+ recursively.
1368
+ def traverse(&block)
1369
+ children.each { |j| j.traverse(&block) }
1370
+ yield(self)
1371
+ end
1372
+
1373
+ ###
1374
+ # Accept a visitor. This method calls "visit" on +visitor+ with self.
1375
+ def accept(visitor)
1376
+ visitor.visit(self)
1377
+ end
1378
+
1379
+ ###
1380
+ # Test to see if this Node is equal to +other+
1381
+ def ==(other)
1382
+ return false unless other
1383
+ return false unless other.respond_to?(:pointer_id)
1384
+
1385
+ pointer_id == other.pointer_id
1386
+ end
1387
+
1388
+ ###
1389
+ # Compare two Node objects with respect to their Document. Nodes from
1390
+ # different documents cannot be compared.
1391
+ def <=>(other)
1392
+ return unless other.is_a?(Nokogiri::XML::Node)
1393
+ return unless document == other.document
1394
+
1395
+ compare(other)
1396
+ end
1397
+
1398
+ # :section: Serialization and Generating Output
1399
+
1400
+ ###
1401
+ # Serialize Node using +options+. Save options can also be set using a block.
1402
+ #
1403
+ # See also Nokogiri::XML::Node::SaveOptions and Node@Serialization+and+Generating+Output.
1404
+ #
1405
+ # These two statements are equivalent:
1406
+ #
1407
+ # node.serialize(encoding: 'UTF-8', save_with: FORMAT | AS_XML)
1408
+ #
1409
+ # or
1410
+ #
1411
+ # node.serialize(encoding: 'UTF-8') do |config|
1412
+ # config.format.as_xml
1413
+ # end
1414
+ #
1415
+ def serialize(*args, &block)
1416
+ # TODO: deprecate non-hash options, see 46c68ed 2009-06-20 for context
1417
+ options = if args.first.is_a?(Hash)
1418
+ args.shift
1419
+ else
1420
+ {
1421
+ encoding: args[0],
1422
+ save_with: args[1],
1423
+ }
1424
+ end
1425
+
1426
+ options[:encoding] ||= document.encoding
1427
+ encoding = Encoding.find(options[:encoding] || "UTF-8")
1428
+
1429
+ io = StringIO.new(String.new(encoding: encoding))
1430
+
1431
+ write_to(io, options, &block)
1432
+ io.string
1433
+ end
1434
+
1435
+ ###
1436
+ # Serialize this Node to HTML
1437
+ #
1438
+ # doc.to_html
1439
+ #
1440
+ # See Node#write_to for a list of +options+. For formatted output,
1441
+ # use Node#to_xhtml instead.
1442
+ def to_html(options = {})
1443
+ to_format(SaveOptions::DEFAULT_HTML, options)
1444
+ end
1445
+
1446
+ ###
1447
+ # Serialize this Node to XML using +options+
1448
+ #
1449
+ # doc.to_xml(indent: 5, encoding: 'UTF-8')
1450
+ #
1451
+ # See Node#write_to for a list of +options+
1452
+ def to_xml(options = {})
1453
+ options[:save_with] ||= SaveOptions::DEFAULT_XML
1454
+ serialize(options)
1455
+ end
1456
+
1457
+ ###
1458
+ # Serialize this Node to XHTML using +options+
1459
+ #
1460
+ # doc.to_xhtml(indent: 5, encoding: 'UTF-8')
1461
+ #
1462
+ # See Node#write_to for a list of +options+
1463
+ def to_xhtml(options = {})
1464
+ to_format(SaveOptions::DEFAULT_XHTML, options)
1465
+ end
1466
+
1467
+ ###
1468
+ # :call-seq:
1469
+ # write_to(io, *options)
1470
+ #
1471
+ # Serialize this node or document to +io+.
1472
+ #
1473
+ # [Parameters]
1474
+ # - +io+ (IO) An IO-like object to which the serialized content will be written.
1475
+ # - +options+ (Hash) See below
1476
+ #
1477
+ # [Options]
1478
+ # * +:encoding+ (String or Encoding) specify the encoding of the output (defaults to document encoding)
1479
+ # * +:indent_text+ (String) the indentation text (defaults to <code>" "</code>)
1480
+ # * +:indent+ (Integer) the number of +:indent_text+ to use (defaults to +2+)
1481
+ # * +:save_with+ (Integer) a combination of SaveOptions constants
1482
+ #
1483
+ # To save with UTF-8 indented twice:
1484
+ #
1485
+ # node.write_to(io, encoding: 'UTF-8', indent: 2)
1486
+ #
1487
+ # To save indented with two dashes:
1488
+ #
1489
+ # node.write_to(io, indent_text: '-', indent: 2)
1490
+ #
1491
+ def write_to(io, *options)
1492
+ options = options.first.is_a?(Hash) ? options.shift : {}
1493
+ encoding = options[:encoding] || options[0] || document.encoding
1494
+ if Nokogiri.jruby?
1495
+ save_options = options[:save_with] || options[1]
1496
+ indent_times = options[:indent] || 0
1497
+ else
1498
+ save_options = options[:save_with] || options[1] || SaveOptions::FORMAT
1499
+ indent_times = options[:indent] || 2
1500
+ end
1501
+ indent_text = options[:indent_text] || " "
1502
+
1503
+ # Any string times 0 returns an empty string. Therefore, use the same
1504
+ # string instead of generating a new empty string for every node with
1505
+ # zero indentation.
1506
+ indentation = indent_times.zero? ? "" : (indent_text * indent_times)
1507
+
1508
+ config = SaveOptions.new(save_options.to_i)
1509
+ yield config if block_given?
1510
+
1511
+ encoding = encoding.is_a?(Encoding) ? encoding.name : encoding
1512
+
1513
+ native_write_to(io, encoding, indentation, config.options)
1514
+ end
1515
+
1516
+ ###
1517
+ # Write Node as HTML to +io+ with +options+
1518
+ #
1519
+ # See Node#write_to for a list of +options+
1520
+ def write_html_to(io, options = {})
1521
+ write_format_to(SaveOptions::DEFAULT_HTML, io, options)
1522
+ end
1523
+
1524
+ ###
1525
+ # Write Node as XHTML to +io+ with +options+
1526
+ #
1527
+ # See Node#write_to for a list of +options+
1528
+ def write_xhtml_to(io, options = {})
1529
+ write_format_to(SaveOptions::DEFAULT_XHTML, io, options)
1530
+ end
1531
+
1532
+ ###
1533
+ # Write Node as XML to +io+ with +options+
1534
+ #
1535
+ # doc.write_xml_to io, :encoding => 'UTF-8'
1536
+ #
1537
+ # See Node#write_to for a list of options
1538
+ def write_xml_to(io, options = {})
1539
+ options[:save_with] ||= SaveOptions::DEFAULT_XML
1540
+ write_to(io, options)
1541
+ end
1542
+
1543
+ def canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false)
1544
+ c14n_root = self
1545
+ document.canonicalize(mode, inclusive_namespaces, with_comments) do |node, parent|
1546
+ tn = node.is_a?(XML::Node) ? node : parent
1547
+ tn == c14n_root || tn.ancestors.include?(c14n_root)
1548
+ end
1549
+ end
1550
+
1551
+ DECONSTRUCT_KEYS = [:name, :attributes, :children, :namespace, :content, :elements, :inner_html].freeze # :nodoc:
1552
+ DECONSTRUCT_METHODS = { attributes: :attribute_nodes }.freeze # :nodoc:
1553
+
1554
+ #
1555
+ # :call-seq: deconstruct_keys(array_of_names) → Hash
1556
+ #
1557
+ # Returns a hash describing the Node, to use in pattern matching.
1558
+ #
1559
+ # Valid keys and their values:
1560
+ # - +name+ → (String) The name of this node, or "text" if it is a Text node.
1561
+ # - +namespace+ → (Namespace, nil) The namespace of this node, or nil if there is no namespace.
1562
+ # - +attributes+ → (Array<Attr>) The attributes of this node.
1563
+ # - +children+ → (Array<Node>) The children of this node. 💡 Note this includes text nodes.
1564
+ # - +elements+ → (Array<Node>) The child elements of this node. 💡 Note this does not include text nodes.
1565
+ # - +content+ → (String) The contents of all the text nodes in this node's subtree. See #content.
1566
+ # - +inner_html+ → (String) The inner markup for the children of this node. See #inner_html.
1567
+ #
1568
+ # *Example*
1569
+ #
1570
+ # doc = Nokogiri::XML.parse(<<~XML)
1571
+ # <?xml version="1.0"?>
1572
+ # <parent xmlns="http://nokogiri.org/ns/default" xmlns:noko="http://nokogiri.org/ns/noko">
1573
+ # <child1 foo="abc" noko:bar="def">First</child1>
1574
+ # <noko:child2 foo="qwe" noko:bar="rty">Second</noko:child2>
1575
+ # </parent>
1576
+ # XML
1577
+ #
1578
+ # doc.root.deconstruct_keys([:name, :namespace])
1579
+ # # => {:name=>"parent",
1580
+ # # :namespace=>
1581
+ # # #(Namespace:0x35c { href = "http://nokogiri.org/ns/default" })}
1582
+ #
1583
+ # doc.root.deconstruct_keys([:inner_html, :content])
1584
+ # # => {:content=>"\n" + " First\n" + " Second\n",
1585
+ # # :inner_html=>
1586
+ # # "\n" +
1587
+ # # " <child1 foo=\"abc\" noko:bar=\"def\">First</child1>\n" +
1588
+ # # " <noko:child2 foo=\"qwe\" noko:bar=\"rty\">Second</noko:child2>\n"}
1589
+ #
1590
+ # doc.root.elements.first.deconstruct_keys([:attributes])
1591
+ # # => {:attributes=>
1592
+ # # [#(Attr:0x370 { name = "foo", value = "abc" }),
1593
+ # # #(Attr:0x384 {
1594
+ # # name = "bar",
1595
+ # # namespace = #(Namespace:0x398 {
1596
+ # # prefix = "noko",
1597
+ # # href = "http://nokogiri.org/ns/noko"
1598
+ # # }),
1599
+ # # value = "def"
1600
+ # # })]}
1601
+ #
1602
+ # Since v1.14.0
1603
+ #
1604
+ def deconstruct_keys(keys)
1605
+ requested_keys = DECONSTRUCT_KEYS & keys
1606
+ {}.tap do |values|
1607
+ requested_keys.each do |key|
1608
+ method = DECONSTRUCT_METHODS[key] || key
1609
+ values[key] = send(method)
1610
+ end
1611
+ end
1612
+ end
1613
+
1614
+ # :section:
1615
+
1616
+ protected
1617
+
1618
+ def coerce(data)
1619
+ case data
1620
+ when XML::NodeSet
1621
+ return data
1622
+ when XML::DocumentFragment
1623
+ return data.children
1624
+ when String
1625
+ return fragment(data).children
1626
+ when Document, XML::Attr
1627
+ # unacceptable
1628
+ when XML::Node
1629
+ return data
1630
+ end
1631
+
1632
+ raise ArgumentError, <<~EOERR
1633
+ Requires a Node, NodeSet or String argument, and cannot accept a #{data.class}.
1634
+ (You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
1635
+ EOERR
1636
+ end
1637
+
1638
+ private
1639
+
1640
+ def keywordify(keywords)
1641
+ case keywords
1642
+ when Enumerable
1643
+ keywords
1644
+ when String
1645
+ keywords.scan(/\S+/)
1646
+ else
1647
+ raise ArgumentError,
1648
+ "Keyword attributes must be passed as either a String or an Enumerable, but received #{keywords.class}"
1649
+ end
1650
+ end
1651
+
1652
+ def add_sibling(next_or_previous, node_or_tags)
1653
+ raise("Cannot add sibling to a node with no parent") unless parent
1654
+
1655
+ impl = next_or_previous == :next ? :add_next_sibling_node : :add_previous_sibling_node
1656
+ iter = next_or_previous == :next ? :reverse_each : :each
1657
+
1658
+ node_or_tags = parent.coerce(node_or_tags)
1659
+ if node_or_tags.is_a?(XML::NodeSet)
1660
+ if text?
1661
+ pivot = Nokogiri::XML::Node.new("dummy", document)
1662
+ send(impl, pivot)
1663
+ else
1664
+ pivot = self
1665
+ end
1666
+ node_or_tags.send(iter) { |n| pivot.send(impl, n) }
1667
+ pivot.unlink if text?
1668
+ else
1669
+ send(impl, node_or_tags)
1670
+ end
1671
+ node_or_tags
1672
+ end
1673
+
1674
+ def to_format(save_option, options)
1675
+ options[:save_with] = save_option unless options[:save_with]
1676
+ serialize(options)
1677
+ end
1678
+
1679
+ def write_format_to(save_option, io, options)
1680
+ options[:save_with] ||= save_option
1681
+ write_to(io, options)
1682
+ end
1683
+
1684
+ def inspect_attributes
1685
+ [:name, :namespace, :attribute_nodes, :children]
1686
+ end
1687
+
1688
+ IMPLIED_XPATH_CONTEXTS = [".//"].freeze
1689
+
1690
+ def add_child_node_and_reparent_attrs(node)
1691
+ add_child_node(node)
1692
+ node.attribute_nodes.find_all { |a| a.name.include?(":") }.each do |attr_node|
1693
+ attr_node.remove
1694
+ node[attr_node.name] = attr_node.value
1695
+ end
1696
+ end
1697
+ end
1698
+ end
1699
+ end
1700
+
1701
+ require_relative "node/save_options"