Skip to content
Open
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
5 changes: 2 additions & 3 deletions lib/rexml/formatters/pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ def write_element(node, output)
end

def write_text( node, output )
s = node.to_s()
s.gsub!(/\s/,' ')
s.squeeze!(" ")
# Not gsub!/squeeze!: Text#to_s returns the node's own string.
s = node.to_s().gsub(/\s/, ' ').squeeze(" ")
Comment on lines +89 to +90
s = wrap(s, @width - @level)
s = indent_text(s, @level, " ", true)
output << (' '*@level + s)
Expand Down
34 changes: 34 additions & 0 deletions test/formatter/test_pretty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module REXMLTests
class PrettyFormatterTest < Test::Unit::TestCase
def format(node, indentation=2)
formatter = REXML::Formatters::Pretty.new(indentation)
output = +""
formatter.write(node, output)
output
end

class TextTest < self
def test_source_document_is_not_modified
document = REXML::Document.new("<a><b>hello world</b></a>")
format(document)
assert_equal("<a><b>hello world</b></a>", document.to_s)
end

def test_raw_text_value_is_not_modified
text = REXML::Text.new("hello world", true, nil, true)
format(text)
assert_equal("hello world", text.value)
end

def test_whitespace_is_replaced_with_space
document = REXML::Document.new("<a><b>x\ty</b></a>")
assert_equal("<a>\n <b>\n x y\n </b>\n</a>", format(document))
end

def test_consecutive_spaces_are_squeezed
document = REXML::Document.new("<a><b>x y</b></a>")
assert_equal("<a>\n <b>\n x y\n </b>\n</a>", format(document))
end
end
end
end