nokogiri 1.1.1-java
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.
- data/History.ja.txt +99 -0
- data/History.txt +99 -0
- data/Manifest.txt +141 -0
- data/README.ja.txt +100 -0
- data/README.txt +109 -0
- data/Rakefile +354 -0
- data/ext/nokogiri/extconf.rb +93 -0
- data/ext/nokogiri/html_document.c +86 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_sax_parser.c +36 -0
- data/ext/nokogiri/html_sax_parser.h +11 -0
- data/ext/nokogiri/native.c +41 -0
- data/ext/nokogiri/native.h +50 -0
- data/ext/nokogiri/xml_cdata.c +44 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +42 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +206 -0
- data/ext/nokogiri/xml_document.h +10 -0
- data/ext/nokogiri/xml_dtd.c +121 -0
- data/ext/nokogiri/xml_dtd.h +8 -0
- data/ext/nokogiri/xml_io.c +17 -0
- data/ext/nokogiri/xml_io.h +9 -0
- data/ext/nokogiri/xml_node.c +727 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +118 -0
- data/ext/nokogiri/xml_node_set.h +9 -0
- data/ext/nokogiri/xml_reader.c +465 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_sax_parser.c +201 -0
- data/ext/nokogiri/xml_sax_parser.h +10 -0
- data/ext/nokogiri/xml_syntax_error.c +199 -0
- data/ext/nokogiri/xml_syntax_error.h +11 -0
- data/ext/nokogiri/xml_text.c +40 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath.c +53 -0
- data/ext/nokogiri/xml_xpath.h +11 -0
- data/ext/nokogiri/xml_xpath_context.c +214 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +123 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/lib/action-nokogiri.rb +30 -0
- data/lib/nokogiri.rb +72 -0
- data/lib/nokogiri/css.rb +25 -0
- data/lib/nokogiri/css/generated_parser.rb +721 -0
- data/lib/nokogiri/css/generated_tokenizer.rb +159 -0
- data/lib/nokogiri/css/node.rb +97 -0
- data/lib/nokogiri/css/parser.rb +64 -0
- data/lib/nokogiri/css/parser.y +216 -0
- data/lib/nokogiri/css/syntax_error.rb +6 -0
- data/lib/nokogiri/css/tokenizer.rb +9 -0
- data/lib/nokogiri/css/tokenizer.rex +63 -0
- data/lib/nokogiri/css/xpath_visitor.rb +168 -0
- data/lib/nokogiri/decorators.rb +2 -0
- data/lib/nokogiri/decorators/hpricot.rb +3 -0
- data/lib/nokogiri/decorators/hpricot/node.rb +56 -0
- data/lib/nokogiri/decorators/hpricot/node_set.rb +54 -0
- data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +28 -0
- data/lib/nokogiri/decorators/slop.rb +31 -0
- data/lib/nokogiri/hpricot.rb +51 -0
- data/lib/nokogiri/html.rb +105 -0
- data/lib/nokogiri/html/builder.rb +9 -0
- data/lib/nokogiri/html/document.rb +9 -0
- data/lib/nokogiri/html/sax/parser.rb +21 -0
- data/lib/nokogiri/version.rb +3 -0
- data/lib/nokogiri/xml.rb +83 -0
- data/lib/nokogiri/xml/after_handler.rb +18 -0
- data/lib/nokogiri/xml/attr.rb +10 -0
- data/lib/nokogiri/xml/before_handler.rb +33 -0
- data/lib/nokogiri/xml/builder.rb +84 -0
- data/lib/nokogiri/xml/cdata.rb +9 -0
- data/lib/nokogiri/xml/comment.rb +6 -0
- data/lib/nokogiri/xml/document.rb +55 -0
- data/lib/nokogiri/xml/dtd.rb +6 -0
- data/lib/nokogiri/xml/element.rb +6 -0
- data/lib/nokogiri/xml/entity_declaration.rb +9 -0
- data/lib/nokogiri/xml/node.rb +333 -0
- data/lib/nokogiri/xml/node_set.rb +197 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/reader.rb +20 -0
- data/lib/nokogiri/xml/sax.rb +9 -0
- data/lib/nokogiri/xml/sax/document.rb +59 -0
- data/lib/nokogiri/xml/sax/parser.rb +37 -0
- data/lib/nokogiri/xml/syntax_error.rb +21 -0
- data/lib/nokogiri/xml/text.rb +6 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
- data/lib/nokogiri/xml/xpath_context.rb +14 -0
- data/lib/nokogiri/xslt.rb +28 -0
- data/lib/nokogiri/xslt/stylesheet.rb +6 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +237 -0
- data/test/css/test_tokenizer.rb +162 -0
- data/test/css/test_xpath_visitor.rb +64 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/helper.rb +78 -0
- data/test/hpricot/files/basic.xhtml +17 -0
- data/test/hpricot/files/boingboing.html +2266 -0
- data/test/hpricot/files/cy0.html +3653 -0
- data/test/hpricot/files/immob.html +400 -0
- data/test/hpricot/files/pace_application.html +1320 -0
- data/test/hpricot/files/tenderlove.html +16 -0
- data/test/hpricot/files/uswebgen.html +220 -0
- data/test/hpricot/files/utf8.html +1054 -0
- data/test/hpricot/files/week9.html +1723 -0
- data/test/hpricot/files/why.xml +19 -0
- data/test/hpricot/load_files.rb +11 -0
- data/test/hpricot/test_alter.rb +67 -0
- data/test/hpricot/test_builder.rb +27 -0
- data/test/hpricot/test_parser.rb +426 -0
- data/test/hpricot/test_paths.rb +15 -0
- data/test/hpricot/test_preserved.rb +77 -0
- data/test/hpricot/test_xml.rb +30 -0
- data/test/html/sax/test_parser.rb +27 -0
- data/test/html/test_builder.rb +89 -0
- data/test/html/test_document.rb +150 -0
- data/test/html/test_node.rb +21 -0
- data/test/test_convert_xpath.rb +185 -0
- data/test/test_css_cache.rb +57 -0
- data/test/test_gc.rb +15 -0
- data/test/test_memory_leak.rb +38 -0
- data/test/test_nokogiri.rb +97 -0
- data/test/test_reader.rb +222 -0
- data/test/test_xslt_transforms.rb +93 -0
- data/test/xml/sax/test_parser.rb +95 -0
- data/test/xml/test_attr.rb +15 -0
- data/test/xml/test_builder.rb +16 -0
- data/test/xml/test_cdata.rb +18 -0
- data/test/xml/test_comment.rb +16 -0
- data/test/xml/test_document.rb +195 -0
- data/test/xml/test_dtd.rb +43 -0
- data/test/xml/test_node.rb +394 -0
- data/test/xml/test_node_set.rb +143 -0
- data/test/xml/test_text.rb +13 -0
- data/test/xml/test_xpath.rb +105 -0
- data/vendor/hoe.rb +1020 -0
- metadata +233 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestCssCache < Nokogiri::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@css = "a1 > b2 > c3"
|
7
|
+
@parse_result = Nokogiri::CSS.parse(@css)
|
8
|
+
@to_xpath_result = @parse_result.map {|ast| ast.to_xpath}
|
9
|
+
Nokogiri::CSS::Parser.class_eval do
|
10
|
+
class << @cache
|
11
|
+
alias :old_bracket :[]
|
12
|
+
attr_reader :count
|
13
|
+
def [](key)
|
14
|
+
@count ||= 0
|
15
|
+
@count += 1
|
16
|
+
old_bracket(key)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
assert Nokogiri::CSS::Parser.cache_on?
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown
|
24
|
+
Nokogiri::CSS::Parser.clear_cache
|
25
|
+
Nokogiri::CSS::Parser.set_cache true
|
26
|
+
end
|
27
|
+
|
28
|
+
[ false, true ].each do |cache_setting|
|
29
|
+
define_method "test_css_cache_#{cache_setting ? "true" : "false"}" do
|
30
|
+
times = cache_setting ? 6 : nil
|
31
|
+
|
32
|
+
Nokogiri::CSS::Parser.set_cache cache_setting
|
33
|
+
|
34
|
+
Nokogiri::CSS.xpath_for(@css)
|
35
|
+
Nokogiri::CSS.xpath_for(@css)
|
36
|
+
Nokogiri::CSS::Parser.xpath_for(@css)
|
37
|
+
Nokogiri::CSS::Parser.xpath_for(@css)
|
38
|
+
Nokogiri::CSS::Parser.new.xpath_for(@css)
|
39
|
+
Nokogiri::CSS::Parser.new.xpath_for(@css)
|
40
|
+
|
41
|
+
assert_equal(times, Nokogiri::CSS::Parser.class_eval { @cache.count })
|
42
|
+
end
|
43
|
+
|
44
|
+
define_method "test_hpricot_cache_#{cache_setting ? "true" : "false"}" do
|
45
|
+
times = cache_setting ? 2 : nil
|
46
|
+
Nokogiri::CSS::Parser.set_cache cache_setting
|
47
|
+
|
48
|
+
nh = Nokogiri.Hpricot("<html></html>")
|
49
|
+
|
50
|
+
nh.convert_to_xpath(@css)
|
51
|
+
nh.convert_to_xpath(@css)
|
52
|
+
assert_equal(times, Nokogiri::CSS::Parser.class_eval { @cache.count })
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
end
|
data/test/test_gc.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestGc < Nokogiri::TestCase
|
4
|
+
|
5
|
+
def test_dont_hurt_em_why
|
6
|
+
content = File.open("#{File.dirname(__FILE__)}/files/dont_hurt_em_why.xml").read
|
7
|
+
ndoc = Nokogiri::XML(content)
|
8
|
+
2.times do
|
9
|
+
info = ndoc.search('status text').first.inner_text
|
10
|
+
url = ndoc.search('user name').first.inner_text
|
11
|
+
GC.start
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestMemoryLeak < Nokogiri::TestCase
|
4
|
+
def test_for_memory_leak
|
5
|
+
begin
|
6
|
+
# we don't use Dike in any tests, but requiring it has side effects
|
7
|
+
# that can create memory leaks, and that's what we're testing for.
|
8
|
+
require 'rubygems'
|
9
|
+
require 'dike' # do not remove!
|
10
|
+
|
11
|
+
count_start = count_object_space_documents
|
12
|
+
xml_data = <<-EOS
|
13
|
+
<test>
|
14
|
+
<items>
|
15
|
+
<item>abc</item>
|
16
|
+
<item>1234</item>
|
17
|
+
<item>Zzz</item>
|
18
|
+
<items>
|
19
|
+
</test>
|
20
|
+
EOS
|
21
|
+
20.times do
|
22
|
+
doc = Nokogiri::XML(xml_data)
|
23
|
+
doc.xpath("//item")
|
24
|
+
end
|
25
|
+
2.times { GC.start }
|
26
|
+
count_end = count_object_space_documents
|
27
|
+
assert((count_end - count_start) <= 2, "memory leak detected")
|
28
|
+
rescue LoadError
|
29
|
+
puts "\ndike is not installed, skipping memory leak test"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def count_object_space_documents
|
34
|
+
count = 0
|
35
|
+
ObjectSpace.each_object {|j| count += 1 if j.is_a?(Nokogiri::XML::Document) }
|
36
|
+
count
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestNokogiri < Nokogiri::TestCase
|
4
|
+
def test_xml?
|
5
|
+
doc = Nokogiri.parse(File.read(XML_FILE))
|
6
|
+
assert doc.xml?
|
7
|
+
assert !doc.html?
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_html?
|
11
|
+
doc = Nokogiri.parse(File.read(HTML_FILE))
|
12
|
+
assert !doc.xml?
|
13
|
+
assert doc.html?
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_nokogiri_method_with_html
|
17
|
+
doc1 = Nokogiri(File.read(HTML_FILE))
|
18
|
+
doc2 = Nokogiri.parse(File.read(HTML_FILE))
|
19
|
+
assert_equal doc1.serialize, doc2.serialize
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_nokogiri_method_with_block
|
23
|
+
doc = Nokogiri { b "bold tag" }
|
24
|
+
assert_equal('<b>bold tag</b>', doc.to_html.chomp)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_make_with_html
|
28
|
+
doc = Nokogiri.make("<b>bold tag</b>")
|
29
|
+
assert_equal('<b>bold tag</b>', doc.to_html.chomp)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_make_with_block
|
33
|
+
doc = Nokogiri.make { b "bold tag" }
|
34
|
+
assert_equal('<b>bold tag</b>', doc.to_html.chomp)
|
35
|
+
end
|
36
|
+
|
37
|
+
SLOP_HTML = <<-END
|
38
|
+
<html>
|
39
|
+
<body>
|
40
|
+
<ul>
|
41
|
+
<li class='red'>one</li>
|
42
|
+
<li class='blue'>two</li>
|
43
|
+
</ul>
|
44
|
+
<div>
|
45
|
+
one
|
46
|
+
<div>div two</div>
|
47
|
+
</div>
|
48
|
+
</body>
|
49
|
+
</html>
|
50
|
+
END
|
51
|
+
|
52
|
+
def test_slop_css
|
53
|
+
doc = Nokogiri::Slop(<<-eohtml)
|
54
|
+
<html>
|
55
|
+
<body>
|
56
|
+
<div>
|
57
|
+
one
|
58
|
+
<div class='foo'>
|
59
|
+
div two
|
60
|
+
<div class='foo'>
|
61
|
+
div three
|
62
|
+
</div>
|
63
|
+
</div>
|
64
|
+
</div>
|
65
|
+
</body>
|
66
|
+
</html>
|
67
|
+
eohtml
|
68
|
+
assert_equal "div", doc.html.body.div.div('.foo').name
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_slop
|
72
|
+
doc = Nokogiri::Slop(SLOP_HTML)
|
73
|
+
|
74
|
+
assert_equal "one", doc.html.body.ul.li.first.text
|
75
|
+
assert_equal "two", doc.html.body.ul.li(".blue").text
|
76
|
+
assert_equal "div two", doc.html.body.div.div.text
|
77
|
+
|
78
|
+
assert_equal "two", doc.html.body.ul.li(:css => ".blue").text
|
79
|
+
|
80
|
+
assert_equal "two", doc.html.body.ul.li(:xpath => "position()=2").text
|
81
|
+
assert_equal "one", doc.html.body.ul.li(:xpath => ["contains(text(),'o')"]).first.text
|
82
|
+
assert_equal "two", doc.html.body.ul.li(:xpath => ["contains(text(),'o')","contains(text(),'t')"]).text
|
83
|
+
|
84
|
+
assert_raise(NoMethodError) { doc.nonexistent }
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_slop_decorator
|
88
|
+
doc = Nokogiri(SLOP_HTML)
|
89
|
+
assert !doc.decorators(Nokogiri::XML::Node).include?(Nokogiri::Decorators::Slop)
|
90
|
+
|
91
|
+
doc.slop!
|
92
|
+
assert doc.decorators(Nokogiri::XML::Node).include?(Nokogiri::Decorators::Slop)
|
93
|
+
|
94
|
+
doc.slop!
|
95
|
+
assert_equal 1, doc.decorators(Nokogiri::XML::Node).select { |d| d == Nokogiri::Decorators::Slop }.size
|
96
|
+
end
|
97
|
+
end
|
data/test/test_reader.rb
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
3
|
+
|
4
|
+
class TestReader < Nokogiri::TestCase
|
5
|
+
def test_default?
|
6
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
7
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
8
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
9
|
+
</x>
|
10
|
+
eoxml
|
11
|
+
assert_equal false, reader.default?
|
12
|
+
assert_equal [false, false, false, false, false, false, false],
|
13
|
+
reader.map { |x| x.default? }
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_value?
|
17
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
18
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
19
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
20
|
+
</x>
|
21
|
+
eoxml
|
22
|
+
assert_equal false, reader.value?
|
23
|
+
assert_equal [false, true, false, true, false, true, false],
|
24
|
+
reader.map { |x| x.value? }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_attributes?
|
28
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
29
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
30
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
31
|
+
</x>
|
32
|
+
eoxml
|
33
|
+
assert_equal false, reader.attributes?
|
34
|
+
assert_equal [true, false, true, false, true, false, true],
|
35
|
+
reader.map { |x| x.attributes? }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_attributes
|
39
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
40
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'
|
41
|
+
xmlns='http://mothership.connection.com/'
|
42
|
+
>
|
43
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
44
|
+
</x>
|
45
|
+
eoxml
|
46
|
+
assert_equal({}, reader.attributes)
|
47
|
+
assert_equal [{'xmlns:tenderlove'=>'http://tenderlovemaking.com/',
|
48
|
+
'xmlns'=>'http://mothership.connection.com/'},
|
49
|
+
{}, {"awesome"=>"true"}, {}, {"awesome"=>"true"}, {},
|
50
|
+
{'xmlns:tenderlove'=>'http://tenderlovemaking.com/',
|
51
|
+
'xmlns'=>'http://mothership.connection.com/'}],
|
52
|
+
reader.map { |x| x.attributes }
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_attribute_roundtrip
|
56
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
57
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'
|
58
|
+
xmlns='http://mothership.connection.com/'
|
59
|
+
>
|
60
|
+
<tenderlove:foo awesome='true' size='giant'>snuggles!</tenderlove:foo>
|
61
|
+
</x>
|
62
|
+
eoxml
|
63
|
+
reader.each do |node|
|
64
|
+
node.attributes.each do |key, value|
|
65
|
+
assert_equal value, node.attribute(key)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_attribute_at
|
71
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
72
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
73
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
74
|
+
</x>
|
75
|
+
eoxml
|
76
|
+
assert_nil reader.attribute_at(nil)
|
77
|
+
assert_nil reader.attribute_at(0)
|
78
|
+
assert_equal ['http://tenderlovemaking.com/', nil, 'true', nil, 'true', nil, 'http://tenderlovemaking.com/'],
|
79
|
+
reader.map { |x| x.attribute_at(0) }
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_attribute
|
83
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
84
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
85
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
86
|
+
</x>
|
87
|
+
eoxml
|
88
|
+
assert_nil reader.attribute(nil)
|
89
|
+
assert_nil reader.attribute('awesome')
|
90
|
+
assert_equal [nil, nil, 'true', nil, 'true', nil, nil],
|
91
|
+
reader.map { |x| x.attribute('awesome') }
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_attribute_length
|
95
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
96
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
97
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
98
|
+
</x>
|
99
|
+
eoxml
|
100
|
+
assert_equal 0, reader.attribute_count
|
101
|
+
assert_equal [1, 0, 1, 0, 0, 0, 0], reader.map { |x| x.attribute_count }
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_depth
|
105
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
106
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
107
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
108
|
+
</x>
|
109
|
+
eoxml
|
110
|
+
assert_equal 0, reader.depth
|
111
|
+
assert_equal [0, 1, 1, 2, 1, 1, 0], reader.map { |x| x.depth }
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_encoding
|
115
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
116
|
+
<awesome>
|
117
|
+
<p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
|
118
|
+
<p xml:lang="ja">日本語が上手です</p>
|
119
|
+
</awesome>
|
120
|
+
eoxml
|
121
|
+
assert_nil reader.encoding
|
122
|
+
assert_equal [nil], reader.map { |x| x.encoding }.uniq
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_xml_version
|
126
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
127
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
128
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
129
|
+
</x>
|
130
|
+
eoxml
|
131
|
+
assert_nil reader.xml_version
|
132
|
+
assert_equal ['1.0'], reader.map { |x| x.xml_version }.uniq
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_lang
|
136
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
137
|
+
<awesome>
|
138
|
+
<p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
|
139
|
+
<p xml:lang="ja">日本語が上手です</p>
|
140
|
+
</awesome>
|
141
|
+
eoxml
|
142
|
+
assert_nil reader.lang
|
143
|
+
assert_equal [nil, nil, "en", "en", "en", nil, "ja", "ja", "ja", nil, nil],
|
144
|
+
reader.map { |x| x.lang }
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_value
|
148
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
149
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
150
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
151
|
+
</x>
|
152
|
+
eoxml
|
153
|
+
assert_nil reader.value
|
154
|
+
assert_equal [nil, "\n ", nil, "snuggles!", nil, "\n ", nil],
|
155
|
+
reader.map { |x| x.value }
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_prefix
|
159
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
160
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
161
|
+
<edi:foo>hello</edi:foo>
|
162
|
+
</x>
|
163
|
+
eoxml
|
164
|
+
assert_nil reader.prefix
|
165
|
+
assert_equal [nil, nil, "edi", nil, "edi", nil, nil],
|
166
|
+
reader.map { |n| n.prefix }
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_state
|
170
|
+
reader = Nokogiri::XML::Reader.from_memory('<foo>bar</bar>')
|
171
|
+
assert reader.state
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_ns_uri
|
175
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
176
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
177
|
+
<edi:foo>hello</edi:foo>
|
178
|
+
</x>
|
179
|
+
eoxml
|
180
|
+
assert_nil reader.namespace_uri
|
181
|
+
assert_equal([nil,
|
182
|
+
nil,
|
183
|
+
"http://ecommerce.example.org/schema",
|
184
|
+
nil,
|
185
|
+
"http://ecommerce.example.org/schema",
|
186
|
+
nil,
|
187
|
+
nil],
|
188
|
+
reader.map { |n| n.namespace_uri })
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_local_name
|
192
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
193
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
194
|
+
<edi:foo>hello</edi:foo>
|
195
|
+
</x>
|
196
|
+
eoxml
|
197
|
+
assert_nil reader.local_name
|
198
|
+
assert_equal(["x", "#text", "foo", "#text", "foo", "#text", "x"],
|
199
|
+
reader.map { |n| n.local_name })
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_name
|
203
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
204
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
205
|
+
<edi:foo>hello</edi:foo>
|
206
|
+
</x>
|
207
|
+
eoxml
|
208
|
+
assert_nil reader.name
|
209
|
+
assert_equal(["x", "#text", "edi:foo", "#text", "edi:foo", "#text", "x"],
|
210
|
+
reader.map { |n| n.name })
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_read_from_memory
|
214
|
+
called = false
|
215
|
+
reader = Nokogiri::XML::Reader.from_memory('<foo>bar</foo>')
|
216
|
+
reader.each do |node|
|
217
|
+
called = true
|
218
|
+
assert node
|
219
|
+
end
|
220
|
+
assert called
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestXsltTransforms < Nokogiri::TestCase
|
4
|
+
def test_transform
|
5
|
+
assert doc = Nokogiri::XML.parse(File.read(XML_FILE))
|
6
|
+
assert doc.xml?
|
7
|
+
|
8
|
+
assert style = Nokogiri::XSLT.parse(File.read(XSLT_FILE))
|
9
|
+
|
10
|
+
assert result = style.apply_to(doc, ['title', '"Booyah"'])
|
11
|
+
assert_match %r{<h1>Booyah</h1>}, result
|
12
|
+
assert_match %r{<th.*Employee ID</th>}, result
|
13
|
+
assert_match %r{<th.*Name</th>}, result
|
14
|
+
assert_match %r{<th.*Position</th>}, result
|
15
|
+
assert_match %r{<th.*Salary</th>}, result
|
16
|
+
assert_match %r{<td>EMP0003</td>}, result
|
17
|
+
assert_match %r{<td>Margaret Martin</td>}, result
|
18
|
+
assert_match %r{<td>Computer Specialist</td>}, result
|
19
|
+
assert_match %r{<td>100,000</td>}, result
|
20
|
+
assert_no_match %r{Dallas|Texas}, result
|
21
|
+
assert_no_match %r{Female}, result
|
22
|
+
|
23
|
+
assert result = style.apply_to(doc, ['title', '"Grandma"'])
|
24
|
+
assert_match %r{<h1>Grandma</h1>}, result
|
25
|
+
|
26
|
+
assert result = style.apply_to(doc)
|
27
|
+
assert_match %r{<h1></h1>}, result
|
28
|
+
end
|
29
|
+
def test_transform2
|
30
|
+
assert doc = Nokogiri::XML.parse(File.read(XML_FILE))
|
31
|
+
assert doc.xml?
|
32
|
+
|
33
|
+
assert style = Nokogiri::XSLT.parse(File.read(XSLT_FILE))
|
34
|
+
assert result_doc = style.transform(doc, ['title', '"Booyah"'])
|
35
|
+
assert doc.xml?
|
36
|
+
|
37
|
+
assert result_string = style.apply_to(doc, ['title', '"Booyah"'])
|
38
|
+
assert_equal result_string, style.serialize(result_doc)
|
39
|
+
end
|
40
|
+
def test_quote_params
|
41
|
+
h = {
|
42
|
+
:sym => %{xxx},
|
43
|
+
'str' => %{"xxx"},
|
44
|
+
:sym2 => %{'xxx'},
|
45
|
+
'str2' => %{x'x'x},
|
46
|
+
:sym3 => %{x"x"x},
|
47
|
+
}
|
48
|
+
hh=h.dup
|
49
|
+
result_hash = Nokogiri::XSLT.quote_params(h)
|
50
|
+
assert_equal hh, h # non-destructive
|
51
|
+
|
52
|
+
a=h.to_a.flatten
|
53
|
+
result_array = Nokogiri::XSLT.quote_params(a)
|
54
|
+
assert_equal h.to_a.flatten, a #non-destructive
|
55
|
+
|
56
|
+
assert_equal result_array, result_hash
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_exslt
|
60
|
+
assert doc = Nokogiri::XML.parse(File.read(EXML_FILE))
|
61
|
+
assert doc.xml?
|
62
|
+
|
63
|
+
assert style = Nokogiri::XSLT.parse(File.read(EXSLT_FILE))
|
64
|
+
params = {
|
65
|
+
:p1 => 'xxx',
|
66
|
+
:p2 => "x'x'x",
|
67
|
+
:p3 => 'x"x"x',
|
68
|
+
:p4 => '"xxx"'
|
69
|
+
}
|
70
|
+
result_doc = Nokogiri::XML.parse(style.apply_to(doc,
|
71
|
+
Nokogiri::XSLT.quote_params(params)))
|
72
|
+
|
73
|
+
assert_equal 'func-result', result_doc.at('/root/function').content
|
74
|
+
assert_equal 3, result_doc.at('/root/max').content.to_i
|
75
|
+
assert_match(
|
76
|
+
/\d{4}-\d\d-\d\d-\d\d:\d\d/,
|
77
|
+
result_doc.at('/root/date').content
|
78
|
+
)
|
79
|
+
result_doc.xpath('/root/params/*').each do |p|
|
80
|
+
assert_equal p.content, params[p.name.intern]
|
81
|
+
end
|
82
|
+
check_params result_doc, params
|
83
|
+
result_doc = Nokogiri::XML.parse(style.apply_to(doc,
|
84
|
+
Nokogiri::XSLT.quote_params(params.to_a.flatten)))
|
85
|
+
check_params result_doc, params
|
86
|
+
end
|
87
|
+
|
88
|
+
def check_params result_doc, params
|
89
|
+
result_doc.xpath('/root/params/*').each do |p|
|
90
|
+
assert_equal p.content, params[p.name.intern]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|