Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nokogiri::XML() and Nokogiri::XML.parse() support argument forwarding #3332

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/nokogiri/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

####
Expand Down
17 changes: 17 additions & 0 deletions test/xml/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ def test_strict_document_throws_syntax_error
Nokogiri::XML("<foo><bar></foo>", nil, nil, 0)
end

assert_raises(Nokogiri::XML::SyntaxError) do
Nokogiri::XML("<foo><bar></foo>", options: 0)
end

assert_raises(Nokogiri::XML::SyntaxError) do
Nokogiri::XML("<foo><bar></foo>", &:strict)
end
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/xml/test_document_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/xml/test_node_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<frag>shahid ὡ 𐄣 𢂁</frag>"
hex = "<frag>shahid &#x1f61; &#x10123; &#x22081;</frag>"
decimal = "<frag>shahid &#8033; &#65827; &#139393;</frag>"
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
<root>
<car xmlns:part="http://general-motors.com/">
Expand Down
Loading