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,122 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class TestEntityDecl < Nokogiri::TestCase
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@xml = Nokogiri::XML(<<-eoxml)
|
9
|
+
<?xml version="1.0"?><?TEST-STYLE PIDATA?>
|
10
|
+
<!DOCTYPE staff SYSTEM "staff.dtd" [
|
11
|
+
<!ENTITY ent1 "es">
|
12
|
+
<!ENTITY nocontent "">
|
13
|
+
]>
|
14
|
+
<root />
|
15
|
+
eoxml
|
16
|
+
|
17
|
+
@entities = @xml.internal_subset.children
|
18
|
+
@entity_decl = @entities.first
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_constants
|
22
|
+
assert_equal 1, EntityDecl::INTERNAL_GENERAL
|
23
|
+
assert_equal 2, EntityDecl::EXTERNAL_GENERAL_PARSED
|
24
|
+
assert_equal 3, EntityDecl::EXTERNAL_GENERAL_UNPARSED
|
25
|
+
assert_equal 4, EntityDecl::INTERNAL_PARAMETER
|
26
|
+
assert_equal 5, EntityDecl::EXTERNAL_PARAMETER
|
27
|
+
assert_equal 6, EntityDecl::INTERNAL_PREDEFINED
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_create_typed_entity
|
31
|
+
entity = @xml.create_entity(
|
32
|
+
'foo', EntityDecl::INTERNAL_GENERAL, nil, nil, nil
|
33
|
+
)
|
34
|
+
assert_equal EntityDecl::INTERNAL_GENERAL, entity.entity_type
|
35
|
+
assert_equal 'foo', entity.name
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_new
|
39
|
+
entity = Nokogiri::XML::EntityDecl.new(
|
40
|
+
'foo', @xml, EntityDecl::INTERNAL_GENERAL, nil, nil, nil
|
41
|
+
)
|
42
|
+
assert_equal EntityDecl::INTERNAL_GENERAL, entity.entity_type
|
43
|
+
assert_equal 'foo', entity.name
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_create_default_args
|
47
|
+
entity = @xml.create_entity('foo')
|
48
|
+
assert_equal EntityDecl::INTERNAL_GENERAL, entity.entity_type
|
49
|
+
assert_equal 'foo', entity.name
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_external_id
|
53
|
+
assert_nil @entity_decl.external_id
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_system_id
|
57
|
+
assert_nil @entity_decl.system_id
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_entity_type
|
61
|
+
assert_equal 1, @entity_decl.entity_type
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_original_content
|
65
|
+
assert_equal "es", @entity_decl.original_content
|
66
|
+
if Nokogiri.jruby?
|
67
|
+
assert_nil @entities[1].original_content
|
68
|
+
else
|
69
|
+
assert_equal "", @entities[1].original_content
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_content
|
74
|
+
assert_equal "es", @entity_decl.content
|
75
|
+
if Nokogiri.jruby?
|
76
|
+
assert_nil @entities[1].content
|
77
|
+
else
|
78
|
+
assert_equal "", @entities[1].content
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_type
|
83
|
+
assert_equal 17, @entities.first.type
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_class
|
87
|
+
assert_instance_of Nokogiri::XML::EntityDecl, @entities.first
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_attributes
|
91
|
+
assert_raise NoMethodError do
|
92
|
+
@entity_decl.attributes
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_namespace
|
97
|
+
assert_raise NoMethodError do
|
98
|
+
@entity_decl.namespace
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_namespace_definitions
|
103
|
+
assert_raise NoMethodError do
|
104
|
+
@entity_decl.namespace_definitions
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_line
|
109
|
+
assert_raise NoMethodError do
|
110
|
+
@entity_decl.line
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_inspect
|
115
|
+
assert_equal(
|
116
|
+
"#<#{@entity_decl.class.name}:#{sprintf("0x%x", @entity_decl.object_id)} #{@entity_decl.to_s.inspect}>",
|
117
|
+
@entity_decl.inspect
|
118
|
+
)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,235 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class TestEntityReference < Nokogiri::TestCase
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@xml = Nokogiri::XML(File.open(XML_FILE), XML_FILE)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_new
|
12
|
+
assert ref = EntityReference.new(@xml, 'ent4')
|
13
|
+
assert_instance_of EntityReference, ref
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_many_references
|
17
|
+
100.times { EntityReference.new(@xml, 'foo') }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module Common
|
22
|
+
PATH = 'test/files/test_document_url/'
|
23
|
+
|
24
|
+
attr_accessor :path, :parser
|
25
|
+
|
26
|
+
def xml_document
|
27
|
+
File.join path, 'document.xml'
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.included base
|
31
|
+
def base.test_relative_and_absolute_path method_name, &block
|
32
|
+
test_relative_path method_name, &block
|
33
|
+
test_absolute_path method_name, &block
|
34
|
+
end
|
35
|
+
|
36
|
+
def base.test_absolute_path method_name, &block
|
37
|
+
define_method "#{method_name}_with_absolute_path" do
|
38
|
+
self.path = "#{File.expand_path PATH}/"
|
39
|
+
instance_eval(&block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def base.test_relative_path method_name, &block
|
44
|
+
define_method method_name do
|
45
|
+
self.path = PATH
|
46
|
+
instance_eval(&block)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class TestDOMEntityReference < Nokogiri::TestCase
|
53
|
+
include Common
|
54
|
+
|
55
|
+
def setup
|
56
|
+
super
|
57
|
+
@parser = Nokogiri::XML::Document
|
58
|
+
end
|
59
|
+
|
60
|
+
test_relative_and_absolute_path :test_dom_entity_reference_with_dtdloda do
|
61
|
+
# Make sure that we can parse entity references and include them in the document
|
62
|
+
html = File.read xml_document
|
63
|
+
doc = @parser.parse html, path do |cfg|
|
64
|
+
cfg.default_xml
|
65
|
+
cfg.dtdload
|
66
|
+
cfg.noent
|
67
|
+
end
|
68
|
+
assert_equal [], doc.errors
|
69
|
+
assert_equal "foobar", doc.xpath('//blah').text
|
70
|
+
end
|
71
|
+
|
72
|
+
test_relative_and_absolute_path :test_dom_entity_reference_with_dtdvalid do
|
73
|
+
# Make sure that we can parse entity references and include them in the document
|
74
|
+
html = File.read xml_document
|
75
|
+
doc = @parser.parse html, path do |cfg|
|
76
|
+
cfg.default_xml
|
77
|
+
cfg.dtdvalid
|
78
|
+
cfg.noent
|
79
|
+
end
|
80
|
+
assert_equal [], doc.errors
|
81
|
+
assert_equal "foobar", doc.xpath('//blah').text
|
82
|
+
end
|
83
|
+
|
84
|
+
test_absolute_path :test_dom_dtd_loading_with_absolute_path do
|
85
|
+
# Make sure that we can parse entity references and include them in the document
|
86
|
+
html = %Q[<?xml version="1.0" encoding="UTF-8" ?>
|
87
|
+
<!DOCTYPE document SYSTEM "#{path}/document.dtd">
|
88
|
+
<document>
|
89
|
+
<body>&bar;</body>
|
90
|
+
</document>
|
91
|
+
]
|
92
|
+
doc = @parser.parse html, xml_document do |cfg|
|
93
|
+
cfg.default_xml
|
94
|
+
cfg.dtdvalid
|
95
|
+
cfg.noent
|
96
|
+
end
|
97
|
+
assert_equal [], doc.errors
|
98
|
+
assert_equal "foobar", doc.xpath('//blah').text
|
99
|
+
end
|
100
|
+
|
101
|
+
test_relative_and_absolute_path :test_dom_entity_reference_with_io do
|
102
|
+
# Make sure that we can parse entity references and include them in the document
|
103
|
+
html = File.open xml_document
|
104
|
+
doc = @parser.parse html, nil do |cfg|
|
105
|
+
cfg.default_xml
|
106
|
+
cfg.dtdload
|
107
|
+
cfg.noent
|
108
|
+
end
|
109
|
+
assert_equal [], doc.errors
|
110
|
+
assert_equal "foobar", doc.xpath('//blah').text
|
111
|
+
end
|
112
|
+
|
113
|
+
test_relative_and_absolute_path :test_dom_entity_reference_without_noent do
|
114
|
+
# Make sure that we don't include entity references unless NOENT is set to true
|
115
|
+
html = File.read xml_document
|
116
|
+
doc = @parser.parse html, path do |cfg|
|
117
|
+
cfg.default_xml
|
118
|
+
cfg.dtdload
|
119
|
+
end
|
120
|
+
assert_equal [], doc.errors
|
121
|
+
assert_kind_of Nokogiri::XML::EntityReference, doc.xpath('//body').first.children.first
|
122
|
+
end
|
123
|
+
|
124
|
+
test_relative_and_absolute_path :test_dom_entity_reference_without_dtdload do
|
125
|
+
# Make sure that we don't include entity references unless NOENT is set to true
|
126
|
+
html = File.read xml_document
|
127
|
+
doc = @parser.parse html, path do |cfg|
|
128
|
+
cfg.default_xml
|
129
|
+
end
|
130
|
+
assert_kind_of Nokogiri::XML::EntityReference, doc.xpath('//body').first.children.first
|
131
|
+
if Nokogiri.uses_libxml?
|
132
|
+
assert_equal ["Entity 'bar' not defined"], doc.errors.map(&:to_s)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
test_relative_and_absolute_path :test_document_dtd_loading_with_nonet do
|
137
|
+
# Make sure that we don't include remote entities unless NOENT is set to true
|
138
|
+
html = %Q[<?xml version="1.0" encoding="UTF-8" ?>
|
139
|
+
<!DOCTYPE document SYSTEM "http://foo.bar.com/">
|
140
|
+
<document>
|
141
|
+
<body>&bar;</body>
|
142
|
+
</document>
|
143
|
+
]
|
144
|
+
doc = @parser.parse html, path do |cfg|
|
145
|
+
cfg.default_xml
|
146
|
+
cfg.dtdload
|
147
|
+
end
|
148
|
+
assert_kind_of Nokogiri::XML::EntityReference, doc.xpath('//body').first.children.first
|
149
|
+
if Nokogiri.uses_libxml?
|
150
|
+
assert_equal ["Attempt to load network entity http://foo.bar.com/", "Entity 'bar' not defined"], doc.errors.map(&:to_s)
|
151
|
+
else
|
152
|
+
assert_equal ["Attempt to load network entity http://foo.bar.com/"], doc.errors.map(&:to_s)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
# TODO: can we retreive a resource pointing to localhost when NONET is set to true ?
|
156
|
+
end
|
157
|
+
|
158
|
+
class TestSaxEntityReference < Nokogiri::SAX::TestCase
|
159
|
+
include Common
|
160
|
+
|
161
|
+
def setup
|
162
|
+
super
|
163
|
+
@parser = XML::SAX::Parser.new(Doc.new) do |ctx|
|
164
|
+
ctx.replace_entities = true
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
test_relative_and_absolute_path :test_sax_entity_reference do
|
169
|
+
# Make sure that we can parse entity references and include them in the document
|
170
|
+
html = File.read xml_document
|
171
|
+
@parser.parse html
|
172
|
+
refute_nil @parser.document.errors
|
173
|
+
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
|
174
|
+
end
|
175
|
+
|
176
|
+
test_relative_and_absolute_path :test_more_sax_entity_reference do
|
177
|
+
# Make sure that we don't include entity references unless NOENT is set to true
|
178
|
+
html = %Q[<?xml version="1.0" encoding="UTF-8" ?>
|
179
|
+
<!DOCTYPE document SYSTEM "http://foo.bar.com/">
|
180
|
+
<document>
|
181
|
+
<body>&bar;</body>
|
182
|
+
</document>
|
183
|
+
]
|
184
|
+
@parser.parse html
|
185
|
+
refute_nil @parser.document.errors
|
186
|
+
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
class TestReaderEntityReference < Nokogiri::TestCase
|
191
|
+
include Common
|
192
|
+
|
193
|
+
def setup
|
194
|
+
super
|
195
|
+
end
|
196
|
+
|
197
|
+
test_relative_and_absolute_path :test_reader_entity_reference do
|
198
|
+
# Make sure that we can parse entity references and include them in the document
|
199
|
+
html = File.read xml_document
|
200
|
+
reader = Nokogiri::XML::Reader html, path do |cfg|
|
201
|
+
cfg.default_xml
|
202
|
+
cfg.dtdload
|
203
|
+
cfg.noent
|
204
|
+
end
|
205
|
+
nodes = []
|
206
|
+
reader.each { |n| nodes << n.value }
|
207
|
+
assert_equal ['foobar'], nodes.compact.map(&:strip).reject(&:empty?)
|
208
|
+
end
|
209
|
+
|
210
|
+
test_relative_and_absolute_path :test_reader_entity_reference_without_noent do
|
211
|
+
# Make sure that we can parse entity references and include them in the document
|
212
|
+
html = File.read xml_document
|
213
|
+
reader = Nokogiri::XML::Reader html, path do |cfg|
|
214
|
+
cfg.default_xml
|
215
|
+
cfg.dtdload
|
216
|
+
end
|
217
|
+
nodes = []
|
218
|
+
reader.each { |n| nodes << n.value }
|
219
|
+
assert_equal [], nodes.compact.map(&:strip).reject(&:empty?)
|
220
|
+
end
|
221
|
+
|
222
|
+
test_relative_and_absolute_path :test_reader_entity_reference_without_dtdload do
|
223
|
+
# Make sure that we can parse entity references and include them in the document
|
224
|
+
html = File.read xml_document
|
225
|
+
assert_raises(Nokogiri::XML::SyntaxError) do
|
226
|
+
reader = Nokogiri::XML::Reader html, path do |cfg|
|
227
|
+
cfg.default_xml
|
228
|
+
end
|
229
|
+
nodes = []
|
230
|
+
reader.each { |n| nodes << n.value }
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class TestNamespace < Nokogiri::TestCase
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@xml = Nokogiri::XML <<-eoxml
|
9
|
+
<root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
|
10
|
+
<awesome/>
|
11
|
+
</root>
|
12
|
+
eoxml
|
13
|
+
end
|
14
|
+
|
15
|
+
if Nokogiri.uses_libxml?
|
16
|
+
def test_namespace_is_in_node_cache
|
17
|
+
node = @xml.root.namespace
|
18
|
+
assert @xml.instance_variable_get(:@node_cache).include?(node)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_built_nodes_keep_namespace_decls
|
23
|
+
doc = Document.new
|
24
|
+
e = Node.new 'element', doc
|
25
|
+
c = Node.new 'child', doc
|
26
|
+
c.default_namespace = 'woop:de:doo'
|
27
|
+
|
28
|
+
assert c.namespace, 'has a namespace'
|
29
|
+
e.add_child c
|
30
|
+
assert c.namespace, 'has a namespace'
|
31
|
+
|
32
|
+
doc.add_child e
|
33
|
+
assert c.namespace, 'has a namespace'
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_inspect
|
37
|
+
ns = @xml.root.namespace
|
38
|
+
assert_equal "#<#{ns.class.name}:#{sprintf("0x%x", ns.object_id)} href=#{ns.href.inspect}>", ns.inspect
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_namespace_node_prefix
|
42
|
+
namespaces = @xml.root.namespace_definitions
|
43
|
+
assert_equal [nil, 'foo'], namespaces.map { |x| x.prefix }
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_namespace_node_href
|
47
|
+
namespaces = @xml.root.namespace_definitions
|
48
|
+
assert_equal [
|
49
|
+
'http://tenderlovemaking.com/',
|
50
|
+
'bar'
|
51
|
+
], namespaces.map { |x| x.href }
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_equality
|
55
|
+
namespaces = @xml.root.namespace_definitions
|
56
|
+
assert_equal namespaces, @xml.root.namespace_definitions
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_add_definition
|
60
|
+
@xml.root.add_namespace_definition('baz', 'bar')
|
61
|
+
assert_equal 3, @xml.root.namespace_definitions.length
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_add_definition_return
|
65
|
+
ns = @xml.root.add_namespace_definition('baz', 'bar')
|
66
|
+
assert_equal 'baz', ns.prefix
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_remove_entity_namespace
|
70
|
+
s = %q{<?xml version='1.0'?><!DOCTYPE schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd" [<!ENTITY % p ''>]>}
|
71
|
+
Nokogiri::XML(s).remove_namespaces!
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,1029 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module Nokogiri
|
6
|
+
module XML
|
7
|
+
class TestNode < Nokogiri::TestCase
|
8
|
+
def setup
|
9
|
+
super
|
10
|
+
@xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_first_element_child
|
14
|
+
node = @xml.root.first_element_child
|
15
|
+
assert_equal 'employee', node.name
|
16
|
+
assert node.element?, 'node is an element'
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_element_children
|
20
|
+
nodes = @xml.root.element_children
|
21
|
+
assert_equal @xml.root.first_element_child, nodes.first
|
22
|
+
assert nodes.all? { |node| node.element? }, 'all nodes are elements'
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_last_element_child
|
26
|
+
nodes = @xml.root.element_children
|
27
|
+
assert_equal nodes.last, @xml.root.element_children.last
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_bad_xpath
|
31
|
+
bad_xpath = '//foo['
|
32
|
+
|
33
|
+
begin
|
34
|
+
@xml.xpath(bad_xpath)
|
35
|
+
rescue Nokogiri::XML::XPath::SyntaxError => e
|
36
|
+
assert_match(bad_xpath, e.to_s)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_namespace_type_error
|
41
|
+
assert_raises(TypeError) do
|
42
|
+
@xml.root.namespace = Object.new
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_remove_namespace
|
47
|
+
@xml = Nokogiri::XML('<r xmlns="v"><s /></r>')
|
48
|
+
tag = @xml.at('s')
|
49
|
+
assert tag.namespace
|
50
|
+
tag.namespace = nil
|
51
|
+
assert_nil tag.namespace
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_parse_needs_doc
|
55
|
+
list = @xml.root.parse('fooooooo <hello />')
|
56
|
+
assert_equal 1, list.css('hello').length
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_parse
|
60
|
+
list = @xml.root.parse('fooooooo <hello />')
|
61
|
+
assert_equal 2, list.length
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_parse_with_block
|
65
|
+
called = false
|
66
|
+
list = @xml.root.parse('<hello />') { |cfg|
|
67
|
+
called = true
|
68
|
+
assert_instance_of Nokogiri::XML::ParseOptions, cfg
|
69
|
+
}
|
70
|
+
assert called, 'config block called'
|
71
|
+
assert_equal 1, list.length
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_parse_with_io
|
75
|
+
list = @xml.root.parse(StringIO.new('<hello />'))
|
76
|
+
assert_equal 1, list.length
|
77
|
+
assert_equal 'hello', list.first.name
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_parse_with_empty_string
|
81
|
+
list = @xml.root.parse('')
|
82
|
+
assert_equal 0, list.length
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_parse_config_option
|
86
|
+
node = @xml.root
|
87
|
+
options = nil
|
88
|
+
node.parse("<item></item>") do |config|
|
89
|
+
options = config
|
90
|
+
end
|
91
|
+
assert_equal Nokogiri::XML::ParseOptions::DEFAULT_XML, options.to_i
|
92
|
+
end
|
93
|
+
|
94
|
+
# descriptive, not prescriptive.
|
95
|
+
def test_parse_invalid_html_markup_results_in_empty_nodeset
|
96
|
+
doc = Nokogiri::HTML("<html></html>")
|
97
|
+
nodeset = doc.root.parse "<div><div>a</div><snippet>b</snippet></div>"
|
98
|
+
assert_equal 1, doc.errors.length # "Tag snippet invalid"
|
99
|
+
assert_equal 1, nodeset.length
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_node_context_parsing_of_malformed_html_fragment_with_recover_is_corrected
|
103
|
+
doc = HTML.parse "<html><body><div></div></body></html>"
|
104
|
+
context_node = doc.at_css "div"
|
105
|
+
nodeset = context_node.parse("<div </div>") do |options|
|
106
|
+
options.recover
|
107
|
+
end
|
108
|
+
assert_equal "<div></div>", nodeset.to_s
|
109
|
+
assert_equal 1, doc.errors.length
|
110
|
+
assert_equal 1, nodeset.length
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_node_context_parsing_of_malformed_html_fragment_without_recover_is_not_corrected
|
114
|
+
doc = HTML.parse "<html><body><div></div></body></html>"
|
115
|
+
context_node = doc.at_css "div"
|
116
|
+
nodeset = context_node.parse("<div </div>") do |options|
|
117
|
+
options.strict
|
118
|
+
end
|
119
|
+
assert_equal 1, doc.errors.length
|
120
|
+
assert_equal 0, nodeset.length
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_parse_error_list
|
124
|
+
error_count = @xml.errors.length
|
125
|
+
@xml.root.parse('<hello>')
|
126
|
+
assert(error_count < @xml.errors.length, "errors should have increased")
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_parse_error_on_fragment_with_empty_document
|
130
|
+
doc = Document.new
|
131
|
+
fragment = DocumentFragment.new(doc, '<foo><bar/></foo>')
|
132
|
+
node = fragment%'bar'
|
133
|
+
node.parse('<baz><</baz>')
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_subclass_dup
|
137
|
+
subclass = Class.new(Nokogiri::XML::Node)
|
138
|
+
node = subclass.new('foo', @xml).dup
|
139
|
+
assert_instance_of subclass, node
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_gt_string_arg
|
143
|
+
node = @xml.at('employee')
|
144
|
+
nodes = (node > 'name')
|
145
|
+
assert_equal 1, nodes.length
|
146
|
+
assert_equal node, nodes.first.parent
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_next_element_when_next_sibling_is_element_should_return_next_sibling
|
150
|
+
doc = Nokogiri::XML "<root><foo /><quux /></root>"
|
151
|
+
node = doc.at_css("foo")
|
152
|
+
next_element = node.next_element
|
153
|
+
assert next_element.element?
|
154
|
+
assert_equal doc.at_css("quux"), next_element
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_next_element_when_there_is_no_next_sibling_should_return_nil
|
158
|
+
doc = Nokogiri::XML "<root><foo /><quux /></root>"
|
159
|
+
assert_nil doc.at_css("quux").next_element
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_next_element_when_next_sibling_is_not_an_element_should_return_closest_next_element_sibling
|
163
|
+
doc = Nokogiri::XML "<root><foo />bar<quux /></root>"
|
164
|
+
node = doc.at_css("foo")
|
165
|
+
next_element = node.next_element
|
166
|
+
assert next_element.element?
|
167
|
+
assert_equal doc.at_css("quux"), next_element
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_next_element_when_next_sibling_is_not_an_element_and_no_following_element_should_return_nil
|
171
|
+
doc = Nokogiri::XML "<root><foo />bar</root>"
|
172
|
+
node = doc.at_css("foo")
|
173
|
+
next_element = node.next_element
|
174
|
+
assert_nil next_element
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_previous_element_when_previous_sibling_is_element_should_return_previous_sibling
|
178
|
+
doc = Nokogiri::XML "<root><foo /><quux /></root>"
|
179
|
+
node = doc.at_css("quux")
|
180
|
+
previous_element = node.previous_element
|
181
|
+
assert previous_element.element?
|
182
|
+
assert_equal doc.at_css("foo"), previous_element
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_previous_element_when_there_is_no_previous_sibling_should_return_nil
|
186
|
+
doc = Nokogiri::XML "<root><foo /><quux /></root>"
|
187
|
+
assert_nil doc.at_css("foo").previous_element
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_previous_element_when_previous_sibling_is_not_an_element_should_return_closest_previous_element_sibling
|
191
|
+
doc = Nokogiri::XML "<root><foo />bar<quux /></root>"
|
192
|
+
node = doc.at_css("quux")
|
193
|
+
previous_element = node.previous_element
|
194
|
+
assert previous_element.element?
|
195
|
+
assert_equal doc.at_css("foo"), previous_element
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_previous_element_when_previous_sibling_is_not_an_element_and_no_following_element_should_return_nil
|
199
|
+
doc = Nokogiri::XML "<root>foo<bar /></root>"
|
200
|
+
node = doc.at_css("bar")
|
201
|
+
previous_element = node.previous_element
|
202
|
+
assert_nil previous_element
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_element?
|
206
|
+
assert @xml.root.element?, 'is an element'
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_slash_search
|
210
|
+
assert_equal 'EMP0001', (@xml/:staff/:employee/:employeeId).first.text
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_append_with_document
|
214
|
+
assert_raises(ArgumentError) do
|
215
|
+
@xml.root << Nokogiri::XML::Document.new
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_append_with_attr
|
220
|
+
r = Nokogiri.XML('<r a="1" />').root
|
221
|
+
assert_raises(ArgumentError) do
|
222
|
+
r << r.at_xpath('@a')
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_inspect_ns
|
227
|
+
xml = Nokogiri::XML(<<-eoxml) { |c| c.noblanks }
|
228
|
+
<root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
|
229
|
+
<awesome/>
|
230
|
+
</root>
|
231
|
+
eoxml
|
232
|
+
ins = xml.inspect
|
233
|
+
|
234
|
+
xml.traverse do |node|
|
235
|
+
assert_match node.class.name, ins
|
236
|
+
if node.respond_to? :attributes
|
237
|
+
node.attributes.each do |k,v|
|
238
|
+
assert_match k, ins
|
239
|
+
assert_match v, ins
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
if node.respond_to?(:namespace) && node.namespace
|
244
|
+
assert_match node.namespace.class.name, ins
|
245
|
+
assert_match node.namespace.href, ins
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_namespace_definitions_when_some_exist
|
251
|
+
xml = Nokogiri::XML <<-eoxml
|
252
|
+
<root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
|
253
|
+
<awesome/>
|
254
|
+
</root>
|
255
|
+
eoxml
|
256
|
+
namespace_definitions = xml.root.namespace_definitions
|
257
|
+
assert_equal 2, namespace_definitions.length
|
258
|
+
end
|
259
|
+
|
260
|
+
def test_namespace_definitions_when_no_exist
|
261
|
+
xml = Nokogiri::XML <<-eoxml
|
262
|
+
<root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
|
263
|
+
<awesome/>
|
264
|
+
</root>
|
265
|
+
eoxml
|
266
|
+
namespace_definitions = xml.at_xpath('//xmlns:awesome').namespace_definitions
|
267
|
+
assert_equal 0, namespace_definitions.length
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_namespace_goes_to_children
|
271
|
+
fruits = Nokogiri::XML(<<-eoxml)
|
272
|
+
<Fruit xmlns='www.fruits.org'>
|
273
|
+
</Fruit>
|
274
|
+
eoxml
|
275
|
+
apple = Nokogiri::XML::Node.new('Apple', fruits)
|
276
|
+
orange = Nokogiri::XML::Node.new('Orange', fruits)
|
277
|
+
apple << orange
|
278
|
+
fruits.root << apple
|
279
|
+
assert fruits.at('//fruit:Orange',{'fruit'=>'www.fruits.org'})
|
280
|
+
assert fruits.at('//fruit:Apple',{'fruit'=>'www.fruits.org'})
|
281
|
+
end
|
282
|
+
|
283
|
+
def test_description
|
284
|
+
assert_nil @xml.at('employee').description
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_spaceship
|
288
|
+
nodes = @xml.xpath('//employee')
|
289
|
+
assert_equal(-1, (nodes.first <=> nodes.last))
|
290
|
+
list = [nodes.first, nodes.last].sort
|
291
|
+
assert_equal nodes.first, list.first
|
292
|
+
assert_equal nodes.last, list.last
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_incorrect_spaceship
|
296
|
+
nodes = @xml.xpath('//employee')
|
297
|
+
assert_nil(nodes.first <=> 'asdf')
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_document_compare
|
301
|
+
nodes = @xml.xpath('//employee')
|
302
|
+
assert_equal(-1, (nodes.first <=> @xml))
|
303
|
+
end
|
304
|
+
|
305
|
+
def test_different_document_compare
|
306
|
+
nodes = @xml.xpath('//employee')
|
307
|
+
doc = Nokogiri::XML('<a><b/></a>')
|
308
|
+
b = doc.at('b')
|
309
|
+
assert_nil(nodes.first <=> b)
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_duplicate_node_removes_namespace
|
313
|
+
fruits = Nokogiri::XML(<<-eoxml)
|
314
|
+
<Fruit xmlns='www.fruits.org'>
|
315
|
+
<Apple></Apple>
|
316
|
+
</Fruit>
|
317
|
+
eoxml
|
318
|
+
apple = fruits.root.xpath('fruit:Apple', {'fruit'=>'www.fruits.org'})[0]
|
319
|
+
new_apple = apple.dup
|
320
|
+
fruits.root << new_apple
|
321
|
+
assert_equal 2, fruits.xpath('//xmlns:Apple').length
|
322
|
+
assert_equal 1, fruits.to_xml.scan('www.fruits.org').length
|
323
|
+
end
|
324
|
+
|
325
|
+
[:clone, :dup].each do |symbol|
|
326
|
+
define_method "test_#{symbol}" do
|
327
|
+
node = @xml.at('//employee')
|
328
|
+
other = node.send(symbol)
|
329
|
+
assert_equal "employee", other.name
|
330
|
+
assert_nil other.parent
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
def test_fragment_creates_elements
|
335
|
+
apple = @xml.fragment('<Apple/>')
|
336
|
+
apple.children.each do |child|
|
337
|
+
assert_equal Nokogiri::XML::Node::ELEMENT_NODE, child.type
|
338
|
+
assert_instance_of Nokogiri::XML::Element, child
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_node_added_to_root_should_get_namespace
|
343
|
+
fruits = Nokogiri::XML(<<-eoxml)
|
344
|
+
<Fruit xmlns='http://www.fruits.org'>
|
345
|
+
</Fruit>
|
346
|
+
eoxml
|
347
|
+
apple = fruits.fragment('<Apple/>')
|
348
|
+
fruits.root << apple
|
349
|
+
assert_equal 1, fruits.xpath('//xmlns:Apple').length
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_new_node_can_have_ancestors
|
353
|
+
xml = Nokogiri::XML('<root>text</root>')
|
354
|
+
item = Nokogiri::XML::Element.new('item', xml)
|
355
|
+
assert_equal 0, item.ancestors.length
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_children
|
359
|
+
doc = Nokogiri::XML(<<-eoxml)
|
360
|
+
<root>#{'<a/>' * 9 }</root>
|
361
|
+
eoxml
|
362
|
+
assert_equal 9, doc.root.children.length
|
363
|
+
assert_equal 9, doc.root.children.to_a.length
|
364
|
+
|
365
|
+
doc = Nokogiri::XML(<<-eoxml)
|
366
|
+
<root>#{'<a/>' * 15 }</root>
|
367
|
+
eoxml
|
368
|
+
assert_equal 15, doc.root.children.length
|
369
|
+
assert_equal 15, doc.root.children.to_a.length
|
370
|
+
end
|
371
|
+
|
372
|
+
def test_add_namespace
|
373
|
+
node = @xml.at('address')
|
374
|
+
node.add_namespace('foo', 'http://tenderlovemaking.com')
|
375
|
+
assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_add_namespace_twice
|
379
|
+
node = @xml.at('address')
|
380
|
+
ns = node.add_namespace('foo', 'http://tenderlovemaking.com')
|
381
|
+
ns2 = node.add_namespace('foo', 'http://tenderlovemaking.com')
|
382
|
+
assert_equal ns, ns2
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_add_default_ns
|
386
|
+
node = @xml.at('address')
|
387
|
+
node.add_namespace(nil, 'http://tenderlovemaking.com')
|
388
|
+
assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
|
389
|
+
end
|
390
|
+
|
391
|
+
def test_add_multiple_namespaces
|
392
|
+
node = @xml.at('address')
|
393
|
+
|
394
|
+
node.add_namespace(nil, 'http://tenderlovemaking.com')
|
395
|
+
assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
|
396
|
+
|
397
|
+
node.add_namespace('foo', 'http://tenderlovemaking.com')
|
398
|
+
assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
|
399
|
+
end
|
400
|
+
|
401
|
+
def test_default_namespace=
|
402
|
+
node = @xml.at('address')
|
403
|
+
node.default_namespace = 'http://tenderlovemaking.com'
|
404
|
+
assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_namespace=
|
408
|
+
node = @xml.at('address')
|
409
|
+
assert_nil node.namespace
|
410
|
+
definition = node.add_namespace_definition 'bar', 'http://tlm.com/'
|
411
|
+
|
412
|
+
node.namespace = definition
|
413
|
+
|
414
|
+
assert_equal definition, node.namespace
|
415
|
+
|
416
|
+
assert_equal node, @xml.at('//foo:address', {
|
417
|
+
'foo' => 'http://tlm.com/'
|
418
|
+
})
|
419
|
+
end
|
420
|
+
|
421
|
+
def test_add_namespace_with_nil_associates_node
|
422
|
+
node = @xml.at('address')
|
423
|
+
assert_nil node.namespace
|
424
|
+
definition = node.add_namespace_definition nil, 'http://tlm.com/'
|
425
|
+
assert_equal definition, node.namespace
|
426
|
+
end
|
427
|
+
|
428
|
+
def test_add_namespace_does_not_associate_node
|
429
|
+
node = @xml.at('address')
|
430
|
+
assert_nil node.namespace
|
431
|
+
assert node.add_namespace_definition 'foo', 'http://tlm.com/'
|
432
|
+
assert_nil node.namespace
|
433
|
+
end
|
434
|
+
|
435
|
+
def test_set_namespace_from_different_doc
|
436
|
+
node = @xml.at('address')
|
437
|
+
doc = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
|
438
|
+
decl = doc.root.add_namespace_definition 'foo', 'bar'
|
439
|
+
|
440
|
+
assert_raises(ArgumentError) do
|
441
|
+
node.namespace = decl
|
442
|
+
end
|
443
|
+
end
|
444
|
+
|
445
|
+
def test_set_namespace_must_only_take_a_namespace
|
446
|
+
node = @xml.at('address')
|
447
|
+
assert_raises(TypeError) do
|
448
|
+
node.namespace = node
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
def test_at
|
453
|
+
node = @xml.at('address')
|
454
|
+
assert_equal node, @xml.xpath('//address').first
|
455
|
+
end
|
456
|
+
|
457
|
+
def test_at_self
|
458
|
+
node = @xml.at('address')
|
459
|
+
assert_equal node, node.at('.')
|
460
|
+
end
|
461
|
+
|
462
|
+
def test_at_xpath
|
463
|
+
node = @xml.at_xpath('//address')
|
464
|
+
nodes = @xml.xpath('//address')
|
465
|
+
assert_equal 5, nodes.size
|
466
|
+
assert_equal node, nodes.first
|
467
|
+
end
|
468
|
+
|
469
|
+
def test_at_css
|
470
|
+
node = @xml.at_css('address')
|
471
|
+
nodes = @xml.css('address')
|
472
|
+
assert_equal 5, nodes.size
|
473
|
+
assert_equal node, nodes.first
|
474
|
+
end
|
475
|
+
|
476
|
+
def test_percent
|
477
|
+
node = @xml % ('address')
|
478
|
+
assert_equal node, @xml.xpath('//address').first
|
479
|
+
end
|
480
|
+
|
481
|
+
def test_accept
|
482
|
+
visitor = Class.new {
|
483
|
+
attr_accessor :visited
|
484
|
+
def accept target
|
485
|
+
target.accept(self)
|
486
|
+
end
|
487
|
+
|
488
|
+
def visit node
|
489
|
+
node.children.each { |c| c.accept(self) }
|
490
|
+
@visited = true
|
491
|
+
end
|
492
|
+
}.new
|
493
|
+
visitor.accept(@xml.root)
|
494
|
+
assert visitor.visited
|
495
|
+
end
|
496
|
+
|
497
|
+
def test_write_to
|
498
|
+
io = StringIO.new
|
499
|
+
@xml.write_to io
|
500
|
+
io.rewind
|
501
|
+
assert_equal @xml.to_xml, io.read
|
502
|
+
end
|
503
|
+
|
504
|
+
def test_attribute_with_symbol
|
505
|
+
assert_equal 'Yes', @xml.css('address').first[:domestic]
|
506
|
+
end
|
507
|
+
|
508
|
+
def test_non_existent_attribute_should_return_nil
|
509
|
+
node = @xml.root.first_element_child
|
510
|
+
assert_nil node.attribute('type')
|
511
|
+
end
|
512
|
+
|
513
|
+
def test_write_to_with_block
|
514
|
+
called = false
|
515
|
+
io = StringIO.new
|
516
|
+
conf = nil
|
517
|
+
@xml.write_to io do |config|
|
518
|
+
called = true
|
519
|
+
conf = config
|
520
|
+
config.format.as_html.no_empty_tags
|
521
|
+
end
|
522
|
+
io.rewind
|
523
|
+
assert called
|
524
|
+
string = io.read
|
525
|
+
assert_equal @xml.serialize(nil, conf.options), string
|
526
|
+
assert_equal @xml.serialize(nil, conf), string
|
527
|
+
end
|
528
|
+
|
529
|
+
%w{ xml html xhtml }.each do |type|
|
530
|
+
define_method(:"test_write_#{type}_to") do
|
531
|
+
io = StringIO.new
|
532
|
+
assert @xml.send(:"write_#{type}_to", io)
|
533
|
+
io.rewind
|
534
|
+
assert_match @xml.send(:"to_#{type}"), io.read
|
535
|
+
end
|
536
|
+
end
|
537
|
+
|
538
|
+
def test_serialize_with_block
|
539
|
+
called = false
|
540
|
+
conf = nil
|
541
|
+
string = @xml.serialize do |config|
|
542
|
+
called = true
|
543
|
+
conf = config
|
544
|
+
config.format.as_html.no_empty_tags
|
545
|
+
end
|
546
|
+
assert called
|
547
|
+
assert_equal @xml.serialize(nil, conf.options), string
|
548
|
+
assert_equal @xml.serialize(nil, conf), string
|
549
|
+
end
|
550
|
+
|
551
|
+
def test_hold_refence_to_subnode
|
552
|
+
doc = Nokogiri::XML(<<-eoxml)
|
553
|
+
<root>
|
554
|
+
<a>
|
555
|
+
<b />
|
556
|
+
</a>
|
557
|
+
</root>
|
558
|
+
eoxml
|
559
|
+
assert node_a = doc.css('a').first
|
560
|
+
assert node_b = node_a.css('b').first
|
561
|
+
node_a.unlink
|
562
|
+
assert_equal 'b', node_b.name
|
563
|
+
end
|
564
|
+
|
565
|
+
def test_values
|
566
|
+
assert_equal %w{ Yes Yes }, @xml.xpath('//address')[1].values
|
567
|
+
end
|
568
|
+
|
569
|
+
def test_keys
|
570
|
+
assert_equal %w{ domestic street }, @xml.xpath('//address')[1].keys
|
571
|
+
end
|
572
|
+
|
573
|
+
def test_each
|
574
|
+
attributes = []
|
575
|
+
@xml.xpath('//address')[1].each do |key, value|
|
576
|
+
attributes << [key, value]
|
577
|
+
end
|
578
|
+
assert_equal [['domestic', 'Yes'], ['street', 'Yes']], attributes
|
579
|
+
end
|
580
|
+
|
581
|
+
def test_new
|
582
|
+
assert node = Nokogiri::XML::Node.new('input', @xml)
|
583
|
+
assert_equal 1, node.node_type
|
584
|
+
assert_instance_of Nokogiri::XML::Element, node
|
585
|
+
end
|
586
|
+
|
587
|
+
def test_to_str
|
588
|
+
name = @xml.xpath('//name').first
|
589
|
+
assert_match(/Margaret/, '' + name)
|
590
|
+
assert_equal('Margaret Martin', '' + name.children.first)
|
591
|
+
end
|
592
|
+
|
593
|
+
def test_ancestors
|
594
|
+
address = @xml.xpath('//address').first
|
595
|
+
assert_equal 3, address.ancestors.length
|
596
|
+
assert_equal ['employee', 'staff', 'document'],
|
597
|
+
address.ancestors.map { |x| x.name }
|
598
|
+
end
|
599
|
+
|
600
|
+
def test_read_only?
|
601
|
+
assert entity_decl = @xml.internal_subset.children.find { |x|
|
602
|
+
x.type == Node::ENTITY_DECL
|
603
|
+
}
|
604
|
+
assert entity_decl.read_only?
|
605
|
+
end
|
606
|
+
|
607
|
+
def test_remove_attribute
|
608
|
+
address = @xml.xpath('/staff/employee/address').first
|
609
|
+
assert_equal 'Yes', address['domestic']
|
610
|
+
address.remove_attribute 'domestic'
|
611
|
+
assert_nil address['domestic']
|
612
|
+
end
|
613
|
+
|
614
|
+
def test_attribute_setter_accepts_non_string
|
615
|
+
address = @xml.xpath("/staff/employee/address").first
|
616
|
+
assert_equal "Yes", address[:domestic]
|
617
|
+
address[:domestic] = "Altered Yes"
|
618
|
+
assert_equal "Altered Yes", address[:domestic]
|
619
|
+
end
|
620
|
+
|
621
|
+
def test_attribute_accessor_accepts_non_string
|
622
|
+
address = @xml.xpath("/staff/employee/address").first
|
623
|
+
assert_equal "Yes", address["domestic"]
|
624
|
+
assert_equal "Yes", address[:domestic]
|
625
|
+
end
|
626
|
+
|
627
|
+
def test_delete
|
628
|
+
address = @xml.xpath('/staff/employee/address').first
|
629
|
+
assert_equal 'Yes', address['domestic']
|
630
|
+
address.delete 'domestic'
|
631
|
+
assert_nil address['domestic']
|
632
|
+
end
|
633
|
+
|
634
|
+
def test_set_content_with_symbol
|
635
|
+
node = @xml.at('//name')
|
636
|
+
node.content = :foo
|
637
|
+
assert_equal 'foo', node.content
|
638
|
+
end
|
639
|
+
|
640
|
+
def test_find_by_css_with_tilde_eql
|
641
|
+
xml = Nokogiri::XML.parse(<<-eoxml)
|
642
|
+
<root>
|
643
|
+
<a>Hello world</a>
|
644
|
+
<a class='foo bar'>Bar</a>
|
645
|
+
<a class='bar foo'>Bar</a>
|
646
|
+
<a class='bar'>Bar</a>
|
647
|
+
<a class='baz bar foo'>Bar</a>
|
648
|
+
<a class='bazbarfoo'>Awesome</a>
|
649
|
+
<a class='bazbar'>Awesome</a>
|
650
|
+
</root>
|
651
|
+
eoxml
|
652
|
+
set = xml.css('a[@class~="bar"]')
|
653
|
+
assert_equal 4, set.length
|
654
|
+
assert_equal ['Bar'], set.map { |node| node.content }.uniq
|
655
|
+
end
|
656
|
+
|
657
|
+
def test_unlink
|
658
|
+
xml = Nokogiri::XML.parse(<<-eoxml)
|
659
|
+
<root>
|
660
|
+
<a class='foo bar'>Bar</a>
|
661
|
+
<a class='bar foo'>Bar</a>
|
662
|
+
<a class='bar'>Bar</a>
|
663
|
+
<a>Hello world</a>
|
664
|
+
<a class='baz bar foo'>Bar</a>
|
665
|
+
<a class='bazbarfoo'>Awesome</a>
|
666
|
+
<a class='bazbar'>Awesome</a>
|
667
|
+
</root>
|
668
|
+
eoxml
|
669
|
+
node = xml.xpath('//a')[3]
|
670
|
+
assert_equal('Hello world', node.text)
|
671
|
+
assert_match(/Hello world/, xml.to_s)
|
672
|
+
assert node.parent
|
673
|
+
assert node.document
|
674
|
+
assert node.previous_sibling
|
675
|
+
assert node.next_sibling
|
676
|
+
node.unlink
|
677
|
+
assert !node.parent
|
678
|
+
#assert !node.document
|
679
|
+
assert !node.previous_sibling
|
680
|
+
assert !node.next_sibling
|
681
|
+
assert_no_match(/Hello world/, xml.to_s)
|
682
|
+
end
|
683
|
+
|
684
|
+
def test_next_sibling
|
685
|
+
assert node = @xml.root
|
686
|
+
assert sibling = node.child.next_sibling
|
687
|
+
assert_equal('employee', sibling.name)
|
688
|
+
end
|
689
|
+
|
690
|
+
def test_previous_sibling
|
691
|
+
assert node = @xml.root
|
692
|
+
assert sibling = node.child.next_sibling
|
693
|
+
assert_equal('employee', sibling.name)
|
694
|
+
assert_equal(sibling.previous_sibling, node.child)
|
695
|
+
end
|
696
|
+
|
697
|
+
def test_name=
|
698
|
+
assert node = @xml.root
|
699
|
+
node.name = 'awesome'
|
700
|
+
assert_equal('awesome', node.name)
|
701
|
+
end
|
702
|
+
|
703
|
+
def test_child
|
704
|
+
assert node = @xml.root
|
705
|
+
assert child = node.child
|
706
|
+
assert_equal('text', child.name)
|
707
|
+
end
|
708
|
+
|
709
|
+
def test_key?
|
710
|
+
assert node = @xml.search('//address').first
|
711
|
+
assert(!node.key?('asdfasdf'))
|
712
|
+
end
|
713
|
+
|
714
|
+
def test_set_property
|
715
|
+
assert node = @xml.search('//address').first
|
716
|
+
node['foo'] = 'bar'
|
717
|
+
assert_equal('bar', node['foo'])
|
718
|
+
end
|
719
|
+
|
720
|
+
def test_attributes
|
721
|
+
assert node = @xml.search('//address').first
|
722
|
+
assert_nil(node['asdfasdfasdf'])
|
723
|
+
assert_equal('Yes', node['domestic'])
|
724
|
+
|
725
|
+
assert node = @xml.search('//address')[2]
|
726
|
+
attr = node.attributes
|
727
|
+
assert_equal 2, attr.size
|
728
|
+
assert_equal 'Yes', attr['domestic'].value
|
729
|
+
assert_equal 'Yes', attr['domestic'].to_s
|
730
|
+
assert_equal 'No', attr['street'].value
|
731
|
+
end
|
732
|
+
|
733
|
+
def test_path
|
734
|
+
assert set = @xml.search('//employee')
|
735
|
+
assert node = set.first
|
736
|
+
assert_equal('/staff/employee[1]', node.path)
|
737
|
+
end
|
738
|
+
|
739
|
+
def test_parent_xpath
|
740
|
+
assert set = @xml.search('//employee')
|
741
|
+
assert node = set.first
|
742
|
+
assert parent_set = node.search('..')
|
743
|
+
assert parent_node = parent_set.first
|
744
|
+
assert_equal '/staff', parent_node.path
|
745
|
+
assert_equal node.parent, parent_node
|
746
|
+
end
|
747
|
+
|
748
|
+
def test_search_self
|
749
|
+
node = @xml.at('//employee')
|
750
|
+
assert_equal node.search('.').to_a, [node]
|
751
|
+
end
|
752
|
+
|
753
|
+
def test_search_by_symbol
|
754
|
+
assert set = @xml.search(:employee)
|
755
|
+
assert 5, set.length
|
756
|
+
|
757
|
+
assert node = @xml.at(:employee)
|
758
|
+
assert node.text =~ /EMP0001/
|
759
|
+
end
|
760
|
+
|
761
|
+
def test_new_node
|
762
|
+
node = Nokogiri::XML::Node.new('form', @xml)
|
763
|
+
assert_equal('form', node.name)
|
764
|
+
assert(node.document)
|
765
|
+
end
|
766
|
+
|
767
|
+
def test_encode_special_chars
|
768
|
+
foo = @xml.css('employee').first.encode_special_chars('&')
|
769
|
+
assert_equal '&', foo
|
770
|
+
end
|
771
|
+
|
772
|
+
def test_content_equals
|
773
|
+
node = Nokogiri::XML::Node.new('form', @xml)
|
774
|
+
assert_equal('', node.content)
|
775
|
+
|
776
|
+
node.content = 'hello world!'
|
777
|
+
assert_equal('hello world!', node.content)
|
778
|
+
|
779
|
+
node.content = '& <foo> &'
|
780
|
+
assert_equal('& <foo> &', node.content)
|
781
|
+
assert_equal('<form>& <foo> &amp;</form>', node.to_xml)
|
782
|
+
|
783
|
+
node.content = "1234 <-> 1234"
|
784
|
+
assert_equal "1234 <-> 1234", node.content
|
785
|
+
assert_equal "<form>1234 <-> 1234</form>", node.to_xml
|
786
|
+
end
|
787
|
+
|
788
|
+
def test_set_content_should_unlink_existing_content
|
789
|
+
node = @xml.at_css("employee")
|
790
|
+
children = node.children
|
791
|
+
node.content = "hello"
|
792
|
+
children.each { |child| assert_nil child.parent }
|
793
|
+
end
|
794
|
+
|
795
|
+
def test_whitespace_nodes
|
796
|
+
doc = Nokogiri::XML.parse("<root><b>Foo</b>\n<i>Bar</i> <p>Bazz</p></root>")
|
797
|
+
children = doc.at('//root').children.collect{|j| j.to_s}
|
798
|
+
assert_equal "\n", children[1]
|
799
|
+
assert_equal " ", children[3]
|
800
|
+
end
|
801
|
+
|
802
|
+
def test_node_equality
|
803
|
+
doc1 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
|
804
|
+
doc2 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
|
805
|
+
|
806
|
+
address1_1 = doc1.xpath('//address').first
|
807
|
+
address1_2 = doc1.xpath('//address').first
|
808
|
+
|
809
|
+
address2 = doc2.xpath('//address').first
|
810
|
+
|
811
|
+
assert_not_equal address1_1, address2 # two references to very, very similar nodes
|
812
|
+
assert_equal address1_1, address1_2 # two references to the exact same node
|
813
|
+
end
|
814
|
+
|
815
|
+
def test_namespace_search_with_xpath_and_hash
|
816
|
+
xml = Nokogiri::XML.parse(<<-eoxml)
|
817
|
+
<root>
|
818
|
+
<car xmlns:part="http://general-motors.com/">
|
819
|
+
<part:tire>Michelin Model XGV</part:tire>
|
820
|
+
</car>
|
821
|
+
<bicycle xmlns:part="http://schwinn.com/">
|
822
|
+
<part:tire>I'm a bicycle tire!</part:tire>
|
823
|
+
</bicycle>
|
824
|
+
</root>
|
825
|
+
eoxml
|
826
|
+
|
827
|
+
tires = xml.xpath('//bike:tire', {'bike' => 'http://schwinn.com/'})
|
828
|
+
assert_equal 1, tires.length
|
829
|
+
end
|
830
|
+
|
831
|
+
def test_namespace_search_with_css
|
832
|
+
xml = Nokogiri::XML.parse(<<-eoxml)
|
833
|
+
<root>
|
834
|
+
<car xmlns:part="http://general-motors.com/">
|
835
|
+
<part:tire>Michelin Model XGV</part:tire>
|
836
|
+
</car>
|
837
|
+
<bicycle xmlns:part="http://schwinn.com/">
|
838
|
+
<part:tire>I'm a bicycle tire!</part:tire>
|
839
|
+
</bicycle>
|
840
|
+
</root>
|
841
|
+
eoxml
|
842
|
+
|
843
|
+
tires = xml.css('bike|tire', 'bike' => 'http://schwinn.com/')
|
844
|
+
assert_equal 1, tires.length
|
845
|
+
end
|
846
|
+
|
847
|
+
def test_namespaced_attribute_search_with_xpath
|
848
|
+
# from #593
|
849
|
+
xmlContent = <<-EOXML
|
850
|
+
<?xml version="1.0"?>
|
851
|
+
<ns1:el1 xmlns:ns1="http://blabla.com" >
|
852
|
+
<ns1:el2 ns1:att="123">with namespace</ns1:el2 >
|
853
|
+
<ns1:el2 att="noNameSpace">no namespace</ns1:el2 >
|
854
|
+
</ns1:el1>
|
855
|
+
EOXML
|
856
|
+
xml_doc = Nokogiri::XML(xmlContent)
|
857
|
+
|
858
|
+
no_ns = xml_doc.xpath("//*[@att]")
|
859
|
+
assert_equal no_ns.length, 1
|
860
|
+
assert_equal no_ns.first.content, "no namespace"
|
861
|
+
|
862
|
+
with_ns = xml_doc.xpath("//*[@ns1:att]")
|
863
|
+
assert_equal with_ns.length, 1
|
864
|
+
assert_equal with_ns.first.content, "with namespace"
|
865
|
+
end
|
866
|
+
|
867
|
+
def test_namespaced_attribute_search_with_css
|
868
|
+
# from #593
|
869
|
+
xmlContent = <<-EOXML
|
870
|
+
<?xml version="1.0"?>
|
871
|
+
<ns1:el1 xmlns:ns1="http://blabla.com" >
|
872
|
+
<ns1:el2 ns1:att="123">with namespace</ns1:el2 >
|
873
|
+
<ns1:el2 att="noNameSpace">no namespace</ns1:el2 >
|
874
|
+
</ns1:el1>
|
875
|
+
EOXML
|
876
|
+
xml_doc = Nokogiri::XML(xmlContent)
|
877
|
+
|
878
|
+
no_ns = xml_doc.css('*[att]')
|
879
|
+
assert_equal no_ns.length, 1
|
880
|
+
assert_equal no_ns.first.content, "no namespace"
|
881
|
+
|
882
|
+
with_ns = xml_doc.css('*[ns1|att]')
|
883
|
+
assert_equal with_ns.length, 1
|
884
|
+
assert_equal with_ns.first.content, "with namespace"
|
885
|
+
end
|
886
|
+
|
887
|
+
def test_namespaces_should_include_all_namespace_definitions
|
888
|
+
xml = Nokogiri::XML.parse(<<-EOF)
|
889
|
+
<x xmlns="http://quux.com/" xmlns:a="http://foo.com/" xmlns:b="http://bar.com/">
|
890
|
+
<y xmlns:c="http://bazz.com/">
|
891
|
+
<z>hello</z>
|
892
|
+
<a xmlns:c="http://newc.com/" />
|
893
|
+
</y>
|
894
|
+
</x>
|
895
|
+
EOF
|
896
|
+
|
897
|
+
namespaces = xml.namespaces # Document#namespace
|
898
|
+
assert_equal({"xmlns" => "http://quux.com/",
|
899
|
+
"xmlns:a" => "http://foo.com/",
|
900
|
+
"xmlns:b" => "http://bar.com/"}, namespaces)
|
901
|
+
|
902
|
+
namespaces = xml.root.namespaces
|
903
|
+
assert_equal({"xmlns" => "http://quux.com/",
|
904
|
+
"xmlns:a" => "http://foo.com/",
|
905
|
+
"xmlns:b" => "http://bar.com/"}, namespaces)
|
906
|
+
|
907
|
+
namespaces = xml.at_xpath("//xmlns:y").namespaces
|
908
|
+
assert_equal({"xmlns" => "http://quux.com/",
|
909
|
+
"xmlns:a" => "http://foo.com/",
|
910
|
+
"xmlns:b" => "http://bar.com/",
|
911
|
+
"xmlns:c" => "http://bazz.com/"}, namespaces)
|
912
|
+
|
913
|
+
namespaces = xml.at_xpath("//xmlns:z").namespaces
|
914
|
+
assert_equal({"xmlns" => "http://quux.com/",
|
915
|
+
"xmlns:a" => "http://foo.com/",
|
916
|
+
"xmlns:b" => "http://bar.com/",
|
917
|
+
"xmlns:c" => "http://bazz.com/"}, namespaces)
|
918
|
+
|
919
|
+
namespaces = xml.at_xpath("//xmlns:a").namespaces
|
920
|
+
assert_equal({"xmlns" => "http://quux.com/",
|
921
|
+
"xmlns:a" => "http://foo.com/",
|
922
|
+
"xmlns:b" => "http://bar.com/",
|
923
|
+
"xmlns:c" => "http://newc.com/"}, namespaces)
|
924
|
+
end
|
925
|
+
|
926
|
+
def test_namespace
|
927
|
+
xml = Nokogiri::XML.parse(<<-EOF)
|
928
|
+
<x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
|
929
|
+
<y xmlns:c='http://bazz.com/'>
|
930
|
+
<a:div>hello a</a:div>
|
931
|
+
<b:div>hello b</b:div>
|
932
|
+
<c:div>hello c</c:div>
|
933
|
+
<div>hello moon</div>
|
934
|
+
</y>
|
935
|
+
</x>
|
936
|
+
EOF
|
937
|
+
set = xml.search("//y/*")
|
938
|
+
assert_equal "a", set[0].namespace.prefix
|
939
|
+
assert_equal "b", set[1].namespace.prefix
|
940
|
+
assert_equal "c", set[2].namespace.prefix
|
941
|
+
assert_equal nil, set[3].namespace
|
942
|
+
end
|
943
|
+
|
944
|
+
if Nokogiri.uses_libxml?
|
945
|
+
def test_namespace_without_an_href_on_html_node
|
946
|
+
# because microsoft word's HTML formatting does this. ick.
|
947
|
+
xml = Nokogiri::HTML.parse <<-EOF
|
948
|
+
<div><o:p>foo</o:p></div>
|
949
|
+
EOF
|
950
|
+
|
951
|
+
assert_not_nil(node = xml.at('p'))
|
952
|
+
|
953
|
+
assert_equal 1, node.namespaces.keys.size
|
954
|
+
assert node.namespaces.has_key?('xmlns:o')
|
955
|
+
assert_equal nil, node.namespaces['xmlns:o']
|
956
|
+
end
|
957
|
+
end
|
958
|
+
|
959
|
+
def test_line
|
960
|
+
xml = Nokogiri::XML(<<-eoxml)
|
961
|
+
<root>
|
962
|
+
<a>
|
963
|
+
Hello world
|
964
|
+
</a>
|
965
|
+
</root>
|
966
|
+
eoxml
|
967
|
+
|
968
|
+
set = xml.search("//a")
|
969
|
+
node = set.first
|
970
|
+
assert_equal 2, node.line
|
971
|
+
end
|
972
|
+
|
973
|
+
def test_xpath_results_have_document_and_are_decorated
|
974
|
+
x = Module.new do
|
975
|
+
def awesome! ; end
|
976
|
+
end
|
977
|
+
util_decorate(@xml, x)
|
978
|
+
node_set = @xml.xpath("//employee")
|
979
|
+
assert_equal @xml, node_set.document
|
980
|
+
assert node_set.respond_to?(:awesome!)
|
981
|
+
end
|
982
|
+
|
983
|
+
def test_css_results_have_document_and_are_decorated
|
984
|
+
x = Module.new do
|
985
|
+
def awesome! ; end
|
986
|
+
end
|
987
|
+
util_decorate(@xml, x)
|
988
|
+
node_set = @xml.css("employee")
|
989
|
+
assert_equal @xml, node_set.document
|
990
|
+
assert node_set.respond_to?(:awesome!)
|
991
|
+
end
|
992
|
+
|
993
|
+
def test_blank
|
994
|
+
doc = Nokogiri::XML('')
|
995
|
+
assert_equal false, doc.blank?
|
996
|
+
end
|
997
|
+
|
998
|
+
def test_to_xml_allows_to_serialize_with_as_xml_save_option
|
999
|
+
xml = Nokogiri::XML("<root><ul><li>Hello world</li></ul></root>")
|
1000
|
+
set = xml.search("//ul")
|
1001
|
+
node = set.first
|
1002
|
+
|
1003
|
+
assert_no_match("<ul>\n <li>", xml.to_xml(:save_with => XML::Node::SaveOptions::AS_XML))
|
1004
|
+
assert_no_match("<ul>\n <li>", node.to_xml(:save_with => XML::Node::SaveOptions::AS_XML))
|
1005
|
+
end
|
1006
|
+
|
1007
|
+
# issue 647
|
1008
|
+
def test_default_namespace_should_be_created
|
1009
|
+
subject = Nokogiri::XML.parse('<foo xml:bar="http://bar.com"/>').root
|
1010
|
+
ns = subject.attributes['bar'].namespace
|
1011
|
+
assert_not_nil ns
|
1012
|
+
assert_equal ns.class, Nokogiri::XML::Namespace
|
1013
|
+
assert_equal 'xml', ns.prefix
|
1014
|
+
assert_equal "http://www.w3.org/XML/1998/namespace", ns.href
|
1015
|
+
end
|
1016
|
+
|
1017
|
+
# issue 648
|
1018
|
+
def test_namespace_without_prefix_should_be_set
|
1019
|
+
node = Nokogiri::XML.parse('<foo xmlns="http://bar.com"/>').root
|
1020
|
+
subject = Nokogiri::XML::Node.new 'foo', node.document
|
1021
|
+
subject.namespace = node.namespace
|
1022
|
+
ns = subject.namespace
|
1023
|
+
assert_equal ns.class, Nokogiri::XML::Namespace
|
1024
|
+
assert_nil ns.prefix
|
1025
|
+
assert_equal ns.href, "http://bar.com"
|
1026
|
+
end
|
1027
|
+
end
|
1028
|
+
end
|
1029
|
+
end
|