Skip to content

Commit a0180c7

Browse files
committed
fix: HTML4::Document.to_xhtml self-closing tags
Commit 1d06b4f introduced NO_EMPTY_TAGS into SaveOptions::DEFAULT_XHTML which libxml2 ignored due to a long-standing bug in serialization. libxml2 v2.9.11 fixed that serialization bug (https://gitlab.gnome.org/GNOME/libxml2/-/commit/dc6f009) and started paying attention to the NO_EMPTY_TAGS save option, resulting in seeing output containing, e.g. `<col></col>` instead of `<col/>`. This commit updates the default XHTML save options to drop the NO_EMPTY_TAGS flag, restoring this behavior. Closes #2324
1 parent 564ac17 commit a0180c7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/nokogiri/xml/node/save_options.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SaveOptions
3434
DEFAULT_HTML = FORMAT | NO_DECLARATION | NO_EMPTY_TAGS | AS_HTML
3535
end
3636
# the default for XHTML document
37-
DEFAULT_XHTML = FORMAT | NO_DECLARATION | NO_EMPTY_TAGS | AS_XHTML
37+
DEFAULT_XHTML = FORMAT | NO_DECLARATION | AS_XHTML
3838

3939
# Integer representation of the SaveOptions
4040
attr_reader :options

test/html4/test_document.rb

+9
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,15 @@ def test_to_xhtml
370370
assert_match("UTF-8", html.to_xhtml(encoding: "UTF-8"))
371371
end
372372

373+
def test_to_xhtml_self_closing_tags
374+
# https://github.com/sparklemotion/nokogiri/issues/2324
375+
html = "<html><body><br><table><colgroup><col>"
376+
doc = Nokogiri::HTML::Document.parse(html)
377+
xhtml = doc.to_xhtml
378+
assert_match(%r(<br ?/>), xhtml)
379+
assert_match(%r(<col ?/>), xhtml)
380+
end
381+
373382
def test_no_xml_header
374383
html = Nokogiri::HTML(<<~EOHTML)
375384
<html>

0 commit comments

Comments
 (0)