nokogiri-fitzsimmons 1.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/.gemtest +0 -0
- data/CHANGELOG.ja.rdoc +666 -0
- data/CHANGELOG.rdoc +659 -0
- data/C_CODING_STYLE.rdoc +33 -0
- data/Manifest.txt +295 -0
- data/README.ja.rdoc +106 -0
- data/README.rdoc +178 -0
- data/ROADMAP.md +86 -0
- data/Rakefile +194 -0
- data/STANDARD_RESPONSES.md +47 -0
- data/Y_U_NO_GEMSPEC.md +155 -0
- data/bin/nokogiri +63 -0
- data/build_all +58 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +142 -0
- data/ext/nokogiri/html_document.c +170 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +276 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +116 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/html_sax_push_parser.c +87 -0
- data/ext/nokogiri/html_sax_push_parser.h +9 -0
- data/ext/nokogiri/nokogiri.c +133 -0
- data/ext/nokogiri/nokogiri.h +160 -0
- data/ext/nokogiri/xml_attr.c +94 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +56 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +54 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +576 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +56 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +78 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1480 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +467 -0
- data/ext/nokogiri/xml_node_set.h +14 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +684 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +161 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +293 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +222 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +115 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +205 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +58 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +52 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +319 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +270 -0
- data/ext/nokogiri/xslt_stylesheet.h +14 -0
- data/lib/nokogiri.rb +127 -0
- data/lib/nokogiri/css.rb +27 -0
- data/lib/nokogiri/css/node.rb +102 -0
- data/lib/nokogiri/css/parser.rb +720 -0
- data/lib/nokogiri/css/parser.y +258 -0
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +152 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +171 -0
- data/lib/nokogiri/decorators/slop.rb +35 -0
- data/lib/nokogiri/html.rb +37 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +254 -0
- data/lib/nokogiri/html/document_fragment.rb +41 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +52 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/html/sax/push_parser.rb +16 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +88 -0
- data/lib/nokogiri/xml.rb +73 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +431 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +267 -0
- data/lib/nokogiri/xml/document_fragment.rb +103 -0
- data/lib/nokogiri/xml/dtd.rb +22 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +946 -0
- data/lib/nokogiri/xml/node/save_options.rb +61 -0
- data/lib/nokogiri/xml/node_set.rb +357 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +98 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +112 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +164 -0
- data/lib/nokogiri/xml/sax/parser.rb +115 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +63 -0
- data/lib/nokogiri/xml/syntax_error.rb +47 -0
- data/lib/nokogiri/xml/text.rb +9 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +56 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +90 -0
- data/tasks/cross_compile.rb +153 -0
- data/tasks/nokogiri.org.rb +24 -0
- data/tasks/test.rb +95 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +341 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +91 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/files/to_be_xincluded.xml +2 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/files/xinclude.xml +4 -0
- data/test/helper.rb +147 -0
- data/test/html/sax/test_parser.rb +138 -0
- data/test/html/sax/test_parser_context.rb +46 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +529 -0
- data/test/html/test_document_encoding.rb +138 -0
- data/test/html/test_document_fragment.rb +254 -0
- data/test/html/test_element_description.rb +100 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +188 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +152 -0
- data/test/test_nokogiri.rb +132 -0
- data/test/test_reader.rb +488 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +254 -0
- data/test/xml/node/test_save_options.rb +28 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +338 -0
- data/test/xml/sax/test_parser_context.rb +106 -0
- data/test/xml/sax/test_push_parser.rb +157 -0
- data/test/xml/test_attr.rb +64 -0
- data/test/xml/test_attribute_decl.rb +86 -0
- data/test/xml/test_builder.rb +248 -0
- data/test/xml/test_c14n.rb +151 -0
- data/test/xml/test_cdata.rb +48 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +742 -0
- data/test/xml/test_document_encoding.rb +28 -0
- data/test/xml/test_document_fragment.rb +216 -0
- data/test/xml/test_dtd.rb +103 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +122 -0
- data/test/xml/test_entity_reference.rb +235 -0
- data/test/xml/test_namespace.rb +75 -0
- data/test/xml/test_node.rb +1029 -0
- data/test/xml/test_node_attributes.rb +53 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_inheritance.rb +32 -0
- data/test/xml/test_node_reparenting.rb +374 -0
- data/test/xml/test_node_set.rb +755 -0
- data/test/xml/test_parse_options.rb +64 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +142 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +94 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +45 -0
- data/test/xml/test_unparented_node.rb +413 -0
- data/test/xml/test_xinclude.rb +83 -0
- data/test/xml/test_xpath.rb +295 -0
- data/test/xslt/test_custom_functions.rb +129 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- data/test_all +84 -0
- metadata +534 -0
@@ -0,0 +1,267 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
module XML
|
3
|
+
##
|
4
|
+
# Nokogiri::XML::Document is the main entry point for dealing with
|
5
|
+
# XML documents. The Document is created by parsing an XML document.
|
6
|
+
# See Nokogiri::XML::Document.parse() for more information on parsing.
|
7
|
+
#
|
8
|
+
# For searching a Document, see Nokogiri::XML::Node#css and
|
9
|
+
# Nokogiri::XML::Node#xpath
|
10
|
+
#
|
11
|
+
class Document < Nokogiri::XML::Node
|
12
|
+
# I'm ignoring unicode characters here.
|
13
|
+
# See http://www.w3.org/TR/REC-xml-names/#ns-decl for more details.
|
14
|
+
NCNAME_START_CHAR = "A-Za-z_"
|
15
|
+
NCNAME_CHAR = NCNAME_START_CHAR + "\\-.0-9"
|
16
|
+
NCNAME_RE = /^xmlns(:[#{NCNAME_START_CHAR}][#{NCNAME_CHAR}]*)?$/
|
17
|
+
|
18
|
+
##
|
19
|
+
# Parse an XML file.
|
20
|
+
#
|
21
|
+
# +string_or_io+ may be a String, or any object that responds to
|
22
|
+
# _read_ and _close_ such as an IO, or StringIO.
|
23
|
+
#
|
24
|
+
# +url+ (optional) is the URI where this document is located.
|
25
|
+
#
|
26
|
+
# +encoding+ (optional) is the encoding that should be used when processing
|
27
|
+
# the document.
|
28
|
+
#
|
29
|
+
# +options+ (optional) is a configuration object that sets options during
|
30
|
+
# parsing, such as Nokogiri::XML::ParseOptions::RECOVER. See the
|
31
|
+
# Nokogiri::XML::ParseOptions for more information.
|
32
|
+
#
|
33
|
+
# +block+ (optional) is passed a configuration object on which
|
34
|
+
# parse options may be set.
|
35
|
+
#
|
36
|
+
# When parsing untrusted documents, it's recommended that the
|
37
|
+
# +nonet+ option be used, as shown in this example code:
|
38
|
+
#
|
39
|
+
# Nokogiri::XML::Document.parse(xml_string) { |config| config.nonet }
|
40
|
+
#
|
41
|
+
# Nokogiri.XML() is a convenience method which will call this method.
|
42
|
+
#
|
43
|
+
def self.parse string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML, &block
|
44
|
+
options = Nokogiri::XML::ParseOptions.new(options) if Fixnum === options
|
45
|
+
# Give the options to the user
|
46
|
+
yield options if block_given?
|
47
|
+
|
48
|
+
doc = if string_or_io.respond_to?(:read)
|
49
|
+
url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
|
50
|
+
read_io(string_or_io, url, encoding, options.to_i)
|
51
|
+
else
|
52
|
+
# read_memory pukes on empty docs
|
53
|
+
return new if string_or_io.nil? or string_or_io.empty?
|
54
|
+
read_memory(string_or_io, url, encoding, options.to_i)
|
55
|
+
end
|
56
|
+
|
57
|
+
# do xinclude processing
|
58
|
+
doc.do_xinclude(options) if options.xinclude?
|
59
|
+
|
60
|
+
return doc
|
61
|
+
end
|
62
|
+
|
63
|
+
# A list of Nokogiri::XML::SyntaxError found when parsing a document
|
64
|
+
attr_accessor :errors
|
65
|
+
|
66
|
+
def initialize *args # :nodoc:
|
67
|
+
@errors = []
|
68
|
+
@decorators = nil
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Create an element with +name+, and optionally setting the content and attributes.
|
73
|
+
#
|
74
|
+
# doc.create_element "div" # <div></div>
|
75
|
+
# doc.create_element "div", :class => "container" # <div class='container'></div>
|
76
|
+
# doc.create_element "div", "contents" # <div>contents</div>
|
77
|
+
# doc.create_element "div", "contents", :class => "container" # <div class='container'>contents</div>
|
78
|
+
# doc.create_element "div" { |node| node['class'] = "container" } # <div class='container'></div>
|
79
|
+
#
|
80
|
+
def create_element name, *args, &block
|
81
|
+
elm = Nokogiri::XML::Element.new(name, self, &block)
|
82
|
+
args.each do |arg|
|
83
|
+
case arg
|
84
|
+
when Hash
|
85
|
+
arg.each { |k,v|
|
86
|
+
key = k.to_s
|
87
|
+
if key =~ NCNAME_RE
|
88
|
+
ns_name = key.split(":", 2)[1]
|
89
|
+
elm.add_namespace_definition ns_name, v
|
90
|
+
next
|
91
|
+
end
|
92
|
+
elm[k.to_s] = v.to_s
|
93
|
+
}
|
94
|
+
else
|
95
|
+
elm.content = arg
|
96
|
+
end
|
97
|
+
end
|
98
|
+
elm
|
99
|
+
end
|
100
|
+
|
101
|
+
# Create a Text Node with +string+
|
102
|
+
def create_text_node string, &block
|
103
|
+
Nokogiri::XML::Text.new string.to_s, self, &block
|
104
|
+
end
|
105
|
+
|
106
|
+
# Create a CDATA Node containing +string+
|
107
|
+
def create_cdata string, &block
|
108
|
+
Nokogiri::XML::CDATA.new self, string.to_s, &block
|
109
|
+
end
|
110
|
+
|
111
|
+
# Create a Comment Node containing +string+
|
112
|
+
def create_comment string, &block
|
113
|
+
Nokogiri::XML::Comment.new self, string.to_s, &block
|
114
|
+
end
|
115
|
+
|
116
|
+
# The name of this document. Always returns "document"
|
117
|
+
def name
|
118
|
+
'document'
|
119
|
+
end
|
120
|
+
|
121
|
+
# A reference to +self+
|
122
|
+
def document
|
123
|
+
self
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# Recursively get all namespaces from this node and its subtree and
|
128
|
+
# return them as a hash.
|
129
|
+
#
|
130
|
+
# For example, given this document:
|
131
|
+
#
|
132
|
+
# <root xmlns:foo="bar">
|
133
|
+
# <bar xmlns:hello="world" />
|
134
|
+
# </root>
|
135
|
+
#
|
136
|
+
# This method will return:
|
137
|
+
#
|
138
|
+
# { 'xmlns:foo' => 'bar', 'xmlns:hello' => 'world' }
|
139
|
+
#
|
140
|
+
# WARNING: this method will clobber duplicate names in the keys.
|
141
|
+
# For example, given this document:
|
142
|
+
#
|
143
|
+
# <root xmlns:foo="bar">
|
144
|
+
# <bar xmlns:foo="baz" />
|
145
|
+
# </root>
|
146
|
+
#
|
147
|
+
# The hash returned will look like this: { 'xmlns:foo' => 'bar' }
|
148
|
+
#
|
149
|
+
# Non-prefixed default namespaces (as in "xmlns=") are not included
|
150
|
+
# in the hash.
|
151
|
+
#
|
152
|
+
# Note this is a very expensive operation in current implementation, as it
|
153
|
+
# traverses the entire graph, and also has to bring each node across the
|
154
|
+
# libxml bridge into a ruby object.
|
155
|
+
def collect_namespaces
|
156
|
+
ns = {}
|
157
|
+
traverse { |j| ns.merge!(j.namespaces) }
|
158
|
+
ns
|
159
|
+
end
|
160
|
+
|
161
|
+
# Get the list of decorators given +key+
|
162
|
+
def decorators key
|
163
|
+
@decorators ||= Hash.new
|
164
|
+
@decorators[key] ||= []
|
165
|
+
end
|
166
|
+
|
167
|
+
##
|
168
|
+
# Validate this Document against it's DTD. Returns a list of errors on
|
169
|
+
# the document or +nil+ when there is no DTD.
|
170
|
+
def validate
|
171
|
+
return nil unless internal_subset
|
172
|
+
internal_subset.validate self
|
173
|
+
end
|
174
|
+
|
175
|
+
##
|
176
|
+
# Explore a document with shortcut methods. See Nokogiri::Slop for details.
|
177
|
+
#
|
178
|
+
# Note that any nodes that have been instantiated before #slop!
|
179
|
+
# is called will not be decorated with sloppy behavior. So, if you're in
|
180
|
+
# irb, the preferred idiom is:
|
181
|
+
#
|
182
|
+
# irb> doc = Nokogiri::Slop my_markup
|
183
|
+
#
|
184
|
+
# and not
|
185
|
+
#
|
186
|
+
# irb> doc = Nokogiri::HTML my_markup
|
187
|
+
# ... followed by irb's implicit inspect (and therefore instantiation of every node) ...
|
188
|
+
# irb> doc.slop!
|
189
|
+
# ... which does absolutely nothing.
|
190
|
+
#
|
191
|
+
def slop!
|
192
|
+
unless decorators(XML::Node).include? Nokogiri::Decorators::Slop
|
193
|
+
decorators(XML::Node) << Nokogiri::Decorators::Slop
|
194
|
+
decorate!
|
195
|
+
end
|
196
|
+
|
197
|
+
self
|
198
|
+
end
|
199
|
+
|
200
|
+
##
|
201
|
+
# Apply any decorators to +node+
|
202
|
+
def decorate node
|
203
|
+
return unless @decorators
|
204
|
+
@decorators.each { |klass,list|
|
205
|
+
next unless node.is_a?(klass)
|
206
|
+
list.each { |moodule| node.extend(moodule) }
|
207
|
+
}
|
208
|
+
end
|
209
|
+
|
210
|
+
alias :to_xml :serialize
|
211
|
+
alias :clone :dup
|
212
|
+
|
213
|
+
# Get the hash of namespaces on the root Nokogiri::XML::Node
|
214
|
+
def namespaces
|
215
|
+
root ? root.namespaces : {}
|
216
|
+
end
|
217
|
+
|
218
|
+
##
|
219
|
+
# Create a Nokogiri::XML::DocumentFragment from +tags+
|
220
|
+
# Returns an empty fragment if +tags+ is nil.
|
221
|
+
def fragment tags = nil
|
222
|
+
DocumentFragment.new(self, tags, self.root)
|
223
|
+
end
|
224
|
+
|
225
|
+
undef_method :swap, :parent, :namespace, :default_namespace=
|
226
|
+
undef_method :add_namespace_definition, :attributes
|
227
|
+
undef_method :namespace_definitions, :line, :add_namespace
|
228
|
+
|
229
|
+
def add_child node_or_tags
|
230
|
+
raise "Document already has a root node" if root
|
231
|
+
node_or_tags = coerce(node_or_tags)
|
232
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
233
|
+
raise "Document cannot have multiple root nodes" if node_or_tags.size > 1
|
234
|
+
super(node_or_tags.first)
|
235
|
+
else
|
236
|
+
super
|
237
|
+
end
|
238
|
+
end
|
239
|
+
alias :<< :add_child
|
240
|
+
|
241
|
+
##
|
242
|
+
# +JRuby+
|
243
|
+
# Wraps Java's org.w3c.dom.document and returns Nokogiri::XML::Document
|
244
|
+
def self.wrap document
|
245
|
+
raise "JRuby only method" unless Nokogiri.jruby?
|
246
|
+
return wrapJavaDocument(document)
|
247
|
+
end
|
248
|
+
|
249
|
+
##
|
250
|
+
# +JRuby+
|
251
|
+
# Returns Java's org.w3c.dom.document of this Document.
|
252
|
+
def to_java
|
253
|
+
raise "JRuby only method" unless Nokogiri.jruby?
|
254
|
+
return toJavaDocument()
|
255
|
+
end
|
256
|
+
|
257
|
+
private
|
258
|
+
def implied_xpath_context
|
259
|
+
"/"
|
260
|
+
end
|
261
|
+
|
262
|
+
def inspect_attributes
|
263
|
+
[:name, :children]
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
module XML
|
3
|
+
class DocumentFragment < Nokogiri::XML::Node
|
4
|
+
##
|
5
|
+
# Create a new DocumentFragment from +tags+.
|
6
|
+
#
|
7
|
+
# If +ctx+ is present, it is used as a context node for the
|
8
|
+
# subtree created, e.g., namespaces will be resolved relative
|
9
|
+
# to +ctx+.
|
10
|
+
def initialize document, tags = nil, ctx = nil
|
11
|
+
return self unless tags
|
12
|
+
|
13
|
+
children = if ctx
|
14
|
+
# Fix for issue#490
|
15
|
+
if Nokogiri.jruby?
|
16
|
+
ctx.parse("<root>#{tags}</root>").xpath("/root/node()")
|
17
|
+
else
|
18
|
+
ctx.parse(tags)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
XML::Document.parse("<root>#{tags}</root>") \
|
22
|
+
.xpath("/root/node()")
|
23
|
+
end
|
24
|
+
children.each { |child| child.parent = self }
|
25
|
+
end
|
26
|
+
|
27
|
+
###
|
28
|
+
# return the name for DocumentFragment
|
29
|
+
def name
|
30
|
+
'#document-fragment'
|
31
|
+
end
|
32
|
+
|
33
|
+
###
|
34
|
+
# Convert this DocumentFragment to a string
|
35
|
+
def to_s
|
36
|
+
children.to_s
|
37
|
+
end
|
38
|
+
|
39
|
+
###
|
40
|
+
# Convert this DocumentFragment to html
|
41
|
+
# See Nokogiri::XML::NodeSet#to_html
|
42
|
+
def to_html *args
|
43
|
+
if Nokogiri.jruby?
|
44
|
+
options = args.first.is_a?(Hash) ? args.shift : {}
|
45
|
+
if !options[:save_with]
|
46
|
+
options[:save_with] = Node::SaveOptions::NO_DECLARATION | Node::SaveOptions::NO_EMPTY_TAGS | Node::SaveOptions::AS_HTML
|
47
|
+
end
|
48
|
+
args.insert(0, options)
|
49
|
+
end
|
50
|
+
children.to_html(*args)
|
51
|
+
end
|
52
|
+
|
53
|
+
###
|
54
|
+
# Convert this DocumentFragment to xhtml
|
55
|
+
# See Nokogiri::XML::NodeSet#to_xhtml
|
56
|
+
def to_xhtml *args
|
57
|
+
if Nokogiri.jruby?
|
58
|
+
options = args.first.is_a?(Hash) ? args.shift : {}
|
59
|
+
if !options[:save_with]
|
60
|
+
options[:save_with] = Node::SaveOptions::NO_DECLARATION | Node::SaveOptions::NO_EMPTY_TAGS | Node::SaveOptions::AS_XHTML
|
61
|
+
end
|
62
|
+
args.insert(0, options)
|
63
|
+
end
|
64
|
+
children.to_xhtml(*args)
|
65
|
+
end
|
66
|
+
|
67
|
+
###
|
68
|
+
# Convert this DocumentFragment to xml
|
69
|
+
# See Nokogiri::XML::NodeSet#to_xml
|
70
|
+
def to_xml *args
|
71
|
+
children.to_xml(*args)
|
72
|
+
end
|
73
|
+
|
74
|
+
###
|
75
|
+
# Search this fragment. See Nokogiri::XML::Node#css
|
76
|
+
def css *args
|
77
|
+
if children.any?
|
78
|
+
children.css(*args)
|
79
|
+
else
|
80
|
+
NodeSet.new(document)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
alias :serialize :to_s
|
85
|
+
|
86
|
+
class << self
|
87
|
+
####
|
88
|
+
# Create a Nokogiri::XML::DocumentFragment from +tags+
|
89
|
+
def parse tags
|
90
|
+
self.new(XML::Document.new, tags)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def coerce data
|
97
|
+
return super unless String === data
|
98
|
+
|
99
|
+
document.fragment(data).children
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
module XML
|
3
|
+
class DTD < Nokogiri::XML::Node
|
4
|
+
undef_method :attribute_nodes
|
5
|
+
undef_method :values
|
6
|
+
undef_method :content
|
7
|
+
undef_method :namespace
|
8
|
+
undef_method :namespace_definitions
|
9
|
+
undef_method :line if method_defined?(:line)
|
10
|
+
|
11
|
+
def keys
|
12
|
+
attributes.keys
|
13
|
+
end
|
14
|
+
|
15
|
+
def each &block
|
16
|
+
attributes.each { |key, value|
|
17
|
+
block.call([key, value])
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
module XML
|
3
|
+
###
|
4
|
+
# Represents the allowed content in an Element Declaration inside a DTD:
|
5
|
+
#
|
6
|
+
# <?xml version="1.0"?><?TEST-STYLE PIDATA?>
|
7
|
+
# <!DOCTYPE staff SYSTEM "staff.dtd" [
|
8
|
+
# <!ELEMENT div1 (head, (p | list | note)*, div2*)>
|
9
|
+
# ]>
|
10
|
+
# </root>
|
11
|
+
#
|
12
|
+
# ElementContent represents the tree inside the <!ELEMENT> tag shown above
|
13
|
+
# that lists the possible content for the div1 tag.
|
14
|
+
class ElementContent
|
15
|
+
# Possible definitions of type
|
16
|
+
PCDATA = 1
|
17
|
+
ELEMENT = 2
|
18
|
+
SEQ = 3
|
19
|
+
OR = 4
|
20
|
+
|
21
|
+
# Possible content occurrences
|
22
|
+
ONCE = 1
|
23
|
+
OPT = 2
|
24
|
+
MULT = 3
|
25
|
+
PLUS = 4
|
26
|
+
|
27
|
+
attr_reader :document
|
28
|
+
|
29
|
+
###
|
30
|
+
# Get the children of this ElementContent node
|
31
|
+
def children
|
32
|
+
[c1, c2].compact
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|