nokogiri 1.13.0-x64-mingw-ucrt
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/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 +536 -0
@@ -0,0 +1,485 @@
|
|
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), access 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
|
+
@ns = a.namespace_definitions.find { |x| x.prefix == ns.to_s }
|
367
|
+
return self if @ns
|
368
|
+
end
|
369
|
+
|
370
|
+
@ns = { pending: ns.to_s }
|
371
|
+
self
|
372
|
+
end
|
373
|
+
|
374
|
+
###
|
375
|
+
# Convert this Builder object to XML
|
376
|
+
def to_xml(*args)
|
377
|
+
if Nokogiri.jruby?
|
378
|
+
options = args.first.is_a?(Hash) ? args.shift : {}
|
379
|
+
unless options[:save_with]
|
380
|
+
options[:save_with] = Node::SaveOptions::AS_BUILDER
|
381
|
+
end
|
382
|
+
args.insert(0, options)
|
383
|
+
end
|
384
|
+
@doc.to_xml(*args)
|
385
|
+
end
|
386
|
+
|
387
|
+
###
|
388
|
+
# Append the given raw XML +string+ to the document
|
389
|
+
def <<(string)
|
390
|
+
@doc.fragment(string).children.each { |x| insert(x) }
|
391
|
+
end
|
392
|
+
|
393
|
+
def method_missing(method, *args, &block) # :nodoc:
|
394
|
+
if @context&.respond_to?(method)
|
395
|
+
@context.send(method, *args, &block)
|
396
|
+
else
|
397
|
+
node = @doc.create_element(method.to_s.sub(/[_!]$/, ""), *args) do |n|
|
398
|
+
# Set up the namespace
|
399
|
+
if @ns.is_a?(Nokogiri::XML::Namespace)
|
400
|
+
n.namespace = @ns
|
401
|
+
@ns = nil
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
405
|
+
if @ns.is_a?(Hash)
|
406
|
+
node.namespace = node.namespace_definitions.find { |x| x.prefix == @ns[:pending] }
|
407
|
+
if node.namespace.nil?
|
408
|
+
raise ArgumentError, "Namespace #{@ns[:pending]} has not been defined"
|
409
|
+
end
|
410
|
+
@ns = nil
|
411
|
+
end
|
412
|
+
|
413
|
+
insert(node, &block)
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
private
|
418
|
+
|
419
|
+
###
|
420
|
+
# Insert +node+ as a child of the current Node
|
421
|
+
def insert(node, &block)
|
422
|
+
node = @parent.add_child(node)
|
423
|
+
if block
|
424
|
+
begin
|
425
|
+
old_parent = @parent
|
426
|
+
@parent = node
|
427
|
+
@arity ||= block.arity
|
428
|
+
if @arity <= 0
|
429
|
+
instance_eval(&block)
|
430
|
+
else
|
431
|
+
yield(self)
|
432
|
+
end
|
433
|
+
ensure
|
434
|
+
@parent = old_parent
|
435
|
+
end
|
436
|
+
end
|
437
|
+
NodeBuilder.new(node, self)
|
438
|
+
end
|
439
|
+
|
440
|
+
class NodeBuilder # :nodoc:
|
441
|
+
def initialize(node, doc_builder)
|
442
|
+
@node = node
|
443
|
+
@doc_builder = doc_builder
|
444
|
+
end
|
445
|
+
|
446
|
+
def []=(k, v)
|
447
|
+
@node[k] = v
|
448
|
+
end
|
449
|
+
|
450
|
+
def [](k)
|
451
|
+
@node[k]
|
452
|
+
end
|
453
|
+
|
454
|
+
def method_missing(method, *args, &block)
|
455
|
+
opts = args.last.is_a?(Hash) ? args.pop : {}
|
456
|
+
case method.to_s
|
457
|
+
when /^(.*)!$/
|
458
|
+
@node["id"] = Regexp.last_match(1)
|
459
|
+
@node.content = args.first if args.first
|
460
|
+
when /^(.*)=/
|
461
|
+
@node[Regexp.last_match(1)] = args.first
|
462
|
+
else
|
463
|
+
@node["class"] =
|
464
|
+
((@node["class"] || "").split(/\s/) + [method.to_s]).join(" ")
|
465
|
+
@node.content = args.first if args.first
|
466
|
+
end
|
467
|
+
|
468
|
+
# Assign any extra options
|
469
|
+
opts.each do |k, v|
|
470
|
+
@node[k.to_s] = ((@node[k.to_s] || "").split(/\s/) + [v]).join(" ")
|
471
|
+
end
|
472
|
+
|
473
|
+
if block
|
474
|
+
old_parent = @doc_builder.parent
|
475
|
+
@doc_builder.parent = @node
|
476
|
+
value = @doc_builder.instance_eval(&block)
|
477
|
+
@doc_builder.parent = old_parent
|
478
|
+
return value
|
479
|
+
end
|
480
|
+
self
|
481
|
+
end
|
482
|
+
end
|
483
|
+
end
|
484
|
+
end
|
485
|
+
end
|