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.

Files changed (195) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +5 -0
  3. data/LICENSE-DEPENDENCIES.md +1903 -0
  4. data/LICENSE.md +9 -0
  5. data/README.md +280 -0
  6. data/bin/nokogiri +131 -0
  7. data/dependencies.yml +73 -0
  8. data/ext/nokogiri/depend +38 -0
  9. data/ext/nokogiri/extconf.rb +1000 -0
  10. data/ext/nokogiri/gumbo.c +584 -0
  11. data/ext/nokogiri/html4_document.c +166 -0
  12. data/ext/nokogiri/html4_element_description.c +294 -0
  13. data/ext/nokogiri/html4_entity_lookup.c +37 -0
  14. data/ext/nokogiri/html4_sax_parser_context.c +120 -0
  15. data/ext/nokogiri/html4_sax_push_parser.c +95 -0
  16. data/ext/nokogiri/include/libexslt/exslt.h +102 -0
  17. data/ext/nokogiri/include/libexslt/exsltconfig.h +70 -0
  18. data/ext/nokogiri/include/libexslt/exsltexports.h +140 -0
  19. data/ext/nokogiri/include/libxml2/libxml/DOCBparser.h +96 -0
  20. data/ext/nokogiri/include/libxml2/libxml/HTMLparser.h +306 -0
  21. data/ext/nokogiri/include/libxml2/libxml/HTMLtree.h +147 -0
  22. data/ext/nokogiri/include/libxml2/libxml/SAX.h +173 -0
  23. data/ext/nokogiri/include/libxml2/libxml/SAX2.h +178 -0
  24. data/ext/nokogiri/include/libxml2/libxml/c14n.h +128 -0
  25. data/ext/nokogiri/include/libxml2/libxml/catalog.h +182 -0
  26. data/ext/nokogiri/include/libxml2/libxml/chvalid.h +230 -0
  27. data/ext/nokogiri/include/libxml2/libxml/debugXML.h +217 -0
  28. data/ext/nokogiri/include/libxml2/libxml/dict.h +79 -0
  29. data/ext/nokogiri/include/libxml2/libxml/encoding.h +245 -0
  30. data/ext/nokogiri/include/libxml2/libxml/entities.h +151 -0
  31. data/ext/nokogiri/include/libxml2/libxml/globals.h +508 -0
  32. data/ext/nokogiri/include/libxml2/libxml/hash.h +236 -0
  33. data/ext/nokogiri/include/libxml2/libxml/list.h +137 -0
  34. data/ext/nokogiri/include/libxml2/libxml/nanoftp.h +163 -0
  35. data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +81 -0
  36. data/ext/nokogiri/include/libxml2/libxml/parser.h +1243 -0
  37. data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +644 -0
  38. data/ext/nokogiri/include/libxml2/libxml/pattern.h +100 -0
  39. data/ext/nokogiri/include/libxml2/libxml/relaxng.h +217 -0
  40. data/ext/nokogiri/include/libxml2/libxml/schemasInternals.h +958 -0
  41. data/ext/nokogiri/include/libxml2/libxml/schematron.h +142 -0
  42. data/ext/nokogiri/include/libxml2/libxml/threads.h +89 -0
  43. data/ext/nokogiri/include/libxml2/libxml/tree.h +1311 -0
  44. data/ext/nokogiri/include/libxml2/libxml/uri.h +94 -0
  45. data/ext/nokogiri/include/libxml2/libxml/valid.h +458 -0
  46. data/ext/nokogiri/include/libxml2/libxml/xinclude.h +129 -0
  47. data/ext/nokogiri/include/libxml2/libxml/xlink.h +189 -0
  48. data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +368 -0
  49. data/ext/nokogiri/include/libxml2/libxml/xmlautomata.h +146 -0
  50. data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +946 -0
  51. data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +77 -0
  52. data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +224 -0
  53. data/ext/nokogiri/include/libxml2/libxml/xmlmodule.h +57 -0
  54. data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +428 -0
  55. data/ext/nokogiri/include/libxml2/libxml/xmlregexp.h +222 -0
  56. data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +88 -0
  57. data/ext/nokogiri/include/libxml2/libxml/xmlschemas.h +246 -0
  58. data/ext/nokogiri/include/libxml2/libxml/xmlschemastypes.h +151 -0
  59. data/ext/nokogiri/include/libxml2/libxml/xmlstring.h +140 -0
  60. data/ext/nokogiri/include/libxml2/libxml/xmlunicode.h +202 -0
  61. data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +485 -0
  62. data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +488 -0
  63. data/ext/nokogiri/include/libxml2/libxml/xpath.h +564 -0
  64. data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +632 -0
  65. data/ext/nokogiri/include/libxml2/libxml/xpointer.h +114 -0
  66. data/ext/nokogiri/include/libxslt/attributes.h +38 -0
  67. data/ext/nokogiri/include/libxslt/documents.h +93 -0
  68. data/ext/nokogiri/include/libxslt/extensions.h +262 -0
  69. data/ext/nokogiri/include/libxslt/extra.h +72 -0
  70. data/ext/nokogiri/include/libxslt/functions.h +78 -0
  71. data/ext/nokogiri/include/libxslt/imports.h +75 -0
  72. data/ext/nokogiri/include/libxslt/keys.h +53 -0
  73. data/ext/nokogiri/include/libxslt/namespaces.h +68 -0
  74. data/ext/nokogiri/include/libxslt/numbersInternals.h +73 -0
  75. data/ext/nokogiri/include/libxslt/pattern.h +84 -0
  76. data/ext/nokogiri/include/libxslt/preproc.h +43 -0
  77. data/ext/nokogiri/include/libxslt/security.h +104 -0
  78. data/ext/nokogiri/include/libxslt/templates.h +77 -0
  79. data/ext/nokogiri/include/libxslt/transform.h +207 -0
  80. data/ext/nokogiri/include/libxslt/variables.h +118 -0
  81. data/ext/nokogiri/include/libxslt/xslt.h +110 -0
  82. data/ext/nokogiri/include/libxslt/xsltInternals.h +1978 -0
  83. data/ext/nokogiri/include/libxslt/xsltconfig.h +180 -0
  84. data/ext/nokogiri/include/libxslt/xsltexports.h +142 -0
  85. data/ext/nokogiri/include/libxslt/xsltlocale.h +76 -0
  86. data/ext/nokogiri/include/libxslt/xsltutils.h +313 -0
  87. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  88. data/ext/nokogiri/nokogiri.c +278 -0
  89. data/ext/nokogiri/nokogiri.h +223 -0
  90. data/ext/nokogiri/test_global_handlers.c +40 -0
  91. data/ext/nokogiri/xml_attr.c +103 -0
  92. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  93. data/ext/nokogiri/xml_cdata.c +57 -0
  94. data/ext/nokogiri/xml_comment.c +62 -0
  95. data/ext/nokogiri/xml_document.c +680 -0
  96. data/ext/nokogiri/xml_document_fragment.c +44 -0
  97. data/ext/nokogiri/xml_dtd.c +208 -0
  98. data/ext/nokogiri/xml_element_content.c +128 -0
  99. data/ext/nokogiri/xml_element_decl.c +69 -0
  100. data/ext/nokogiri/xml_encoding_handler.c +104 -0
  101. data/ext/nokogiri/xml_entity_decl.c +112 -0
  102. data/ext/nokogiri/xml_entity_reference.c +50 -0
  103. data/ext/nokogiri/xml_namespace.c +120 -0
  104. data/ext/nokogiri/xml_node.c +2144 -0
  105. data/ext/nokogiri/xml_node_set.c +498 -0
  106. data/ext/nokogiri/xml_processing_instruction.c +54 -0
  107. data/ext/nokogiri/xml_reader.c +719 -0
  108. data/ext/nokogiri/xml_relax_ng.c +185 -0
  109. data/ext/nokogiri/xml_sax_parser.c +310 -0
  110. data/ext/nokogiri/xml_sax_parser_context.c +281 -0
  111. data/ext/nokogiri/xml_sax_push_parser.c +168 -0
  112. data/ext/nokogiri/xml_schema.c +284 -0
  113. data/ext/nokogiri/xml_syntax_error.c +85 -0
  114. data/ext/nokogiri/xml_text.c +48 -0
  115. data/ext/nokogiri/xml_xpath_context.c +406 -0
  116. data/ext/nokogiri/xslt_stylesheet.c +264 -0
  117. data/gumbo-parser/CHANGES.md +63 -0
  118. data/gumbo-parser/Makefile +101 -0
  119. data/gumbo-parser/THANKS +27 -0
  120. data/lib/nokogiri/3.1/nokogiri.so +0 -0
  121. data/lib/nokogiri/class_resolver.rb +67 -0
  122. data/lib/nokogiri/css/node.rb +54 -0
  123. data/lib/nokogiri/css/parser.rb +759 -0
  124. data/lib/nokogiri/css/parser.y +280 -0
  125. data/lib/nokogiri/css/parser_extras.rb +94 -0
  126. data/lib/nokogiri/css/syntax_error.rb +9 -0
  127. data/lib/nokogiri/css/tokenizer.rb +155 -0
  128. data/lib/nokogiri/css/tokenizer.rex +56 -0
  129. data/lib/nokogiri/css/xpath_visitor.rb +359 -0
  130. data/lib/nokogiri/css.rb +60 -0
  131. data/lib/nokogiri/decorators/slop.rb +44 -0
  132. data/lib/nokogiri/extension.rb +31 -0
  133. data/lib/nokogiri/gumbo.rb +15 -0
  134. data/lib/nokogiri/html.rb +48 -0
  135. data/lib/nokogiri/html4/builder.rb +37 -0
  136. data/lib/nokogiri/html4/document.rb +331 -0
  137. data/lib/nokogiri/html4/document_fragment.rb +54 -0
  138. data/lib/nokogiri/html4/element_description.rb +25 -0
  139. data/lib/nokogiri/html4/element_description_defaults.rb +578 -0
  140. data/lib/nokogiri/html4/entity_lookup.rb +15 -0
  141. data/lib/nokogiri/html4/sax/parser.rb +61 -0
  142. data/lib/nokogiri/html4/sax/parser_context.rb +20 -0
  143. data/lib/nokogiri/html4/sax/push_parser.rb +37 -0
  144. data/lib/nokogiri/html4.rb +46 -0
  145. data/lib/nokogiri/html5/document.rb +88 -0
  146. data/lib/nokogiri/html5/document_fragment.rb +83 -0
  147. data/lib/nokogiri/html5/node.rb +96 -0
  148. data/lib/nokogiri/html5.rb +477 -0
  149. data/lib/nokogiri/jruby/dependencies.rb +21 -0
  150. data/lib/nokogiri/syntax_error.rb +6 -0
  151. data/lib/nokogiri/version/constant.rb +6 -0
  152. data/lib/nokogiri/version/info.rb +221 -0
  153. data/lib/nokogiri/version.rb +4 -0
  154. data/lib/nokogiri/xml/attr.rb +17 -0
  155. data/lib/nokogiri/xml/attribute_decl.rb +20 -0
  156. data/lib/nokogiri/xml/builder.rb +485 -0
  157. data/lib/nokogiri/xml/cdata.rb +13 -0
  158. data/lib/nokogiri/xml/character_data.rb +9 -0
  159. data/lib/nokogiri/xml/document.rb +418 -0
  160. data/lib/nokogiri/xml/document_fragment.rb +162 -0
  161. data/lib/nokogiri/xml/dtd.rb +34 -0
  162. data/lib/nokogiri/xml/element_content.rb +38 -0
  163. data/lib/nokogiri/xml/element_decl.rb +15 -0
  164. data/lib/nokogiri/xml/entity_decl.rb +21 -0
  165. data/lib/nokogiri/xml/entity_reference.rb +20 -0
  166. data/lib/nokogiri/xml/namespace.rb +16 -0
  167. data/lib/nokogiri/xml/node/save_options.rb +65 -0
  168. data/lib/nokogiri/xml/node.rb +1402 -0
  169. data/lib/nokogiri/xml/node_set.rb +364 -0
  170. data/lib/nokogiri/xml/notation.rb +19 -0
  171. data/lib/nokogiri/xml/parse_options.rb +133 -0
  172. data/lib/nokogiri/xml/pp/character_data.rb +21 -0
  173. data/lib/nokogiri/xml/pp/node.rb +55 -0
  174. data/lib/nokogiri/xml/pp.rb +4 -0
  175. data/lib/nokogiri/xml/processing_instruction.rb +10 -0
  176. data/lib/nokogiri/xml/reader.rb +107 -0
  177. data/lib/nokogiri/xml/relax_ng.rb +38 -0
  178. data/lib/nokogiri/xml/sax/document.rb +167 -0
  179. data/lib/nokogiri/xml/sax/parser.rb +125 -0
  180. data/lib/nokogiri/xml/sax/parser_context.rb +21 -0
  181. data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
  182. data/lib/nokogiri/xml/sax.rb +6 -0
  183. data/lib/nokogiri/xml/schema.rb +73 -0
  184. data/lib/nokogiri/xml/searchable.rb +259 -0
  185. data/lib/nokogiri/xml/syntax_error.rb +71 -0
  186. data/lib/nokogiri/xml/text.rb +11 -0
  187. data/lib/nokogiri/xml/xpath/syntax_error.rb +13 -0
  188. data/lib/nokogiri/xml/xpath.rb +21 -0
  189. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  190. data/lib/nokogiri/xml.rb +75 -0
  191. data/lib/nokogiri/xslt/stylesheet.rb +27 -0
  192. data/lib/nokogiri/xslt.rb +58 -0
  193. data/lib/nokogiri.rb +128 -0
  194. data/lib/xsd/xmlparser/nokogiri.rb +104 -0
  195. 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
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class CDATA < Nokogiri::XML::Text
6
+ ###
7
+ # Get the name of this CDATA node
8
+ def name
9
+ "#cdata-section"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class CharacterData < Nokogiri::XML::Node
6
+ include Nokogiri::XML::PP::CharacterData
7
+ end
8
+ end
9
+ end