diff --git a/lib/redmine/wiki_formatting/html_parser.rb b/lib/redmine/wiki_formatting/html_parser.rb index 1a268e4a0..c86034d52 100644 --- a/lib/redmine/wiki_formatting/html_parser.rb +++ b/lib/redmine/wiki_formatting/html_parser.rb @@ -30,13 +30,13 @@ module Redmine } def self.to_text(html) - html = html.gsub(/[\n\r]/, '').squeeze(' ') + html = html.gsub(/[\n\r]/, ' ') doc = Loofah.document(html) doc.scrub!(WikiTags.new(tags)) doc.scrub!(:newline_block_elements) - Loofah.remove_extraneous_whitespace(doc.text).strip + Loofah.remove_extraneous_whitespace(doc.text).strip.squeeze(' ').gsub(/^ +/, '') end class WikiTags < ::Loofah::Scrubber diff --git a/test/unit/lib/redmine/wiki_formatting/html_parser_test.rb b/test/unit/lib/redmine/wiki_formatting/html_parser_test.rb index 7900a5af8..af6e1287e 100644 --- a/test/unit/lib/redmine/wiki_formatting/html_parser_test.rb +++ b/test/unit/lib/redmine/wiki_formatting/html_parser_test.rb @@ -34,4 +34,16 @@ class Redmine::WikiFormatting::HtmlParserTest < ActiveSupport::TestCase assert_equal "Text", @parser.to_text('Text') end + + def test_should_remove_preceding_whitespaces + to_test = { + "
blocks with
\n

\n preceding whitespaces\n

" => "blocks with\n\npreceding whitespaces", + "
blocks without
\n

\npreceding whitespaces\n

" => "blocks without\n\npreceding whitespaces", + " span with\n preceding whitespaces" => "span with preceding whitespaces", + "span without\npreceding whitespaces" => "span without preceding whitespaces" + } + to_test.each do |html, expected| + assert_equal expected, @parser.to_text(html) + end + end end