Skip to content

Commit

Permalink
Stop adding extra new line after XML declaration with pretty format (#…
Browse files Browse the repository at this point in the history
…164)

If the XML file does not end with a newline, a space is added to the end
of the first line.

```ruby
Failure: test_indent(REXMLTests::TestDocument::WriteTest::ArgumentsTest)
/Users/naitoh/ghq/github.com/naitoh/rexml/test/test_document.rb:270:in `test_indent'
     267:           output = ""
     268:           indent = 2
     269:           @document.write(output, indent)
  => 270:           assert_equal(<<-EOX.chomp, output)
     271: <?xml version='1.0' encoding='UTF-8'?>
     272: <message>
     273:   Hello world!
<"<?xml version='1.0' encoding='UTF-8'?>\n" +
"<message>\n" +
"  Hello world!\n" +
"</message>"> expected but was
<"<?xml version='1.0' encoding='UTF-8'?> \n" +
"<message>\n" +
"  Hello world!\n" +
"</message>">

diff:
? <?xml version='1.0' encoding='UTF-8'?>
  <message>
    Hello world!
  </message>

```

This is happen because `REXML::Formatters::Pretty#write_document` has a
logic that depends on the last text node.

We should ignore all top-level text nodes with pretty format.
  • Loading branch information
naitoh committed Jul 11, 2024
1 parent b2ec329 commit 5e140ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/rexml/formatters/pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def write_document( node, output )
# itself, then we don't need a carriage return... which makes this
# logic more complex.
node.children.each { |child|
next if child == node.children[-1] and child.instance_of?(Text)
next if child.instance_of?(Text)
unless child == node.children[0] or child.instance_of?(Text) or
(child == node.children[1] and !node.children[0].writethis)
output << "\n"
Expand Down
14 changes: 7 additions & 7 deletions test/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_each_recursive

class WriteTest < Test::Unit::TestCase
def setup
@document = REXML::Document.new(<<-EOX)
@document = REXML::Document.new(<<-EOX.chomp)
<?xml version="1.0" encoding="UTF-8"?>
<message>Hello world!</message>
EOX
Expand All @@ -246,7 +246,7 @@ class ArgumentsTest < self
def test_output
output = ""
@document.write(output)
assert_equal(<<-EOX, output)
assert_equal(<<-EOX.chomp, output)
<?xml version='1.0' encoding='UTF-8'?>
<message>Hello world!</message>
EOX
Expand All @@ -269,7 +269,7 @@ def test_transitive
indent = 2
transitive = true
@document.write(output, indent, transitive)
assert_equal(<<-EOX, output)
assert_equal(<<-EOX.chomp, output)
<?xml version='1.0' encoding='UTF-8'?>
<message
>Hello world!</message
Expand Down Expand Up @@ -298,7 +298,7 @@ def test_encoding
japanese_text = "こんにちは"
@document.root.text = japanese_text
@document.write(output, indent, transitive, ie_hack, encoding)
assert_equal(<<-EOX.encode(encoding), output)
assert_equal(<<-EOX.chomp.encode(encoding), output)
<?xml version='1.0' encoding='SHIFT_JIS'?>
<message>#{japanese_text}</message>
EOX
Expand All @@ -309,7 +309,7 @@ class OptionsTest < self
def test_output
output = ""
@document.write(:output => output)
assert_equal(<<-EOX, output)
assert_equal(<<-EOX.chomp, output)
<?xml version='1.0' encoding='UTF-8'?>
<message>Hello world!</message>
EOX
Expand All @@ -329,7 +329,7 @@ def test_indent
def test_transitive
output = ""
@document.write(:output => output, :indent => 2, :transitive => true)
assert_equal(<<-EOX, output)
assert_equal(<<-EOX.chomp, output)
<?xml version='1.0' encoding='UTF-8'?>
<message
>Hello world!</message
Expand All @@ -351,7 +351,7 @@ def test_encoding
japanese_text = "こんにちは"
@document.root.text = japanese_text
@document.write(:output => output, :encoding => encoding)
assert_equal(<<-EOX.encode(encoding), output)
assert_equal(<<-EOX.chomp.encode(encoding), output)
<?xml version='1.0' encoding='SHIFT_JIS'?>
<message>#{japanese_text}</message>
EOX
Expand Down

0 comments on commit 5e140ed

Please sign in to comment.