smile-xml 1.0.0-jruby → 1.0.2-jruby

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/lib/smile-xml.jar +0 -0
  2. data/src/main/java/smile/xml/AttrJ.java +160 -0
  3. data/src/main/java/smile/xml/AttributesJ.java +212 -0
  4. data/src/main/java/smile/xml/BaseJ.java +60 -0
  5. data/src/main/java/smile/xml/DocumentJ.java +435 -0
  6. data/src/main/java/smile/xml/EncodingJ.java +94 -0
  7. data/src/main/java/smile/xml/ErrorJ.java +162 -0
  8. data/src/main/java/smile/xml/NamespaceJ.java +67 -0
  9. data/src/main/java/smile/xml/NamespacesJ.java +62 -0
  10. data/src/main/java/smile/xml/NodeJ.java +1029 -0
  11. data/src/main/java/smile/xml/NodeSetJ.java +90 -0
  12. data/src/main/java/smile/xml/ParserContextJ.java +44 -0
  13. data/src/main/java/smile/xml/ParserJ.java +196 -0
  14. data/src/main/java/smile/xml/ParserOptionsJ.java +58 -0
  15. data/src/main/java/smile/xml/ReaderJ.java +34 -0
  16. data/src/main/java/smile/xml/SchemaJ.java +66 -0
  17. data/src/main/java/smile/xml/SmileXML.java +65 -0
  18. data/src/main/java/smile/xml/XmlJ.java +58 -0
  19. data/src/main/java/smile/xml/sax/CallbackHandler.java +113 -0
  20. data/src/main/java/smile/xml/sax/SaxParserCallbacksJ.java +71 -0
  21. data/src/main/java/smile/xml/sax/SaxParserJ.java +153 -0
  22. data/src/main/java/smile/xml/util/UtilJ.java +424 -0
  23. data/src/main/java/smile/xml/xpath/CustomNamespaceContext.java +59 -0
  24. data/src/main/java/smile/xml/xpath/XPathContextJ.java +154 -0
  25. data/src/main/java/smile/xml/xpath/XPathExpressionJ.java +62 -0
  26. data/src/main/java/smile/xml/xpath/XPathJ.java +36 -0
  27. data/src/main/java/smile/xml/xpath/XPathObjectJ.java +196 -0
  28. data/src/main/java/smile/xml/xpath/XPointerJ.java +32 -0
  29. data/src/main/ruby/xml/libxml.rb +1 -0
  30. data/src/main/ruby/xml.rb +5 -0
  31. data/src/test/ruby/etc_doc_to_s.rb +21 -0
  32. data/src/test/ruby/ets_doc_file.rb +17 -0
  33. data/src/test/ruby/ets_doc_to_s.rb +23 -0
  34. data/src/test/ruby/ets_gpx.rb +28 -0
  35. data/src/test/ruby/ets_node_gc.rb +23 -0
  36. data/src/test/ruby/ets_test.xml +2 -0
  37. data/src/test/ruby/ets_tsr.rb +11 -0
  38. data/src/test/ruby/model/atom.xml +13 -0
  39. data/src/test/ruby/model/bands.iso-8859-1.xml +5 -0
  40. data/src/test/ruby/model/bands.utf-8.xml +5 -0
  41. data/src/test/ruby/model/bands.xml +5 -0
  42. data/src/test/ruby/model/books.xml +146 -0
  43. data/src/test/ruby/model/merge_bug_data.xml +58 -0
  44. data/src/test/ruby/model/ruby-lang.html +238 -0
  45. data/src/test/ruby/model/rubynet.xml +79 -0
  46. data/src/test/ruby/model/shiporder.rnc +28 -0
  47. data/src/test/ruby/model/shiporder.rng +86 -0
  48. data/src/test/ruby/model/shiporder.xml +23 -0
  49. data/src/test/ruby/model/shiporder.xsd +31 -0
  50. data/src/test/ruby/model/soap.xml +27 -0
  51. data/src/test/ruby/model/xinclude.xml +5 -0
  52. data/src/test/ruby/smile_xml_test.rb +64 -0
  53. data/src/test/ruby/tc_attr.rb +191 -0
  54. data/src/test/ruby/tc_attr_decl.rb +133 -0
  55. data/src/test/ruby/tc_attributes.rb +135 -0
  56. data/src/test/ruby/tc_deprecated_require.rb +13 -0
  57. data/src/test/ruby/tc_document.rb +162 -0
  58. data/src/test/ruby/tc_document_write.rb +212 -0
  59. data/src/test/ruby/tc_dtd.rb +125 -0
  60. data/src/test/ruby/tc_error.rb +150 -0
  61. data/src/test/ruby/tc_html_parser.rb +140 -0
  62. data/src/test/ruby/tc_namespace.rb +62 -0
  63. data/src/test/ruby/tc_namespaces.rb +210 -0
  64. data/src/test/ruby/tc_node.rb +273 -0
  65. data/src/test/ruby/tc_node_cdata.rb +51 -0
  66. data/src/test/ruby/tc_node_comment.rb +33 -0
  67. data/src/test/ruby/tc_node_copy.rb +42 -0
  68. data/src/test/ruby/tc_node_edit.rb +178 -0
  69. data/src/test/ruby/tc_node_text.rb +73 -0
  70. data/src/test/ruby/tc_node_write.rb +108 -0
  71. data/src/test/ruby/tc_node_xlink.rb +29 -0
  72. data/src/test/ruby/tc_parser.rb +371 -0
  73. data/src/test/ruby/tc_parser_context.rb +189 -0
  74. data/src/test/ruby/tc_properties.rb +40 -0
  75. data/src/test/ruby/tc_reader.rb +306 -0
  76. data/src/test/ruby/tc_relaxng.rb +54 -0
  77. data/src/test/ruby/tc_sax_parser.rb +340 -0
  78. data/src/test/ruby/tc_schema.rb +59 -0
  79. data/src/test/ruby/tc_traversal.rb +222 -0
  80. data/src/test/ruby/tc_xinclude.rb +21 -0
  81. data/src/test/ruby/tc_xml.rb +226 -0
  82. data/src/test/ruby/tc_xpath.rb +210 -0
  83. data/src/test/ruby/tc_xpath_context.rb +80 -0
  84. data/src/test/ruby/tc_xpath_expression.rb +38 -0
  85. data/src/test/ruby/tc_xpointer.rb +74 -0
  86. data/src/test/ruby/test_helper.rb +23 -0
  87. data/src/test/ruby/test_suite.rb +41 -0
  88. metadata +91 -5
