nokogiri-backport 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE-DEPENDENCIES.md +1682 -0
- data/LICENSE.md +9 -0
- data/README.md +272 -0
- data/bin/nokogiri +118 -0
- data/dependencies.yml +74 -0
- data/ext/java/nokogiri/EncodingHandler.java +124 -0
- data/ext/java/nokogiri/HtmlDocument.java +178 -0
- data/ext/java/nokogiri/HtmlElementDescription.java +148 -0
- data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
- data/ext/java/nokogiri/HtmlSaxParserContext.java +282 -0
- data/ext/java/nokogiri/HtmlSaxPushParser.java +222 -0
- data/ext/java/nokogiri/NokogiriService.java +597 -0
- data/ext/java/nokogiri/XmlAttr.java +162 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +129 -0
- data/ext/java/nokogiri/XmlCdata.java +82 -0
- data/ext/java/nokogiri/XmlComment.java +97 -0
- data/ext/java/nokogiri/XmlDocument.java +633 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +185 -0
- data/ext/java/nokogiri/XmlDtd.java +481 -0
- data/ext/java/nokogiri/XmlElement.java +68 -0
- data/ext/java/nokogiri/XmlElementContent.java +382 -0
- data/ext/java/nokogiri/XmlElementDecl.java +147 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +157 -0
- data/ext/java/nokogiri/XmlEntityReference.java +101 -0
- data/ext/java/nokogiri/XmlNamespace.java +199 -0
- data/ext/java/nokogiri/XmlNode.java +1684 -0
- data/ext/java/nokogiri/XmlNodeSet.java +434 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +100 -0
- data/ext/java/nokogiri/XmlReader.java +531 -0
- data/ext/java/nokogiri/XmlRelaxng.java +151 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +374 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +286 -0
- data/ext/java/nokogiri/XmlSchema.java +388 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +138 -0
- data/ext/java/nokogiri/XmlText.java +110 -0
- data/ext/java/nokogiri/XmlXpathContext.java +301 -0
- data/ext/java/nokogiri/XsltStylesheet.java +347 -0
- data/ext/java/nokogiri/internals/ClosedStreamException.java +10 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +252 -0
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +20 -0
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +151 -0
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +116 -0
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +69 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +327 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +734 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +217 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +127 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +100 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +78 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +180 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +72 -0
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +60 -0
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +87 -0
- data/ext/java/nokogiri/internals/ParserContext.java +259 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +488 -0
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +778 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +73 -0
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +168 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +274 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +119 -0
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +159 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +37 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +93 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +252 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +639 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +38 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +38 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +367 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +295 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +40 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +44 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +44 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +43 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +630 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +173 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +76 -0
- data/ext/java/nokogiri/internals/c14n/Constants.java +42 -0
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +293 -0
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +93 -0
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +79 -0
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +166 -0
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +76 -0
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +402 -0
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +51 -0
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +179 -0
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +507 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1745 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +685 -0
- data/ext/nokogiri/depend +477 -0
- data/ext/nokogiri/extconf.rb +836 -0
- data/ext/nokogiri/html_document.c +171 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +279 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +116 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/html_sax_push_parser.c +87 -0
- data/ext/nokogiri/html_sax_push_parser.h +9 -0
- data/ext/nokogiri/nokogiri.c +135 -0
- data/ext/nokogiri/nokogiri.h +130 -0
- data/ext/nokogiri/xml_attr.c +103 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +62 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +69 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +622 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +63 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +111 -0
- data/ext/nokogiri/xml_namespace.h +14 -0
- data/ext/nokogiri/xml_node.c +1773 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +486 -0
- data/ext/nokogiri/xml_node_set.h +12 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +657 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +179 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +305 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +262 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +159 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +276 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +64 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +52 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +374 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +263 -0
- data/ext/nokogiri/xslt_stylesheet.h +14 -0
- data/lib/isorelax.jar +0 -0
- data/lib/jing.jar +0 -0
- data/lib/nekodtd.jar +0 -0
- data/lib/nekohtml.jar +0 -0
- data/lib/nokogiri/css/node.rb +53 -0
- data/lib/nokogiri/css/parser.rb +751 -0
- data/lib/nokogiri/css/parser.y +272 -0
- data/lib/nokogiri/css/parser_extras.rb +94 -0
- data/lib/nokogiri/css/syntax_error.rb +8 -0
- data/lib/nokogiri/css/tokenizer.rb +154 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +260 -0
- data/lib/nokogiri/css.rb +28 -0
- data/lib/nokogiri/decorators/slop.rb +43 -0
- data/lib/nokogiri/html/builder.rb +36 -0
- data/lib/nokogiri/html/document.rb +322 -0
- data/lib/nokogiri/html/document_fragment.rb +50 -0
- data/lib/nokogiri/html/element_description.rb +24 -0
- data/lib/nokogiri/html/element_description_defaults.rb +672 -0
- data/lib/nokogiri/html/entity_lookup.rb +14 -0
- data/lib/nokogiri/html/sax/parser.rb +63 -0
- data/lib/nokogiri/html/sax/parser_context.rb +17 -0
- data/lib/nokogiri/html/sax/push_parser.rb +37 -0
- data/lib/nokogiri/html.rb +38 -0
- data/lib/nokogiri/jruby/dependencies.rb +20 -0
- data/lib/nokogiri/syntax_error.rb +5 -0
- data/lib/nokogiri/version/constant.rb +5 -0
- data/lib/nokogiri/version/info.rb +182 -0
- data/lib/nokogiri/version.rb +3 -0
- data/lib/nokogiri/xml/attr.rb +15 -0
- data/lib/nokogiri/xml/attribute_decl.rb +19 -0
- data/lib/nokogiri/xml/builder.rb +447 -0
- data/lib/nokogiri/xml/cdata.rb +12 -0
- data/lib/nokogiri/xml/character_data.rb +8 -0
- data/lib/nokogiri/xml/document.rb +290 -0
- data/lib/nokogiri/xml/document_fragment.rb +159 -0
- data/lib/nokogiri/xml/dtd.rb +33 -0
- data/lib/nokogiri/xml/element_content.rb +37 -0
- data/lib/nokogiri/xml/element_decl.rb +14 -0
- data/lib/nokogiri/xml/entity_decl.rb +20 -0
- data/lib/nokogiri/xml/entity_reference.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +14 -0
- data/lib/nokogiri/xml/node/save_options.rb +62 -0
- data/lib/nokogiri/xml/node.rb +1240 -0
- data/lib/nokogiri/xml/node_set.rb +372 -0
- data/lib/nokogiri/xml/notation.rb +7 -0
- data/lib/nokogiri/xml/parse_options.rb +127 -0
- data/lib/nokogiri/xml/pp/character_data.rb +19 -0
- data/lib/nokogiri/xml/pp/node.rb +57 -0
- data/lib/nokogiri/xml/pp.rb +3 -0
- data/lib/nokogiri/xml/processing_instruction.rb +9 -0
- data/lib/nokogiri/xml/reader.rb +116 -0
- data/lib/nokogiri/xml/relax_ng.rb +37 -0
- data/lib/nokogiri/xml/sax/document.rb +172 -0
- data/lib/nokogiri/xml/sax/parser.rb +123 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +17 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
- data/lib/nokogiri/xml/sax.rb +5 -0
- data/lib/nokogiri/xml/schema.rb +72 -0
- data/lib/nokogiri/xml/searchable.rb +239 -0
- data/lib/nokogiri/xml/syntax_error.rb +71 -0
- data/lib/nokogiri/xml/text.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +12 -0
- data/lib/nokogiri/xml/xpath.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +17 -0
- data/lib/nokogiri/xml.rb +76 -0
- data/lib/nokogiri/xslt/stylesheet.rb +26 -0
- data/lib/nokogiri/xslt.rb +57 -0
- data/lib/nokogiri.rb +144 -0
- data/lib/serializer.jar +0 -0
- data/lib/xalan.jar +0 -0
- data/lib/xercesImpl.jar +0 -0
- data/lib/xml-apis.jar +0 -0
- data/lib/xsd/xmlparser/nokogiri.rb +103 -0
- metadata +531 -0
@@ -0,0 +1,372 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Nokogiri
|
3
|
+
module XML
|
4
|
+
####
|
5
|
+
# A NodeSet contains a list of Nokogiri::XML::Node objects. Typically
|
6
|
+
# a NodeSet is return as a result of searching a Document via
|
7
|
+
# Nokogiri::XML::Searchable#css or Nokogiri::XML::Searchable#xpath
|
8
|
+
class NodeSet
|
9
|
+
include Nokogiri::XML::Searchable
|
10
|
+
include Enumerable
|
11
|
+
|
12
|
+
# The Document this NodeSet is associated with
|
13
|
+
attr_accessor :document
|
14
|
+
|
15
|
+
alias :clone :dup
|
16
|
+
|
17
|
+
# Create a NodeSet with +document+ defaulting to +list+
|
18
|
+
def initialize document, list = []
|
19
|
+
@document = document
|
20
|
+
document.decorate(self)
|
21
|
+
list.each { |x| self << x }
|
22
|
+
yield self if block_given?
|
23
|
+
end
|
24
|
+
|
25
|
+
###
|
26
|
+
# Get the first element of the NodeSet.
|
27
|
+
def first n = nil
|
28
|
+
return self[0] unless n
|
29
|
+
list = []
|
30
|
+
[n, length].min.times { |i| list << self[i] }
|
31
|
+
list
|
32
|
+
end
|
33
|
+
|
34
|
+
###
|
35
|
+
# Get the last element of the NodeSet.
|
36
|
+
def last
|
37
|
+
self[-1]
|
38
|
+
end
|
39
|
+
|
40
|
+
###
|
41
|
+
# Is this NodeSet empty?
|
42
|
+
def empty?
|
43
|
+
length == 0
|
44
|
+
end
|
45
|
+
|
46
|
+
###
|
47
|
+
# Returns the index of the first node in self that is == to +node+ or meets the given block. Returns nil if no match is found.
|
48
|
+
def index(node = nil)
|
49
|
+
if node
|
50
|
+
warn "given block not used" if block_given?
|
51
|
+
each_with_index { |member, j| return j if member == node }
|
52
|
+
elsif block_given?
|
53
|
+
each_with_index { |member, j| return j if yield(member) }
|
54
|
+
end
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
###
|
59
|
+
# Insert +datum+ before the first Node in this NodeSet
|
60
|
+
def before datum
|
61
|
+
first.before datum
|
62
|
+
end
|
63
|
+
|
64
|
+
###
|
65
|
+
# Insert +datum+ after the last Node in this NodeSet
|
66
|
+
def after datum
|
67
|
+
last.after datum
|
68
|
+
end
|
69
|
+
|
70
|
+
alias :<< :push
|
71
|
+
alias :remove :unlink
|
72
|
+
|
73
|
+
###
|
74
|
+
# call-seq: css *rules, [namespace-bindings, custom-pseudo-class]
|
75
|
+
#
|
76
|
+
# Search this node set for CSS +rules+. +rules+ must be one or more CSS
|
77
|
+
# selectors. For example:
|
78
|
+
#
|
79
|
+
# For more information see Nokogiri::XML::Searchable#css
|
80
|
+
def css *args
|
81
|
+
rules, handler, ns, _ = extract_params(args)
|
82
|
+
paths = css_rules_to_xpath(rules, ns)
|
83
|
+
|
84
|
+
inject(NodeSet.new(document)) do |set, node|
|
85
|
+
set + xpath_internal(node, paths, handler, ns, nil)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
###
|
90
|
+
# call-seq: xpath *paths, [namespace-bindings, variable-bindings, custom-handler-class]
|
91
|
+
#
|
92
|
+
# Search this node set for XPath +paths+. +paths+ must be one or more XPath
|
93
|
+
# queries.
|
94
|
+
#
|
95
|
+
# For more information see Nokogiri::XML::Searchable#xpath
|
96
|
+
def xpath *args
|
97
|
+
paths, handler, ns, binds = extract_params(args)
|
98
|
+
|
99
|
+
inject(NodeSet.new(document)) do |set, node|
|
100
|
+
set + xpath_internal(node, paths, handler, ns, binds)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
###
|
105
|
+
# Search this NodeSet's nodes' immediate children using CSS selector +selector+
|
106
|
+
def > selector
|
107
|
+
ns = document.root.namespaces
|
108
|
+
xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first
|
109
|
+
end
|
110
|
+
|
111
|
+
###
|
112
|
+
# call-seq: search *paths, [namespace-bindings, xpath-variable-bindings, custom-handler-class]
|
113
|
+
#
|
114
|
+
# Search this object for +paths+, and return only the first
|
115
|
+
# result. +paths+ must be one or more XPath or CSS queries.
|
116
|
+
#
|
117
|
+
# See Searchable#search for more information.
|
118
|
+
#
|
119
|
+
# Or, if passed an integer, index into the NodeSet:
|
120
|
+
#
|
121
|
+
# node_set.at(3) # same as node_set[3]
|
122
|
+
#
|
123
|
+
def at *args
|
124
|
+
if args.length == 1 && args.first.is_a?(Numeric)
|
125
|
+
return self[args.first]
|
126
|
+
end
|
127
|
+
|
128
|
+
super(*args)
|
129
|
+
end
|
130
|
+
alias :% :at
|
131
|
+
|
132
|
+
###
|
133
|
+
# Filter this list for nodes that match +expr+
|
134
|
+
def filter expr
|
135
|
+
find_all { |node| node.matches?(expr) }
|
136
|
+
end
|
137
|
+
|
138
|
+
###
|
139
|
+
# Add the class attribute +name+ to all Node objects in the
|
140
|
+
# NodeSet.
|
141
|
+
#
|
142
|
+
# See Nokogiri::XML::Node#add_class for more information.
|
143
|
+
def add_class name
|
144
|
+
each do |el|
|
145
|
+
el.add_class(name)
|
146
|
+
end
|
147
|
+
self
|
148
|
+
end
|
149
|
+
|
150
|
+
###
|
151
|
+
# Append the class attribute +name+ to all Node objects in the
|
152
|
+
# NodeSet.
|
153
|
+
#
|
154
|
+
# See Nokogiri::XML::Node#append_class for more information.
|
155
|
+
def append_class name
|
156
|
+
each do |el|
|
157
|
+
el.append_class(name)
|
158
|
+
end
|
159
|
+
self
|
160
|
+
end
|
161
|
+
|
162
|
+
###
|
163
|
+
# Remove the class attribute +name+ from all Node objects in the
|
164
|
+
# NodeSet.
|
165
|
+
#
|
166
|
+
# See Nokogiri::XML::Node#remove_class for more information.
|
167
|
+
def remove_class name = nil
|
168
|
+
each do |el|
|
169
|
+
el.remove_class(name)
|
170
|
+
end
|
171
|
+
self
|
172
|
+
end
|
173
|
+
|
174
|
+
###
|
175
|
+
# Set attributes on each Node in the NodeSet, or get an
|
176
|
+
# attribute from the first Node in the NodeSet.
|
177
|
+
#
|
178
|
+
# To get an attribute from the first Node in a NodeSet:
|
179
|
+
#
|
180
|
+
# node_set.attr("href") # => "https://www.nokogiri.org"
|
181
|
+
#
|
182
|
+
# Note that an empty NodeSet will return nil when +#attr+ is called as a getter.
|
183
|
+
#
|
184
|
+
# To set an attribute on each node, +key+ can either be an
|
185
|
+
# attribute name, or a Hash of attribute names and values. When
|
186
|
+
# called as a setter, +#attr+ returns the NodeSet.
|
187
|
+
#
|
188
|
+
# If +key+ is an attribute name, then either +value+ or +block+
|
189
|
+
# must be passed.
|
190
|
+
#
|
191
|
+
# If +key+ is a Hash then attributes will be set for each
|
192
|
+
# key/value pair:
|
193
|
+
#
|
194
|
+
# node_set.attr("href" => "https://www.nokogiri.org", "class" => "member")
|
195
|
+
#
|
196
|
+
# If +value+ is passed, it will be used as the attribute value
|
197
|
+
# for all nodes:
|
198
|
+
#
|
199
|
+
# node_set.attr("href", "https://www.nokogiri.org")
|
200
|
+
#
|
201
|
+
# If +block+ is passed, it will be called on each Node object in
|
202
|
+
# the NodeSet and the return value used as the attribute value
|
203
|
+
# for that node:
|
204
|
+
#
|
205
|
+
# node_set.attr("class") { |node| node.name }
|
206
|
+
#
|
207
|
+
def attr key, value = nil, &block
|
208
|
+
unless key.is_a?(Hash) || (key && (value || block))
|
209
|
+
return first ? first.attribute(key) : nil
|
210
|
+
end
|
211
|
+
|
212
|
+
hash = key.is_a?(Hash) ? key : { key => value }
|
213
|
+
|
214
|
+
hash.each do |k,v|
|
215
|
+
each do |node|
|
216
|
+
node[k] = v || block.call(node)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
self
|
221
|
+
end
|
222
|
+
alias :set :attr
|
223
|
+
alias :attribute :attr
|
224
|
+
|
225
|
+
###
|
226
|
+
# Remove the attributed named +name+ from all Node objects in the NodeSet
|
227
|
+
def remove_attr name
|
228
|
+
each { |el| el.delete name }
|
229
|
+
self
|
230
|
+
end
|
231
|
+
alias remove_attribute remove_attr
|
232
|
+
|
233
|
+
###
|
234
|
+
# Iterate over each node, yielding to +block+
|
235
|
+
def each
|
236
|
+
return to_enum unless block_given?
|
237
|
+
|
238
|
+
0.upto(length - 1) do |x|
|
239
|
+
yield self[x]
|
240
|
+
end
|
241
|
+
self
|
242
|
+
end
|
243
|
+
|
244
|
+
###
|
245
|
+
# Get the inner text of all contained Node objects
|
246
|
+
#
|
247
|
+
# Note: This joins the text of all Node objects in the NodeSet:
|
248
|
+
#
|
249
|
+
# doc = Nokogiri::XML('<xml><a><d>foo</d><d>bar</d></a></xml>')
|
250
|
+
# doc.css('d').text # => "foobar"
|
251
|
+
#
|
252
|
+
# Instead, if you want to return the text of all nodes in the NodeSet:
|
253
|
+
#
|
254
|
+
# doc.css('d').map(&:text) # => ["foo", "bar"]
|
255
|
+
#
|
256
|
+
# See Nokogiri::XML::Node#content for more information.
|
257
|
+
def inner_text
|
258
|
+
collect(&:inner_text).join('')
|
259
|
+
end
|
260
|
+
alias :text :inner_text
|
261
|
+
|
262
|
+
###
|
263
|
+
# Get the inner html of all contained Node objects
|
264
|
+
def inner_html *args
|
265
|
+
collect{|j| j.inner_html(*args) }.join('')
|
266
|
+
end
|
267
|
+
|
268
|
+
###
|
269
|
+
# Wrap this NodeSet with +html+
|
270
|
+
def wrap html
|
271
|
+
map { |node| node.wrap html }
|
272
|
+
end
|
273
|
+
|
274
|
+
###
|
275
|
+
# Convert this NodeSet to a string.
|
276
|
+
def to_s
|
277
|
+
map(&:to_s).join
|
278
|
+
end
|
279
|
+
|
280
|
+
###
|
281
|
+
# Convert this NodeSet to HTML
|
282
|
+
def to_html *args
|
283
|
+
if Nokogiri.jruby?
|
284
|
+
options = args.first.is_a?(Hash) ? args.shift : {}
|
285
|
+
if !options[:save_with]
|
286
|
+
options[:save_with] = Node::SaveOptions::NO_DECLARATION | Node::SaveOptions::NO_EMPTY_TAGS | Node::SaveOptions::AS_HTML
|
287
|
+
end
|
288
|
+
args.insert(0, options)
|
289
|
+
end
|
290
|
+
map { |x| x.to_html(*args) }.join
|
291
|
+
end
|
292
|
+
|
293
|
+
###
|
294
|
+
# Convert this NodeSet to XHTML
|
295
|
+
def to_xhtml *args
|
296
|
+
map { |x| x.to_xhtml(*args) }.join
|
297
|
+
end
|
298
|
+
|
299
|
+
###
|
300
|
+
# Convert this NodeSet to XML
|
301
|
+
def to_xml *args
|
302
|
+
map { |x| x.to_xml(*args) }.join
|
303
|
+
end
|
304
|
+
|
305
|
+
alias :size :length
|
306
|
+
alias :to_ary :to_a
|
307
|
+
|
308
|
+
###
|
309
|
+
# Removes the last element from set and returns it, or +nil+ if
|
310
|
+
# the set is empty
|
311
|
+
def pop
|
312
|
+
return nil if length == 0
|
313
|
+
delete last
|
314
|
+
end
|
315
|
+
|
316
|
+
###
|
317
|
+
# Returns the first element of the NodeSet and removes it. Returns
|
318
|
+
# +nil+ if the set is empty.
|
319
|
+
def shift
|
320
|
+
return nil if length == 0
|
321
|
+
delete first
|
322
|
+
end
|
323
|
+
|
324
|
+
###
|
325
|
+
# Equality -- Two NodeSets are equal if the contain the same number
|
326
|
+
# of elements and if each element is equal to the corresponding
|
327
|
+
# element in the other NodeSet
|
328
|
+
def == other
|
329
|
+
return false unless other.is_a?(Nokogiri::XML::NodeSet)
|
330
|
+
return false unless length == other.length
|
331
|
+
each_with_index do |node, i|
|
332
|
+
return false unless node == other[i]
|
333
|
+
end
|
334
|
+
true
|
335
|
+
end
|
336
|
+
|
337
|
+
###
|
338
|
+
# Returns a new NodeSet containing all the children of all the nodes in
|
339
|
+
# the NodeSet
|
340
|
+
def children
|
341
|
+
node_set = NodeSet.new(document)
|
342
|
+
each do |node|
|
343
|
+
node.children.each { |n| node_set.push(n) }
|
344
|
+
end
|
345
|
+
node_set
|
346
|
+
end
|
347
|
+
|
348
|
+
###
|
349
|
+
# Returns a new NodeSet containing all the nodes in the NodeSet
|
350
|
+
# in reverse order
|
351
|
+
def reverse
|
352
|
+
node_set = NodeSet.new(document)
|
353
|
+
(length - 1).downto(0) do |x|
|
354
|
+
node_set.push self[x]
|
355
|
+
end
|
356
|
+
node_set
|
357
|
+
end
|
358
|
+
|
359
|
+
###
|
360
|
+
# Return a nicely formated string representation
|
361
|
+
def inspect
|
362
|
+
"[#{map(&:inspect).join ', '}]"
|
363
|
+
end
|
364
|
+
|
365
|
+
alias :+ :|
|
366
|
+
|
367
|
+
# @private
|
368
|
+
IMPLIED_XPATH_CONTEXTS = [ './/'.freeze, 'self::'.freeze ].freeze # :nodoc:
|
369
|
+
|
370
|
+
end
|
371
|
+
end
|
372
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Nokogiri
|
3
|
+
module XML
|
4
|
+
###
|
5
|
+
# Parse options for passing to Nokogiri.XML or Nokogiri.HTML
|
6
|
+
#
|
7
|
+
# == Building combinations of parse options
|
8
|
+
# You can build your own combinations of these parse options by using any of the following methods:
|
9
|
+
# *Note*: All examples attempt to set the +RECOVER+ & +NOENT+ options.
|
10
|
+
# [Ruby's bitwise operators] You can use the Ruby bitwise operators to set various combinations.
|
11
|
+
# Nokogiri.XML('<content>Chapter 1</content', nil, nil, Nokogiri::XML::ParseOptions.new((1 << 0) | (1 << 1)))
|
12
|
+
# [Method chaining] Every option has an equivalent method in lowercase. You can chain these methods together to set various combinations.
|
13
|
+
# Nokogiri.XML('<content>Chapter 1</content', nil, nil, Nokogiri::XML::ParseOptions.new.recover.noent)
|
14
|
+
# [Using Ruby Blocks] You can also setup parse combinations in the block passed to Nokogiri.XML or Nokogiri.HTML
|
15
|
+
# Nokogiri.XML('<content>Chapter 1</content') {|config| config.recover.noent}
|
16
|
+
#
|
17
|
+
# == Removing particular parse options
|
18
|
+
# You can also remove options from an instance of +ParseOptions+ dynamically.
|
19
|
+
# Every option has an equivalent <code>no{option}</code> method in lowercase. You can call these methods on an instance of +ParseOptions+ to remove the option.
|
20
|
+
# Note that this is not available for +STRICT+.
|
21
|
+
#
|
22
|
+
# # Setting the RECOVER & NOENT options...
|
23
|
+
# options = Nokogiri::XML::ParseOptions.new.recover.noent
|
24
|
+
# # later...
|
25
|
+
# options.norecover # Removes the Nokogiri::XML::ParseOptions::RECOVER option
|
26
|
+
# options.nonoent # Removes the Nokogiri::XML::ParseOptions::NOENT option
|
27
|
+
#
|
28
|
+
class ParseOptions
|
29
|
+
# Strict parsing
|
30
|
+
STRICT = 0
|
31
|
+
# Recover from errors
|
32
|
+
RECOVER = 1 << 0
|
33
|
+
# Substitute entities
|
34
|
+
NOENT = 1 << 1
|
35
|
+
# Load external subsets
|
36
|
+
DTDLOAD = 1 << 2
|
37
|
+
# Default DTD attributes
|
38
|
+
DTDATTR = 1 << 3
|
39
|
+
# validate with the DTD
|
40
|
+
DTDVALID = 1 << 4
|
41
|
+
# suppress error reports
|
42
|
+
NOERROR = 1 << 5
|
43
|
+
# suppress warning reports
|
44
|
+
NOWARNING = 1 << 6
|
45
|
+
# pedantic error reporting
|
46
|
+
PEDANTIC = 1 << 7
|
47
|
+
# remove blank nodes
|
48
|
+
NOBLANKS = 1 << 8
|
49
|
+
# use the SAX1 interface internally
|
50
|
+
SAX1 = 1 << 9
|
51
|
+
# Implement XInclude substitution
|
52
|
+
XINCLUDE = 1 << 10
|
53
|
+
# Forbid network access. Recommended for dealing with untrusted documents.
|
54
|
+
NONET = 1 << 11
|
55
|
+
# Do not reuse the context dictionary
|
56
|
+
NODICT = 1 << 12
|
57
|
+
# remove redundant namespaces declarations
|
58
|
+
NSCLEAN = 1 << 13
|
59
|
+
# merge CDATA as text nodes
|
60
|
+
NOCDATA = 1 << 14
|
61
|
+
# do not generate XINCLUDE START/END nodes
|
62
|
+
NOXINCNODE = 1 << 15
|
63
|
+
# compact small text nodes; no modification of the tree allowed afterwards (will possibly crash if you try to modify the tree)
|
64
|
+
COMPACT = 1 << 16
|
65
|
+
# parse using XML-1.0 before update 5
|
66
|
+
OLD10 = 1 << 17
|
67
|
+
# do not fixup XINCLUDE xml:base uris
|
68
|
+
NOBASEFIX = 1 << 18
|
69
|
+
# relax any hardcoded limit from the parser
|
70
|
+
HUGE = 1 << 19
|
71
|
+
|
72
|
+
# the default options used for parsing XML documents
|
73
|
+
DEFAULT_XML = RECOVER | NONET
|
74
|
+
# the default options used for parsing HTML documents
|
75
|
+
DEFAULT_HTML = RECOVER | NOERROR | NOWARNING | NONET
|
76
|
+
# the default options used for parsing XML schemas
|
77
|
+
DEFAULT_SCHEMA = NONET
|
78
|
+
|
79
|
+
attr_accessor :options
|
80
|
+
def initialize options = STRICT
|
81
|
+
@options = options
|
82
|
+
end
|
83
|
+
|
84
|
+
constants.each do |constant|
|
85
|
+
next if constant.to_sym == :STRICT
|
86
|
+
class_eval %{
|
87
|
+
def #{constant.downcase}
|
88
|
+
@options |= #{constant}
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
def no#{constant.downcase}
|
93
|
+
@options &= ~#{constant}
|
94
|
+
self
|
95
|
+
end
|
96
|
+
|
97
|
+
def #{constant.downcase}?
|
98
|
+
#{constant} & @options == #{constant}
|
99
|
+
end
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
def strict
|
104
|
+
@options &= ~RECOVER
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
def strict?
|
109
|
+
@options & RECOVER == STRICT
|
110
|
+
end
|
111
|
+
|
112
|
+
def ==(other)
|
113
|
+
other.to_i == to_i
|
114
|
+
end
|
115
|
+
|
116
|
+
alias :to_i :options
|
117
|
+
|
118
|
+
def inspect
|
119
|
+
options = []
|
120
|
+
self.class.constants.each do |k|
|
121
|
+
options << k.downcase if send(:"#{k.downcase}?")
|
122
|
+
end
|
123
|
+
super.sub(/>$/, " " + options.join(', ') + ">")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Nokogiri
|
3
|
+
module XML
|
4
|
+
module PP
|
5
|
+
module CharacterData
|
6
|
+
def pretty_print pp # :nodoc:
|
7
|
+
nice_name = self.class.name.split('::').last
|
8
|
+
pp.group(2, "#(#{nice_name} ", ')') do
|
9
|
+
pp.pp text
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def inspect # :nodoc:
|
14
|
+
"#<#{self.class.name}:#{sprintf("0x%x",object_id)} #{text.inspect}>"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Nokogiri
|
3
|
+
module XML
|
4
|
+
module PP
|
5
|
+
module Node
|
6
|
+
def inspect # :nodoc:
|
7
|
+
attributes = inspect_attributes.reject { |x|
|
8
|
+
begin
|
9
|
+
attribute = send x
|
10
|
+
!attribute || (attribute.respond_to?(:empty?) && attribute.empty?)
|
11
|
+
rescue NoMethodError
|
12
|
+
true
|
13
|
+
end
|
14
|
+
}.map { |attribute|
|
15
|
+
"#{attribute.to_s.sub(/_\w+/, 's')}=#{send(attribute).inspect}"
|
16
|
+
}.join ' '
|
17
|
+
"#<#{self.class.name}:#{sprintf("0x%x", object_id)} #{attributes}>"
|
18
|
+
end
|
19
|
+
|
20
|
+
def pretty_print pp # :nodoc:
|
21
|
+
nice_name = self.class.name.split('::').last
|
22
|
+
pp.group(2, "#(#{nice_name}:#{sprintf("0x%x", object_id)} {", '})') do
|
23
|
+
|
24
|
+
pp.breakable
|
25
|
+
attrs = inspect_attributes.map { |t|
|
26
|
+
[t, send(t)] if respond_to?(t)
|
27
|
+
}.compact.find_all { |x|
|
28
|
+
if x.last
|
29
|
+
if [:attribute_nodes, :children].include? x.first
|
30
|
+
!x.last.empty?
|
31
|
+
else
|
32
|
+
true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
}
|
36
|
+
|
37
|
+
pp.seplist(attrs) do |v|
|
38
|
+
if [:attribute_nodes, :children].include? v.first
|
39
|
+
pp.group(2, "#{v.first.to_s.sub(/_\w+$/, 's')} = [", "]") do
|
40
|
+
pp.breakable
|
41
|
+
pp.seplist(v.last) do |item|
|
42
|
+
pp.pp item
|
43
|
+
end
|
44
|
+
end
|
45
|
+
else
|
46
|
+
pp.text "#{v.first} = "
|
47
|
+
pp.pp v.last
|
48
|
+
end
|
49
|
+
end
|
50
|
+
pp.breakable
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|