diff --git a/lib/nokogiri/xml.rb b/lib/nokogiri/xml.rb index 888c1f39009..592134f2ba1 100644 --- a/lib/nokogiri/xml.rb +++ b/lib/nokogiri/xml.rb @@ -2,10 +2,9 @@ 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) + # Convenience method for Nokogiri::XML::Document.parse + def XML(...) + Nokogiri::XML::Document.parse(...) end 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 65ea9200d83..f56f344390e 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 @@ -1097,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 @@ -1118,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 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