Skip to content

Commit

Permalink
Nokogiri::XML() and Nokogiri::XML.parse() support argument forwarding (
Browse files Browse the repository at this point in the history
…#3332)

**What problem is this PR intended to solve?**

Related to #3323,
introducing keyword argument support in Nokogiri::XML::Document.parse.

**Have you included adequate test coverage?**

Some minor test coverage mimicking the existing permutations of options.

**Does this change affect the behavior of either the C or the Java
implementations?**

No
  • Loading branch information
flavorjones authored Dec 8, 2024
2 parents 92d2e4b + 9435520 commit 3b8fd7e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
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

# Convenience method for Nokogiri::XML::DocumentFragment.parse
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

0 comments on commit 3b8fd7e

Please sign in to comment.