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

Update HTML4::DocumentFragment#initialize & #parse to take kwargs #3336

Closed
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
12 changes: 3 additions & 9 deletions lib/nokogiri/html4/document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
module Nokogiri
module HTML4
class DocumentFragment < Nokogiri::XML::DocumentFragment
#
# :call-seq:
# parse(tags) => DocumentFragment
# parse(tags, encoding) => DocumentFragment
# parse(tags, encoding, options) => DocumentFragment
# parse(tags, encoding) { |options| ... } => DocumentFragment
#
# Parse an HTML4 fragment.
#
Expand Down Expand Up @@ -38,15 +32,15 @@ class DocumentFragment < Nokogiri::XML::DocumentFragment
#
# *Example:* Specifying encoding
#
# fragment = DocumentFragment.parse(input, "EUC-JP")
# fragment = DocumentFragment.parse(input, encoding: "EUC-JP")
#
# *Example:* Setting parse options dynamically
#
# DocumentFragment.parse("<div>Hello World") do |options|
# options.huge.pedantic
# end
#
def self.parse(tags, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block)
def self.parse(tags, encoding_ = nil, options_ = XML::ParseOptions::DEFAULT_HTML, encoding: encoding_, options: options_, &block)
doc = HTML4::Document.new

if tags.respond_to?(:read)
Expand Down Expand Up @@ -87,7 +81,7 @@ def self.parse(tags, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML,

# It's recommended to use either DocumentFragment.parse or XML::Node#parse rather than call this
# method directly.
def initialize(document, tags = nil, ctx = nil, options = XML::ParseOptions::DEFAULT_HTML) # rubocop:disable Lint/MissingSuper
def initialize(document, tags_ = nil, ctx_ = nil, options_ = XML::ParseOptions::DEFAULT_HTML, tags: tags_, ctx: ctx_, options: options_) # rubocop:disable Lint/MissingSuper
return self unless tags

options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
Expand Down
15 changes: 15 additions & 0 deletions test/html4/test_document_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ def test_parse_with_io
assert_equal("hello world", fragment.content)
end

it "returns a string matching an encoding passed with kwargs" do
input = "<div>hello world</div>"

fragment = Nokogiri::HTML4::DocumentFragment.parse(input, encoding: "ISO-8859-1")
assert_equal("ISO-8859-1", fragment.document.encoding)
assert_equal("hello world", fragment.content)
end

it "respects encoding for empty strings" do
fragment = Nokogiri::HTML::DocumentFragment.parse("", "UTF-8")
assert_equal "UTF-8", fragment.to_html.encoding.to_s
Expand Down Expand Up @@ -412,6 +420,13 @@ def test_parse_with_io
assert_equal(html4_huge, frag.parse_options)
end

it "accepts options as kwargs" do
frag = Nokogiri::HTML4::DocumentFragment.parse(input, options: html4_huge)

assert_equal("<div>foo</div>", frag.to_html)
assert_equal(html4_huge, frag.parse_options)
end

it "takes a config block" do
default_config = nil
frag = Nokogiri::HTML4::DocumentFragment.parse(input) do |config|
Expand Down