nokogiri 1.3.3 → 1.4.0
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/CHANGELOG.ja.rdoc +48 -3
- data/CHANGELOG.rdoc +42 -0
- data/Manifest.txt +44 -29
- data/README.ja.rdoc +0 -2
- data/README.rdoc +4 -7
- data/Rakefile +42 -6
- data/bin/nokogiri +7 -5
- data/ext/nokogiri/extconf.rb +5 -21
- data/ext/nokogiri/html_document.c +14 -50
- data/ext/nokogiri/html_element_description.c +7 -7
- data/ext/nokogiri/html_entity_lookup.c +6 -4
- data/ext/nokogiri/html_sax_parser_context.c +92 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/nokogiri.c +9 -3
- data/ext/nokogiri/nokogiri.h +16 -20
- data/ext/nokogiri/xml_attr.c +1 -1
- data/ext/nokogiri/xml_attribute_decl.c +67 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +6 -5
- data/ext/nokogiri/xml_comment.c +3 -2
- data/ext/nokogiri/xml_document.c +93 -23
- data/ext/nokogiri/xml_document_fragment.c +1 -3
- data/ext/nokogiri/xml_dtd.c +63 -6
- 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_entity_decl.c +97 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +1 -1
- data/ext/nokogiri/xml_io.c +10 -3
- data/ext/nokogiri/xml_io.h +1 -0
- data/ext/nokogiri/xml_namespace.c +2 -2
- data/ext/nokogiri/xml_node.c +139 -34
- data/ext/nokogiri/xml_node.h +0 -1
- data/ext/nokogiri/xml_node_set.c +23 -16
- data/ext/nokogiri/xml_processing_instruction.c +1 -1
- data/ext/nokogiri/xml_reader.c +78 -50
- data/ext/nokogiri/xml_sax_parser.c +109 -168
- data/ext/nokogiri/xml_sax_parser.h +33 -0
- data/ext/nokogiri/xml_sax_parser_context.c +155 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +11 -6
- data/ext/nokogiri/xml_syntax_error.c +63 -12
- data/ext/nokogiri/xml_text.c +4 -3
- data/ext/nokogiri/xml_xpath.c +1 -1
- data/ext/nokogiri/xml_xpath_context.c +12 -25
- data/ext/nokogiri/xslt_stylesheet.c +3 -3
- data/lib/nokogiri.rb +4 -4
- data/lib/nokogiri/css/generated_tokenizer.rb +1 -0
- data/lib/nokogiri/css/node.rb +1 -9
- data/lib/nokogiri/css/xpath_visitor.rb +11 -21
- data/lib/nokogiri/ffi/html/document.rb +0 -9
- data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
- data/lib/nokogiri/ffi/io_callbacks.rb +4 -2
- data/lib/nokogiri/ffi/libxml.rb +44 -10
- data/lib/nokogiri/ffi/structs/common_node.rb +1 -1
- data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
- data/lib/nokogiri/ffi/structs/xml_dtd.rb +3 -1
- data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
- data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
- data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
- data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
- data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
- data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +4 -3
- data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +1 -1
- data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
- data/lib/nokogiri/ffi/xml/comment.rb +2 -2
- data/lib/nokogiri/ffi/xml/document.rb +29 -12
- data/lib/nokogiri/ffi/xml/document_fragment.rb +0 -5
- data/lib/nokogiri/ffi/xml/dtd.rb +14 -3
- data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
- data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
- data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
- data/lib/nokogiri/ffi/xml/node.rb +45 -5
- data/lib/nokogiri/ffi/xml/node_set.rb +1 -1
- data/lib/nokogiri/ffi/xml/reader.rb +45 -24
- data/lib/nokogiri/ffi/xml/sax/parser.rb +27 -34
- data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
- data/lib/nokogiri/ffi/xml/sax/push_parser.rb +5 -4
- data/lib/nokogiri/ffi/xml/syntax_error.rb +31 -16
- data/lib/nokogiri/ffi/xml/text.rb +2 -2
- data/lib/nokogiri/html.rb +1 -0
- data/lib/nokogiri/html/document.rb +39 -24
- data/lib/nokogiri/html/sax/parser.rb +2 -2
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/version.rb +1 -1
- data/lib/nokogiri/xml.rb +6 -1
- data/lib/nokogiri/xml/attr.rb +5 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +121 -13
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +43 -29
- data/lib/nokogiri/xml/document_fragment.rb +26 -6
- data/lib/nokogiri/xml/dtd.rb +5 -5
- 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 +15 -0
- data/lib/nokogiri/xml/fragment_handler.rb +22 -11
- data/lib/nokogiri/xml/namespace.rb +6 -0
- data/lib/nokogiri/xml/node.rb +33 -15
- data/lib/nokogiri/xml/node_set.rb +66 -44
- 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/reader.rb +8 -0
- data/lib/nokogiri/xml/sax.rb +1 -1
- data/lib/nokogiri/xml/sax/document.rb +18 -1
- data/lib/nokogiri/xml/sax/parser.rb +15 -8
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +0 -3
- data/lib/nokogiri/xml/syntax_error.rb +4 -0
- data/lib/nokogiri/xslt/stylesheet.rb +1 -1
- data/test/css/test_nthiness.rb +1 -1
- data/test/css/test_parser.rb +1 -1
- data/test/css/test_tokenizer.rb +1 -1
- data/test/css/test_xpath_visitor.rb +1 -1
- data/test/ffi/test_document.rb +1 -1
- data/test/files/shift_jis.html +10 -0
- data/test/files/staff.dtd +10 -0
- data/test/helper.rb +12 -3
- data/test/html/sax/test_parser.rb +1 -1
- data/test/html/sax/test_parser_context.rb +48 -0
- data/test/html/test_builder.rb +8 -2
- data/test/html/test_document.rb +23 -1
- data/test/html/test_document_encoding.rb +15 -1
- data/test/html/test_document_fragment.rb +10 -1
- data/test/html/test_element_description.rb +1 -2
- data/test/html/test_named_characters.rb +1 -1
- data/test/html/test_node.rb +61 -1
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +1 -3
- data/test/test_css_cache.rb +1 -1
- data/test/test_gc.rb +1 -1
- data/test/test_memory_leak.rb +1 -1
- data/test/test_nokogiri.rb +3 -3
- data/test/test_reader.rb +29 -1
- data/test/test_xslt_transforms.rb +1 -1
- data/test/xml/node/test_save_options.rb +1 -1
- data/test/xml/node/test_subclass.rb +1 -1
- data/test/xml/sax/test_parser.rb +64 -3
- data/test/xml/sax/test_parser_context.rb +56 -0
- data/test/xml/sax/test_push_parser.rb +11 -1
- data/test/xml/test_attr.rb +1 -1
- data/test/xml/test_attribute_decl.rb +82 -0
- data/test/xml/test_builder.rb +95 -1
- data/test/xml/test_cdata.rb +1 -1
- data/test/xml/test_comment.rb +7 -1
- data/test/xml/test_document.rb +147 -6
- data/test/xml/test_document_encoding.rb +1 -1
- data/test/xml/test_document_fragment.rb +55 -5
- data/test/xml/test_dtd.rb +40 -5
- data/test/xml/test_dtd_encoding.rb +3 -1
- 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 +83 -0
- data/test/xml/test_entity_reference.rb +1 -1
- data/test/xml/test_namespace.rb +21 -1
- data/test/xml/test_node.rb +70 -4
- data/test/xml/test_node_attributes.rb +1 -1
- data/test/xml/test_node_encoding.rb +1 -1
- data/test/xml/test_node_set.rb +136 -2
- data/test/xml/test_parse_options.rb +1 -1
- data/test/xml/test_processing_instruction.rb +1 -1
- data/test/xml/test_reader_encoding.rb +1 -1
- data/test/xml/test_relax_ng.rb +1 -1
- data/test/xml/test_schema.rb +1 -1
- data/test/xml/test_syntax_error.rb +27 -0
- data/test/xml/test_text.rb +13 -1
- data/test/xml/test_unparented_node.rb +1 -1
- data/test/xml/test_xpath.rb +1 -1
- metadata +57 -40
- data/ext/nokogiri/html_sax_parser.c +0 -57
- data/ext/nokogiri/html_sax_parser.h +0 -11
- data/lib/action-nokogiri.rb +0 -38
- data/lib/nokogiri/decorators.rb +0 -2
- data/lib/nokogiri/decorators/hpricot.rb +0 -3
- data/lib/nokogiri/decorators/hpricot/node.rb +0 -56
- data/lib/nokogiri/decorators/hpricot/node_set.rb +0 -54
- data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +0 -30
- data/lib/nokogiri/ffi/html/sax/parser.rb +0 -21
- data/lib/nokogiri/hpricot.rb +0 -92
- data/lib/nokogiri/xml/entity_declaration.rb +0 -11
- data/lib/nokogiri/xml/sax/legacy_handlers.rb +0 -65
- data/test/hpricot/files/basic.xhtml +0 -17
- data/test/hpricot/files/boingboing.html +0 -2266
- data/test/hpricot/files/cy0.html +0 -3653
- data/test/hpricot/files/immob.html +0 -400
- data/test/hpricot/files/pace_application.html +0 -1320
- data/test/hpricot/files/tenderlove.html +0 -16
- data/test/hpricot/files/uswebgen.html +0 -220
- data/test/hpricot/files/utf8.html +0 -1054
- data/test/hpricot/files/week9.html +0 -1723
- data/test/hpricot/files/why.xml +0 -19
- data/test/hpricot/load_files.rb +0 -11
- data/test/hpricot/test_alter.rb +0 -68
- data/test/hpricot/test_builder.rb +0 -20
- data/test/hpricot/test_parser.rb +0 -350
- data/test/hpricot/test_paths.rb +0 -15
- data/test/hpricot/test_preserved.rb +0 -77
- data/test/hpricot/test_xml.rb +0 -30
@@ -1,10 +1,24 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
require
|
2
|
+
require "helper"
|
3
3
|
|
4
4
|
module Nokogiri
|
5
5
|
module HTML
|
6
6
|
if RUBY_VERSION =~ /^1\.9/
|
7
7
|
class TestDocumentEncoding < Nokogiri::TestCase
|
8
|
+
def test_encoding
|
9
|
+
doc = Nokogiri::HTML File.open(SHIFT_JIS_HTML, 'rb')
|
10
|
+
|
11
|
+
hello = "こんにちは"
|
12
|
+
|
13
|
+
assert_match doc.encoding, doc.to_html
|
14
|
+
assert_match hello.encode('Shift_JIS'), doc.to_html
|
15
|
+
assert_equal 'Shift_JIS', doc.to_html.encoding.name
|
16
|
+
|
17
|
+
assert_match hello, doc.to_html(:encoding => 'UTF-8')
|
18
|
+
assert_match 'UTF-8', doc.to_html(:encoding => 'UTF-8')
|
19
|
+
assert_match 'UTF-8', doc.to_html(:encoding => 'UTF-8').encoding.name
|
20
|
+
end
|
21
|
+
|
8
22
|
def test_default_to_encoding_from_string
|
9
23
|
bad_charset = <<-eohtml
|
10
24
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "helper"
|
2
3
|
|
3
4
|
module Nokogiri
|
4
5
|
module HTML
|
@@ -8,6 +9,14 @@ module Nokogiri
|
|
8
9
|
@html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE)
|
9
10
|
end
|
10
11
|
|
12
|
+
def test_fun_encoding
|
13
|
+
string = %Q(<body>こんにちは</body>)
|
14
|
+
html = Nokogiri::HTML::DocumentFragment.parse(
|
15
|
+
string
|
16
|
+
).to_html(:encoding => 'UTF-8')
|
17
|
+
assert_equal string, html
|
18
|
+
end
|
19
|
+
|
11
20
|
def test_new
|
12
21
|
fragment = Nokogiri::HTML::DocumentFragment.new(@html)
|
13
22
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "helper"
|
2
2
|
|
3
3
|
module Nokogiri
|
4
4
|
module HTML
|
@@ -66,7 +66,6 @@ module Nokogiri
|
|
66
66
|
def test_optional_attributes
|
67
67
|
attrs = ElementDescription['table'].optional_attributes
|
68
68
|
assert attrs
|
69
|
-
assert_equal 22, attrs.length
|
70
69
|
end
|
71
70
|
|
72
71
|
def test_deprecated_attributes
|
data/test/html/test_node.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "helper"
|
2
2
|
|
3
3
|
require 'nkf'
|
4
4
|
|
@@ -17,6 +17,43 @@ module Nokogiri
|
|
17
17
|
eohtml
|
18
18
|
end
|
19
19
|
|
20
|
+
def test_get_attribute
|
21
|
+
element = @html.at('div')
|
22
|
+
assert_equal 'baz', element.get_attribute('class')
|
23
|
+
assert_equal 'baz', element['class']
|
24
|
+
element['href'] = "javascript:alert(\"AGGA-KA-BOO!\")"
|
25
|
+
assert_match(/%22AGGA-KA-BOO!%22/, element.to_html)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_css_path_round_trip
|
29
|
+
doc = Nokogiri::HTML(File.read(HTML_FILE))
|
30
|
+
%w{ #header small div[2] div.post body }.each do |css_sel|
|
31
|
+
ele = doc.at css_sel
|
32
|
+
assert_equal ele, doc.at(ele.css_path), ele.css_path
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_path_round_trip
|
37
|
+
doc = Nokogiri::HTML(File.read(HTML_FILE))
|
38
|
+
%w{ #header small div[2] div.post body }.each do |css_sel|
|
39
|
+
ele = doc.at css_sel
|
40
|
+
assert_equal ele, doc.at(ele.path), ele.path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_append_with_document
|
45
|
+
assert_raises(ArgumentError) do
|
46
|
+
@html.root << Nokogiri::HTML::Document.new
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
###
|
51
|
+
# Make sure a document that doesn't declare a meta encoding returns
|
52
|
+
# nil.
|
53
|
+
def test_meta_encoding
|
54
|
+
assert_nil @html.meta_encoding
|
55
|
+
end
|
56
|
+
|
20
57
|
def test_description
|
21
58
|
assert desc = @html.at('a.bar').description
|
22
59
|
assert_equal 'a', desc.name
|
@@ -51,6 +88,19 @@ module Nokogiri
|
|
51
88
|
assert node.matches?('//a')
|
52
89
|
end
|
53
90
|
|
91
|
+
def test_unlink_then_swap
|
92
|
+
node = @html.at('a')
|
93
|
+
node.unlink
|
94
|
+
|
95
|
+
another_node = @html.at('div')
|
96
|
+
assert another_node, 'should have a node'
|
97
|
+
|
98
|
+
# This used to segv
|
99
|
+
assert_nothing_raised do
|
100
|
+
node.add_previous_sibling another_node
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
54
104
|
def test_swap
|
55
105
|
@html.at('div').swap('<a href="foo">bar</a>')
|
56
106
|
a_tag = @html.css('a').first
|
@@ -79,6 +129,11 @@ module Nokogiri
|
|
79
129
|
assert_equal 'some text', @html.at('//body').children[0].content.strip
|
80
130
|
end
|
81
131
|
|
132
|
+
def test_before
|
133
|
+
@html.at('//div').before('<a href="awesome">town</a>')
|
134
|
+
assert_equal 'awesome', @html.at('//div').previous['href']
|
135
|
+
end
|
136
|
+
|
82
137
|
def test_fragment_handler_does_not_regurge_on_invalid_attributes
|
83
138
|
iframe = %Q{<iframe style="width: 0%; height: 0px" src="http://someurl" allowtransparency></iframe>}
|
84
139
|
assert_nothing_raised { @html.at('div').before(iframe) }
|
@@ -128,6 +183,11 @@ module Nokogiri
|
|
128
183
|
assert_equal 'some text', node.next.text.strip
|
129
184
|
end
|
130
185
|
|
186
|
+
def test_after
|
187
|
+
@html.at('//div').after('<a href="awesome">town</a>')
|
188
|
+
assert_equal 'awesome', @html.at('//div').next['href']
|
189
|
+
end
|
190
|
+
|
131
191
|
def test_replace
|
132
192
|
doc = Nokogiri::HTML(<<-eohtml)
|
133
193
|
<html>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "helper"
|
3
|
+
|
4
|
+
module Nokogiri
|
5
|
+
module HTML
|
6
|
+
if RUBY_VERSION =~ /^1\.9/
|
7
|
+
class TestNodeEncoding < Nokogiri::TestCase
|
8
|
+
def test_inner_html
|
9
|
+
doc = Nokogiri::HTML File.open(SHIFT_JIS_HTML, 'rb')
|
10
|
+
|
11
|
+
hello = "こんにちは"
|
12
|
+
|
13
|
+
contents = doc.at('h2').inner_html
|
14
|
+
assert_equal doc.encoding, contents.encoding.name
|
15
|
+
assert_match hello.encode('Shift_JIS'), contents
|
16
|
+
|
17
|
+
contents = doc.at('h2').inner_html(:encoding => 'UTF-8')
|
18
|
+
assert_match hello, contents
|
19
|
+
|
20
|
+
doc.encoding = 'UTF-8'
|
21
|
+
contents = doc.at('h2').inner_html
|
22
|
+
assert_match hello, contents
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/test_convert_xpath.rb
CHANGED
data/test/test_css_cache.rb
CHANGED
data/test/test_gc.rb
CHANGED
data/test/test_memory_leak.rb
CHANGED
data/test/test_nokogiri.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "helper"
|
2
2
|
|
3
3
|
class TestNokogiri < Nokogiri::TestCase
|
4
4
|
def test_versions
|
@@ -69,7 +69,7 @@ class TestNokogiri < Nokogiri::TestCase
|
|
69
69
|
doc = Nokogiri.make { b "bold tag" }
|
70
70
|
assert_equal('<b>bold tag</b>', doc.to_html.chomp)
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
SLOP_HTML = <<-END
|
74
74
|
<html>
|
75
75
|
<body>
|
@@ -119,7 +119,7 @@ class TestNokogiri < Nokogiri::TestCase
|
|
119
119
|
|
120
120
|
assert_raise(NoMethodError) { doc.nonexistent }
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
def test_slop_decorator
|
124
124
|
doc = Nokogiri(SLOP_HTML)
|
125
125
|
assert !doc.decorators(Nokogiri::XML::Node).include?(Nokogiri::Decorators::Slop)
|
data/test/test_reader.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
require
|
2
|
+
require "helper"
|
3
3
|
|
4
4
|
class TestReader < Nokogiri::TestCase
|
5
5
|
def test_from_io_sets_io_as_source
|
@@ -260,6 +260,34 @@ class TestReader < Nokogiri::TestCase
|
|
260
260
|
reader.map { |n| n.prefix }
|
261
261
|
end
|
262
262
|
|
263
|
+
def test_node_type
|
264
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
265
|
+
<x>
|
266
|
+
<y>hello</y>
|
267
|
+
</x>
|
268
|
+
eoxml
|
269
|
+
assert_equal 0, reader.node_type
|
270
|
+
assert_equal [1, 14, 1, 3, 15, 14, 15], reader.map { |n| n.node_type }
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_inner_xml
|
274
|
+
str = "<x><y>hello</y></x>"
|
275
|
+
reader = Nokogiri::XML::Reader.from_memory(str)
|
276
|
+
|
277
|
+
reader.read
|
278
|
+
|
279
|
+
assert_equal "<y>hello</y>", reader.inner_xml
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_outer_xml
|
283
|
+
str = "<x><y>hello</y></x>"
|
284
|
+
reader = Nokogiri::XML::Reader.from_memory(str)
|
285
|
+
|
286
|
+
reader.read
|
287
|
+
|
288
|
+
assert_equal str, reader.outer_xml
|
289
|
+
end
|
290
|
+
|
263
291
|
def test_state
|
264
292
|
reader = Nokogiri::XML::Reader.from_memory('<foo>bar</bar>')
|
265
293
|
assert reader.state
|
data/test/xml/sax/test_parser.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require "helper"
|
2
4
|
|
3
5
|
module Nokogiri
|
4
6
|
module XML
|
@@ -9,6 +11,61 @@ module Nokogiri
|
|
9
11
|
@parser = XML::SAX::Parser.new(Doc.new)
|
10
12
|
end
|
11
13
|
|
14
|
+
def test_parser_context_yielded_io
|
15
|
+
doc = Doc.new
|
16
|
+
parser = XML::SAX::Parser.new doc
|
17
|
+
xml = "<foo a='&b'/>"
|
18
|
+
|
19
|
+
block_called = false
|
20
|
+
parser.parse(StringIO.new(xml)) { |ctx|
|
21
|
+
block_called = true
|
22
|
+
ctx.replace_entities = true
|
23
|
+
}
|
24
|
+
|
25
|
+
assert block_called
|
26
|
+
|
27
|
+
assert_equal ['a', '&b'], doc.start_elements.first.last
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_parser_context_yielded_in_memory
|
31
|
+
doc = Doc.new
|
32
|
+
parser = XML::SAX::Parser.new doc
|
33
|
+
xml = "<foo a='&b'/>"
|
34
|
+
|
35
|
+
block_called = false
|
36
|
+
parser.parse(xml) { |ctx|
|
37
|
+
block_called = true
|
38
|
+
ctx.replace_entities = true
|
39
|
+
}
|
40
|
+
|
41
|
+
assert block_called
|
42
|
+
|
43
|
+
assert_equal ['a', '&b'], doc.start_elements.first.last
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_xml_decl
|
47
|
+
{
|
48
|
+
'' => nil,
|
49
|
+
'<?xml version="1.0" ?>' => ['1.0'],
|
50
|
+
'<?xml version="1.0" encoding="UTF-8" ?>' => ['1.0', 'UTF-8'],
|
51
|
+
'<?xml version="1.0" standalone="yes"?>' => ['1.0', 'yes'],
|
52
|
+
'<?xml version="1.0" standalone="no"?>' => ['1.0', 'no'],
|
53
|
+
}.each do |decl,value|
|
54
|
+
parser = XML::SAX::Parser.new(Doc.new)
|
55
|
+
|
56
|
+
xml = "#{decl}\n<root />"
|
57
|
+
parser.parse xml
|
58
|
+
assert parser.document.start_document_called, xml
|
59
|
+
assert_equal value, parser.document.xmldecls, xml
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_parse_empty
|
64
|
+
assert_raises RuntimeError do
|
65
|
+
@parser.parse('')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
12
69
|
def test_namespace_declaration_order_is_saved
|
13
70
|
@parser.parse <<-eoxml
|
14
71
|
<root xmlns:foo='http://foo.example.com/' xmlns='http://example.com/'>
|
@@ -114,11 +171,15 @@ module Nokogiri
|
|
114
171
|
assert_equal doc.errors.length, @parser.document.errors.length
|
115
172
|
end
|
116
173
|
|
117
|
-
def
|
174
|
+
def test_parse_with_memory_argument
|
175
|
+
@parser.parse(File.read(XML_FILE))
|
176
|
+
assert(@parser.document.cdata_blocks.length > 0)
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_parse_with_io_argument
|
118
180
|
File.open(XML_FILE, 'rb') { |f|
|
119
181
|
@parser.parse(f)
|
120
182
|
}
|
121
|
-
@parser.parse(File.read(XML_FILE))
|
122
183
|
assert(@parser.document.cdata_blocks.length > 0)
|
123
184
|
end
|
124
185
|
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require "helper"
|
4
|
+
|
5
|
+
module Nokogiri
|
6
|
+
module XML
|
7
|
+
module SAX
|
8
|
+
class TestParserContext < Nokogiri::SAX::TestCase
|
9
|
+
def test_replace_entities
|
10
|
+
pc = ParserContext.new StringIO.new('<root />'), 'UTF-8'
|
11
|
+
pc.replace_entities = false
|
12
|
+
assert_equal false, pc.replace_entities
|
13
|
+
|
14
|
+
pc.replace_entities = true
|
15
|
+
assert_equal true, pc.replace_entities
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_from_io
|
19
|
+
assert_nothing_raised do
|
20
|
+
ParserContext.new StringIO.new('fo'), 'UTF-8'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_from_string
|
25
|
+
assert_nothing_raised do
|
26
|
+
ParserContext.new 'blah blah'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_parse_with
|
31
|
+
ctx = ParserContext.new 'blah'
|
32
|
+
assert_raises ArgumentError do
|
33
|
+
ctx.parse_with nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_parse_with_sax_parser
|
38
|
+
assert_nothing_raised do
|
39
|
+
xml = "<root />"
|
40
|
+
ctx = ParserContext.new xml
|
41
|
+
parser = Parser.new Doc.new
|
42
|
+
ctx.parse_with parser
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_from_file
|
47
|
+
assert_nothing_raised do
|
48
|
+
ctx = ParserContext.file XML_FILE
|
49
|
+
parser = Parser.new Doc.new
|
50
|
+
ctx.parse_with parser
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|