nokogiri 1.18.0.rc1-arm-linux-musl
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +38 -0
- data/LICENSE-DEPENDENCIES.md +2224 -0
- data/LICENSE.md +9 -0
- data/README.md +293 -0
- data/bin/nokogiri +131 -0
- data/dependencies.yml +42 -0
- data/ext/nokogiri/depend +38 -0
- data/ext/nokogiri/extconf.rb +1173 -0
- data/ext/nokogiri/gumbo.c +610 -0
- data/ext/nokogiri/html4_document.c +171 -0
- data/ext/nokogiri/html4_element_description.c +299 -0
- data/ext/nokogiri/html4_entity_lookup.c +37 -0
- data/ext/nokogiri/html4_sax_parser.c +40 -0
- data/ext/nokogiri/html4_sax_parser_context.c +98 -0
- data/ext/nokogiri/html4_sax_push_parser.c +96 -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 +336 -0
- data/ext/nokogiri/include/libxml2/libxml/HTMLtree.h +147 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX.h +202 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX2.h +171 -0
- data/ext/nokogiri/include/libxml2/libxml/c14n.h +115 -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 +82 -0
- data/ext/nokogiri/include/libxml2/libxml/encoding.h +244 -0
- data/ext/nokogiri/include/libxml2/libxml/entities.h +166 -0
- data/ext/nokogiri/include/libxml2/libxml/globals.h +41 -0
- data/ext/nokogiri/include/libxml2/libxml/hash.h +251 -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 +98 -0
- data/ext/nokogiri/include/libxml2/libxml/parser.h +1390 -0
- data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +671 -0
- data/ext/nokogiri/include/libxml2/libxml/pattern.h +106 -0
- data/ext/nokogiri/include/libxml2/libxml/relaxng.h +219 -0
- data/ext/nokogiri/include/libxml2/libxml/schemasInternals.h +959 -0
- data/ext/nokogiri/include/libxml2/libxml/schematron.h +143 -0
- data/ext/nokogiri/include/libxml2/libxml/threads.h +87 -0
- data/ext/nokogiri/include/libxml2/libxml/tree.h +1382 -0
- data/ext/nokogiri/include/libxml2/libxml/uri.h +106 -0
- data/ext/nokogiri/include/libxml2/libxml/valid.h +477 -0
- data/ext/nokogiri/include/libxml2/libxml/xinclude.h +136 -0
- data/ext/nokogiri/include/libxml2/libxml/xlink.h +189 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +438 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlautomata.h +146 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +962 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +146 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +188 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmodule.h +57 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +436 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlregexp.h +215 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +102 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlschemas.h +249 -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 +366 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +347 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +489 -0
- data/ext/nokogiri/include/libxml2/libxml/xpath.h +579 -0
- data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +633 -0
- data/ext/nokogiri/include/libxml2/libxml/xpointer.h +138 -0
- data/ext/nokogiri/include/libxslt/attributes.h +39 -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 +1995 -0
- data/ext/nokogiri/include/libxslt/xsltconfig.h +146 -0
- data/ext/nokogiri/include/libxslt/xsltexports.h +64 -0
- data/ext/nokogiri/include/libxslt/xsltlocale.h +44 -0
- data/ext/nokogiri/include/libxslt/xsltutils.h +343 -0
- data/ext/nokogiri/libxml2_polyfill.c +114 -0
- data/ext/nokogiri/nokogiri.c +294 -0
- data/ext/nokogiri/nokogiri.h +238 -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 +62 -0
- data/ext/nokogiri/xml_comment.c +57 -0
- data/ext/nokogiri/xml_document.c +784 -0
- data/ext/nokogiri/xml_document_fragment.c +29 -0
- data/ext/nokogiri/xml_dtd.c +208 -0
- data/ext/nokogiri/xml_element_content.c +131 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_encoding_handler.c +112 -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 +181 -0
- data/ext/nokogiri/xml_node.c +2459 -0
- data/ext/nokogiri/xml_node_set.c +518 -0
- data/ext/nokogiri/xml_processing_instruction.c +54 -0
- data/ext/nokogiri/xml_reader.c +777 -0
- data/ext/nokogiri/xml_relax_ng.c +149 -0
- data/ext/nokogiri/xml_sax_parser.c +403 -0
- data/ext/nokogiri/xml_sax_parser_context.c +390 -0
- data/ext/nokogiri/xml_sax_push_parser.c +206 -0
- data/ext/nokogiri/xml_schema.c +226 -0
- data/ext/nokogiri/xml_syntax_error.c +93 -0
- data/ext/nokogiri/xml_text.c +59 -0
- data/ext/nokogiri/xml_xpath_context.c +502 -0
- data/ext/nokogiri/xslt_stylesheet.c +421 -0
- data/gumbo-parser/CHANGES.md +63 -0
- data/gumbo-parser/Makefile +129 -0
- data/gumbo-parser/THANKS +27 -0
- data/lib/nokogiri/3.1/nokogiri.so +0 -0
- data/lib/nokogiri/3.2/nokogiri.so +0 -0
- data/lib/nokogiri/3.3/nokogiri.so +0 -0
- data/lib/nokogiri/3.4/nokogiri.so +0 -0
- data/lib/nokogiri/class_resolver.rb +67 -0
- data/lib/nokogiri/css/node.rb +58 -0
- data/lib/nokogiri/css/parser.rb +772 -0
- data/lib/nokogiri/css/parser.y +277 -0
- data/lib/nokogiri/css/parser_extras.rb +36 -0
- data/lib/nokogiri/css/selector_cache.rb +38 -0
- data/lib/nokogiri/css/syntax_error.rb +9 -0
- data/lib/nokogiri/css/tokenizer.rb +155 -0
- data/lib/nokogiri/css/tokenizer.rex +57 -0
- data/lib/nokogiri/css/xpath_visitor.rb +375 -0
- data/lib/nokogiri/css.rb +132 -0
- data/lib/nokogiri/decorators/slop.rb +42 -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 +235 -0
- data/lib/nokogiri/html4/document_fragment.rb +166 -0
- data/lib/nokogiri/html4/element_description.rb +25 -0
- data/lib/nokogiri/html4/element_description_defaults.rb +2040 -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 +48 -0
- data/lib/nokogiri/html4/sax/parser_context.rb +15 -0
- data/lib/nokogiri/html4/sax/push_parser.rb +37 -0
- data/lib/nokogiri/html4.rb +42 -0
- data/lib/nokogiri/html5/builder.rb +40 -0
- data/lib/nokogiri/html5/document.rb +199 -0
- data/lib/nokogiri/html5/document_fragment.rb +200 -0
- data/lib/nokogiri/html5/node.rb +103 -0
- data/lib/nokogiri/html5.rb +368 -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 +224 -0
- data/lib/nokogiri/version.rb +4 -0
- data/lib/nokogiri/xml/attr.rb +66 -0
- data/lib/nokogiri/xml/attribute_decl.rb +22 -0
- data/lib/nokogiri/xml/builder.rb +494 -0
- data/lib/nokogiri/xml/cdata.rb +13 -0
- data/lib/nokogiri/xml/character_data.rb +9 -0
- data/lib/nokogiri/xml/document.rb +514 -0
- data/lib/nokogiri/xml/document_fragment.rb +276 -0
- data/lib/nokogiri/xml/dtd.rb +34 -0
- data/lib/nokogiri/xml/element_content.rb +46 -0
- data/lib/nokogiri/xml/element_decl.rb +17 -0
- data/lib/nokogiri/xml/entity_decl.rb +23 -0
- data/lib/nokogiri/xml/entity_reference.rb +20 -0
- data/lib/nokogiri/xml/namespace.rb +57 -0
- data/lib/nokogiri/xml/node/save_options.rb +76 -0
- data/lib/nokogiri/xml/node.rb +1650 -0
- data/lib/nokogiri/xml/node_set.rb +449 -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 +73 -0
- data/lib/nokogiri/xml/pp.rb +4 -0
- data/lib/nokogiri/xml/processing_instruction.rb +11 -0
- data/lib/nokogiri/xml/reader.rb +139 -0
- data/lib/nokogiri/xml/relax_ng.rb +75 -0
- data/lib/nokogiri/xml/sax/document.rb +258 -0
- data/lib/nokogiri/xml/sax/parser.rb +199 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +129 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +64 -0
- data/lib/nokogiri/xml/sax.rb +54 -0
- data/lib/nokogiri/xml/schema.rb +140 -0
- data/lib/nokogiri/xml/searchable.rb +297 -0
- data/lib/nokogiri/xml/syntax_error.rb +94 -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 +49 -0
- data/lib/nokogiri/xml.rb +65 -0
- data/lib/nokogiri/xslt/stylesheet.rb +49 -0
- data/lib/nokogiri/xslt.rb +129 -0
- data/lib/nokogiri.rb +128 -0
- data/lib/xsd/xmlparser/nokogiri.rb +105 -0
- metadata +324 -0
@@ -0,0 +1,494 @@
|
|
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
|
+
|
479
|
+
arity = @doc_builder.arity || block.arity
|
480
|
+
value = if arity <= 0
|
481
|
+
@doc_builder.instance_eval(&block)
|
482
|
+
else
|
483
|
+
yield(@doc_builder)
|
484
|
+
end
|
485
|
+
|
486
|
+
@doc_builder.parent = old_parent
|
487
|
+
return value
|
488
|
+
end
|
489
|
+
self
|
490
|
+
end
|
491
|
+
end
|
492
|
+
end
|
493
|
+
end
|
494
|
+
end
|