nokogiri 1.13.0-aarch64-linux
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/LICENSE-DEPENDENCIES.md +1903 -0
- data/LICENSE.md +9 -0
- data/README.md +280 -0
- data/bin/nokogiri +131 -0
- data/dependencies.yml +73 -0
- data/ext/nokogiri/depend +38 -0
- data/ext/nokogiri/extconf.rb +1000 -0
- data/ext/nokogiri/gumbo.c +584 -0
- data/ext/nokogiri/html4_document.c +166 -0
- data/ext/nokogiri/html4_element_description.c +294 -0
- data/ext/nokogiri/html4_entity_lookup.c +37 -0
- data/ext/nokogiri/html4_sax_parser_context.c +120 -0
- data/ext/nokogiri/html4_sax_push_parser.c +95 -0
- data/ext/nokogiri/include/libexslt/exslt.h +102 -0
- data/ext/nokogiri/include/libexslt/exsltconfig.h +70 -0
- data/ext/nokogiri/include/libexslt/exsltexports.h +140 -0
- data/ext/nokogiri/include/libxml2/libxml/DOCBparser.h +96 -0
- data/ext/nokogiri/include/libxml2/libxml/HTMLparser.h +306 -0
- data/ext/nokogiri/include/libxml2/libxml/HTMLtree.h +147 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX.h +173 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX2.h +178 -0
- data/ext/nokogiri/include/libxml2/libxml/c14n.h +128 -0
- data/ext/nokogiri/include/libxml2/libxml/catalog.h +182 -0
- data/ext/nokogiri/include/libxml2/libxml/chvalid.h +230 -0
- data/ext/nokogiri/include/libxml2/libxml/debugXML.h +217 -0
- data/ext/nokogiri/include/libxml2/libxml/dict.h +79 -0
- data/ext/nokogiri/include/libxml2/libxml/encoding.h +245 -0
- data/ext/nokogiri/include/libxml2/libxml/entities.h +151 -0
- data/ext/nokogiri/include/libxml2/libxml/globals.h +508 -0
- data/ext/nokogiri/include/libxml2/libxml/hash.h +236 -0
- data/ext/nokogiri/include/libxml2/libxml/list.h +137 -0
- data/ext/nokogiri/include/libxml2/libxml/nanoftp.h +163 -0
- data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +81 -0
- data/ext/nokogiri/include/libxml2/libxml/parser.h +1243 -0
- data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +644 -0
- data/ext/nokogiri/include/libxml2/libxml/pattern.h +100 -0
- data/ext/nokogiri/include/libxml2/libxml/relaxng.h +217 -0
- data/ext/nokogiri/include/libxml2/libxml/schemasInternals.h +958 -0
- data/ext/nokogiri/include/libxml2/libxml/schematron.h +142 -0
- data/ext/nokogiri/include/libxml2/libxml/threads.h +89 -0
- data/ext/nokogiri/include/libxml2/libxml/tree.h +1311 -0
- data/ext/nokogiri/include/libxml2/libxml/uri.h +94 -0
- data/ext/nokogiri/include/libxml2/libxml/valid.h +458 -0
- data/ext/nokogiri/include/libxml2/libxml/xinclude.h +129 -0
- data/ext/nokogiri/include/libxml2/libxml/xlink.h +189 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +368 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlautomata.h +146 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +946 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +77 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +224 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmodule.h +57 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +428 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlregexp.h +222 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +88 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlschemas.h +246 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlschemastypes.h +151 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlstring.h +140 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlunicode.h +202 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +485 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +488 -0
- data/ext/nokogiri/include/libxml2/libxml/xpath.h +564 -0
- data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +632 -0
- data/ext/nokogiri/include/libxml2/libxml/xpointer.h +114 -0
- data/ext/nokogiri/include/libxslt/attributes.h +38 -0
- data/ext/nokogiri/include/libxslt/documents.h +93 -0
- data/ext/nokogiri/include/libxslt/extensions.h +262 -0
- data/ext/nokogiri/include/libxslt/extra.h +72 -0
- data/ext/nokogiri/include/libxslt/functions.h +78 -0
- data/ext/nokogiri/include/libxslt/imports.h +75 -0
- data/ext/nokogiri/include/libxslt/keys.h +53 -0
- data/ext/nokogiri/include/libxslt/namespaces.h +68 -0
- data/ext/nokogiri/include/libxslt/numbersInternals.h +73 -0
- data/ext/nokogiri/include/libxslt/pattern.h +84 -0
- data/ext/nokogiri/include/libxslt/preproc.h +43 -0
- data/ext/nokogiri/include/libxslt/security.h +104 -0
- data/ext/nokogiri/include/libxslt/templates.h +77 -0
- data/ext/nokogiri/include/libxslt/transform.h +207 -0
- data/ext/nokogiri/include/libxslt/variables.h +118 -0
- data/ext/nokogiri/include/libxslt/xslt.h +110 -0
- data/ext/nokogiri/include/libxslt/xsltInternals.h +1978 -0
- data/ext/nokogiri/include/libxslt/xsltconfig.h +180 -0
- data/ext/nokogiri/include/libxslt/xsltexports.h +142 -0
- data/ext/nokogiri/include/libxslt/xsltlocale.h +76 -0
- data/ext/nokogiri/include/libxslt/xsltutils.h +313 -0
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +278 -0
- data/ext/nokogiri/nokogiri.h +223 -0
- data/ext/nokogiri/test_global_handlers.c +40 -0
- data/ext/nokogiri/xml_attr.c +103 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_cdata.c +57 -0
- data/ext/nokogiri/xml_comment.c +62 -0
- data/ext/nokogiri/xml_document.c +680 -0
- data/ext/nokogiri/xml_document_fragment.c +44 -0
- data/ext/nokogiri/xml_dtd.c +208 -0
- data/ext/nokogiri/xml_element_content.c +128 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_encoding_handler.c +104 -0
- data/ext/nokogiri/xml_entity_decl.c +112 -0
- data/ext/nokogiri/xml_entity_reference.c +50 -0
- data/ext/nokogiri/xml_namespace.c +120 -0
- data/ext/nokogiri/xml_node.c +2144 -0
- data/ext/nokogiri/xml_node_set.c +498 -0
- data/ext/nokogiri/xml_processing_instruction.c +54 -0
- data/ext/nokogiri/xml_reader.c +719 -0
- data/ext/nokogiri/xml_relax_ng.c +185 -0
- data/ext/nokogiri/xml_sax_parser.c +310 -0
- data/ext/nokogiri/xml_sax_parser_context.c +281 -0
- data/ext/nokogiri/xml_sax_push_parser.c +168 -0
- data/ext/nokogiri/xml_schema.c +284 -0
- data/ext/nokogiri/xml_syntax_error.c +85 -0
- data/ext/nokogiri/xml_text.c +48 -0
- data/ext/nokogiri/xml_xpath_context.c +406 -0
- data/ext/nokogiri/xslt_stylesheet.c +264 -0
- data/gumbo-parser/CHANGES.md +63 -0
- data/gumbo-parser/Makefile +101 -0
- data/gumbo-parser/THANKS +27 -0
- data/lib/nokogiri/2.6/nokogiri.so +0 -0
- data/lib/nokogiri/2.7/nokogiri.so +0 -0
- data/lib/nokogiri/3.0/nokogiri.so +0 -0
- data/lib/nokogiri/3.1/nokogiri.so +0 -0
- data/lib/nokogiri/class_resolver.rb +67 -0
- data/lib/nokogiri/css/node.rb +54 -0
- data/lib/nokogiri/css/parser.rb +759 -0
- data/lib/nokogiri/css/parser.y +280 -0
- data/lib/nokogiri/css/parser_extras.rb +94 -0
- data/lib/nokogiri/css/syntax_error.rb +9 -0
- data/lib/nokogiri/css/tokenizer.rb +155 -0
- data/lib/nokogiri/css/tokenizer.rex +56 -0
- data/lib/nokogiri/css/xpath_visitor.rb +359 -0
- data/lib/nokogiri/css.rb +60 -0
- data/lib/nokogiri/decorators/slop.rb +44 -0
- data/lib/nokogiri/extension.rb +31 -0
- data/lib/nokogiri/gumbo.rb +15 -0
- data/lib/nokogiri/html.rb +48 -0
- data/lib/nokogiri/html4/builder.rb +37 -0
- data/lib/nokogiri/html4/document.rb +331 -0
- data/lib/nokogiri/html4/document_fragment.rb +54 -0
- data/lib/nokogiri/html4/element_description.rb +25 -0
- data/lib/nokogiri/html4/element_description_defaults.rb +578 -0
- data/lib/nokogiri/html4/entity_lookup.rb +15 -0
- data/lib/nokogiri/html4/sax/parser.rb +61 -0
- data/lib/nokogiri/html4/sax/parser_context.rb +20 -0
- data/lib/nokogiri/html4/sax/push_parser.rb +37 -0
- data/lib/nokogiri/html4.rb +46 -0
- data/lib/nokogiri/html5/document.rb +88 -0
- data/lib/nokogiri/html5/document_fragment.rb +83 -0
- data/lib/nokogiri/html5/node.rb +96 -0
- data/lib/nokogiri/html5.rb +477 -0
- data/lib/nokogiri/jruby/dependencies.rb +21 -0
- data/lib/nokogiri/syntax_error.rb +6 -0
- data/lib/nokogiri/version/constant.rb +6 -0
- data/lib/nokogiri/version/info.rb +221 -0
- data/lib/nokogiri/version.rb +4 -0
- data/lib/nokogiri/xml/attr.rb +17 -0
- data/lib/nokogiri/xml/attribute_decl.rb +20 -0
- data/lib/nokogiri/xml/builder.rb +485 -0
- data/lib/nokogiri/xml/cdata.rb +13 -0
- data/lib/nokogiri/xml/character_data.rb +9 -0
- data/lib/nokogiri/xml/document.rb +418 -0
- data/lib/nokogiri/xml/document_fragment.rb +162 -0
- data/lib/nokogiri/xml/dtd.rb +34 -0
- data/lib/nokogiri/xml/element_content.rb +38 -0
- data/lib/nokogiri/xml/element_decl.rb +15 -0
- data/lib/nokogiri/xml/entity_decl.rb +21 -0
- data/lib/nokogiri/xml/entity_reference.rb +20 -0
- data/lib/nokogiri/xml/namespace.rb +16 -0
- data/lib/nokogiri/xml/node/save_options.rb +65 -0
- data/lib/nokogiri/xml/node.rb +1402 -0
- data/lib/nokogiri/xml/node_set.rb +364 -0
- data/lib/nokogiri/xml/notation.rb +19 -0
- data/lib/nokogiri/xml/parse_options.rb +133 -0
- data/lib/nokogiri/xml/pp/character_data.rb +21 -0
- data/lib/nokogiri/xml/pp/node.rb +55 -0
- data/lib/nokogiri/xml/pp.rb +4 -0
- data/lib/nokogiri/xml/processing_instruction.rb +10 -0
- data/lib/nokogiri/xml/reader.rb +107 -0
- data/lib/nokogiri/xml/relax_ng.rb +38 -0
- data/lib/nokogiri/xml/sax/document.rb +167 -0
- data/lib/nokogiri/xml/sax/parser.rb +125 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +21 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
- data/lib/nokogiri/xml/sax.rb +6 -0
- data/lib/nokogiri/xml/schema.rb +73 -0
- data/lib/nokogiri/xml/searchable.rb +259 -0
- data/lib/nokogiri/xml/syntax_error.rb +71 -0
- data/lib/nokogiri/xml/text.rb +11 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +13 -0
- data/lib/nokogiri/xml/xpath.rb +21 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xml.rb +75 -0
- data/lib/nokogiri/xslt/stylesheet.rb +27 -0
- data/lib/nokogiri/xslt.rb +58 -0
- data/lib/nokogiri.rb +128 -0
- data/lib/xsd/xmlparser/nokogiri.rb +104 -0
- metadata +539 -0
@@ -0,0 +1,1402 @@
|
|
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., +<+ 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.
|
128
|
+
end
|
129
|
+
|
130
|
+
###
|
131
|
+
# Decorate this node with the decorators set up in this node's Document
|
132
|
+
def decorate!
|
133
|
+
document.decorate(self)
|
134
|
+
end
|
135
|
+
|
136
|
+
# :section: Manipulating Document Structure
|
137
|
+
|
138
|
+
###
|
139
|
+
# Add +node_or_tags+ as a child of this Node.
|
140
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.
|
141
|
+
#
|
142
|
+
# Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is a DocumentFragment, NodeSet, or string).
|
143
|
+
#
|
144
|
+
# Also see related method +<<+.
|
145
|
+
def add_child(node_or_tags)
|
146
|
+
node_or_tags = coerce(node_or_tags)
|
147
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
148
|
+
node_or_tags.each { |n| add_child_node_and_reparent_attrs(n) }
|
149
|
+
else
|
150
|
+
add_child_node_and_reparent_attrs(node_or_tags)
|
151
|
+
end
|
152
|
+
node_or_tags
|
153
|
+
end
|
154
|
+
|
155
|
+
###
|
156
|
+
# Add +node_or_tags+ as the first child of this Node.
|
157
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.
|
158
|
+
#
|
159
|
+
# Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is a DocumentFragment, NodeSet, or string).
|
160
|
+
#
|
161
|
+
# Also see related method +add_child+.
|
162
|
+
def prepend_child(node_or_tags)
|
163
|
+
if (first = children.first)
|
164
|
+
# Mimic the error add_child would raise.
|
165
|
+
raise "Document already has a root node" if document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
|
166
|
+
first.__send__(:add_sibling, :previous, node_or_tags)
|
167
|
+
else
|
168
|
+
add_child(node_or_tags)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
###
|
173
|
+
# Add html around this node
|
174
|
+
#
|
175
|
+
# Returns self
|
176
|
+
def wrap(html)
|
177
|
+
new_parent = document.parse(html).first
|
178
|
+
add_next_sibling(new_parent)
|
179
|
+
new_parent.add_child(self)
|
180
|
+
self
|
181
|
+
end
|
182
|
+
|
183
|
+
###
|
184
|
+
# Add +node_or_tags+ as a child of this Node.
|
185
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.
|
186
|
+
#
|
187
|
+
# Returns self, to support chaining of calls (e.g., root << child1 << child2)
|
188
|
+
#
|
189
|
+
# Also see related method +add_child+.
|
190
|
+
def <<(node_or_tags)
|
191
|
+
add_child(node_or_tags)
|
192
|
+
self
|
193
|
+
end
|
194
|
+
|
195
|
+
###
|
196
|
+
# Insert +node_or_tags+ before this Node (as a sibling).
|
197
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.
|
198
|
+
#
|
199
|
+
# Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is a DocumentFragment, NodeSet, or string).
|
200
|
+
#
|
201
|
+
# Also see related method +before+.
|
202
|
+
def add_previous_sibling(node_or_tags)
|
203
|
+
raise ArgumentError,
|
204
|
+
"A document may not have multiple root nodes." if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
|
205
|
+
|
206
|
+
add_sibling(:previous, node_or_tags)
|
207
|
+
end
|
208
|
+
|
209
|
+
###
|
210
|
+
# Insert +node_or_tags+ after this Node (as a sibling).
|
211
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.
|
212
|
+
#
|
213
|
+
# Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is a DocumentFragment, NodeSet, or string).
|
214
|
+
#
|
215
|
+
# Also see related method +after+.
|
216
|
+
def add_next_sibling(node_or_tags)
|
217
|
+
raise ArgumentError,
|
218
|
+
"A document may not have multiple root nodes." if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
|
219
|
+
|
220
|
+
add_sibling(:next, node_or_tags)
|
221
|
+
end
|
222
|
+
|
223
|
+
####
|
224
|
+
# Insert +node_or_tags+ before this node (as a sibling).
|
225
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.
|
226
|
+
#
|
227
|
+
# Returns self, to support chaining of calls.
|
228
|
+
#
|
229
|
+
# Also see related method +add_previous_sibling+.
|
230
|
+
def before(node_or_tags)
|
231
|
+
add_previous_sibling(node_or_tags)
|
232
|
+
self
|
233
|
+
end
|
234
|
+
|
235
|
+
####
|
236
|
+
# Insert +node_or_tags+ after this node (as a sibling).
|
237
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.
|
238
|
+
#
|
239
|
+
# Returns self, to support chaining of calls.
|
240
|
+
#
|
241
|
+
# Also see related method +add_next_sibling+.
|
242
|
+
def after(node_or_tags)
|
243
|
+
add_next_sibling(node_or_tags)
|
244
|
+
self
|
245
|
+
end
|
246
|
+
|
247
|
+
####
|
248
|
+
# Set the inner html for this Node to +node_or_tags+
|
249
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.
|
250
|
+
#
|
251
|
+
# Also see related method +children=+
|
252
|
+
def inner_html=(node_or_tags)
|
253
|
+
self.children = node_or_tags
|
254
|
+
end
|
255
|
+
|
256
|
+
####
|
257
|
+
# Set the inner html for this Node +node_or_tags+
|
258
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.
|
259
|
+
#
|
260
|
+
# Also see related method +inner_html=+
|
261
|
+
def children=(node_or_tags)
|
262
|
+
node_or_tags = coerce(node_or_tags)
|
263
|
+
children.unlink
|
264
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
265
|
+
node_or_tags.each { |n| add_child_node_and_reparent_attrs(n) }
|
266
|
+
else
|
267
|
+
add_child_node_and_reparent_attrs(node_or_tags)
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
####
|
272
|
+
# Replace this Node with +node_or_tags+.
|
273
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.
|
274
|
+
#
|
275
|
+
# Returns the reparented node (if +node_or_tags+ is a Node), or NodeSet (if +node_or_tags+ is a DocumentFragment, NodeSet, or string).
|
276
|
+
#
|
277
|
+
# Also see related method +swap+.
|
278
|
+
def replace(node_or_tags)
|
279
|
+
raise("Cannot replace a node with no parent") unless parent
|
280
|
+
|
281
|
+
# We cannot replace a text node directly, otherwise libxml will return
|
282
|
+
# an internal error at parser.c:13031, I don't know exactly why
|
283
|
+
# libxml is trying to find a parent node that is an element or document
|
284
|
+
# so I can't tell if this is bug in libxml or not. issue #775.
|
285
|
+
if text?
|
286
|
+
replacee = Nokogiri::XML::Node.new("dummy", document)
|
287
|
+
add_previous_sibling_node(replacee)
|
288
|
+
unlink
|
289
|
+
return replacee.replace(node_or_tags)
|
290
|
+
end
|
291
|
+
|
292
|
+
node_or_tags = parent.coerce(node_or_tags)
|
293
|
+
|
294
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
295
|
+
node_or_tags.each { |n| add_previous_sibling(n) }
|
296
|
+
unlink
|
297
|
+
else
|
298
|
+
replace_node(node_or_tags)
|
299
|
+
end
|
300
|
+
node_or_tags
|
301
|
+
end
|
302
|
+
|
303
|
+
####
|
304
|
+
# Swap this Node for +node_or_tags+
|
305
|
+
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.
|
306
|
+
#
|
307
|
+
# Returns self, to support chaining of calls.
|
308
|
+
#
|
309
|
+
# Also see related method +replace+.
|
310
|
+
def swap(node_or_tags)
|
311
|
+
replace(node_or_tags)
|
312
|
+
self
|
313
|
+
end
|
314
|
+
|
315
|
+
####
|
316
|
+
# Set the Node's content to a Text node containing +string+. The string gets XML escaped, not interpreted as markup.
|
317
|
+
def content=(string)
|
318
|
+
self.native_content = encode_special_chars(string.to_s)
|
319
|
+
end
|
320
|
+
|
321
|
+
###
|
322
|
+
# Set the parent Node for this Node
|
323
|
+
def parent=(parent_node)
|
324
|
+
parent_node.add_child(self)
|
325
|
+
end
|
326
|
+
|
327
|
+
###
|
328
|
+
# Adds a default namespace supplied as a string +url+ href, to self.
|
329
|
+
# The consequence is as an xmlns attribute with supplied argument were
|
330
|
+
# present in parsed XML. A default namespace set with this method will
|
331
|
+
# now show up in #attributes, but when this node is serialized to XML an
|
332
|
+
# "xmlns" attribute will appear. See also #namespace and #namespace=
|
333
|
+
def default_namespace=(url)
|
334
|
+
add_namespace_definition(nil, url)
|
335
|
+
end
|
336
|
+
|
337
|
+
###
|
338
|
+
# Set the default namespace on this node (as would be defined with an
|
339
|
+
# "xmlns=" attribute in XML source), as a Namespace object +ns+. Note that
|
340
|
+
# a Namespace added this way will NOT be serialized as an xmlns attribute
|
341
|
+
# for this node. You probably want #default_namespace= instead, or perhaps
|
342
|
+
# #add_namespace_definition with a nil prefix argument.
|
343
|
+
def namespace=(ns)
|
344
|
+
return set_namespace(ns) unless ns
|
345
|
+
|
346
|
+
unless Nokogiri::XML::Namespace === ns
|
347
|
+
raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace"
|
348
|
+
end
|
349
|
+
if ns.document != document
|
350
|
+
raise ArgumentError, "namespace must be declared on the same document"
|
351
|
+
end
|
352
|
+
|
353
|
+
set_namespace(ns)
|
354
|
+
end
|
355
|
+
|
356
|
+
###
|
357
|
+
# Do xinclude substitution on the subtree below node. If given a block, a
|
358
|
+
# Nokogiri::XML::ParseOptions object initialized from +options+, will be
|
359
|
+
# passed to it, allowing more convenient modification of the parser options.
|
360
|
+
def do_xinclude(options = XML::ParseOptions::DEFAULT_XML)
|
361
|
+
options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
|
362
|
+
yield options if block_given?
|
363
|
+
|
364
|
+
# call c extension
|
365
|
+
process_xincludes(options.to_i)
|
366
|
+
end
|
367
|
+
|
368
|
+
alias_method :next, :next_sibling
|
369
|
+
alias_method :previous, :previous_sibling
|
370
|
+
alias_method :next=, :add_next_sibling
|
371
|
+
alias_method :previous=, :add_previous_sibling
|
372
|
+
alias_method :remove, :unlink
|
373
|
+
alias_method :name=, :node_name=
|
374
|
+
alias_method :add_namespace, :add_namespace_definition
|
375
|
+
|
376
|
+
# :section:
|
377
|
+
|
378
|
+
alias_method :inner_text, :content
|
379
|
+
alias_method :text, :content
|
380
|
+
alias_method :to_str, :content
|
381
|
+
alias_method :name, :node_name
|
382
|
+
alias_method :type, :node_type
|
383
|
+
alias_method :clone, :dup
|
384
|
+
alias_method :elements, :element_children
|
385
|
+
|
386
|
+
# :section: Working With Node Attributes
|
387
|
+
|
388
|
+
# :call-seq: [](name) → (String, nil)
|
389
|
+
#
|
390
|
+
# Fetch an attribute from this node.
|
391
|
+
#
|
392
|
+
# ⚠ Note that attributes with namespaces cannot be accessed with this method. To access
|
393
|
+
# namespaced attributes, use #attribute_with_ns.
|
394
|
+
#
|
395
|
+
# [Returns] (String, nil) value of the attribute +name+, or +nil+ if no matching attribute exists
|
396
|
+
#
|
397
|
+
# *Example*
|
398
|
+
#
|
399
|
+
# doc = Nokogiri::XML("<root><child size='large' class='big wide tall'/></root>")
|
400
|
+
# child = doc.at_css("child")
|
401
|
+
# child["size"] # => "large"
|
402
|
+
# child["class"] # => "big wide tall"
|
403
|
+
#
|
404
|
+
# *Example:* Namespaced attributes will not be returned.
|
405
|
+
#
|
406
|
+
# ⚠ Note namespaced attributes may be accessed with #attribute or #attribute_with_ns
|
407
|
+
#
|
408
|
+
# doc = Nokogiri::XML(<<~EOF)
|
409
|
+
# <root xmlns:width='http://example.com/widths'>
|
410
|
+
# <child width:size='broad'/>
|
411
|
+
# </root>
|
412
|
+
# EOF
|
413
|
+
# doc.at_css("child")["size"] # => nil
|
414
|
+
# doc.at_css("child").attribute("size").value # => "broad"
|
415
|
+
# doc.at_css("child").attribute_with_ns("size", "http://example.com/widths").value
|
416
|
+
# # => "broad"
|
417
|
+
#
|
418
|
+
def [](name)
|
419
|
+
get(name.to_s)
|
420
|
+
end
|
421
|
+
|
422
|
+
# :call-seq: []=(name, value) → value
|
423
|
+
#
|
424
|
+
# Update the attribute +name+ to +value+, or create the attribute if it does not exist.
|
425
|
+
#
|
426
|
+
# ⚠ Note that attributes with namespaces cannot be accessed with this method. To access
|
427
|
+
# namespaced attributes for update, use #attribute_with_ns. To add a namespaced attribute,
|
428
|
+
# see the example below.
|
429
|
+
#
|
430
|
+
# [Returns] +value+
|
431
|
+
#
|
432
|
+
# *Example*
|
433
|
+
#
|
434
|
+
# doc = Nokogiri::XML("<root><child/></root>")
|
435
|
+
# child = doc.at_css("child")
|
436
|
+
# child["size"] = "broad"
|
437
|
+
# child.to_html
|
438
|
+
# # => "<child size=\"broad\"></child>"
|
439
|
+
#
|
440
|
+
# *Example:* Add a namespaced attribute.
|
441
|
+
#
|
442
|
+
# doc = Nokogiri::XML(<<~EOF)
|
443
|
+
# <root xmlns:width='http://example.com/widths'>
|
444
|
+
# <child/>
|
445
|
+
# </root>
|
446
|
+
# EOF
|
447
|
+
# child = doc.at_css("child")
|
448
|
+
# child["size"] = "broad"
|
449
|
+
# ns = doc.root.namespace_definitions.find { |ns| ns.prefix == "width" }
|
450
|
+
# child.attribute("size").namespace = ns
|
451
|
+
# doc.to_html
|
452
|
+
# # => "<root xmlns:width=\"http://example.com/widths\">\n" +
|
453
|
+
# # " <child width:size=\"broad\"></child>\n" +
|
454
|
+
# # "</root>\n"
|
455
|
+
#
|
456
|
+
def []=(name, value)
|
457
|
+
set(name.to_s, value.to_s)
|
458
|
+
end
|
459
|
+
|
460
|
+
#
|
461
|
+
# :call-seq: attributes() → Hash<String ⇒ Nokogiri::XML::Attr>
|
462
|
+
#
|
463
|
+
# Fetch this node's attributes.
|
464
|
+
#
|
465
|
+
# ⚠ Because the keys do not include any namespace information for the attribute, in case of a
|
466
|
+
# simple name collision, not all attributes will be returned. In this case, you will need to
|
467
|
+
# use #attribute_nodes.
|
468
|
+
#
|
469
|
+
# [Returns]
|
470
|
+
# Hash containing attributes belonging to +self+. The hash keys are String attribute
|
471
|
+
# names (without the namespace), and the hash values are Nokogiri::XML::Attr.
|
472
|
+
#
|
473
|
+
# *Example* with no namespaces:
|
474
|
+
#
|
475
|
+
# doc = Nokogiri::XML("<root><child size='large' class='big wide tall'/></root>")
|
476
|
+
# doc.at_css("child").attributes
|
477
|
+
# # => {"size"=>#(Attr:0x550 { name = "size", value = "large" }),
|
478
|
+
# # "class"=>#(Attr:0x564 { name = "class", value = "big wide tall" })}
|
479
|
+
#
|
480
|
+
# *Example* with a namespace:
|
481
|
+
#
|
482
|
+
# doc = Nokogiri::XML("<root xmlns:desc='http://example.com/sizes'><child desc:size='large'/></root>")
|
483
|
+
# doc.at_css("child").attributes
|
484
|
+
# # => {"size"=>
|
485
|
+
# # #(Attr:0x550 {
|
486
|
+
# # name = "size",
|
487
|
+
# # namespace = #(Namespace:0x564 {
|
488
|
+
# # prefix = "desc",
|
489
|
+
# # href = "http://example.com/sizes"
|
490
|
+
# # }),
|
491
|
+
# # value = "large"
|
492
|
+
# # })}
|
493
|
+
#
|
494
|
+
# *Example* with an attribute name collision:
|
495
|
+
#
|
496
|
+
# ⚠ Note that only one of the attributes is returned in the Hash.
|
497
|
+
#
|
498
|
+
# doc = Nokogiri::XML(<<~EOF)
|
499
|
+
# <root xmlns:width='http://example.com/widths'
|
500
|
+
# xmlns:height='http://example.com/heights'>
|
501
|
+
# <child width:size='broad' height:size='tall'/>
|
502
|
+
# </root>
|
503
|
+
# EOF
|
504
|
+
# doc.at_css("child").attributes
|
505
|
+
# # => {"size"=>
|
506
|
+
# # #(Attr:0x550 {
|
507
|
+
# # name = "size",
|
508
|
+
# # namespace = #(Namespace:0x564 {
|
509
|
+
# # prefix = "height",
|
510
|
+
# # href = "http://example.com/heights"
|
511
|
+
# # }),
|
512
|
+
# # value = "tall"
|
513
|
+
# # })}
|
514
|
+
#
|
515
|
+
def attributes
|
516
|
+
attribute_nodes.each_with_object({}) do |node, hash|
|
517
|
+
hash[node.node_name] = node
|
518
|
+
end
|
519
|
+
end
|
520
|
+
|
521
|
+
###
|
522
|
+
# Get the attribute values for this Node.
|
523
|
+
def values
|
524
|
+
attribute_nodes.map(&:value)
|
525
|
+
end
|
526
|
+
|
527
|
+
###
|
528
|
+
# Does this Node's attributes include <value>
|
529
|
+
def value?(value)
|
530
|
+
values.include?(value)
|
531
|
+
end
|
532
|
+
|
533
|
+
###
|
534
|
+
# Get the attribute names for this Node.
|
535
|
+
def keys
|
536
|
+
attribute_nodes.map(&:node_name)
|
537
|
+
end
|
538
|
+
|
539
|
+
###
|
540
|
+
# Iterate over each attribute name and value pair for this Node.
|
541
|
+
def each
|
542
|
+
attribute_nodes.each do |node|
|
543
|
+
yield [node.node_name, node.value]
|
544
|
+
end
|
545
|
+
end
|
546
|
+
|
547
|
+
###
|
548
|
+
# Remove the attribute named +name+
|
549
|
+
def remove_attribute(name)
|
550
|
+
attr = attributes[name].remove if key?(name)
|
551
|
+
clear_xpath_context if Nokogiri.jruby?
|
552
|
+
attr
|
553
|
+
end
|
554
|
+
|
555
|
+
#
|
556
|
+
# :call-seq: classes() → Array<String>
|
557
|
+
#
|
558
|
+
# Fetch CSS class names of a Node.
|
559
|
+
#
|
560
|
+
# This is a convenience function and is equivalent to:
|
561
|
+
#
|
562
|
+
# node.kwattr_values("class")
|
563
|
+
#
|
564
|
+
# See related: #kwattr_values, #add_class, #append_class, #remove_class
|
565
|
+
#
|
566
|
+
# [Returns]
|
567
|
+
# The CSS classes (Array of String) present in the Node's "class" attribute. If the
|
568
|
+
# attribute is empty or non-existent, the return value is an empty array.
|
569
|
+
#
|
570
|
+
# *Example*
|
571
|
+
#
|
572
|
+
# node # => <div class="section title header"></div>
|
573
|
+
# node.classes # => ["section", "title", "header"]
|
574
|
+
#
|
575
|
+
def classes
|
576
|
+
kwattr_values("class")
|
577
|
+
end
|
578
|
+
|
579
|
+
#
|
580
|
+
# :call-seq: add_class(names) → self
|
581
|
+
#
|
582
|
+
# Ensure HTML CSS classes are present on +self+. Any CSS classes in +names+ that already exist
|
583
|
+
# in the "class" attribute are _not_ added. Note that any existing duplicates in the
|
584
|
+
# "class" attribute are not removed. Compare with #append_class.
|
585
|
+
#
|
586
|
+
# This is a convenience function and is equivalent to:
|
587
|
+
#
|
588
|
+
# node.kwattr_add("class", names)
|
589
|
+
#
|
590
|
+
# See related: #kwattr_add, #classes, #append_class, #remove_class
|
591
|
+
#
|
592
|
+
# [Parameters]
|
593
|
+
# - +names+ (String, Array<String>)
|
594
|
+
#
|
595
|
+
# CSS class names to be added to the Node's "class" attribute. May be a string containing
|
596
|
+
# whitespace-delimited names, or an Array of String names. Any class names already present
|
597
|
+
# will not be added. Any class names not present will be added. If no "class" attribute
|
598
|
+
# exists, one is created.
|
599
|
+
#
|
600
|
+
# [Returns] +self+ (Node) for ease of chaining method calls.
|
601
|
+
#
|
602
|
+
# *Example:* Ensure that the node has CSS class "section"
|
603
|
+
#
|
604
|
+
# node # => <div></div>
|
605
|
+
# node.add_class("section") # => <div class="section"></div>
|
606
|
+
# node.add_class("section") # => <div class="section"></div> # duplicate not added
|
607
|
+
#
|
608
|
+
# *Example:* Ensure that the node has CSS classes "section" and "header", via a String argument
|
609
|
+
#
|
610
|
+
# Note that the CSS class "section" is not added because it is already present.
|
611
|
+
# Note also that the pre-existing duplicate CSS class "section" is not removed.
|
612
|
+
#
|
613
|
+
# node # => <div class="section section"></div>
|
614
|
+
# node.add_class("section header") # => <div class="section section header"></div>
|
615
|
+
#
|
616
|
+
# *Example:* Ensure that the node has CSS classes "section" and "header", via an Array argument
|
617
|
+
#
|
618
|
+
# node # => <div></div>
|
619
|
+
# node.add_class(["section", "header"]) # => <div class="section header"></div>
|
620
|
+
#
|
621
|
+
def add_class(names)
|
622
|
+
kwattr_add("class", names)
|
623
|
+
end
|
624
|
+
|
625
|
+
#
|
626
|
+
# :call-seq: append_class(names) → self
|
627
|
+
#
|
628
|
+
# Add HTML CSS classes to +self+, regardless of duplication. Compare with #add_class.
|
629
|
+
#
|
630
|
+
# This is a convenience function and is equivalent to:
|
631
|
+
#
|
632
|
+
# node.kwattr_append("class", names)
|
633
|
+
#
|
634
|
+
# See related: #kwattr_append, #classes, #add_class, #remove_class
|
635
|
+
#
|
636
|
+
# [Parameters]
|
637
|
+
# - +names+ (String, Array<String>)
|
638
|
+
#
|
639
|
+
# CSS class names to be appended to the Node's "class" attribute. May be a string containing
|
640
|
+
# whitespace-delimited names, or an Array of String names. All class names passed in will be
|
641
|
+
# appended to the "class" attribute even if they are already present in the attribute
|
642
|
+
# value. If no "class" attribute exists, one is created.
|
643
|
+
#
|
644
|
+
# [Returns] +self+ (Node) for ease of chaining method calls.
|
645
|
+
#
|
646
|
+
# *Example:* Append "section" to the node's CSS "class" attribute
|
647
|
+
#
|
648
|
+
# node # => <div></div>
|
649
|
+
# node.append_class("section") # => <div class="section"></div>
|
650
|
+
# node.append_class("section") # => <div class="section section"></div> # duplicate added!
|
651
|
+
#
|
652
|
+
# *Example:* Append "section" and "header" to the noded's CSS "class" attribute, via a String argument
|
653
|
+
#
|
654
|
+
# Note that the CSS class "section" is appended even though it is already present
|
655
|
+
#
|
656
|
+
# node # => <div class="section section"></div>
|
657
|
+
# node.append_class("section header") # => <div class="section section section header"></div>
|
658
|
+
#
|
659
|
+
# *Example:* Append "section" and "header" to the node's CSS "class" attribute, via an Array argument
|
660
|
+
#
|
661
|
+
# node # => <div></div>
|
662
|
+
# node.append_class(["section", "header"]) # => <div class="section header"></div>
|
663
|
+
# node.append_class(["section", "header"]) # => <div class="section header section header"></div>
|
664
|
+
#
|
665
|
+
def append_class(names)
|
666
|
+
kwattr_append("class", names)
|
667
|
+
end
|
668
|
+
|
669
|
+
# :call-seq:
|
670
|
+
# remove_class(css_classes) → self
|
671
|
+
#
|
672
|
+
# Remove HTML CSS classes from this node. Any CSS class names in +css_classes+ that exist in
|
673
|
+
# this node's "class" attribute are removed, including any multiple entries.
|
674
|
+
#
|
675
|
+
# If no CSS classes remain after this operation, or if +css_classes+ is +nil+, the "class"
|
676
|
+
# attribute is deleted from the node.
|
677
|
+
#
|
678
|
+
# This is a convenience function and is equivalent to:
|
679
|
+
#
|
680
|
+
# node.kwattr_remove("class", css_classes)
|
681
|
+
#
|
682
|
+
# Also see #kwattr_remove, #classes, #add_class, #append_class
|
683
|
+
#
|
684
|
+
# [Parameters]
|
685
|
+
# - +css_classes+ (String, Array<String>)
|
686
|
+
#
|
687
|
+
# CSS class names to be removed from the Node's
|
688
|
+
# "class" attribute. May be a string containing whitespace-delimited names, or an Array of
|
689
|
+
# String names. Any class names already present will be removed. If no CSS classes remain,
|
690
|
+
# the "class" attribute is deleted.
|
691
|
+
#
|
692
|
+
# [Returns] +self+ (Nokogiri::XML::Node) for ease of chaining method calls.
|
693
|
+
#
|
694
|
+
# *Example*: Deleting a CSS class
|
695
|
+
#
|
696
|
+
# Note that all instances of the class "section" are removed from the "class" attribute.
|
697
|
+
#
|
698
|
+
# node # => <div class="section header section"></div>
|
699
|
+
# node.remove_class("section") # => <div class="header"></div>
|
700
|
+
#
|
701
|
+
# *Example*: Deleting the only remaining CSS class
|
702
|
+
#
|
703
|
+
# Note that the attribute is removed once there are no remaining classes.
|
704
|
+
#
|
705
|
+
# node # => <div class="section"></div>
|
706
|
+
# node.remove_class("section") # => <div></div>
|
707
|
+
#
|
708
|
+
# *Example*: Deleting multiple CSS classes
|
709
|
+
#
|
710
|
+
# Note that the "class" attribute is deleted once it's empty.
|
711
|
+
#
|
712
|
+
# node # => <div class="section header float"></div>
|
713
|
+
# node.remove_class(["section", "float"]) # => <div class="header"></div>
|
714
|
+
#
|
715
|
+
def remove_class(names = nil)
|
716
|
+
kwattr_remove("class", names)
|
717
|
+
end
|
718
|
+
|
719
|
+
# :call-seq:
|
720
|
+
# kwattr_values(attribute_name) → Array<String>
|
721
|
+
#
|
722
|
+
# Fetch values from a keyword attribute of a Node.
|
723
|
+
#
|
724
|
+
# A "keyword attribute" is a node attribute that contains a set of space-delimited
|
725
|
+
# values. Perhaps the most familiar example of this is the HTML "class" attribute used to
|
726
|
+
# contain CSS classes. But other keyword attributes exist, for instance
|
727
|
+
# {the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
|
728
|
+
#
|
729
|
+
# See also #classes, #kwattr_add, #kwattr_append, #kwattr_remove
|
730
|
+
#
|
731
|
+
# [Parameters]
|
732
|
+
# - +attribute_name+ (String) The name of the keyword attribute to be inspected.
|
733
|
+
#
|
734
|
+
# [Returns]
|
735
|
+
# (Array<String>) The values present in the Node's +attribute_name+ attribute. If the
|
736
|
+
# attribute is empty or non-existent, the return value is an empty array.
|
737
|
+
#
|
738
|
+
# *Example:*
|
739
|
+
#
|
740
|
+
# node # => <a rel="nofollow noopener external">link</a>
|
741
|
+
# node.kwattr_values("rel") # => ["nofollow", "noopener", "external"]
|
742
|
+
#
|
743
|
+
# Since v1.11.0
|
744
|
+
def kwattr_values(attribute_name)
|
745
|
+
keywordify(get_attribute(attribute_name) || [])
|
746
|
+
end
|
747
|
+
|
748
|
+
# :call-seq:
|
749
|
+
# kwattr_add(attribute_name, keywords) → self
|
750
|
+
#
|
751
|
+
# Ensure that values are present in a keyword attribute.
|
752
|
+
#
|
753
|
+
# Any values in +keywords+ that already exist in the Node's attribute values are _not_
|
754
|
+
# added. Note that any existing duplicates in the attribute values are not removed. Compare
|
755
|
+
# with #kwattr_append.
|
756
|
+
#
|
757
|
+
# A "keyword attribute" is a node attribute that contains a set of space-delimited
|
758
|
+
# values. Perhaps the most familiar example of this is the HTML "class" attribute used to
|
759
|
+
# contain CSS classes. But other keyword attributes exist, for instance
|
760
|
+
# {the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
|
761
|
+
#
|
762
|
+
# See also #add_class, #kwattr_values, #kwattr_append, #kwattr_remove
|
763
|
+
#
|
764
|
+
# [Parameters]
|
765
|
+
# - +attribute_name+ (String) The name of the keyword attribute to be modified.
|
766
|
+
# - +keywords+ (String, Array<String>)
|
767
|
+
# Keywords to be added to the attribute named +attribute_name+. May be a string containing
|
768
|
+
# whitespace-delimited values, or an Array of String values. Any values already present will
|
769
|
+
# not be added. Any values not present will be added. If the named attribute does not exist,
|
770
|
+
# it is created.
|
771
|
+
#
|
772
|
+
# [Returns] +self+ (Nokogiri::XML::Node) for ease of chaining method calls.
|
773
|
+
#
|
774
|
+
# *Example:* Ensure that a +Node+ has "nofollow" in its +rel+ attribute.
|
775
|
+
#
|
776
|
+
# Note that duplicates are not added.
|
777
|
+
#
|
778
|
+
# node # => <a></a>
|
779
|
+
# node.kwattr_add("rel", "nofollow") # => <a rel="nofollow"></a>
|
780
|
+
# node.kwattr_add("rel", "nofollow") # => <a rel="nofollow"></a>
|
781
|
+
#
|
782
|
+
# *Example:* Ensure that a +Node+ has "nofollow" and "noreferrer" in its +rel+ attribute, via a
|
783
|
+
# String argument.
|
784
|
+
#
|
785
|
+
# Note that "nofollow" is not added because it is already present. Note also that the
|
786
|
+
# pre-existing duplicate "nofollow" is not removed.
|
787
|
+
#
|
788
|
+
# node # => <a rel="nofollow nofollow"></a>
|
789
|
+
# node.kwattr_add("rel", "nofollow noreferrer") # => <a rel="nofollow nofollow noreferrer"></a>
|
790
|
+
#
|
791
|
+
# *Example:* Ensure that a +Node+ has "nofollow" and "noreferrer" in its +rel+ attribute, via
|
792
|
+
# an Array argument.
|
793
|
+
#
|
794
|
+
# node # => <a></a>
|
795
|
+
# node.kwattr_add("rel", ["nofollow", "noreferrer"]) # => <a rel="nofollow noreferrer"></a>
|
796
|
+
#
|
797
|
+
# Since v1.11.0
|
798
|
+
def kwattr_add(attribute_name, keywords)
|
799
|
+
keywords = keywordify(keywords)
|
800
|
+
current_kws = kwattr_values(attribute_name)
|
801
|
+
new_kws = (current_kws + (keywords - current_kws)).join(" ")
|
802
|
+
set_attribute(attribute_name, new_kws)
|
803
|
+
self
|
804
|
+
end
|
805
|
+
|
806
|
+
# :call-seq:
|
807
|
+
# kwattr_append(attribute_name, keywords) → self
|
808
|
+
#
|
809
|
+
# Add keywords to a Node's keyword attribute, regardless of duplication. Compare with
|
810
|
+
# #kwattr_add.
|
811
|
+
#
|
812
|
+
# A "keyword attribute" is a node attribute that contains a set of space-delimited
|
813
|
+
# values. Perhaps the most familiar example of this is the HTML "class" attribute used to
|
814
|
+
# contain CSS classes. But other keyword attributes exist, for instance
|
815
|
+
# {the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
|
816
|
+
#
|
817
|
+
# See also #append_class, #kwattr_values, #kwattr_add, #kwattr_remove
|
818
|
+
#
|
819
|
+
# [Parameters]
|
820
|
+
# - +attribute_name+ (String) The name of the keyword attribute to be modified.
|
821
|
+
# - +keywords+ (String, Array<String>)
|
822
|
+
# Keywords to be added to the attribute named +attribute_name+. May be a string containing
|
823
|
+
# whitespace-delimited values, or an Array of String values. All values passed in will be
|
824
|
+
# appended to the named attribute even if they are already present in the attribute. If the
|
825
|
+
# named attribute does not exist, it is created.
|
826
|
+
#
|
827
|
+
# [Returns] +self+ (Node) for ease of chaining method calls.
|
828
|
+
#
|
829
|
+
# *Example:* Append "nofollow" to the +rel+ attribute.
|
830
|
+
#
|
831
|
+
# Note that duplicates are added.
|
832
|
+
#
|
833
|
+
# node # => <a></a>
|
834
|
+
# node.kwattr_append("rel", "nofollow") # => <a rel="nofollow"></a>
|
835
|
+
# node.kwattr_append("rel", "nofollow") # => <a rel="nofollow nofollow"></a>
|
836
|
+
#
|
837
|
+
# *Example:* Append "nofollow" and "noreferrer" to the +rel+ attribute, via a String argument.
|
838
|
+
#
|
839
|
+
# Note that "nofollow" is appended even though it is already present.
|
840
|
+
#
|
841
|
+
# node # => <a rel="nofollow"></a>
|
842
|
+
# node.kwattr_append("rel", "nofollow noreferrer") # => <a rel="nofollow nofollow noreferrer"></a>
|
843
|
+
#
|
844
|
+
#
|
845
|
+
# *Example:* Append "nofollow" and "noreferrer" to the +rel+ attribute, via an Array argument.
|
846
|
+
#
|
847
|
+
# node # => <a></a>
|
848
|
+
# node.kwattr_append("rel", ["nofollow", "noreferrer"]) # => <a rel="nofollow noreferrer"></a>
|
849
|
+
#
|
850
|
+
# Since v1.11.0
|
851
|
+
def kwattr_append(attribute_name, keywords)
|
852
|
+
keywords = keywordify(keywords)
|
853
|
+
current_kws = kwattr_values(attribute_name)
|
854
|
+
new_kws = (current_kws + keywords).join(" ")
|
855
|
+
set_attribute(attribute_name, new_kws)
|
856
|
+
self
|
857
|
+
end
|
858
|
+
|
859
|
+
# :call-seq:
|
860
|
+
# kwattr_remove(attribute_name, keywords) → self
|
861
|
+
#
|
862
|
+
# Remove keywords from a keyword attribute. Any matching keywords that exist in the named
|
863
|
+
# attribute are removed, including any multiple entries.
|
864
|
+
#
|
865
|
+
# If no keywords remain after this operation, or if +keywords+ is +nil+, the attribute is
|
866
|
+
# deleted from the node.
|
867
|
+
#
|
868
|
+
# A "keyword attribute" is a node attribute that contains a set of space-delimited
|
869
|
+
# values. Perhaps the most familiar example of this is the HTML "class" attribute used to
|
870
|
+
# contain CSS classes. But other keyword attributes exist, for instance
|
871
|
+
# {the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
|
872
|
+
#
|
873
|
+
# See also #remove_class, #kwattr_values, #kwattr_add, #kwattr_append
|
874
|
+
#
|
875
|
+
# [Parameters]
|
876
|
+
# - +attribute_name+ (String) The name of the keyword attribute to be modified.
|
877
|
+
# - +keywords+ (String, Array<String>)
|
878
|
+
# Keywords to be removed from the attribute named +attribute_name+. May be a string
|
879
|
+
# containing whitespace-delimited values, or an Array of String values. Any keywords present
|
880
|
+
# in the named attribute will be removed. If no keywords remain, or if +keywords+ is nil,
|
881
|
+
# the attribute is deleted.
|
882
|
+
#
|
883
|
+
# [Returns] +self+ (Node) for ease of chaining method calls.
|
884
|
+
#
|
885
|
+
# *Example:*
|
886
|
+
#
|
887
|
+
# Note that the +rel+ attribute is deleted when empty.
|
888
|
+
#
|
889
|
+
# node # => <a rel="nofollow noreferrer">link</a>
|
890
|
+
# node.kwattr_remove("rel", "nofollow") # => <a rel="noreferrer">link</a>
|
891
|
+
# node.kwattr_remove("rel", "noreferrer") # => <a>link</a>
|
892
|
+
#
|
893
|
+
# Since v1.11.0
|
894
|
+
def kwattr_remove(attribute_name, keywords)
|
895
|
+
if keywords.nil?
|
896
|
+
remove_attribute(attribute_name)
|
897
|
+
return self
|
898
|
+
end
|
899
|
+
|
900
|
+
keywords = keywordify(keywords)
|
901
|
+
current_kws = kwattr_values(attribute_name)
|
902
|
+
new_kws = current_kws - keywords
|
903
|
+
if new_kws.empty?
|
904
|
+
remove_attribute(attribute_name)
|
905
|
+
else
|
906
|
+
set_attribute(attribute_name, new_kws.join(" "))
|
907
|
+
end
|
908
|
+
self
|
909
|
+
end
|
910
|
+
|
911
|
+
alias_method :delete, :remove_attribute
|
912
|
+
alias_method :get_attribute, :[]
|
913
|
+
alias_method :attr, :[]
|
914
|
+
alias_method :set_attribute, :[]=
|
915
|
+
alias_method :has_attribute?, :key?
|
916
|
+
|
917
|
+
# :section:
|
918
|
+
|
919
|
+
###
|
920
|
+
# Returns true if this Node matches +selector+
|
921
|
+
def matches?(selector)
|
922
|
+
ancestors.last.search(selector).include?(self)
|
923
|
+
end
|
924
|
+
|
925
|
+
###
|
926
|
+
# Create a DocumentFragment containing +tags+ that is relative to _this_
|
927
|
+
# context node.
|
928
|
+
def fragment(tags)
|
929
|
+
document.related_class("DocumentFragment").new(document, tags, self)
|
930
|
+
end
|
931
|
+
|
932
|
+
###
|
933
|
+
# Parse +string_or_io+ as a document fragment within the context of
|
934
|
+
# *this* node. Returns a XML::NodeSet containing the nodes parsed from
|
935
|
+
# +string_or_io+.
|
936
|
+
def parse(string_or_io, options = nil)
|
937
|
+
##
|
938
|
+
# When the current node is unparented and not an element node, use the
|
939
|
+
# document as the parsing context instead. Otherwise, the in-context
|
940
|
+
# parser cannot find an element or a document node.
|
941
|
+
# Document Fragments are also not usable by the in-context parser.
|
942
|
+
if !element? && !document? && (!parent || parent.fragment?)
|
943
|
+
return document.parse(string_or_io, options)
|
944
|
+
end
|
945
|
+
|
946
|
+
options ||= (document.html? ? ParseOptions::DEFAULT_HTML : ParseOptions::DEFAULT_XML)
|
947
|
+
options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
|
948
|
+
yield options if block_given?
|
949
|
+
|
950
|
+
contents = if string_or_io.respond_to?(:read)
|
951
|
+
string_or_io.read
|
952
|
+
else
|
953
|
+
string_or_io
|
954
|
+
end
|
955
|
+
|
956
|
+
return Nokogiri::XML::NodeSet.new(document) if contents.empty?
|
957
|
+
|
958
|
+
# libxml2 does not obey the +recover+ option after encountering errors during +in_context+
|
959
|
+
# parsing, and so this horrible hack is here to try to emulate recovery behavior.
|
960
|
+
#
|
961
|
+
# Unfortunately, this means we're no longer parsing "in context" and so namespaces that
|
962
|
+
# would have been inherited from the context node won't be handled correctly. This hack was
|
963
|
+
# written in 2010, and I regret it, because it's silently degrading functionality in a way
|
964
|
+
# that's not easily prevented (or even detected).
|
965
|
+
#
|
966
|
+
# I think preferable behavior would be to either:
|
967
|
+
#
|
968
|
+
# a. add an error noting that we "fell back" and pointing the user to turning off the +recover+ option
|
969
|
+
# b. don't recover, but raise a sensible exception
|
970
|
+
#
|
971
|
+
# For context and background: https://github.com/sparklemotion/nokogiri/issues/313
|
972
|
+
# FIXME bug report: https://github.com/sparklemotion/nokogiri/issues/2092
|
973
|
+
error_count = document.errors.length
|
974
|
+
node_set = in_context(contents, options.to_i)
|
975
|
+
if node_set.empty? && (document.errors.length > error_count)
|
976
|
+
if options.recover?
|
977
|
+
fragment = document.related_class("DocumentFragment").parse(contents)
|
978
|
+
node_set = fragment.children
|
979
|
+
else
|
980
|
+
raise document.errors[error_count]
|
981
|
+
end
|
982
|
+
end
|
983
|
+
node_set
|
984
|
+
end
|
985
|
+
|
986
|
+
# :call-seq:
|
987
|
+
# namespaces() → Hash<String(Namespace#prefix) ⇒ String(Namespace#href)>
|
988
|
+
#
|
989
|
+
# Fetch all the namespaces on this node and its ancestors.
|
990
|
+
#
|
991
|
+
# Note that the keys in this hash XML attributes that would be used to define this namespace,
|
992
|
+
# such as "xmlns:prefix", not just the prefix.
|
993
|
+
#
|
994
|
+
# The default namespace for this node will be included with key "xmlns".
|
995
|
+
#
|
996
|
+
# See also #namespace_scopes
|
997
|
+
#
|
998
|
+
# [Returns]
|
999
|
+
# Hash containing all the namespaces on this node and its ancestors. The hash keys are the
|
1000
|
+
# namespace prefix, and the hash value for each key is the namespace URI.
|
1001
|
+
#
|
1002
|
+
# *Example:*
|
1003
|
+
#
|
1004
|
+
# doc = Nokogiri::XML(<<~EOF)
|
1005
|
+
# <root xmlns="http://example.com/root" xmlns:in_scope="http://example.com/in_scope">
|
1006
|
+
# <first/>
|
1007
|
+
# <second xmlns="http://example.com/child"/>
|
1008
|
+
# <third xmlns:foo="http://example.com/foo"/>
|
1009
|
+
# </root>
|
1010
|
+
# EOF
|
1011
|
+
# doc.at_xpath("//root:first", "root" => "http://example.com/root").namespaces
|
1012
|
+
# # => {"xmlns"=>"http://example.com/root",
|
1013
|
+
# # "xmlns:in_scope"=>"http://example.com/in_scope"}
|
1014
|
+
# doc.at_xpath("//child:second", "child" => "http://example.com/child").namespaces
|
1015
|
+
# # => {"xmlns"=>"http://example.com/child",
|
1016
|
+
# # "xmlns:in_scope"=>"http://example.com/in_scope"}
|
1017
|
+
# doc.at_xpath("//root:third", "root" => "http://example.com/root").namespaces
|
1018
|
+
# # => {"xmlns:foo"=>"http://example.com/foo",
|
1019
|
+
# # "xmlns"=>"http://example.com/root",
|
1020
|
+
# # "xmlns:in_scope"=>"http://example.com/in_scope"}
|
1021
|
+
#
|
1022
|
+
def namespaces
|
1023
|
+
namespace_scopes.each_with_object({}) do |ns, hash|
|
1024
|
+
prefix = ns.prefix
|
1025
|
+
key = prefix ? "xmlns:#{prefix}" : "xmlns"
|
1026
|
+
hash[key] = ns.href
|
1027
|
+
end
|
1028
|
+
end
|
1029
|
+
|
1030
|
+
# Returns true if this is a Comment
|
1031
|
+
def comment?
|
1032
|
+
type == COMMENT_NODE
|
1033
|
+
end
|
1034
|
+
|
1035
|
+
# Returns true if this is a CDATA
|
1036
|
+
def cdata?
|
1037
|
+
type == CDATA_SECTION_NODE
|
1038
|
+
end
|
1039
|
+
|
1040
|
+
# Returns true if this is an XML::Document node
|
1041
|
+
def xml?
|
1042
|
+
type == DOCUMENT_NODE
|
1043
|
+
end
|
1044
|
+
|
1045
|
+
# Returns true if this is an HTML4::Document or HTML5::Document node
|
1046
|
+
def html?
|
1047
|
+
type == HTML_DOCUMENT_NODE
|
1048
|
+
end
|
1049
|
+
|
1050
|
+
# Returns true if this is a Document
|
1051
|
+
def document?
|
1052
|
+
is_a?(XML::Document)
|
1053
|
+
end
|
1054
|
+
|
1055
|
+
# Returns true if this is a ProcessingInstruction node
|
1056
|
+
def processing_instruction?
|
1057
|
+
type == PI_NODE
|
1058
|
+
end
|
1059
|
+
|
1060
|
+
# Returns true if this is a Text node
|
1061
|
+
def text?
|
1062
|
+
type == TEXT_NODE
|
1063
|
+
end
|
1064
|
+
|
1065
|
+
# Returns true if this is a DocumentFragment
|
1066
|
+
def fragment?
|
1067
|
+
type == DOCUMENT_FRAG_NODE
|
1068
|
+
end
|
1069
|
+
|
1070
|
+
###
|
1071
|
+
# Fetch the Nokogiri::HTML4::ElementDescription for this node. Returns
|
1072
|
+
# nil on XML documents and on unknown tags.
|
1073
|
+
def description
|
1074
|
+
return nil if document.xml?
|
1075
|
+
Nokogiri::HTML4::ElementDescription[name]
|
1076
|
+
end
|
1077
|
+
|
1078
|
+
###
|
1079
|
+
# Is this a read only node?
|
1080
|
+
def read_only?
|
1081
|
+
# According to gdome2, these are read-only node types
|
1082
|
+
[NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
|
1083
|
+
end
|
1084
|
+
|
1085
|
+
# Returns true if this is an Element node
|
1086
|
+
def element?
|
1087
|
+
type == ELEMENT_NODE
|
1088
|
+
end
|
1089
|
+
|
1090
|
+
alias_method :elem?, :element?
|
1091
|
+
|
1092
|
+
###
|
1093
|
+
# Turn this node in to a string. If the document is HTML, this method
|
1094
|
+
# returns html. If the document is XML, this method returns XML.
|
1095
|
+
def to_s
|
1096
|
+
document.xml? ? to_xml : to_html
|
1097
|
+
end
|
1098
|
+
|
1099
|
+
# Get the inner_html for this node's Node#children
|
1100
|
+
def inner_html(*args)
|
1101
|
+
children.map { |x| x.to_html(*args) }.join
|
1102
|
+
end
|
1103
|
+
|
1104
|
+
# Get the path to this node as a CSS expression
|
1105
|
+
def css_path
|
1106
|
+
path.split(%r{/}).map do |part|
|
1107
|
+
part.empty? ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
|
1108
|
+
end.compact.join(" > ")
|
1109
|
+
end
|
1110
|
+
|
1111
|
+
###
|
1112
|
+
# Get a list of ancestor Node for this Node. If +selector+ is given,
|
1113
|
+
# the ancestors must match +selector+
|
1114
|
+
def ancestors(selector = nil)
|
1115
|
+
return NodeSet.new(document) unless respond_to?(:parent)
|
1116
|
+
return NodeSet.new(document) unless parent
|
1117
|
+
|
1118
|
+
parents = [parent]
|
1119
|
+
|
1120
|
+
while parents.last.respond_to?(:parent)
|
1121
|
+
break unless (ctx_parent = parents.last.parent)
|
1122
|
+
parents << ctx_parent
|
1123
|
+
end
|
1124
|
+
|
1125
|
+
return NodeSet.new(document, parents) unless selector
|
1126
|
+
|
1127
|
+
root = parents.last
|
1128
|
+
search_results = root.search(selector)
|
1129
|
+
|
1130
|
+
NodeSet.new(document, parents.find_all do |parent|
|
1131
|
+
search_results.include?(parent)
|
1132
|
+
end)
|
1133
|
+
end
|
1134
|
+
|
1135
|
+
####
|
1136
|
+
# Yields self and all children to +block+ recursively.
|
1137
|
+
def traverse(&block)
|
1138
|
+
children.each { |j| j.traverse(&block) }
|
1139
|
+
yield(self)
|
1140
|
+
end
|
1141
|
+
|
1142
|
+
###
|
1143
|
+
# Accept a visitor. This method calls "visit" on +visitor+ with self.
|
1144
|
+
def accept(visitor)
|
1145
|
+
visitor.visit(self)
|
1146
|
+
end
|
1147
|
+
|
1148
|
+
###
|
1149
|
+
# Test to see if this Node is equal to +other+
|
1150
|
+
def ==(other)
|
1151
|
+
return false unless other
|
1152
|
+
return false unless other.respond_to?(:pointer_id)
|
1153
|
+
pointer_id == other.pointer_id
|
1154
|
+
end
|
1155
|
+
|
1156
|
+
###
|
1157
|
+
# Compare two Node objects with respect to their Document. Nodes from
|
1158
|
+
# different documents cannot be compared.
|
1159
|
+
def <=>(other)
|
1160
|
+
return nil unless other.is_a?(Nokogiri::XML::Node)
|
1161
|
+
return nil unless document == other.document
|
1162
|
+
compare(other)
|
1163
|
+
end
|
1164
|
+
|
1165
|
+
# :section: Serialization and Generating Output
|
1166
|
+
|
1167
|
+
###
|
1168
|
+
# Serialize Node using +options+. Save options can also be set using a
|
1169
|
+
# block. See SaveOptions.
|
1170
|
+
#
|
1171
|
+
# These two statements are equivalent:
|
1172
|
+
#
|
1173
|
+
# node.serialize(:encoding => 'UTF-8', :save_with => FORMAT | AS_XML)
|
1174
|
+
#
|
1175
|
+
# or
|
1176
|
+
#
|
1177
|
+
# node.serialize(:encoding => 'UTF-8') do |config|
|
1178
|
+
# config.format.as_xml
|
1179
|
+
# end
|
1180
|
+
#
|
1181
|
+
def serialize(*args, &block)
|
1182
|
+
options = if args.first.is_a?(Hash)
|
1183
|
+
args.shift
|
1184
|
+
else
|
1185
|
+
{
|
1186
|
+
encoding: args[0],
|
1187
|
+
save_with: args[1],
|
1188
|
+
}
|
1189
|
+
end
|
1190
|
+
|
1191
|
+
encoding = options[:encoding] || document.encoding
|
1192
|
+
options[:encoding] = encoding
|
1193
|
+
|
1194
|
+
outstring = +""
|
1195
|
+
outstring.force_encoding(Encoding.find(encoding || "utf-8"))
|
1196
|
+
io = StringIO.new(outstring)
|
1197
|
+
write_to(io, options, &block)
|
1198
|
+
io.string
|
1199
|
+
end
|
1200
|
+
|
1201
|
+
###
|
1202
|
+
# Serialize this Node to HTML
|
1203
|
+
#
|
1204
|
+
# doc.to_html
|
1205
|
+
#
|
1206
|
+
# See Node#write_to for a list of +options+. For formatted output,
|
1207
|
+
# use Node#to_xhtml instead.
|
1208
|
+
def to_html(options = {})
|
1209
|
+
to_format(SaveOptions::DEFAULT_HTML, options)
|
1210
|
+
end
|
1211
|
+
|
1212
|
+
###
|
1213
|
+
# Serialize this Node to XML using +options+
|
1214
|
+
#
|
1215
|
+
# doc.to_xml(:indent => 5, :encoding => 'UTF-8')
|
1216
|
+
#
|
1217
|
+
# See Node#write_to for a list of +options+
|
1218
|
+
def to_xml(options = {})
|
1219
|
+
options[:save_with] ||= SaveOptions::DEFAULT_XML
|
1220
|
+
serialize(options)
|
1221
|
+
end
|
1222
|
+
|
1223
|
+
###
|
1224
|
+
# Serialize this Node to XHTML using +options+
|
1225
|
+
#
|
1226
|
+
# doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')
|
1227
|
+
#
|
1228
|
+
# See Node#write_to for a list of +options+
|
1229
|
+
def to_xhtml(options = {})
|
1230
|
+
to_format(SaveOptions::DEFAULT_XHTML, options)
|
1231
|
+
end
|
1232
|
+
|
1233
|
+
###
|
1234
|
+
# Write Node to +io+ with +options+. +options+ modify the output of
|
1235
|
+
# this method. Valid options are:
|
1236
|
+
#
|
1237
|
+
# * +:encoding+ for changing the encoding
|
1238
|
+
# * +:indent_text+ the indentation text, defaults to one space
|
1239
|
+
# * +:indent+ the number of +:indent_text+ to use, defaults to 2
|
1240
|
+
# * +:save_with+ a combination of SaveOptions constants.
|
1241
|
+
#
|
1242
|
+
# To save with UTF-8 indented twice:
|
1243
|
+
#
|
1244
|
+
# node.write_to(io, :encoding => 'UTF-8', :indent => 2)
|
1245
|
+
#
|
1246
|
+
# To save indented with two dashes:
|
1247
|
+
#
|
1248
|
+
# node.write_to(io, :indent_text => '-', :indent => 2)
|
1249
|
+
#
|
1250
|
+
def write_to(io, *options)
|
1251
|
+
options = options.first.is_a?(Hash) ? options.shift : {}
|
1252
|
+
encoding = options[:encoding] || options[0]
|
1253
|
+
if Nokogiri.jruby?
|
1254
|
+
save_options = options[:save_with] || options[1]
|
1255
|
+
indent_times = options[:indent] || 0
|
1256
|
+
else
|
1257
|
+
save_options = options[:save_with] || options[1] || SaveOptions::FORMAT
|
1258
|
+
indent_times = options[:indent] || 2
|
1259
|
+
end
|
1260
|
+
indent_text = options[:indent_text] || " "
|
1261
|
+
|
1262
|
+
# Any string times 0 returns an empty string. Therefore, use the same
|
1263
|
+
# string instead of generating a new empty string for every node with
|
1264
|
+
# zero indentation.
|
1265
|
+
indentation = indent_times.zero? ? "" : (indent_text * indent_times)
|
1266
|
+
|
1267
|
+
config = SaveOptions.new(save_options.to_i)
|
1268
|
+
yield config if block_given?
|
1269
|
+
|
1270
|
+
native_write_to(io, encoding, indentation, config.options)
|
1271
|
+
end
|
1272
|
+
|
1273
|
+
###
|
1274
|
+
# Write Node as HTML to +io+ with +options+
|
1275
|
+
#
|
1276
|
+
# See Node#write_to for a list of +options+
|
1277
|
+
def write_html_to(io, options = {})
|
1278
|
+
write_format_to(SaveOptions::DEFAULT_HTML, io, options)
|
1279
|
+
end
|
1280
|
+
|
1281
|
+
###
|
1282
|
+
# Write Node as XHTML to +io+ with +options+
|
1283
|
+
#
|
1284
|
+
# See Node#write_to for a list of +options+
|
1285
|
+
def write_xhtml_to(io, options = {})
|
1286
|
+
write_format_to(SaveOptions::DEFAULT_XHTML, io, options)
|
1287
|
+
end
|
1288
|
+
|
1289
|
+
###
|
1290
|
+
# Write Node as XML to +io+ with +options+
|
1291
|
+
#
|
1292
|
+
# doc.write_xml_to io, :encoding => 'UTF-8'
|
1293
|
+
#
|
1294
|
+
# See Node#write_to for a list of options
|
1295
|
+
def write_xml_to(io, options = {})
|
1296
|
+
options[:save_with] ||= SaveOptions::DEFAULT_XML
|
1297
|
+
write_to(io, options)
|
1298
|
+
end
|
1299
|
+
|
1300
|
+
def canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false)
|
1301
|
+
c14n_root = self
|
1302
|
+
document.canonicalize(mode, inclusive_namespaces, with_comments) do |node, parent|
|
1303
|
+
tn = node.is_a?(XML::Node) ? node : parent
|
1304
|
+
tn == c14n_root || tn.ancestors.include?(c14n_root)
|
1305
|
+
end
|
1306
|
+
end
|
1307
|
+
|
1308
|
+
# :section:
|
1309
|
+
|
1310
|
+
protected
|
1311
|
+
|
1312
|
+
def coerce(data)
|
1313
|
+
case data
|
1314
|
+
when XML::NodeSet
|
1315
|
+
return data
|
1316
|
+
when XML::DocumentFragment
|
1317
|
+
return data.children
|
1318
|
+
when String
|
1319
|
+
return fragment(data).children
|
1320
|
+
when Document, XML::Attr
|
1321
|
+
# unacceptable
|
1322
|
+
when XML::Node
|
1323
|
+
return data
|
1324
|
+
end
|
1325
|
+
|
1326
|
+
raise ArgumentError, <<~EOERR
|
1327
|
+
Requires a Node, NodeSet or String argument, and cannot accept a #{data.class}.
|
1328
|
+
(You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
|
1329
|
+
EOERR
|
1330
|
+
end
|
1331
|
+
|
1332
|
+
private
|
1333
|
+
|
1334
|
+
def keywordify(keywords)
|
1335
|
+
case keywords
|
1336
|
+
when Enumerable
|
1337
|
+
keywords
|
1338
|
+
when String
|
1339
|
+
keywords.scan(/\S+/)
|
1340
|
+
else
|
1341
|
+
raise ArgumentError,
|
1342
|
+
"Keyword attributes must be passed as either a String or an Enumerable, but received #{keywords.class}"
|
1343
|
+
end
|
1344
|
+
end
|
1345
|
+
|
1346
|
+
def add_sibling(next_or_previous, node_or_tags)
|
1347
|
+
raise("Cannot add sibling to a node with no parent") unless parent
|
1348
|
+
|
1349
|
+
impl = next_or_previous == :next ? :add_next_sibling_node : :add_previous_sibling_node
|
1350
|
+
iter = next_or_previous == :next ? :reverse_each : :each
|
1351
|
+
|
1352
|
+
node_or_tags = parent.coerce(node_or_tags)
|
1353
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
1354
|
+
if text?
|
1355
|
+
pivot = Nokogiri::XML::Node.new("dummy", document)
|
1356
|
+
send(impl, pivot)
|
1357
|
+
else
|
1358
|
+
pivot = self
|
1359
|
+
end
|
1360
|
+
node_or_tags.send(iter) { |n| pivot.send(impl, n) }
|
1361
|
+
pivot.unlink if text?
|
1362
|
+
else
|
1363
|
+
send(impl, node_or_tags)
|
1364
|
+
end
|
1365
|
+
node_or_tags
|
1366
|
+
end
|
1367
|
+
|
1368
|
+
USING_LIBXML_WITH_BROKEN_SERIALIZATION = Nokogiri.uses_libxml?("~> 2.6.0").freeze
|
1369
|
+
private_constant :USING_LIBXML_WITH_BROKEN_SERIALIZATION
|
1370
|
+
|
1371
|
+
def to_format(save_option, options)
|
1372
|
+
return dump_html if USING_LIBXML_WITH_BROKEN_SERIALIZATION
|
1373
|
+
|
1374
|
+
options[:save_with] = save_option unless options[:save_with]
|
1375
|
+
serialize(options)
|
1376
|
+
end
|
1377
|
+
|
1378
|
+
def write_format_to(save_option, io, options)
|
1379
|
+
return (io << dump_html) if USING_LIBXML_WITH_BROKEN_SERIALIZATION
|
1380
|
+
|
1381
|
+
options[:save_with] ||= save_option
|
1382
|
+
write_to(io, options)
|
1383
|
+
end
|
1384
|
+
|
1385
|
+
def inspect_attributes
|
1386
|
+
[:name, :namespace, :attribute_nodes, :children]
|
1387
|
+
end
|
1388
|
+
|
1389
|
+
IMPLIED_XPATH_CONTEXTS = [".//"].freeze
|
1390
|
+
|
1391
|
+
def add_child_node_and_reparent_attrs(node)
|
1392
|
+
add_child_node(node)
|
1393
|
+
node.attribute_nodes.find_all { |a| a.name.include?(":") }.each do |attr_node|
|
1394
|
+
attr_node.remove
|
1395
|
+
node[attr_node.name] = attr_node.value
|
1396
|
+
end
|
1397
|
+
end
|
1398
|
+
end
|
1399
|
+
end
|
1400
|
+
end
|
1401
|
+
|
1402
|
+
require_relative "node/save_options"
|