Skip to content

Commit

Permalink
Nokogiri::XML.fragment() 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 c029eb6 commit ce05c0d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/nokogiri/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ def parse(thing, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML,
Document.parse(thing, url, encoding, options, &block)
end

####
# Parse a fragment from +string+ in to a NodeSet.
def fragment(string, options = ParseOptions::DEFAULT_XML, &block)
XML::DocumentFragment.parse(string, options, &block)
# Convenience method for Nokogiri::XML::DocumentFragment.parse
def fragment(...)
XML::DocumentFragment.parse(...)
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/xml/test_document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ def test_for_libxml_in_context_memory_badness_when_encountering_encoding_errors
end
end

it "accepts kwargs" do
frag = Nokogiri::XML.fragment(input, options: xml_default)
assert_equal("<a>foo</a>", frag.to_html)
refute_empty(frag.errors)

assert_raises(Nokogiri::SyntaxError) do
Nokogiri::XML.fragment(input, options: xml_strict)
end
end

it "takes a config block" do
default_config = nil
Nokogiri::XML.fragment(input) do |config|
Expand Down

0 comments on commit ce05c0d

Please sign in to comment.