@@ -0,0 +1,210 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ require 'test/unit'
6
+
7
+ class TestNamespaces < Test::Unit::TestCase
8
+ def setup
9
+ file = File.join(File.dirname(__FILE__), 'model/soap.xml')
10
+ @doc = XML::Document.file(file)
11
+ end
12
+
13
+ def teardown
14
+ @doc = nil
15
+ end
16
+
17
+ def test_namespace_node
18
+ node = @doc.root
19
+ ns = node.namespaces.namespace
20
+ assert_equal('soap', ns.prefix)
21
+ assert_equal('http://schemas.xmlsoap.org/soap/envelope/', ns.href)
22
+ end
23
+
24
+ def test_namespace_attr
25
+ node = @doc.root
26
+ attr = node.attributes.get_attribute('encodingStyle')
27
+ assert_equal('soap', attr.ns.prefix)
28
+ assert_equal('soap', attr.namespaces.namespace.prefix)
29
+ end
30
+
31
+ def test_set_namespace_node
32
+ node = XML::Node.new('Envelope')
33
+ assert_equal('<Envelope/>', node.to_s)
34
+
35
+ ns = XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
36
+ assert_equal("<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
37
+ assert_nil(node.namespaces.namespace)
38
+
39
+ # Now put the node in the soap namespace
40
+ node.namespaces.namespace = ns
41
+ assert_not_nil(node.namespaces.namespace)
42
+ assert_equal("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
43
+ end
44
+
45
+ def test_set_namespace_attribute
46
+ # Create node
47
+ node = XML::Node.new('Envelope')
48
+ assert_equal('<Envelope/>', node.to_s)
49
+
50
+ # Create attribute
51
+ attr = XML::Attr.new(node, "encodingStyle", "http://www.w3.org/2001/12/soap-encoding")
52
+ assert_equal('<Envelope encodingStyle="http://www.w3.org/2001/12/soap-encoding"/>',
53
+ node.to_s)
54
+
55
+ # Create namespace attribute
56
+ ns = XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
57
+ assert_equal('<Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" encodingStyle="http://www.w3.org/2001/12/soap-encoding"/>',
58
+ node.to_s)
59
+ assert_nil(node.namespaces.namespace)
60
+
61
+ # Now put the node in the soap namespace
62
+ node.namespaces.namespace = ns
63
+ assert_not_nil(node.namespaces.namespace)
64
+ assert_equal('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" encodingStyle="http://www.w3.org/2001/12/soap-encoding"/>',
65
+ node.to_s)
66
+
67
+ # Now put the attribute in the soap namespace
68
+ attr.namespaces.namespace = ns
69
+ assert_not_nil(node.namespaces.namespace)
70
+ assert_equal('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"/>',
71
+ node.to_s)
72
+ end
73
+
74
+ def test_define_namespace
75
+ node = XML::Node.new('Envelope')
76
+ assert_equal('<Envelope/>', node.to_s)
77
+
78
+ XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
79
+ assert_equal("<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
80
+ assert_nil(node.namespaces.namespace)
81
+ end
82
+
83
+ def test_define_default_namespace
84
+ node = XML::Node.new('Envelope')
85
+ assert_equal('<Envelope/>', node.to_s)
86
+
87
+ XML::Namespace.new(node, nil, 'http://schemas.xmlsoap.org/soap/envelope/')
88
+ assert_equal("<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
89
+ # This seems wrong, but appears to be the way libxml works
90
+ assert_nil(node.namespaces.namespace)
91
+ end
92
+
93
+ def test_namespaces
94
+ node = @doc.find_first('//ns1:IdAndName',
95
+ :ns1 => 'http://domain.somewhere.com')
96
+
97
+ namespaces = node.namespaces.sort
98
+ assert_equal(5, namespaces.length)
99
+
100
+ namespace = namespaces[0]
101
+ assert_instance_of(XML::Namespace, namespace)
102
+ assert_equal(nil, namespace.prefix)
103
+ assert_equal('http://services.somewhere.com', namespace.href)
104
+
105
+ namespace = namespaces[1]
106
+ assert_instance_of(XML::Namespace, namespace)
107
+ assert_equal('ns1', namespace.prefix)
108
+ assert_equal('http://domain.somewhere.com', namespace.href)
109
+
110
+ namespace = namespaces[2]
111
+ assert_instance_of(XML::Namespace, namespace)
112
+ assert_equal('soap', namespace.prefix)
113
+ assert_equal('http://schemas.xmlsoap.org/soap/envelope/', namespace.href)
114
+
115
+ namespace = namespaces[3]
116
+ assert_instance_of(XML::Namespace, namespace)
117
+ assert_equal('xsd', namespace.prefix)
118
+ assert_equal('http://www.w3.org/2001/XMLSchema', namespace.href)
119
+
120
+ namespace = namespaces[4]
121
+ assert_instance_of(XML::Namespace, namespace)
122
+ assert_equal('xsi', namespace.prefix)
123
+ assert_equal('http://www.w3.org/2001/XMLSchema-instance', namespace.href)
124
+ end
125
+
126
+ def test_namespaces
127
+ node = @doc.find_first('//ns1:IdAndName',
128
+ :ns1 => 'http://domain.somewhere.com')
129
+
130
+ node.namespaces.each do |namespace|
131
+ assert_instance_of(XML::Namespace, namespace)
132
+ end
133
+ end
134
+
135
+ def test_namespace_definitions
136
+ ns_defs = @doc.root.namespaces.definitions
137
+ assert_equal(3, ns_defs.size)
138
+
139
+ namespace = ns_defs[0]
140
+ assert_instance_of(XML::Namespace, namespace)
141
+ assert_equal('soap', namespace.prefix)
142
+ assert_equal('http://schemas.xmlsoap.org/soap/envelope/', namespace.href)
143
+
144
+ namespace = ns_defs[1]
145
+ assert_instance_of(XML::Namespace, namespace)
146
+ assert_equal('xsd', namespace.prefix)
147
+ assert_equal('http://www.w3.org/2001/XMLSchema', namespace.href)
148
+
149
+ namespace = ns_defs[2]
150
+ assert_instance_of(XML::Namespace, namespace)
151
+ assert_equal('xsi', namespace.prefix)
152
+ assert_equal('http://www.w3.org/2001/XMLSchema-instance', namespace.href)
153
+
154
+ node = @doc.root.find_first('//ns:getManufacturerNamesResponse',
155
+ :ns => 'http://services.somewhere.com')
156
+ ns_defs = node.namespaces.definitions
157
+ assert_equal(1, ns_defs.size)
158
+
159
+ namespace = ns_defs[0]
160
+ assert_instance_of(XML::Namespace, namespace)
161
+ assert_equal(nil, namespace.prefix)
162
+ assert_equal('http://services.somewhere.com', namespace.href)
163
+ end
164
+
165
+ def test_find_by_prefix
166
+ namespace = @doc.root.namespaces.find_by_prefix('soap')
167
+
168
+ assert_instance_of(XML::Namespace, namespace)
169
+ assert_equal('soap', namespace.prefix)
170
+ assert_equal('http://schemas.xmlsoap.org/soap/envelope/', namespace.href)
171
+ end
172
+
173
+ def test_find_default_ns
174
+ namespace = @doc.root.namespaces.find_by_prefix(nil)
175
+ assert_nil(namespace)
176
+
177
+ node = @doc.find_first('//ns1:getManufacturerNamesResponse',
178
+ :ns1 => 'http://services.somewhere.com')
179
+ namespace = node.namespaces.find_by_prefix(nil)
180
+
181
+ assert_instance_of(XML::Namespace, namespace)
182
+ assert_equal(nil, namespace.prefix)
183
+ assert_equal('http://services.somewhere.com', namespace.href)
184
+ end
185
+
186
+ def test_find_ns_by_href
187
+ node = @doc.find_first('//ns1:getManufacturerNamesResponse',
188
+ :ns1 => 'http://services.somewhere.com')
189
+
190
+ namespace = node.namespaces.find_by_href('http://schemas.xmlsoap.org/soap/envelope/')
191
+
192
+ assert_instance_of(XML::Namespace, namespace)
193
+ assert_equal('soap', namespace.prefix)
194
+ assert_equal('http://schemas.xmlsoap.org/soap/envelope/', namespace.href)
195
+ end
196
+
197
+ def test_default_namespace
198
+ doc = XML::Document.string('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"/>')
199
+ ns = doc.root.namespaces.default
200
+ assert_equal(ns.href, 'http://schemas.xmlsoap.org/soap/envelope/')
201
+ end
202
+
203
+ def test_default_prefix
204
+ doc = XML::Document.string('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"/>')
205
+ doc.root.namespaces.default_prefix = 'soap'
206
+
207
+ node = doc.root.find_first('/soap:Envelope')
208
+ assert_not_nil(node)
209
+ end
210
+ end
@@ -0,0 +1,273 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class TestNode < Test::Unit::TestCase
7
+ def setup
8
+ load_encoding("utf-8")
9
+ end
10
+
11
+ def teardown
12
+ XML.default_keep_blanks = true
13
+ @doc = nil
14
+ end
15
+
16
+ def load_encoding(name)
17
+ @encoding = Encoding.find(name) if defined?(Encoding)
18
+ @file_name = "model/bands.#{name.downcase}.xml"
19
+
20
+ # Strip spaces to make testing easier
21
+ XML.default_keep_blanks = false
22
+ file = File.join(File.dirname(__FILE__), @file_name)
23
+ @doc = XML::Document.file(file)
24
+ end
25
+
26
+ def nodes
27
+ # Find all nodes with a country attributes
28
+ @doc.find('//*[@country]')
29
+ end
30
+
31
+ def test_doc_class
32
+ assert_instance_of(XML::Document, @doc)
33
+ end
34
+
35
+ def test_doc_node_type
36
+ assert_equal XML::Node::DOCUMENT_NODE, @doc.node_type
37
+ end
38
+
39
+ def test_root_class
40
+ assert_instance_of(XML::Node, @doc.root)
41
+ end
42
+
43
+ def test_root_node_type
44
+ assert_equal XML::Node::ELEMENT_NODE, @doc.root.node_type
45
+ end
46
+
47
+ def test_node_class
48
+ for n in nodes
49
+ assert_instance_of(XML::Node, n)
50
+ end
51
+ end
52
+
53
+ def test_context
54
+ node = @doc.root
55
+ context = node.context
56
+ assert_instance_of(XML::XPath::Context, context)
57
+ end
58
+
59
+ def test_find
60
+ assert_instance_of(XML::XPath::Object, self.nodes)
61
+ end
62
+
63
+ def test_node_child_get
64
+ assert_instance_of(TrueClass, @doc.root.child?)
65
+ assert_instance_of(XML::Node, @doc.root.child)
66
+
67
+ if defined?(Encoding)
68
+ assert_equal(@encoding, @doc.root.child.name.encoding)
69
+ assert_equal("m\u00F6tley_cr\u00FCe", @doc.root.child.name)
70
+ else
71
+ assert_equal("m\303\266tley_cr\303\274e", @doc.root.child.name)
72
+ end
73
+ end
74
+
75
+ def test_node_doc
76
+ for n in nodes
77
+ assert_instance_of(XML::Document, n.doc) if n.document?
78
+ end
79
+ end
80
+
81
+ def test_name
82
+ #assert_equal("m\303\266tley_cr\303\274e", )
83
+ if defined?(Encoding)
84
+ assert_equal(@encoding, nodes[0].name.encoding)
85
+ assert_equal("m\u00F6tley_cr\u00FCe", nodes[0].name)
86
+ else
87
+ assert_equal("m\303\266tley_cr\303\274e", nodes[0].name)
88
+ end
89
+ assert_equal("iron_maiden", nodes[1].name)
90
+ end
91
+
92
+ def test_name_iso_8859_1
93
+ load_encoding("iso-8859-1")
94
+
95
+ if defined?(Encoding)
96
+ assert_equal(@encoding, nodes[0].name.encoding)
97
+ assert_equal("m\303\266tley_cr\303\274e".force_encoding(@encoding), nodes[0].name)
98
+ else
99
+ assert_equal("m\303\266tley_cr\303\274e", nodes[0].name)
100
+ end
101
+ assert_equal("iron_maiden", nodes[1].name)
102
+ end
103
+
104
+ def test_node_find
105
+ nodes = @doc.root.find('./fixnum')
106
+ for node in nodes
107
+ assert_instance_of(XML::Node, node)
108
+ end
109
+ end
110
+
111
+ def test_equality
112
+ node_a = @doc.find_first('//*[@country]')
113
+ node_b = @doc.root.child
114
+
115
+ assert(node_a == node_b)
116
+ assert(node_a.eql?(node_b))
117
+ assert(node_a.equal?(node_b))
118
+
119
+ file = File.join(File.dirname(__FILE__), @file_name)
120
+ doc2 = XML::Document.file(file)
121
+
122
+ node_a2 = doc2.find_first('//*[@country]')
123
+
124
+ assert_equal(node_a.to_s, node_a2.to_s)
125
+ assert_equal(node_a, node_a2)
126
+ assert(node_a.eql?(node_a2))
127
+ assert(!node_a.equal?(node_a2))
128
+ end
129
+
130
+ def test_equality_nil
131
+ node = @doc.root
132
+ assert(node != nil)
133
+ end
134
+
135
+ def test_equality_wrong_type
136
+ node = @doc.root
137
+
138
+ assert_raise(TypeError) do
139
+ assert(node != 'abc')
140
+ end
141
+ end
142
+
143
+ def test_content
144
+ if defined?(Encoding)
145
+ assert_equal(@encoding, @doc.root.content.encoding)
146
+ assert_equal("M\u00F6tley Cr\u00FCe is an American heavy metal band formed in Los Angeles, California in 1981.Iron Maiden is a British heavy metal band formed in 1975.",
147
+ @doc.root.content)
148
+
149
+ first = @doc.root.child
150
+ assert_equal("M\u00F6tley Cr\u00FCe is an American heavy metal band formed in Los Angeles, California in 1981.",
151
+ first.content)
152
+ assert_equal("Iron Maiden is a British heavy metal band formed in 1975.", first.next.content)
153
+ else
154
+ assert_equal("M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.Iron Maiden is a British heavy metal band formed in 1975.",
155
+ @doc.root.content.split("\n").map{|t| t.strip }.join )
156
+
157
+ first = @doc.root.child
158
+ assert_equal("M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.",
159
+ first.content)
160
+ assert_equal("Iron Maiden is a British heavy metal band formed in 1975.",
161
+ first.next.content)
162
+ end
163
+ end
164
+
165
+ def test_content_iso_8859_1
166
+ load_encoding("iso-8859-1")
167
+ if defined?(Encoding)
168
+ assert_equal(@encoding, @doc.root.content.encoding)
169
+ assert_equal("M\xC3\xB6tley Cr\xC3\xBCe is an American heavy metal band formed in Los Angeles, California in 1981.Iron Maiden is a British heavy metal band formed in 1975.".force_encoding(@encoding),
170
+ @doc.root.content)
171
+
172
+ first = @doc.root.child
173
+ assert_equal("M\xC3\xB6tley Cr\xC3\xBCe is an American heavy metal band formed in Los Angeles, California in 1981.".force_encoding(@encoding),
174
+ first.content)
175
+ assert_equal("Iron Maiden is a British heavy metal band formed in 1975.", first.next.content)
176
+ else
177
+ assert_equal("M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.Iron Maiden is a British heavy metal band formed in 1975.",
178
+ @doc.root.content.split("\n").map{|t| t.strip }.join )
179
+
180
+ first = @doc.root.child
181
+ assert_equal("M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.",
182
+ first.content)
183
+ assert_equal("Iron Maiden is a British heavy metal band formed in 1975.", first.next.content)
184
+ end
185
+ end
186
+
187
+ def test_base
188
+ doc = XML::Parser.string('<person />').parse
189
+ assert_nil(doc.root.base_uri)
190
+ end
191
+
192
+ # We use the same facility that libXSLT does here to disable output escaping.
193
+ # This lets you specify that the node's content should be rendered unaltered
194
+ # whenever it is being output. This is useful for things like <script> and
195
+ # <style> nodes in HTML documents if you don't want to be forced to wrap them
196
+ # in CDATA nodes. Or if you are sanitizing existing HTML documents and want
197
+ # to preserve the content of any of the text nodes.
198
+ #
199
+ # TODO
200
+ # def test_output_escaping
201
+ # text = '<bad-script>if (a &lt; b || b &gt; c) { return "text"; }<stop/>return "&gt;&gt;&gt;snip&lt;&lt;&lt;";</bad-script>'
202
+ # node = XML::Parser.string(text).parse.root
203
+ # assert_equal text, node.to_s
204
+ #
205
+ # text_noenc = '<bad-script>if (a < b || b > c) { return "text"; }<stop/>return ">>>snip<<<";</bad-script>'
206
+ # node.output_escaping = false
207
+ # assert_equal text_noenc, node.to_s
208
+ #
209
+ # node.output_escaping = true
210
+ # assert_equal text, node.to_s
211
+ #
212
+ # node.output_escaping = nil
213
+ # assert_equal text_noenc, node.to_s
214
+ #
215
+ # node.output_escaping = true
216
+ # assert_equal text, node.to_s
217
+ # end
218
+ #
219
+ # # Just a sanity check for output escaping.
220
+ # def test_output_escaping_sanity
221
+ # text = '<bad-script>if (a &lt; b || b &gt; c) { return "text"; }<stop/>return "&gt;&gt;&gt;snip&lt;&lt;&lt;";</bad-script>'
222
+ # node = XML::Parser.string(text).parse.root
223
+ # affected = node.find('//text()')
224
+ #
225
+ # check_escaping = lambda do |flag|
226
+ # assert_equal('bad-script', node.name)
227
+ # assert_equal(flag, node.output_escaping?)
228
+ # affected.each do |x|
229
+ # assert_equal(flag ? 'text' : 'textnoenc', x.name)
230
+ # assert_equal(flag, x.output_escaping?)
231
+ # end
232
+ # end
233
+ #
234
+ # node.output_escaping = false
235
+ # check_escaping[false]
236
+ #
237
+ # node.output_escaping = true
238
+ # check_escaping[true]
239
+ #
240
+ # node.output_escaping = nil
241
+ # check_escaping[false]
242
+ #
243
+ # node.output_escaping = true
244
+ # check_escaping[true]
245
+ #
246
+ # affected.first.output_escaping = true
247
+ # affected.last.output_escaping = false
248
+ # assert node.output_escaping?.nil?
249
+ # end
250
+
251
+ def test_space_preserve
252
+ node = @doc.root
253
+
254
+ node.space_preserve = false
255
+ assert_equal XML::Node::SPACE_DEFAULT, node.space_preserve
256
+
257
+ node.space_preserve = true
258
+ assert_equal XML::Node::SPACE_PRESERVE, node.space_preserve
259
+ end
260
+
261
+ def test_empty
262
+ text = '<name> </name>'
263
+ doc = XML::Parser.string(text).parse
264
+
265
+ node = doc.root
266
+ assert(!node.empty?)
267
+
268
+ text_node = node.first
269
+ # TODO first node is nil
270
+ # assert(text_node.empty?)
271
+ end
272
+
273
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class CDataCommentTest < Test::Unit::TestCase
7
+ def setup
8
+ xp = XML::Parser.string('<root></root>')
9
+ @doc = xp.parse
10
+ assert_instance_of(XML::Document, @doc)
11
+ @root = @doc.root
12
+ end
13
+
14
+ def test_node_type
15
+ cnode = XML::Node.new_cdata('test cdata')
16
+ assert_equal(XML::Node::CDATA_SECTION_NODE, cnode.node_type)
17
+ end
18
+
19
+ def test_add_cdata
20
+ @root << XML::Node.new_cdata('mycdata')
21
+ assert_equal '<root><![CDATA[mycdata]]></root>',
22
+ @root.to_s.gsub(/\n\s*/,'')
23
+ end
24
+
25
+ def test_add_cdata_2
26
+ @root << XML::Node.new_cdata('mycdata')
27
+ assert_equal 'cdata',
28
+ @root.child.node_type_name
29
+ end
30
+
31
+ def test_add_cdata_3
32
+ @root << el = XML::Node.new_cdata('mycdata')
33
+ el << "_this_is_added"
34
+ assert_equal '<root><![CDATA[mycdata_this_is_added]]></root>',
35
+ @root.to_s.gsub(/\n\s*/,'')
36
+ end
37
+
38
+ def test_attributes
39
+ cnode = XML::Node.new_cdata('test cdata')
40
+ assert_equal(0, cnode.attributes.length)
41
+ end
42
+
43
+ def test_set_cdata_attribute
44
+ cnode = XML::Node.new_cdata('test cdata')
45
+
46
+ # Can't create attributes on non-element nodes
47
+ assert_raise(ArgumentError) do
48
+ cnode['attr'] = '123'
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ require 'test/unit'
6
+
7
+ class NodeCommentTest < Test::Unit::TestCase
8
+ def setup
9
+ xp = XML::Parser.string('<root></root>')
10
+ @doc = xp.parse
11
+ assert_instance_of(XML::Document, @doc)
12
+ @root = @doc.root
13
+ end
14
+
15
+ def test_libxml_node_add_comment_01
16
+ @root << XML::Node.new_comment('mycomment')
17
+ assert_equal '<root><!--mycomment--></root>',
18
+ @root.to_s.gsub(/\n\s*/,'')
19
+ end
20
+
21
+ def test_libxml_node_add_comment_02
22
+ @root << XML::Node.new_comment('mycomment')
23
+ assert_equal 'comment',
24
+ @root.child.node_type_name
25
+ end
26
+
27
+ def test_libxml_node_add_comment_03
28
+ @root << el = XML::Node.new_comment('mycomment')
29
+ el << "_this_is_added"
30
+ assert_equal '<root><!--mycomment_this_is_added--></root>',
31
+ @root.to_s.gsub(/\n\s*/,'')
32
+ end
33
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ # see mailing list archive
7
+ # [libxml-devel] Segmentation fault when add the cloned/copied node
8
+ # 2007/11/27 20:51
9
+
10
+ class TestNodeCopy < Test::Unit::TestCase
11
+ def setup
12
+ str = <<-STR
13
+ <html><body>
14
+ <div class="textarea" id="t1" style="STATIC">foo</div>
15
+ <div class="textarea" id="t2" style="STATIC">bar</div>
16
+ </body></html>
17
+ STR
18
+
19
+ doc = XML::Parser.string(str).parse
20
+
21
+ xpath = "//div"
22
+ @div1 = doc.find(xpath).to_a[0]
23
+ @div2 = doc.find(xpath).to_a[1]
24
+ end
25
+
26
+ def test_libxml_node_copy_not_segv
27
+ @div2.each do |child|
28
+ c = child.copy(false)
29
+ @div1 << c
30
+ end
31
+ assert @div1.to_s =~ /foo/
32
+ end
33
+
34
+ def test_libxml_node_clone_not_segv
35
+ @div2.each do |child|
36
+ c = child.clone
37
+ @div1 << c
38
+ end
39
+ assert @div1.to_s =~ /foo/
40
+ end
41
+
42
+ end # TC_XML_Node_Copy