ALLOWED_TAGS = %w(redpre pre code)
def escape_html_tags(text)
- text.gsub!(%r{<((\/?)(\w+))}) {|m| ALLOWED_TAGS.include?($3) ? "<#{$1}" : "<#{$1}" }
+ text.gsub!(%r{<(\/?(\w+)[^>\n]*)(>?)}) {|m| ALLOWED_TAGS.include?($2) ? "<#{$1}#{$3}" : "<#{$1}#{'>' if $3}" }
end
end
def test_html_tags
to_test = {
- "<div>content</div>" => "<p><div>content</div></p>",
- "<script>some script;</script>" => "<p><script>some script;</script></p>",
+ "<div>content</div>" => "<p><div>content</div></p>",
+ "<div class=\"bold\">content</div>" => "<p><div class=\"bold\">content</div></p>",
+ "<script>some script;</script>" => "<p><script>some script;</script></p>",
# do not escape pre/code tags
"<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
"<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",