From 9d3141a094d7f684f61071ac1e6d99b81704f324 Mon Sep 17 00:00:00 2001 From: Brian Lesperance Date: Thu, 14 Nov 2024 15:22:57 -0600 Subject: [PATCH 1/2] Nokogiri::XML() supports argument forwarding Related to https://github.com/sparklemotion/nokogiri#3323 --- lib/nokogiri/xml.rb | 4 ++-- test/xml/test_document.rb | 4 ++++ test/xml/test_document_encoding.rb | 5 +++++ test/xml/test_node_encoding.rb | 16 ++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/nokogiri/xml.rb b/lib/nokogiri/xml.rb index 888c1f39009..3a49ce07312 100644 --- a/lib/nokogiri/xml.rb +++ b/lib/nokogiri/xml.rb @@ -4,8 +4,8 @@ module Nokogiri class << self ### # Parse XML. Convenience method for Nokogiri::XML::Document.parse - def XML(thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_XML, &block) - Nokogiri::XML::Document.parse(thing, url, encoding, options, &block) + def XML(...) + Nokogiri::XML::Document.parse(...) end end diff --git a/test/xml/test_document.rb b/test/xml/test_document.rb index 65ea9200d83..5e8182bfc9a 100644 --- a/test/xml/test_document.rb +++ b/test/xml/test_document.rb @@ -681,6 +681,10 @@ def test_strict_document_throws_syntax_error Nokogiri::XML("", nil, nil, 0) end + assert_raises(Nokogiri::XML::SyntaxError) do + Nokogiri::XML("", options: 0) + end + assert_raises(Nokogiri::XML::SyntaxError) do Nokogiri::XML("", &:strict) end diff --git a/test/xml/test_document_encoding.rb b/test/xml/test_document_encoding.rb index c0cb93b2838..aecad95c5b1 100644 --- a/test/xml/test_document_encoding.rb +++ b/test/xml/test_document_encoding.rb @@ -29,6 +29,11 @@ class TestDocumentEncoding < Nokogiri::TestCase encoding = "Shift_JIS" assert_equal(encoding, Nokogiri::XML(nil, nil, encoding).encoding) end + + it "applies the specified kwargs encoding even if on empty documents" do + encoding = "Shift_JIS" + assert_equal(encoding, Nokogiri::XML(nil, encoding: encoding).encoding) + end end describe "#encoding=" do diff --git a/test/xml/test_node_encoding.rb b/test/xml/test_node_encoding.rb index 97e8a1b2eaf..6d68f1ccd56 100644 --- a/test/xml/test_node_encoding.rb +++ b/test/xml/test_node_encoding.rb @@ -39,6 +39,22 @@ def test_encoding_GH_1113 assert_equal(expected, frag.to_xml.sub(/^<.xml[^>]*>\n/m, "").strip) end + def test_encoding_GH_1113_with_kwargs + utf8 = "shahid ὡ 𐄣 𢂁" + hex = "shahid ὡ 𐄣 𢂁" + decimal = "shahid ὡ 𐄣 𢂁" + expected = Nokogiri.jruby? ? hex : decimal + + frag = Nokogiri::XML(utf8, encoding: "UTF-8", options: Nokogiri::XML::ParseOptions::STRICT) + assert_equal(utf8, frag.to_xml.sub(/^<.xml[^>]*>\n/m, "").strip) + + frag = Nokogiri::XML(expected, encoding: "UTF-8", options: Nokogiri::XML::ParseOptions::STRICT) + assert_equal(utf8, frag.to_xml.sub(/^<.xml[^>]*>\n/m, "").strip) + + frag = Nokogiri::XML(expected, encoding: "US-ASCII", options: Nokogiri::XML::ParseOptions::STRICT) + assert_equal(expected, frag.to_xml.sub(/^<.xml[^>]*>\n/m, "").strip) + end + VEHICLE_XML = <<-eoxml From 9435520197fea177ac95dd674f3e918da8c9ca2f Mon Sep 17 00:00:00 2001 From: Brian Lesperance Date: Thu, 14 Nov 2024 15:24:39 -0600 Subject: [PATCH 2/2] Nokogiri::XML.parse() supports argument forwarding Related to https://github.com/sparklemotion/nokogiri#3323 --- lib/nokogiri/xml.rb | 10 ++++------ test/xml/test_document.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/nokogiri/xml.rb b/lib/nokogiri/xml.rb index 3a49ce07312..592134f2ba1 100644 --- a/lib/nokogiri/xml.rb +++ b/lib/nokogiri/xml.rb @@ -2,8 +2,7 @@ module Nokogiri class << self - ### - # Parse XML. Convenience method for Nokogiri::XML::Document.parse + # Convenience method for Nokogiri::XML::Document.parse def XML(...) Nokogiri::XML::Document.parse(...) end @@ -23,10 +22,9 @@ def Reader(...) Reader.new(...) end - ### - # Parse XML. Convenience method for Nokogiri::XML::Document.parse - def parse(thing, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML, &block) - Document.parse(thing, url, encoding, options, &block) + # Convenience method for Nokogiri::XML::Document.parse + def parse(...) + Document.parse(...) end #### diff --git a/test/xml/test_document.rb b/test/xml/test_document.rb index 5e8182bfc9a..f56f344390e 100644 --- a/test/xml/test_document.rb +++ b/test/xml/test_document.rb @@ -1101,6 +1101,13 @@ def test_can_be_closed end assert_nil(error.path) end + + it "raises exception on parse error with kwargs" do + error = assert_raises Nokogiri::SyntaxError do + Nokogiri::XML.parse(input, options: parse_options) + end + assert_nil(error.path) + end end describe "default options" do @@ -1122,6 +1129,12 @@ def test_can_be_closed Nokogiri::XML.parse(input, nil, nil, parse_options) end end + + it "raises exception on parse error with kwargs" do + assert_raises Nokogiri::SyntaxError do + Nokogiri::XML.parse(input, options: parse_options) + end + end end describe "default options" do