More Sanitizer Refactoring
Make the Sanitizer more efficient. Also, update some unit tests.
This commit is contained in:
parent
9b7071d190
commit
d14db51d9e
2 changed files with 24 additions and 22 deletions
|
@ -123,16 +123,16 @@ module Sanitizer
|
||||||
#
|
#
|
||||||
# xhtml_sanitize('<script> do_nasty_stuff() </script>')
|
# xhtml_sanitize('<script> do_nasty_stuff() </script>')
|
||||||
# => <script> do_nasty_stuff() </script>
|
# => <script> do_nasty_stuff() </script>
|
||||||
# xhtml_sanitize_xhtml('<a href="javascript: sucker();">Click here for $100</a>')
|
# xhtml_sanitize('<a href="javascript: sucker();">Click here for $100</a>')
|
||||||
# => <a>Click here for $100</a>
|
# => <a>Click here for $100</a>
|
||||||
def xhtml_sanitize(html)
|
def xhtml_sanitize(html)
|
||||||
if html.index("<")
|
return html unless sanitizeable?(html)
|
||||||
tokenizer = HTML::Tokenizer.new(html.to_utf8)
|
tokenizer = HTML::Tokenizer.new(html.to_utf8)
|
||||||
new_text = ""
|
results = []
|
||||||
|
|
||||||
while token = tokenizer.next
|
while token = tokenizer.next
|
||||||
node = XHTML::Node.parse(nil, 0, 0, token, false)
|
node = XHTML::Node.parse(nil, 0, 0, token, false)
|
||||||
new_text << case node.tag?
|
results << case node.tag?
|
||||||
when true
|
when true
|
||||||
if ALLOWED_ELEMENTS.include?(node.name)
|
if ALLOWED_ELEMENTS.include?(node.name)
|
||||||
process_attributes_for(node)
|
process_attributes_for(node)
|
||||||
|
@ -145,9 +145,11 @@ module Sanitizer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
html = new_text
|
results.join
|
||||||
end
|
end
|
||||||
html
|
|
||||||
|
def sanitizeable?(text)
|
||||||
|
!(text.nil? || text.empty? || !text.index("<"))
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -373,10 +373,10 @@ END_THM
|
||||||
assert_markup_parsed_as(
|
assert_markup_parsed_as(
|
||||||
"<p>should we go <a class='existingWikiWord' href='../show/ThatWay'>" +
|
"<p>should we go <a class='existingWikiWord' href='../show/ThatWay'>" +
|
||||||
"That Way</a> or</p>\n<div class='maruku-equation' id='eq:eq1'>" +
|
"That Way</a> or</p>\n<div class='maruku-equation' id='eq:eq1'>" +
|
||||||
"<math class='maruku-mathml' display='block' " +
|
"<span class='maruku-eq-number'>(1)</span><math class='maruku-mathml' display='block' " +
|
||||||
"xmlns='http://www.w3.org/1998/Math/MathML'><mi>ThisWay</mi></math>" +
|
"xmlns='http://www.w3.org/1998/Math/MathML'><mi>ThisWay</mi></math>" +
|
||||||
"<span class='maruku-eq-tex'><code style='display: none;'>ThisWay</code>" +
|
"<span class='maruku-eq-tex'><code style='display: none;'>ThisWay</code>" +
|
||||||
"</span><span class='maruku-eq-number'>(1)</span></div>",
|
"</span></div>",
|
||||||
"should we go ThatWay or \n\\[ThisWay\\]\n")
|
"should we go ThatWay or \n\\[ThisWay\\]\n")
|
||||||
|
|
||||||
assert_markup_parsed_as(
|
assert_markup_parsed_as(
|
||||||
|
|
Loading…
Add table
Reference in a new issue