libxml-ruby 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +14 -0
- data/LICENSE +1 -1
- data/README.rdoc +14 -19
- data/Rakefile +4 -5
- data/ext/libxml/extconf.rb +2 -0
- data/ext/libxml/libxml.c +1 -0
- data/ext/libxml/ruby_libxml.h +1 -0
- data/ext/libxml/ruby_xml_io.c +22 -0
- data/ext/libxml/ruby_xml_io.h +1 -0
- data/ext/libxml/ruby_xml_reader.c +115 -6
- data/ext/libxml/ruby_xml_schema.c +1 -1
- data/ext/libxml/ruby_xml_schema_type.c +7 -0
- data/ext/libxml/ruby_xml_version.h +3 -3
- data/ext/libxml/ruby_xml_writer.c +1072 -0
- data/ext/libxml/ruby_xml_writer.h +16 -0
- data/lib/libxml/error.rb +19 -20
- data/libxml-ruby.gemspec +1 -0
- data/test/tc_encoding_sax.rb +1 -2
- data/test/tc_error.rb +18 -0
- data/test/tc_reader.rb +43 -1
- data/test/tc_writer.rb +427 -0
- data/test/test_suite.rb +1 -0
- metadata +23 -3
@@ -0,0 +1,16 @@
|
|
1
|
+
#ifndef __RXML_WRITER__
|
2
|
+
# define __RXML_WRITER__
|
3
|
+
|
4
|
+
# include <libxml/xmlversion.h>
|
5
|
+
|
6
|
+
# ifdef LIBXML_WRITER_ENABLED
|
7
|
+
|
8
|
+
# include <libxml/xmlwriter.h>
|
9
|
+
|
10
|
+
extern VALUE cXMLWriter;
|
11
|
+
|
12
|
+
void rxml_init_writer(void);
|
13
|
+
|
14
|
+
# endif /* LIBXML_WRITER_ENABLED */
|
15
|
+
|
16
|
+
#endif /* __RXML_WRITER__ */
|
data/lib/libxml/error.rb
CHANGED
@@ -3,6 +3,23 @@
|
|
3
3
|
module LibXML
|
4
4
|
module XML
|
5
5
|
class Error
|
6
|
+
|
7
|
+
DOMAIN_CODE_MAP =Hash.new.tap do |map|
|
8
|
+
([:NO_ERROR, :PARSER, :TREE, :NAMESPACE, :DTD, :HTML, :MEMORY, :OUTPUT, :IO, :FTP, :HTTP, :XINCLUDE, :XPATH, :XPOINTER, :REGEXP, :DATATYPE, :SCHEMASP, :SCHEMASV, :RELAXNGP, :RELAXNGV, :CATALOG, :C14N, :XSLT, :VALID, :CHECK, :WRITER, :MODULE, :I18N, :SCHEMATRONV]
|
9
|
+
).each do |code|
|
10
|
+
map[const_get(code)] = code.to_s.gsub(/XML_ERR_/, '')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ERROR_CODE_MAP = Hash.new.tap do |map|
|
15
|
+
(constants -
|
16
|
+
DOMAIN_CODE_MAP.values - #Domains
|
17
|
+
[:NONE, :WARNING, :ERROR, :FATAL] # Levels
|
18
|
+
).each do |code|
|
19
|
+
map[const_get(code)] = code.to_s.gsub(/XML_ERR_/, '')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
6
23
|
# Verbose error handler
|
7
24
|
VERBOSE_HANDLER = lambda do |error|
|
8
25
|
STDERR << error.to_s << "\n"
|
@@ -47,29 +64,11 @@ module LibXML
|
|
47
64
|
end
|
48
65
|
|
49
66
|
def domain_to_s
|
50
|
-
|
51
|
-
domains = self.class.constants.grep(/XML_FROM/)
|
52
|
-
domains.each do |domain|
|
53
|
-
human_name = domain.gsub(/XML_FROM_/, '')
|
54
|
-
const_map[self.class.const_get(domain)] = human_name
|
55
|
-
end
|
56
|
-
|
57
|
-
const_map[self.domain]
|
67
|
+
DOMAIN_CODE_MAP[self.domain]
|
58
68
|
end
|
59
69
|
|
60
70
|
def code_to_s
|
61
|
-
|
62
|
-
codes = self.class.constants -
|
63
|
-
self.class.constants.grep(/XML_FROM/) -
|
64
|
-
["XML_ERR_NONE", "XML_ERR_WARNING", "XML_ERR_ERROR", "XML_ERR_FATAL"]
|
65
|
-
|
66
|
-
|
67
|
-
codes.each do |code|
|
68
|
-
human_name = code.gsub(/XML_ERR_/, '')
|
69
|
-
const_map[self.class.const_get(code)] = human_name
|
70
|
-
end
|
71
|
-
|
72
|
-
const_map[self.code]
|
71
|
+
ERROR_CODE_MAP[self.code]
|
73
72
|
end
|
74
73
|
|
75
74
|
def to_s
|
data/libxml-ruby.gemspec
CHANGED
data/test/tc_encoding_sax.rb
CHANGED
data/test/tc_error.rb
CHANGED
@@ -159,4 +159,22 @@ class TestError < Test::Unit::TestCase
|
|
159
159
|
end
|
160
160
|
assert_equal('Must specify a string with one or more characters', error.to_s)
|
161
161
|
end
|
162
|
+
|
163
|
+
def test_error_domain_to_s
|
164
|
+
exception = assert_raise(XML::Error) do
|
165
|
+
XML::Parser.string('<foo href="http://example.org/cgi?k1=v1&k2=v2"></foo>').parse
|
166
|
+
end
|
167
|
+
|
168
|
+
assert_equal(XML::Error::PARSER, exception.domain)
|
169
|
+
assert_equal("PARSER",exception.domain_to_s)
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_error_code_to_s
|
173
|
+
exception = assert_raise(XML::Error) do
|
174
|
+
XML::Parser.string('<foo href="http://example.org/cgi?k1=v1&k2=v2"></foo>').parse
|
175
|
+
end
|
176
|
+
assert_equal(XML::Error::ENTITYREF_SEMICOL_MISSING, exception.code)
|
177
|
+
assert_equal("ENTITYREF_SEMICOL_MISSING",exception.code_to_s)
|
178
|
+
end
|
179
|
+
|
162
180
|
end
|
data/test/tc_reader.rb
CHANGED
@@ -9,7 +9,7 @@ class TestReader < Test::Unit::TestCase
|
|
9
9
|
|
10
10
|
def verify_simple(reader)
|
11
11
|
node_types = []
|
12
|
-
|
12
|
+
|
13
13
|
# Read each node
|
14
14
|
26.times do
|
15
15
|
assert(reader.read)
|
@@ -145,6 +145,48 @@ class TestReader < Test::Unit::TestCase
|
|
145
145
|
assert_equal(nil, parser[2])
|
146
146
|
end
|
147
147
|
|
148
|
+
def test_move_to_attribute_depreciation
|
149
|
+
previous_stderr, $stderr = $stderr, StringIO.new
|
150
|
+
reader = XML::Reader.string("<foo x='1' y='2'/>")
|
151
|
+
assert(reader.read)
|
152
|
+
assert(reader.move_to_attribute 1)
|
153
|
+
assert($stderr.string =~ /deprecated/)
|
154
|
+
ensure
|
155
|
+
$stderr = previous_stderr
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_move_attr
|
159
|
+
reader = XML::Reader.string('<root xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xhtml="http://www.w3.org/1999/xhtml"><link xml:id="abc" xlink:href="def" xhtml:class="ghi" bar="jkl" /></root>')
|
160
|
+
assert(reader.read) # <root/>
|
161
|
+
assert(reader.read) # <link/>
|
162
|
+
|
163
|
+
assert(reader.move_to_attribute_no(1))
|
164
|
+
assert_equal(reader.value, 'def')
|
165
|
+
assert(reader.move_to_attribute_ns('id', 'http://www.w3.org/XML/1998/namespace'))
|
166
|
+
assert_equal(reader.value, 'abc')
|
167
|
+
assert(reader.move_to_attribute('bar'))
|
168
|
+
assert_equal(reader.value, 'jkl')
|
169
|
+
|
170
|
+
# 1 in case of success, -1 in case of error, 0 if not found
|
171
|
+
assert_equal(reader.move_to_attribute_no(12), 0)
|
172
|
+
assert_equal(reader.move_to_attribute('baz'), 0)
|
173
|
+
assert_equal(reader.move_to_attribute_ns('baz', 'http://ruby/namespace'), 0)
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_get_attr
|
177
|
+
reader = XML::Reader.string('<root xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xhtml="http://www.w3.org/1999/xhtml"><link xml:id="abc" xlink:href="def" xhtml:class="ghi" bar="jkl" /></root>')
|
178
|
+
assert(reader.read) # <root/>
|
179
|
+
assert(reader.read) # <link/>
|
180
|
+
|
181
|
+
assert_equal(reader.get_attribute_no(1), 'def')
|
182
|
+
assert_equal(reader.get_attribute_ns('id', 'http://www.w3.org/XML/1998/namespace'), 'abc')
|
183
|
+
assert_equal(reader.get_attribute('bar'), 'jkl')
|
184
|
+
|
185
|
+
assert_equal(reader.get_attribute_no(12), nil)
|
186
|
+
assert_equal(reader.get_attribute('baz'), nil)
|
187
|
+
assert_equal(reader.get_attribute_ns('baz', 'http://ruby/namespace'), nil)
|
188
|
+
end
|
189
|
+
|
148
190
|
def test_value
|
149
191
|
parser = XML::Reader.string("<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>")
|
150
192
|
assert(parser.read)
|
data/test/tc_writer.rb
ADDED
@@ -0,0 +1,427 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
4
|
+
require 'stringio'
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class TestWriter < Test::Unit::TestCase
|
8
|
+
XSL_PREFIX = 'xsl'
|
9
|
+
XSL_URI = 'http://www.w3.org/1999/XSL/Transform'
|
10
|
+
|
11
|
+
def test_generic_failure
|
12
|
+
writer = XML::Writer.string
|
13
|
+
writer.start_document
|
14
|
+
assert(!writer.end_element)
|
15
|
+
writer.end_document
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_empty_doc
|
19
|
+
writer = XML::Writer.string
|
20
|
+
document writer
|
21
|
+
assert_equal(writer.result.strip!, '<?xml version="1.0"?>')
|
22
|
+
|
23
|
+
writer = XML::Writer.string
|
24
|
+
document writer, { :encoding => XML::Encoding::ISO_8859_1 }
|
25
|
+
assert_equal(writer.result.strip!, '<?xml version="1.0" encoding="ISO-8859-1"?>')
|
26
|
+
|
27
|
+
writer = XML::Writer.string
|
28
|
+
document writer, { :standalone => 1 }
|
29
|
+
assert_equal(writer.result.strip!, '<?xml version="1.0" standalone="yes"?>')
|
30
|
+
|
31
|
+
writer = XML::Writer.string
|
32
|
+
document writer, { :standalone => 1, :encoding => XML::Encoding::ISO_8859_1, :foo => :bar }
|
33
|
+
assert_equal(writer.result.strip!, '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>')
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_single_root
|
37
|
+
writer = XML::Writer.string
|
38
|
+
document writer do
|
39
|
+
element writer, 'root'
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root/>")
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_pi
|
46
|
+
expected = "<?xml version=\"1.0\"?>\n<?php echo \"foo\";?>"
|
47
|
+
|
48
|
+
writer = XML::Writer.string
|
49
|
+
document writer do
|
50
|
+
assert(writer.start_pi('php'))
|
51
|
+
assert(writer.write_string('echo "foo";'))
|
52
|
+
assert(writer.end_pi)
|
53
|
+
end
|
54
|
+
assert_equal(writer.result.strip!, expected)
|
55
|
+
|
56
|
+
writer = XML::Writer.string
|
57
|
+
document writer do
|
58
|
+
assert(writer.write_pi('php', 'echo "foo";'))
|
59
|
+
end
|
60
|
+
assert_equal(writer.result.strip!, expected)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_comment
|
64
|
+
expected = "<?xml version=\"1.0\"?>\n<!--foo-->"
|
65
|
+
|
66
|
+
writer = XML::Writer.string
|
67
|
+
document writer do
|
68
|
+
assert(writer.start_comment)
|
69
|
+
assert(writer.write_string 'foo')
|
70
|
+
assert(writer.end_comment)
|
71
|
+
end
|
72
|
+
assert_equal(writer.result.strip!, expected)
|
73
|
+
|
74
|
+
writer = XML::Writer.string
|
75
|
+
document writer do
|
76
|
+
assert(writer.write_comment 'foo')
|
77
|
+
end
|
78
|
+
assert_equal(writer.result.strip!, expected)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_cdata
|
82
|
+
expected = "<?xml version=\"1.0\"?>\n<root><![CDATA[<?php echo $foo; ?>]]></root>"
|
83
|
+
|
84
|
+
writer = XML::Writer.string
|
85
|
+
document writer do
|
86
|
+
element writer, 'root' do
|
87
|
+
assert(writer.start_cdata)
|
88
|
+
assert(writer.write_string '<?php echo $foo; ?>')
|
89
|
+
assert(writer.end_cdata)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
assert_equal(writer.result.strip!, expected)
|
93
|
+
|
94
|
+
writer = XML::Writer.string
|
95
|
+
document writer do
|
96
|
+
element writer, 'root' do
|
97
|
+
assert(writer.write_cdata '<?php echo $foo; ?>')
|
98
|
+
end
|
99
|
+
end
|
100
|
+
assert_equal(writer.result.strip!, expected)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_write_empty_elements
|
104
|
+
writer = XML::Writer.string
|
105
|
+
document writer do
|
106
|
+
assert(writer.write_element 'foo')
|
107
|
+
end
|
108
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<foo/>")
|
109
|
+
|
110
|
+
writer = XML::Writer.string
|
111
|
+
document writer do
|
112
|
+
assert(writer.write_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
|
113
|
+
end
|
114
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<" + XSL_PREFIX + ":stylesheet xmlns:xsl=\"" + XSL_URI + "\"/>")
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_valued_element
|
118
|
+
expected = "<?xml version=\"1.0\"?>\n<abc>123<def>456<ghi>789</ghi>cueillir des cerises</def>nous irons au bois</abc>"
|
119
|
+
|
120
|
+
writer = XML::Writer.string
|
121
|
+
document writer do
|
122
|
+
assert(writer.start_element 'abc')
|
123
|
+
assert(writer.write_string '123')
|
124
|
+
assert(writer.start_element 'def')
|
125
|
+
assert(writer.write_string '456')
|
126
|
+
assert(writer.start_element 'ghi')
|
127
|
+
assert(writer.write_string '789')
|
128
|
+
assert(writer.end_element)
|
129
|
+
assert(writer.write_string 'cueillir des cerises')
|
130
|
+
assert(writer.end_element)
|
131
|
+
assert(writer.write_string 'nous irons au bois')
|
132
|
+
assert(writer.end_element)
|
133
|
+
end
|
134
|
+
assert_equal(writer.result.strip!, expected)
|
135
|
+
|
136
|
+
writer = XML::Writer.string
|
137
|
+
document writer do
|
138
|
+
assert(writer.start_element 'abc')
|
139
|
+
assert(writer.write_string '123')
|
140
|
+
assert(writer.start_element 'def')
|
141
|
+
assert(writer.write_string '456')
|
142
|
+
assert(writer.write_element 'ghi', '789')
|
143
|
+
assert(writer.write_string 'cueillir des cerises')
|
144
|
+
assert(writer.end_element)
|
145
|
+
assert(writer.write_string 'nous irons au bois')
|
146
|
+
assert(writer.end_element)
|
147
|
+
end
|
148
|
+
assert_equal(writer.result.strip!, expected)
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_valued_element_ns
|
152
|
+
expected = "<?xml version=\"1.0\"?>\n" +
|
153
|
+
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
|
154
|
+
"<xsl:attribute-set>" +
|
155
|
+
"<xsl:attribute>20px</xsl:attribute>" +
|
156
|
+
"<xsl:attribute>solid</xsl:attribute>" +
|
157
|
+
"</xsl:attribute-set>" +
|
158
|
+
"</xsl:stylesheet>"
|
159
|
+
|
160
|
+
writer = XML::Writer.string
|
161
|
+
document writer do
|
162
|
+
assert(writer.start_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
|
163
|
+
assert(writer.start_element_ns XSL_PREFIX, 'attribute-set')
|
164
|
+
assert(writer.start_element_ns XSL_PREFIX, 'attribute')
|
165
|
+
assert(writer.write_string '20px')
|
166
|
+
assert(writer.end_element)
|
167
|
+
assert(writer.start_element_ns XSL_PREFIX, 'attribute')
|
168
|
+
assert(writer.write_string 'solid')
|
169
|
+
assert(writer.end_element)
|
170
|
+
assert(writer.end_element)
|
171
|
+
assert(writer.end_element)
|
172
|
+
end
|
173
|
+
assert_equal(writer.result.strip!, expected)
|
174
|
+
|
175
|
+
writer = XML::Writer.string
|
176
|
+
document writer do
|
177
|
+
assert(writer.start_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
|
178
|
+
assert(writer.start_element_ns XSL_PREFIX, 'attribute-set')
|
179
|
+
assert(writer.write_element_ns XSL_PREFIX, 'attribute', nil, '20px')
|
180
|
+
assert(writer.write_element_ns XSL_PREFIX, 'attribute', nil, 'solid')
|
181
|
+
assert(writer.end_element)
|
182
|
+
assert(writer.end_element)
|
183
|
+
end
|
184
|
+
assert_equal(writer.result.strip!, expected)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_attribute
|
188
|
+
writer = XML::Writer.string
|
189
|
+
document writer do
|
190
|
+
element writer, 'root' do
|
191
|
+
element writer, 'child' do
|
192
|
+
assert(writer.start_attribute 'foo')
|
193
|
+
assert(writer.write_string 'bar')
|
194
|
+
assert(writer.end_attribute)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root><child foo=\"bar\"/></root>")
|
199
|
+
|
200
|
+
writer = XML::Writer.string
|
201
|
+
document writer do
|
202
|
+
element writer, 'root' do
|
203
|
+
element writer, 'child' do
|
204
|
+
assert(writer.write_attribute 'abc', 'def')
|
205
|
+
assert(writer.write_string 'ghi') # must be done after attributes
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root><child abc=\"def\">ghi</child></root>")
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_attribute_ns
|
213
|
+
expected = "<?xml version=\"1.0\"?>\n<root><link xlink:href=\"abc\" xhtml:class=\"def\"/></root>"
|
214
|
+
|
215
|
+
writer = XML::Writer.string
|
216
|
+
document writer do
|
217
|
+
element writer, 'root' do
|
218
|
+
element writer, 'link' do
|
219
|
+
assert(writer.write_attribute_ns 'xlink', 'href', nil, 'abc')
|
220
|
+
assert(writer.write_attribute_ns 'xhtml', 'class', nil, 'def')
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
assert_equal(writer.result.strip!, expected)
|
225
|
+
|
226
|
+
writer = XML::Writer.string
|
227
|
+
document writer do
|
228
|
+
element writer, 'root' do
|
229
|
+
element writer, 'link' do
|
230
|
+
assert(writer.start_attribute_ns 'xlink', 'href')
|
231
|
+
assert(writer.write_string 'abc')
|
232
|
+
assert(writer.end_attribute)
|
233
|
+
assert(writer.start_attribute_ns 'xhtml', 'class')
|
234
|
+
assert(writer.write_string 'def')
|
235
|
+
assert(writer.end_attribute)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
assert_equal(writer.result.strip!, expected)
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_quote_char
|
243
|
+
if XML::Writer.method_defined? :set_quote_char
|
244
|
+
writer = XML::Writer.string
|
245
|
+
writer.set_quote_char "'"
|
246
|
+
document writer do
|
247
|
+
element writer, 'root' do
|
248
|
+
assert(writer.start_attribute 'abc')
|
249
|
+
assert(writer.write_string 'def')
|
250
|
+
assert(writer.end_attribute)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root><child abc='def'>ghi</child></root>")
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_indentation_on
|
258
|
+
if XML::Writer.method_defined? :set_indent
|
259
|
+
writer = XML::Writer.string
|
260
|
+
assert(writer.set_indent true)
|
261
|
+
document writer do
|
262
|
+
element writer, 'root' do
|
263
|
+
element writer, 'child' do
|
264
|
+
assert(writer.start_attribute 'abc')
|
265
|
+
assert(writer.write_string 'def')
|
266
|
+
assert(writer.end_attribute)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root>\n <child abc=\"def\"/>\n</root>")
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_indentation_string
|
275
|
+
if XML::Writer.method_defined? :set_indent_string
|
276
|
+
writer = XML::Writer.string
|
277
|
+
assert(writer.set_indent true)
|
278
|
+
assert(writer.set_indent_string ' ' * 4)
|
279
|
+
document writer do
|
280
|
+
element writer, 'root' do
|
281
|
+
element writer, 'child' do
|
282
|
+
assert(writer.start_attribute 'abc')
|
283
|
+
assert(writer.write_string 'def')
|
284
|
+
assert(writer.end_attribute)
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root>\n <child abc=\"def\"/>\n</root>")
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_dtd_declaration
|
293
|
+
writer = XML::Writer.string
|
294
|
+
dtd writer, 'html'
|
295
|
+
assert_equal(writer.result, '<!DOCTYPE html>')
|
296
|
+
|
297
|
+
writer = XML::Writer.string
|
298
|
+
dtd writer, 'html', '-//W3C//DTD XHTML 1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
|
299
|
+
assert_equal(writer.result, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
|
300
|
+
end
|
301
|
+
|
302
|
+
def test_dtd_attlist
|
303
|
+
expected = '<!DOCTYPE http [<!ATTLIST method (get|post) "get">]>'
|
304
|
+
|
305
|
+
writer = XML::Writer.string
|
306
|
+
dtd writer, 'http' do
|
307
|
+
assert(writer.start_dtd_attlist 'method')
|
308
|
+
assert(writer.write_string '(get|post) "get"')
|
309
|
+
assert(writer.end_dtd_attlist)
|
310
|
+
end
|
311
|
+
assert_equal(writer.result, expected)
|
312
|
+
|
313
|
+
writer = XML::Writer.string
|
314
|
+
dtd writer, 'http' do
|
315
|
+
assert(writer.write_dtd_attlist 'method', '(get|post) "get"')
|
316
|
+
end
|
317
|
+
assert_equal(writer.result, expected)
|
318
|
+
end
|
319
|
+
|
320
|
+
def test_dtd_element
|
321
|
+
expected = '<!DOCTYPE html [<!ELEMENT dl (dt|dd)+>]>'
|
322
|
+
|
323
|
+
writer = XML::Writer.string
|
324
|
+
dtd writer, 'html' do
|
325
|
+
assert(writer.start_dtd_element 'dl')
|
326
|
+
assert(writer.write_string '(dt|dd)+')
|
327
|
+
assert(writer.end_dtd_element)
|
328
|
+
end
|
329
|
+
assert_equal(writer.result, expected)
|
330
|
+
|
331
|
+
writer = XML::Writer.string
|
332
|
+
dtd writer, 'html' do
|
333
|
+
assert(writer.write_dtd_element 'dl', '(dt|dd)+')
|
334
|
+
end
|
335
|
+
assert_equal(writer.result, expected)
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_dtd_entity
|
339
|
+
# parameterized entity
|
340
|
+
expected = '<!DOCTYPE html [<!ENTITY % special.pre "br | span | bdo | map"><!ENTITY % special "%special.pre; | object | img">]>'
|
341
|
+
|
342
|
+
writer = XML::Writer.string
|
343
|
+
dtd writer, 'html' do
|
344
|
+
assert(writer.start_dtd_entity 'special.pre', true)
|
345
|
+
assert(writer.write_string 'br | span | bdo | map')
|
346
|
+
assert(writer.end_dtd_entity)
|
347
|
+
assert(writer.start_dtd_entity 'special', true)
|
348
|
+
assert(writer.write_string '%special.pre; | object | img')
|
349
|
+
assert(writer.end_dtd_entity)
|
350
|
+
end
|
351
|
+
assert_equal(writer.result, expected)
|
352
|
+
|
353
|
+
writer = XML::Writer.string
|
354
|
+
dtd writer, 'html' do
|
355
|
+
assert(writer.write_dtd_internal_entity 'special.pre', 'br | span | bdo | map', true)
|
356
|
+
assert(writer.write_dtd_internal_entity 'special', '%special.pre; | object | img', true)
|
357
|
+
end
|
358
|
+
assert_equal(writer.result, expected)
|
359
|
+
|
360
|
+
# non parameterized entity
|
361
|
+
expected = '<!DOCTYPE html [<!ENTITY Alpha "Α">]>'
|
362
|
+
|
363
|
+
writer = XML::Writer.string
|
364
|
+
dtd writer, 'html' do
|
365
|
+
assert(writer.start_dtd_entity 'Alpha')
|
366
|
+
assert(writer.write_string 'Α')
|
367
|
+
assert(writer.end_dtd_entity)
|
368
|
+
end
|
369
|
+
assert_equal(writer.result, expected)
|
370
|
+
|
371
|
+
writer = XML::Writer.string
|
372
|
+
dtd writer, 'html' do
|
373
|
+
assert(writer.start_dtd_entity 'Alpha', false)
|
374
|
+
assert(writer.write_string 'Α')
|
375
|
+
assert(writer.end_dtd_entity)
|
376
|
+
end
|
377
|
+
assert_equal(writer.result, expected)
|
378
|
+
|
379
|
+
writer = XML::Writer.string
|
380
|
+
dtd writer, 'html' do
|
381
|
+
assert(writer.write_dtd_internal_entity 'Alpha', 'Α', false)
|
382
|
+
end
|
383
|
+
assert_equal(writer.result, expected)
|
384
|
+
end
|
385
|
+
|
386
|
+
def test_dtd_notation
|
387
|
+
writer = XML::Writer.string
|
388
|
+
dtd writer, 'pictures' do
|
389
|
+
assert(writer.write_dtd_notation 'GIF89a', '-//Compuserve//NOTATION Graphics Interchange Format 89a//EN', nil)
|
390
|
+
assert(writer.write_dtd_external_entity 'pictures', nil, 'images/plage.gif', 'GIF89a', false)
|
391
|
+
end
|
392
|
+
assert_equal(writer.result, '<!DOCTYPE pictures [<!NOTATION GIF89a PUBLIC "-//Compuserve//NOTATION Graphics Interchange Format 89a//EN"><!ENTITY pictures SYSTEM "images/plage.gif" NDATA GIF89a>]>')
|
393
|
+
end
|
394
|
+
|
395
|
+
def test_encoding
|
396
|
+
if defined?(Encoding)
|
397
|
+
iso = 'éloïse'.encode 'ISO-8859-1'
|
398
|
+
|
399
|
+
writer = XML::Writer.string
|
400
|
+
document writer do
|
401
|
+
assert(writer.write_element iso)
|
402
|
+
end
|
403
|
+
assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<éloïse/>")
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
private
|
408
|
+
|
409
|
+
def document(writer, options = {})
|
410
|
+
assert(writer.start_document options)
|
411
|
+
yield if block_given?
|
412
|
+
assert(writer.end_document)
|
413
|
+
end
|
414
|
+
|
415
|
+
def dtd(writer, name, pubid = nil, sysid = nil)
|
416
|
+
assert(writer.start_dtd name, pubid, sysid)
|
417
|
+
yield if block_given?
|
418
|
+
assert(writer.end_dtd)
|
419
|
+
writer.flush
|
420
|
+
end
|
421
|
+
|
422
|
+
def element(writer, localname)
|
423
|
+
assert(writer.start_element localname)
|
424
|
+
yield if block_given?
|
425
|
+
assert(writer.end_element)
|
426
|
+
end
|
427
|
+
end
|