Skip to content

Commit

Permalink
Nokogiri::XML::Document.parse supports kwargs
Browse files Browse the repository at this point in the history
Related to #3323
  • Loading branch information
flavorjones committed Nov 14, 2024
1 parent e05b994 commit 68985f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
44 changes: 26 additions & 18 deletions lib/nokogiri/xml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,40 @@ class Document < Nokogiri::XML::Node
private_constant :OBJECT_DUP_METHOD, :OBJECT_CLONE_METHOD

class << self
# Parse an XML file.
# call-seq:
# parse(input, url: nil, encoding: nil, options: DEFAULT_XML) { |options| } => Nokogiri::XML::Document
#
# +string_or_io+ may be a String, or any object that responds to
# _read_ and _close_ such as an IO, or StringIO.
# Parse XML input from a String or IO object, and return a new Document object.
#
# +url+ (optional) is the URI where this document is located.
# By default, Nokogiri treats documents as untrusted, and so does not attempt to load DTDs
# or access the network. See Nokogiri::XML::ParseOptions for a complete list of options; and
# that module's DEFAULT_XML constant for what's set (and not set) by default.
#
# +encoding+ (optional) is the encoding that should be used when processing
# the document.
# See also: Nokogiri.XML() which is a convenience method which will call this method.
#
# +options+ (optional) is a configuration object that sets options during
# parsing, such as Nokogiri::XML::ParseOptions::RECOVER. See the
# Nokogiri::XML::ParseOptions for more information.
# [Parameters]
# - +input+ (String, IO) The content to be parsed.
#
# +block+ (optional) is passed a configuration object on which
# parse options may be set.
# [Keyword arguments]
# - +url:+ (String) The URI where this document is located.
#
# By default, Nokogiri treats documents as untrusted, and so
# does not attempt to load DTDs or access the network. See
# Nokogiri::XML::ParseOptions for a complete list of options;
# and that module's DEFAULT_XML constant for what's set (and not
# set) by default.
# - +encoding:+ (String) The name of the encoding that should be used when processing the
# document. (default +nil+ means that the encoding will be determined based on the
# document content)
#
# Nokogiri.XML() is a convenience method which will call this method.
# - +options+ (Nokogiri::XML::ParseOptions) Configuration object that determines some
# behaviors during parsing, such as Nokogiri::XML::ParseOptions::RECOVER. See the
# Nokogiri::XML::ParseOptions for more information.
#
def parse(string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML)
# [Yields]
# If a block is given, a Nokogiri::XML::ParseOptions object is yielded to the block which
# can be configured before parsing. See Nokogiri::XML::ParseOptions for more information.
#
def parse(
string_or_io,
url_ = nil, encoding_ = nil, options_ = XML::ParseOptions::DEFAULT_XML,
url: url_, encoding: encoding_, options: options_
)
options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
yield options if block_given?

Expand Down
5 changes: 5 additions & 0 deletions test/xml/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,11 @@ def test_url
assert_equal(XML_FILE, xml.url)
end

def test_url_kwarg
xml = Nokogiri::XML::Document.parse(File.read(XML_FILE), url: XML_FILE)
assert_equal(XML_FILE, xml.url)
end

def test_document_parent
xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
assert_raises(NoMethodError) do
Expand Down

0 comments on commit 68985f3

Please sign in to comment.