Skip to content

Commit

Permalink
Nokogiri::XML() supports argument forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
openbl authored and flavorjones committed Dec 8, 2024
1 parent 7b87461 commit 9d3141a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/nokogiri/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 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
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 9d3141a

Please sign in to comment.