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

fix: HTML4::Document.to_xhtml self-closing tags #2326

Merged
merged 2 commits into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/nokogiri/xml/node/save_options.rb
Expand Up @@ -34,7 +34,7 @@ class SaveOptions
DEFAULT_HTML = FORMAT | NO_DECLARATION | NO_EMPTY_TAGS | AS_HTML
end
# the default for XHTML document
DEFAULT_XHTML = FORMAT | NO_DECLARATION | NO_EMPTY_TAGS | AS_XHTML
DEFAULT_XHTML = FORMAT | NO_DECLARATION | AS_XHTML

# Integer representation of the SaveOptions
attr_reader :options
Expand Down
55 changes: 34 additions & 21 deletions test/html4/test_document.rb
Expand Up @@ -103,21 +103,21 @@ def test_empty_string_returns_empty_doc
assert_nil(doc.root)
end

unless Nokogiri.uses_libxml?("~> 2.6.0")
def test_to_xhtml_with_indent
doc = Nokogiri::HTML("<html><body><a>foo</a></body></html>")
doc = Nokogiri::HTML(doc.to_xhtml(indent: 2))
assert_indent(2, doc)
end
def test_to_xhtml_with_indent
skip if Nokogiri.uses_libxml?("~> 2.6.0")
doc = Nokogiri::HTML("<html><body><a>foo</a></body></html>")
doc = Nokogiri::HTML(doc.to_xhtml(indent: 2))
assert_indent(2, doc)
end

def test_write_to_xhtml_with_indent
io = StringIO.new
doc = Nokogiri::HTML("<html><body><a>foo</a></body></html>")
doc.write_xhtml_to(io, indent: 5)
io.rewind
doc = Nokogiri::HTML(io.read)
assert_indent(5, doc)
end
def test_write_to_xhtml_with_indent
skip if Nokogiri.uses_libxml?("~> 2.6.0")
io = StringIO.new
doc = Nokogiri::HTML("<html><body><a>foo</a></body></html>")
doc.write_xhtml_to(io, indent: 5)
io.rewind
doc = Nokogiri::HTML(io.read)
assert_indent(5, doc)
end

def test_swap_should_not_exist
Expand Down Expand Up @@ -360,8 +360,10 @@ def test_parse_temp_file
File.open(HTML_FILE, "rb") { |f| temp_html_file.write(f.read) }
temp_html_file.close
temp_html_file.open
assert_equal(Nokogiri::HTML.parse(File.read(HTML_FILE)).xpath("//div/a").length,
Nokogiri::HTML.parse(temp_html_file).xpath("//div/a").length)
assert_equal(
Nokogiri::HTML.parse(File.read(HTML_FILE)).xpath("//div/a").length,
Nokogiri::HTML.parse(temp_html_file).xpath("//div/a").length
)
end

def test_to_xhtml
Expand All @@ -370,6 +372,15 @@ def test_to_xhtml
assert_match("UTF-8", html.to_xhtml(encoding: "UTF-8"))
end

def test_to_xhtml_self_closing_tags
# https://github.com/sparklemotion/nokogiri/issues/2324
html = "<html><body><br><table><colgroup><col>"
doc = Nokogiri::HTML::Document.parse(html)
xhtml = doc.to_xhtml
assert_match(%r(<br ?/>), xhtml)
assert_match(%r(<col ?/>), xhtml)
end

def test_no_xml_header
html = Nokogiri::HTML(<<~EOHTML)
<html>
Expand Down Expand Up @@ -454,8 +465,8 @@ def test_doc_type
assert_equal("-//W3C//DTD XHTML 1.1//EN", html.internal_subset.external_id)
assert_equal("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd", html.internal_subset.system_id)
assert_equal(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">", html.to_s[0,
97]
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">",
html.to_s[0, 97]
)
end

Expand Down Expand Up @@ -614,7 +625,7 @@ def test_find_classes
EOHTML
list = doc.css(".red")
assert_equal(2, list.length)
assert_equal(%w{RED RED}, list.map(&:text))
assert_equal(["RED", "RED"], list.map(&:text))
end

def test_parse_can_take_io
Expand Down Expand Up @@ -836,8 +847,10 @@ def initialize(*args)

it "passes arguments to #initialize" do
doc = klass.new("http://www.w3.org/TR/REC-html40/loose.dtd", "-//W3C//DTD HTML 4.0 Transitional//EN")
assert_equal(["http://www.w3.org/TR/REC-html40/loose.dtd", "-//W3C//DTD HTML 4.0 Transitional//EN"],
doc.initialized_with)
assert_equal(
["http://www.w3.org/TR/REC-html40/loose.dtd", "-//W3C//DTD HTML 4.0 Transitional//EN"],
doc.initialized_with
)
end
end

Expand Down