nokogiri 1.14.0-arm-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 +38 -0
- data/LICENSE-DEPENDENCIES.md +2224 -0
- data/LICENSE.md +9 -0
- data/README.md +287 -0
- data/bin/nokogiri +131 -0
- data/dependencies.yml +41 -0
- data/ext/nokogiri/depend +38 -0
- data/ext/nokogiri/extconf.rb +1082 -0
- data/ext/nokogiri/gumbo.c +594 -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 +114 -0
- data/ext/nokogiri/html4_sax_push_parser.c +95 -0
- data/ext/nokogiri/include/libexslt/exslt.h +108 -0
- data/ext/nokogiri/include/libexslt/exsltconfig.h +70 -0
- data/ext/nokogiri/include/libexslt/exsltexports.h +63 -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 +204 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX2.h +172 -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 +81 -0
- data/ext/nokogiri/include/libxml2/libxml/encoding.h +232 -0
- data/ext/nokogiri/include/libxml2/libxml/entities.h +153 -0
- data/ext/nokogiri/include/libxml2/libxml/globals.h +499 -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 +186 -0
- data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +81 -0
- data/ext/nokogiri/include/libxml2/libxml/parser.h +1244 -0
- data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +656 -0
- data/ext/nokogiri/include/libxml2/libxml/pattern.h +100 -0
- data/ext/nokogiri/include/libxml2/libxml/relaxng.h +218 -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 +91 -0
- data/ext/nokogiri/include/libxml2/libxml/tree.h +1312 -0
- data/ext/nokogiri/include/libxml2/libxml/uri.h +94 -0
- data/ext/nokogiri/include/libxml2/libxml/valid.h +463 -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 +947 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +77 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +226 -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 +152 -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 +503 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +488 -0
- data/ext/nokogiri/include/libxml2/libxml/xpath.h +575 -0
- data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +632 -0
- data/ext/nokogiri/include/libxml2/libxml/xpointer.h +137 -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 +1982 -0
- data/ext/nokogiri/include/libxslt/xsltconfig.h +179 -0
- data/ext/nokogiri/include/libxslt/xsltexports.h +64 -0
- data/ext/nokogiri/include/libxslt/xsltlocale.h +76 -0
- data/ext/nokogiri/include/libxslt/xsltutils.h +310 -0
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +260 -0
- data/ext/nokogiri/nokogiri.h +235 -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 +689 -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 +186 -0
- data/ext/nokogiri/xml_node.c +2425 -0
- data/ext/nokogiri/xml_node_set.c +496 -0
- data/ext/nokogiri/xml_processing_instruction.c +54 -0
- data/ext/nokogiri/xml_reader.c +794 -0
- data/ext/nokogiri/xml_relax_ng.c +183 -0
- data/ext/nokogiri/xml_sax_parser.c +316 -0
- data/ext/nokogiri/xml_sax_parser_context.c +283 -0
- data/ext/nokogiri/xml_sax_push_parser.c +166 -0
- data/ext/nokogiri/xml_schema.c +282 -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 +413 -0
- data/ext/nokogiri/xslt_stylesheet.c +363 -0
- data/gumbo-parser/CHANGES.md +63 -0
- data/gumbo-parser/Makefile +111 -0
- data/gumbo-parser/THANKS +27 -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/3.2/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 +770 -0
- data/lib/nokogiri/css/parser.y +277 -0
- data/lib/nokogiri/css/parser_extras.rb +96 -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 +66 -0
- data/lib/nokogiri/decorators/slop.rb +44 -0
- data/lib/nokogiri/encoding_handler.rb +57 -0
- data/lib/nokogiri/extension.rb +32 -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 +214 -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 +572 -0
- data/lib/nokogiri/html4/encoding_reader.rb +121 -0
- data/lib/nokogiri/html4/entity_lookup.rb +15 -0
- data/lib/nokogiri/html4/sax/parser.rb +63 -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 +47 -0
- data/lib/nokogiri/html5/document.rb +168 -0
- data/lib/nokogiri/html5/document_fragment.rb +90 -0
- data/lib/nokogiri/html5/node.rb +98 -0
- data/lib/nokogiri/html5.rb +389 -0
- data/lib/nokogiri/jruby/dependencies.rb +3 -0
- data/lib/nokogiri/jruby/nokogiri_jars.rb +43 -0
- data/lib/nokogiri/syntax_error.rb +6 -0
- data/lib/nokogiri/version/constant.rb +6 -0
- data/lib/nokogiri/version/info.rb +223 -0
- data/lib/nokogiri/version.rb +4 -0
- data/lib/nokogiri/xml/attr.rb +66 -0
- data/lib/nokogiri/xml/attribute_decl.rb +20 -0
- data/lib/nokogiri/xml/builder.rb +487 -0
- data/lib/nokogiri/xml/cdata.rb +13 -0
- data/lib/nokogiri/xml/character_data.rb +9 -0
- data/lib/nokogiri/xml/document.rb +471 -0
- data/lib/nokogiri/xml/document_fragment.rb +205 -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 +58 -0
- data/lib/nokogiri/xml/node/save_options.rb +68 -0
- data/lib/nokogiri/xml/node.rb +1563 -0
- data/lib/nokogiri/xml/node_set.rb +446 -0
- data/lib/nokogiri/xml/notation.rb +19 -0
- data/lib/nokogiri/xml/parse_options.rb +213 -0
- data/lib/nokogiri/xml/pp/character_data.rb +21 -0
- data/lib/nokogiri/xml/pp/node.rb +57 -0
- data/lib/nokogiri/xml/pp.rb +4 -0
- data/lib/nokogiri/xml/processing_instruction.rb +11 -0
- data/lib/nokogiri/xml/reader.rb +105 -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 +270 -0
- data/lib/nokogiri/xml/syntax_error.rb +72 -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 +76 -0
- data/lib/nokogiri/xslt/stylesheet.rb +27 -0
- data/lib/nokogiri/xslt.rb +65 -0
- data/lib/nokogiri.rb +120 -0
- data/lib/xsd/xmlparser/nokogiri.rb +106 -0
- metadata +317 -0
@@ -0,0 +1,66 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Nokogiri
|
5
|
+
module XML
|
6
|
+
class Attr < Node
|
7
|
+
alias_method :value, :content
|
8
|
+
alias_method :to_s, :content
|
9
|
+
alias_method :content=, :value=
|
10
|
+
|
11
|
+
#
|
12
|
+
# :call-seq: deconstruct_keys(array_of_names) → Hash
|
13
|
+
#
|
14
|
+
# Returns a hash describing the Attr, to use in pattern matching.
|
15
|
+
#
|
16
|
+
# Valid keys and their values:
|
17
|
+
# - +name+ → (String) The name of the attribute.
|
18
|
+
# - +value+ → (String) The value of the attribute.
|
19
|
+
# - +namespace+ → (Namespace, nil) The Namespace of the attribute, or +nil+ if there is no namespace.
|
20
|
+
#
|
21
|
+
# ⚡ This is an experimental feature, available since v1.14.0
|
22
|
+
#
|
23
|
+
# *Example*
|
24
|
+
#
|
25
|
+
# doc = Nokogiri::XML.parse(<<~XML)
|
26
|
+
# <?xml version="1.0"?>
|
27
|
+
# <root xmlns="http://nokogiri.org/ns/default" xmlns:noko="http://nokogiri.org/ns/noko">
|
28
|
+
# <child1 foo="abc" noko:bar="def"/>
|
29
|
+
# </root>
|
30
|
+
# XML
|
31
|
+
#
|
32
|
+
# attributes = doc.root.elements.first.attribute_nodes
|
33
|
+
# # => [#(Attr:0x35c { name = "foo", value = "abc" }),
|
34
|
+
# # #(Attr:0x370 {
|
35
|
+
# # name = "bar",
|
36
|
+
# # namespace = #(Namespace:0x384 {
|
37
|
+
# # prefix = "noko",
|
38
|
+
# # href = "http://nokogiri.org/ns/noko"
|
39
|
+
# # }),
|
40
|
+
# # value = "def"
|
41
|
+
# # })]
|
42
|
+
#
|
43
|
+
# attributes.first.deconstruct_keys([:name, :value, :namespace])
|
44
|
+
# # => {:name=>"foo", :value=>"abc", :namespace=>nil}
|
45
|
+
#
|
46
|
+
# attributes.last.deconstruct_keys([:name, :value, :namespace])
|
47
|
+
# # => {:name=>"bar",
|
48
|
+
# # :value=>"def",
|
49
|
+
# # :namespace=>
|
50
|
+
# # #(Namespace:0x384 {
|
51
|
+
# # prefix = "noko",
|
52
|
+
# # href = "http://nokogiri.org/ns/noko"
|
53
|
+
# # })}
|
54
|
+
#
|
55
|
+
def deconstruct_keys(keys)
|
56
|
+
{ name: name, value: value, namespace: namespace }
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def inspect_attributes
|
62
|
+
[:name, :namespace, :value]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
###
|
6
|
+
# Represents an attribute declaration in a DTD
|
7
|
+
class AttributeDecl < Nokogiri::XML::Node
|
8
|
+
undef_method :attribute_nodes
|
9
|
+
undef_method :attributes
|
10
|
+
undef_method :content
|
11
|
+
undef_method :namespace
|
12
|
+
undef_method :namespace_definitions
|
13
|
+
undef_method :line if method_defined?(:line)
|
14
|
+
|
15
|
+
def inspect
|
16
|
+
"#<#{self.class.name}:#{format("0x%x", object_id)} #{to_s.inspect}>"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,487 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
###
|
6
|
+
# Nokogiri builder can be used for building XML and HTML documents.
|
7
|
+
#
|
8
|
+
# == Synopsis:
|
9
|
+
#
|
10
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
11
|
+
# xml.root {
|
12
|
+
# xml.products {
|
13
|
+
# xml.widget {
|
14
|
+
# xml.id_ "10"
|
15
|
+
# xml.name "Awesome widget"
|
16
|
+
# }
|
17
|
+
# }
|
18
|
+
# }
|
19
|
+
# end
|
20
|
+
# puts builder.to_xml
|
21
|
+
#
|
22
|
+
# Will output:
|
23
|
+
#
|
24
|
+
# <?xml version="1.0"?>
|
25
|
+
# <root>
|
26
|
+
# <products>
|
27
|
+
# <widget>
|
28
|
+
# <id>10</id>
|
29
|
+
# <name>Awesome widget</name>
|
30
|
+
# </widget>
|
31
|
+
# </products>
|
32
|
+
# </root>
|
33
|
+
#
|
34
|
+
#
|
35
|
+
# === Builder scope
|
36
|
+
#
|
37
|
+
# The builder allows two forms. When the builder is supplied with a block
|
38
|
+
# that has a parameter, the outside scope is maintained. This means you
|
39
|
+
# can access variables that are outside your builder. If you don't need
|
40
|
+
# outside scope, you can use the builder without the "xml" prefix like
|
41
|
+
# this:
|
42
|
+
#
|
43
|
+
# builder = Nokogiri::XML::Builder.new do
|
44
|
+
# root {
|
45
|
+
# products {
|
46
|
+
# widget {
|
47
|
+
# id_ "10"
|
48
|
+
# name "Awesome widget"
|
49
|
+
# }
|
50
|
+
# }
|
51
|
+
# }
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# == Special Tags
|
55
|
+
#
|
56
|
+
# The builder works by taking advantage of method_missing. Unfortunately
|
57
|
+
# some methods are defined in ruby that are difficult or dangerous to
|
58
|
+
# remove. You may want to create tags with the name "type", "class", and
|
59
|
+
# "id" for example. In that case, you can use an underscore to
|
60
|
+
# disambiguate your tag name from the method call.
|
61
|
+
#
|
62
|
+
# Here is an example of using the underscore to disambiguate tag names from
|
63
|
+
# ruby methods:
|
64
|
+
#
|
65
|
+
# @objects = [Object.new, Object.new, Object.new]
|
66
|
+
#
|
67
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
68
|
+
# xml.root {
|
69
|
+
# xml.objects {
|
70
|
+
# @objects.each do |o|
|
71
|
+
# xml.object {
|
72
|
+
# xml.type_ o.type
|
73
|
+
# xml.class_ o.class.name
|
74
|
+
# xml.id_ o.id
|
75
|
+
# }
|
76
|
+
# end
|
77
|
+
# }
|
78
|
+
# }
|
79
|
+
# end
|
80
|
+
# puts builder.to_xml
|
81
|
+
#
|
82
|
+
# The underscore may be used with any tag name, and the last underscore
|
83
|
+
# will just be removed. This code will output the following XML:
|
84
|
+
#
|
85
|
+
# <?xml version="1.0"?>
|
86
|
+
# <root>
|
87
|
+
# <objects>
|
88
|
+
# <object>
|
89
|
+
# <type>Object</type>
|
90
|
+
# <class>Object</class>
|
91
|
+
# <id>48390</id>
|
92
|
+
# </object>
|
93
|
+
# <object>
|
94
|
+
# <type>Object</type>
|
95
|
+
# <class>Object</class>
|
96
|
+
# <id>48380</id>
|
97
|
+
# </object>
|
98
|
+
# <object>
|
99
|
+
# <type>Object</type>
|
100
|
+
# <class>Object</class>
|
101
|
+
# <id>48370</id>
|
102
|
+
# </object>
|
103
|
+
# </objects>
|
104
|
+
# </root>
|
105
|
+
#
|
106
|
+
# == Tag Attributes
|
107
|
+
#
|
108
|
+
# Tag attributes may be supplied as method arguments. Here is our
|
109
|
+
# previous example, but using attributes rather than tags:
|
110
|
+
#
|
111
|
+
# @objects = [Object.new, Object.new, Object.new]
|
112
|
+
#
|
113
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
114
|
+
# xml.root {
|
115
|
+
# xml.objects {
|
116
|
+
# @objects.each do |o|
|
117
|
+
# xml.object(:type => o.type, :class => o.class, :id => o.id)
|
118
|
+
# end
|
119
|
+
# }
|
120
|
+
# }
|
121
|
+
# end
|
122
|
+
# puts builder.to_xml
|
123
|
+
#
|
124
|
+
# === Tag Attribute Short Cuts
|
125
|
+
#
|
126
|
+
# A couple attribute short cuts are available when building tags. The
|
127
|
+
# short cuts are available by special method calls when building a tag.
|
128
|
+
#
|
129
|
+
# This example builds an "object" tag with the class attribute "classy"
|
130
|
+
# and the id of "thing":
|
131
|
+
#
|
132
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
133
|
+
# xml.root {
|
134
|
+
# xml.objects {
|
135
|
+
# xml.object.classy.thing!
|
136
|
+
# }
|
137
|
+
# }
|
138
|
+
# end
|
139
|
+
# puts builder.to_xml
|
140
|
+
#
|
141
|
+
# Which will output:
|
142
|
+
#
|
143
|
+
# <?xml version="1.0"?>
|
144
|
+
# <root>
|
145
|
+
# <objects>
|
146
|
+
# <object class="classy" id="thing"/>
|
147
|
+
# </objects>
|
148
|
+
# </root>
|
149
|
+
#
|
150
|
+
# All other options are still supported with this syntax, including
|
151
|
+
# blocks and extra tag attributes.
|
152
|
+
#
|
153
|
+
# == Namespaces
|
154
|
+
#
|
155
|
+
# Namespaces are added similarly to attributes. Nokogiri::XML::Builder
|
156
|
+
# assumes that when an attribute starts with "xmlns", it is meant to be
|
157
|
+
# a namespace:
|
158
|
+
#
|
159
|
+
# builder = Nokogiri::XML::Builder.new { |xml|
|
160
|
+
# xml.root('xmlns' => 'default', 'xmlns:foo' => 'bar') do
|
161
|
+
# xml.tenderlove
|
162
|
+
# end
|
163
|
+
# }
|
164
|
+
# puts builder.to_xml
|
165
|
+
#
|
166
|
+
# Will output XML like this:
|
167
|
+
#
|
168
|
+
# <?xml version="1.0"?>
|
169
|
+
# <root xmlns:foo="bar" xmlns="default">
|
170
|
+
# <tenderlove/>
|
171
|
+
# </root>
|
172
|
+
#
|
173
|
+
# === Referencing declared namespaces
|
174
|
+
#
|
175
|
+
# Tags that reference non-default namespaces (i.e. a tag "foo:bar") can be
|
176
|
+
# built by using the Nokogiri::XML::Builder#[] method.
|
177
|
+
#
|
178
|
+
# For example:
|
179
|
+
#
|
180
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
181
|
+
# xml.root('xmlns:foo' => 'bar') {
|
182
|
+
# xml.objects {
|
183
|
+
# xml['foo'].object.classy.thing!
|
184
|
+
# }
|
185
|
+
# }
|
186
|
+
# end
|
187
|
+
# puts builder.to_xml
|
188
|
+
#
|
189
|
+
# Will output this XML:
|
190
|
+
#
|
191
|
+
# <?xml version="1.0"?>
|
192
|
+
# <root xmlns:foo="bar">
|
193
|
+
# <objects>
|
194
|
+
# <foo:object class="classy" id="thing"/>
|
195
|
+
# </objects>
|
196
|
+
# </root>
|
197
|
+
#
|
198
|
+
# Note the "foo:object" tag.
|
199
|
+
#
|
200
|
+
# === Namespace inheritance
|
201
|
+
#
|
202
|
+
# In the Builder context, children will inherit their parent's namespace. This is the same
|
203
|
+
# behavior as if the underlying {XML::Document} set +namespace_inheritance+ to +true+:
|
204
|
+
#
|
205
|
+
# result = Nokogiri::XML::Builder.new do |xml|
|
206
|
+
# xml["soapenv"].Envelope("xmlns:soapenv" => "http://schemas.xmlsoap.org/soap/envelope/") do
|
207
|
+
# xml.Header
|
208
|
+
# end
|
209
|
+
# end
|
210
|
+
# result.doc.to_xml
|
211
|
+
# # => <?xml version="1.0" encoding="utf-8"?>
|
212
|
+
# # <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
|
213
|
+
# # <soapenv:Header/>
|
214
|
+
# # </soapenv:Envelope>
|
215
|
+
#
|
216
|
+
# Users may turn this behavior off by passing a keyword argument +namespace_inheritance:false+
|
217
|
+
# to the initializer:
|
218
|
+
#
|
219
|
+
# result = Nokogiri::XML::Builder.new(namespace_inheritance: false) do |xml|
|
220
|
+
# xml["soapenv"].Envelope("xmlns:soapenv" => "http://schemas.xmlsoap.org/soap/envelope/") do
|
221
|
+
# xml.Header
|
222
|
+
# xml["soapenv"].Body # users may explicitly opt into the namespace
|
223
|
+
# end
|
224
|
+
# end
|
225
|
+
# result.doc.to_xml
|
226
|
+
# # => <?xml version="1.0" encoding="utf-8"?>
|
227
|
+
# # <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
|
228
|
+
# # <Header/>
|
229
|
+
# # <soapenv:Body/>
|
230
|
+
# # </soapenv:Envelope>
|
231
|
+
#
|
232
|
+
# For more information on namespace inheritance, please see {XML::Document#namespace_inheritance}
|
233
|
+
#
|
234
|
+
#
|
235
|
+
# == Document Types
|
236
|
+
#
|
237
|
+
# To create a document type (DTD), use the Builder#doc method to get
|
238
|
+
# the current context document. Then call Node#create_internal_subset to
|
239
|
+
# create the DTD node.
|
240
|
+
#
|
241
|
+
# For example, this Ruby:
|
242
|
+
#
|
243
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
244
|
+
# xml.doc.create_internal_subset(
|
245
|
+
# 'html',
|
246
|
+
# "-//W3C//DTD HTML 4.01 Transitional//EN",
|
247
|
+
# "http://www.w3.org/TR/html4/loose.dtd"
|
248
|
+
# )
|
249
|
+
# xml.root do
|
250
|
+
# xml.foo
|
251
|
+
# end
|
252
|
+
# end
|
253
|
+
#
|
254
|
+
# puts builder.to_xml
|
255
|
+
#
|
256
|
+
# Will output this xml:
|
257
|
+
#
|
258
|
+
# <?xml version="1.0"?>
|
259
|
+
# <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
260
|
+
# <root>
|
261
|
+
# <foo/>
|
262
|
+
# </root>
|
263
|
+
#
|
264
|
+
class Builder
|
265
|
+
include Nokogiri::ClassResolver
|
266
|
+
|
267
|
+
DEFAULT_DOCUMENT_OPTIONS = { namespace_inheritance: true }
|
268
|
+
|
269
|
+
# The current Document object being built
|
270
|
+
attr_accessor :doc
|
271
|
+
|
272
|
+
# The parent of the current node being built
|
273
|
+
attr_accessor :parent
|
274
|
+
|
275
|
+
# A context object for use when the block has no arguments
|
276
|
+
attr_accessor :context
|
277
|
+
|
278
|
+
attr_accessor :arity # :nodoc:
|
279
|
+
|
280
|
+
###
|
281
|
+
# Create a builder with an existing root object. This is for use when
|
282
|
+
# you have an existing document that you would like to augment with
|
283
|
+
# builder methods. The builder context created will start with the
|
284
|
+
# given +root+ node.
|
285
|
+
#
|
286
|
+
# For example:
|
287
|
+
#
|
288
|
+
# doc = Nokogiri::XML(File.read('somedoc.xml'))
|
289
|
+
# Nokogiri::XML::Builder.with(doc.at_css('some_tag')) do |xml|
|
290
|
+
# # ... Use normal builder methods here ...
|
291
|
+
# xml.awesome # add the "awesome" tag below "some_tag"
|
292
|
+
# end
|
293
|
+
#
|
294
|
+
def self.with(root, &block)
|
295
|
+
new({}, root, &block)
|
296
|
+
end
|
297
|
+
|
298
|
+
###
|
299
|
+
# Create a new Builder object. +options+ are sent to the top level
|
300
|
+
# Document that is being built.
|
301
|
+
#
|
302
|
+
# Building a document with a particular encoding for example:
|
303
|
+
#
|
304
|
+
# Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
|
305
|
+
# ...
|
306
|
+
# end
|
307
|
+
def initialize(options = {}, root = nil, &block)
|
308
|
+
if root
|
309
|
+
@doc = root.document
|
310
|
+
@parent = root
|
311
|
+
else
|
312
|
+
@parent = @doc = related_class("Document").new
|
313
|
+
end
|
314
|
+
|
315
|
+
@context = nil
|
316
|
+
@arity = nil
|
317
|
+
@ns = nil
|
318
|
+
|
319
|
+
options = DEFAULT_DOCUMENT_OPTIONS.merge(options)
|
320
|
+
options.each do |k, v|
|
321
|
+
@doc.send(:"#{k}=", v)
|
322
|
+
end
|
323
|
+
|
324
|
+
return unless block
|
325
|
+
|
326
|
+
@arity = block.arity
|
327
|
+
if @arity <= 0
|
328
|
+
@context = eval("self", block.binding)
|
329
|
+
instance_eval(&block)
|
330
|
+
else
|
331
|
+
yield self
|
332
|
+
end
|
333
|
+
|
334
|
+
@parent = @doc
|
335
|
+
end
|
336
|
+
|
337
|
+
###
|
338
|
+
# Create a Text Node with content of +string+
|
339
|
+
def text(string)
|
340
|
+
insert(@doc.create_text_node(string))
|
341
|
+
end
|
342
|
+
|
343
|
+
###
|
344
|
+
# Create a CDATA Node with content of +string+
|
345
|
+
def cdata(string)
|
346
|
+
insert(doc.create_cdata(string))
|
347
|
+
end
|
348
|
+
|
349
|
+
###
|
350
|
+
# Create a Comment Node with content of +string+
|
351
|
+
def comment(string)
|
352
|
+
insert(doc.create_comment(string))
|
353
|
+
end
|
354
|
+
|
355
|
+
###
|
356
|
+
# Build a tag that is associated with namespace +ns+. Raises an
|
357
|
+
# ArgumentError if +ns+ has not been defined higher in the tree.
|
358
|
+
def [](ns)
|
359
|
+
if @parent != @doc
|
360
|
+
@ns = @parent.namespace_definitions.find { |x| x.prefix == ns.to_s }
|
361
|
+
end
|
362
|
+
return self if @ns
|
363
|
+
|
364
|
+
@parent.ancestors.each do |a|
|
365
|
+
next if a == doc
|
366
|
+
|
367
|
+
@ns = a.namespace_definitions.find { |x| x.prefix == ns.to_s }
|
368
|
+
return self if @ns
|
369
|
+
end
|
370
|
+
|
371
|
+
@ns = { pending: ns.to_s }
|
372
|
+
self
|
373
|
+
end
|
374
|
+
|
375
|
+
###
|
376
|
+
# Convert this Builder object to XML
|
377
|
+
def to_xml(*args)
|
378
|
+
if Nokogiri.jruby?
|
379
|
+
options = args.first.is_a?(Hash) ? args.shift : {}
|
380
|
+
unless options[:save_with]
|
381
|
+
options[:save_with] = Node::SaveOptions::AS_BUILDER
|
382
|
+
end
|
383
|
+
args.insert(0, options)
|
384
|
+
end
|
385
|
+
@doc.to_xml(*args)
|
386
|
+
end
|
387
|
+
|
388
|
+
###
|
389
|
+
# Append the given raw XML +string+ to the document
|
390
|
+
def <<(string)
|
391
|
+
@doc.fragment(string).children.each { |x| insert(x) }
|
392
|
+
end
|
393
|
+
|
394
|
+
def method_missing(method, *args, &block) # :nodoc:
|
395
|
+
if @context&.respond_to?(method)
|
396
|
+
@context.send(method, *args, &block)
|
397
|
+
else
|
398
|
+
node = @doc.create_element(method.to_s.sub(/[_!]$/, ""), *args) do |n|
|
399
|
+
# Set up the namespace
|
400
|
+
if @ns.is_a?(Nokogiri::XML::Namespace)
|
401
|
+
n.namespace = @ns
|
402
|
+
@ns = nil
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
if @ns.is_a?(Hash)
|
407
|
+
node.namespace = node.namespace_definitions.find { |x| x.prefix == @ns[:pending] }
|
408
|
+
if node.namespace.nil?
|
409
|
+
raise ArgumentError, "Namespace #{@ns[:pending]} has not been defined"
|
410
|
+
end
|
411
|
+
|
412
|
+
@ns = nil
|
413
|
+
end
|
414
|
+
|
415
|
+
insert(node, &block)
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
private
|
420
|
+
|
421
|
+
###
|
422
|
+
# Insert +node+ as a child of the current Node
|
423
|
+
def insert(node, &block)
|
424
|
+
node = @parent.add_child(node)
|
425
|
+
if block
|
426
|
+
begin
|
427
|
+
old_parent = @parent
|
428
|
+
@parent = node
|
429
|
+
@arity ||= block.arity
|
430
|
+
if @arity <= 0
|
431
|
+
instance_eval(&block)
|
432
|
+
else
|
433
|
+
yield(self)
|
434
|
+
end
|
435
|
+
ensure
|
436
|
+
@parent = old_parent
|
437
|
+
end
|
438
|
+
end
|
439
|
+
NodeBuilder.new(node, self)
|
440
|
+
end
|
441
|
+
|
442
|
+
class NodeBuilder # :nodoc:
|
443
|
+
def initialize(node, doc_builder)
|
444
|
+
@node = node
|
445
|
+
@doc_builder = doc_builder
|
446
|
+
end
|
447
|
+
|
448
|
+
def []=(k, v)
|
449
|
+
@node[k] = v
|
450
|
+
end
|
451
|
+
|
452
|
+
def [](k)
|
453
|
+
@node[k]
|
454
|
+
end
|
455
|
+
|
456
|
+
def method_missing(method, *args, &block)
|
457
|
+
opts = args.last.is_a?(Hash) ? args.pop : {}
|
458
|
+
case method.to_s
|
459
|
+
when /^(.*)!$/
|
460
|
+
@node["id"] = Regexp.last_match(1)
|
461
|
+
@node.content = args.first if args.first
|
462
|
+
when /^(.*)=/
|
463
|
+
@node[Regexp.last_match(1)] = args.first
|
464
|
+
else
|
465
|
+
@node["class"] =
|
466
|
+
((@node["class"] || "").split(/\s/) + [method.to_s]).join(" ")
|
467
|
+
@node.content = args.first if args.first
|
468
|
+
end
|
469
|
+
|
470
|
+
# Assign any extra options
|
471
|
+
opts.each do |k, v|
|
472
|
+
@node[k.to_s] = ((@node[k.to_s] || "").split(/\s/) + [v]).join(" ")
|
473
|
+
end
|
474
|
+
|
475
|
+
if block
|
476
|
+
old_parent = @doc_builder.parent
|
477
|
+
@doc_builder.parent = @node
|
478
|
+
value = @doc_builder.instance_eval(&block)
|
479
|
+
@doc_builder.parent = old_parent
|
480
|
+
return value
|
481
|
+
end
|
482
|
+
self
|
483
|
+
end
|
484
|
+
end
|
485
|
+
end
|
486
|
+
end
|
487
|
+
end
|