Skip to content

Commit aaf76b0

Browse files
committed
Updates Node#canonicalize for keyword args; Adds a full test; Still needs the Document#canonicalize C func to be updated
1 parent 141d3bc commit aaf76b0

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

lib/nokogiri/xml/node.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ def write_xml_to(io, options = {})
14891489
write_to(io, options)
14901490
end
14911491

1492-
def canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false)
1492+
def canonicalize(mode_ = XML::XML_C14N_1_0, inclusive_namespaces_ = nil, with_comments_ = false, mode: mode_, inclusive_namespaces: inclusive_namespaces_, with_comments: with_comments_)
14931493
c14n_root = self
14941494
document.canonicalize(mode, inclusive_namespaces, with_comments) do |node, parent|
14951495
tn = node.is_a?(XML::Node) ? node : parent

test/xml/test_c14n.rb

+107
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ def test_3_1
3232
assert_match(/Comment/, c14n)
3333
c14n = doc.canonicalize(nil, nil, false)
3434
refute_match(/Comment/, c14n)
35+
36+
# with keyword args
37+
# TODO: when Document#canonicalize is updated to keyword args, these tests should pass
38+
# c14n = doc.canonicalize(mode: nil, inclusive_namespaces: nil, with_comments: true)
39+
# assert_match(/Comment/, c14n)
40+
# c14n = doc.canonicalize(mode: nil, inclusive_namespaces: nil, with_comments: false)
41+
# refute_match(/Comment/, c14n)
3542
end
3643

3744
def test_exclude_block_params
@@ -199,13 +206,113 @@ def test_c14n_modes
199206
end
200207
end
201208

209+
def test_c14n_modes_with_keyword_args
210+
# http://www.w3.org/TR/xml-exc-c14n/#sec-Enveloping
211+
212+
doc1 = Nokogiri.XML(<<~EOXML)
213+
<n0:local xmlns:n0="http://foobar.org" xmlns:n3="ftp://example.org">
214+
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
215+
<n3:stuff xmlns:n3="ftp://example.org"/>
216+
</n1:elem2>
217+
</n0:local>
218+
EOXML
219+
node1 = doc1.at_xpath("//n1:elem2", { "n1" => "http://example.net" })
220+
221+
doc2 = Nokogiri.XML(<<~EOXML)
222+
<n2:pdu xmlns:n1="http://example.com"
223+
xmlns:n2="http://foo.example"
224+
xmlns:n4="http://foo.example"
225+
xml:lang="fr"
226+
xml:space="retain">
227+
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
228+
<n3:stuff xmlns:n3="ftp://example.org"/>
229+
<n4:stuff />
230+
</n1:elem2>
231+
</n2:pdu>
232+
EOXML
233+
node2 = doc2.at_xpath("//n1:elem2", { "n1" => "http://example.net" })
234+
235+
expected = <<~EOF.strip
236+
<n1:elem2 xmlns:n0="http://foobar.org" xmlns:n1="http://example.net" xmlns:n3="ftp://example.org" xml:lang="en">
237+
<n3:stuff></n3:stuff>
238+
</n1:elem2>
239+
EOF
240+
c14n = node1.canonicalize
241+
assert_equal(expected, c14n)
242+
243+
expected = <<~EOF.strip
244+
<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en" xml:space="retain">
245+
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
246+
<n4:stuff></n4:stuff>
247+
</n1:elem2>
248+
EOF
249+
c14n = node2.canonicalize
250+
assert_equal(expected, c14n)
251+
c14n = node2.canonicalize(mode: XML::XML_C14N_1_0)
252+
assert_equal(expected, c14n)
253+
assert_raises(RuntimeError) do
254+
node2.canonicalize(mode: XML::XML_C14N_1_0, inclusive_namespaces: ["n2"])
255+
end
256+
257+
expected = <<~EOF.strip
258+
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
259+
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
260+
</n1:elem2>
261+
EOF
262+
c14n = node1.canonicalize(mode: XML::XML_C14N_EXCLUSIVE_1_0)
263+
assert_equal(expected, c14n)
264+
265+
expected = <<~EOF.strip
266+
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
267+
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
268+
<n4:stuff xmlns:n4="http://foo.example"></n4:stuff>
269+
</n1:elem2>
270+
EOF
271+
c14n = node2.canonicalize(mode: XML::XML_C14N_EXCLUSIVE_1_0)
272+
assert_equal(expected, c14n)
273+
274+
expected = <<~EOF.strip
275+
<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xml:lang="en">
276+
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
277+
<n4:stuff xmlns:n4="http://foo.example"></n4:stuff>
278+
</n1:elem2>
279+
EOF
280+
c14n = node2.canonicalize(mode: XML::XML_C14N_EXCLUSIVE_1_0, inclusive_namespaces: ["n2"])
281+
assert_equal(expected, c14n)
282+
283+
expected = <<~EOF.strip
284+
<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en">
285+
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
286+
<n4:stuff></n4:stuff>
287+
</n1:elem2>
288+
EOF
289+
c14n = node2.canonicalize(mode: XML::XML_C14N_EXCLUSIVE_1_0, inclusive_namespaces: ["n2", "n4"])
290+
assert_equal(expected, c14n)
291+
292+
expected = <<~EOF.strip
293+
<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en" xml:space="retain">
294+
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
295+
<n4:stuff></n4:stuff>
296+
</n1:elem2>
297+
EOF
298+
c14n = node2.canonicalize(mode: XML::XML_C14N_1_1)
299+
assert_equal(expected, c14n)
300+
assert_raises(RuntimeError) do
301+
node2.canonicalize(mode: XML::XML_C14N_1_1, inclusive_namespaces: ["n2"])
302+
end
303+
end
304+
202305
def test_wrong_params
203306
xml = "<a><b></b></a>"
204307
doc = Nokogiri.XML(xml)
205308

206309
assert_raises(TypeError) { doc.canonicalize(:wrong_type) }
207310
assert_raises(TypeError) { doc.canonicalize(nil, :wrong_type) }
208311
doc.canonicalize(nil, nil, :wrong_type)
312+
313+
# with keyword args
314+
# assert_raises(TypeError) { doc.canonicalize(nil, inclusive_namespaces: :wrong_type) }
315+
# doc.canonicalize(nil, inclusive_namespaces: nil, with_comments: :wrong_type)
209316
end
210317
end
211318
end

0 commit comments

Comments
 (0)