Skip to content

Commit

Permalink
fix REXML::Formatters::Pretty#write_document
Browse files Browse the repository at this point in the history
## Why?
Fix REXML::Formatters::Pretty#write_document, which implicitly assumes that the XML file ends with a newline.

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

```
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>

```
  • Loading branch information
naitoh committed Jul 10, 2024
1 parent ebc3e85 commit 01162bd
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 @@ -247,7 +247,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 @@ -257,7 +257,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 @@ -280,7 +280,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 @@ -309,7 +309,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 @@ -320,7 +320,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 @@ -340,7 +340,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 @@ -362,7 +362,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 01162bd

Please sign in to comment.