end
end
- ALLOWED_TAGS = %w(redpre pre code kbd notextile)
+ ALLOWED_TAGS = %w(pre code kbd notextile)
def escape_html_tags(text)
- text.gsub!(%r{<(\/?([!\w]+)[^<>\n]*)(>?)}) do |m|
- if ALLOWED_TAGS.include?($2) && $3.present?
- "<#{$1}#{$3}"
+ text.gsub!(%r{<(\/?([!\w][^ >\t\f\r\n]*)[^<>\n]*)(>?)}) do |m|
+ all, tag, close = $1, $2, $3
+
+ if close.present? && (ALLOWED_TAGS.include?(tag) || (tag =~ /\Aredpre#\d+\z/))
+ "<#{all}#{close}"
else
- "<#{$1}#{'>' unless $3.blank?}"
+ "<#{all}#{'>' unless close.blank?}"
end
end
end
assert_equal expected.gsub(%r{[\r\n\t]}, ''), to_html(text).gsub(%r{[\r\n\t]}, '')
end
+ def test_should_escape_tags_that_start_with_pre
+ text = <<~STR
+ <preä demo>Text
+ STR
+
+ expected = <<~EXPECTED
+ <p><preä demo>Text</p>
+ EXPECTED
+ assert_equal expected.gsub(%r{[\r\n\t]}, ''), to_html(text).gsub(%r{[\r\n\t]}, '')
+ end
+
def test_should_escape_bq_citations
assert_html_output({
%{bq.:http://x/"onmouseover="alert(document.domain) Hover me} =>