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,31 +123,33 @@ module Sanitizer
|
|||
#
|
||||
# xhtml_sanitize('<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>
|
||||
def xhtml_sanitize(html)
|
||||
if html.index("<")
|
||||
tokenizer = HTML::Tokenizer.new(html.to_utf8)
|
||||
new_text = ""
|
||||
return html unless sanitizeable?(html)
|
||||
tokenizer = HTML::Tokenizer.new(html.to_utf8)
|
||||
results = []
|
||||
|
||||
while token = tokenizer.next
|
||||
node = XHTML::Node.parse(nil, 0, 0, token, false)
|
||||
new_text << case node.tag?
|
||||
when true
|
||||
if ALLOWED_ELEMENTS.include?(node.name)
|
||||
process_attributes_for(node)
|
||||
node.to_s
|
||||
else
|
||||
node.to_s.gsub(/</, "<").gsub(/>/, ">")
|
||||
end
|
||||
while token = tokenizer.next
|
||||
node = XHTML::Node.parse(nil, 0, 0, token, false)
|
||||
results << case node.tag?
|
||||
when true
|
||||
if ALLOWED_ELEMENTS.include?(node.name)
|
||||
process_attributes_for(node)
|
||||
node.to_s
|
||||
else
|
||||
node.to_s.unescapeHTML.escapeHTML
|
||||
end
|
||||
node.to_s.gsub(/</, "<").gsub(/>/, ">")
|
||||
end
|
||||
else
|
||||
node.to_s.unescapeHTML.escapeHTML
|
||||
end
|
||||
|
||||
html = new_text
|
||||
end
|
||||
html
|
||||
|
||||
results.join
|
||||
end
|
||||
|
||||
def sanitizeable?(text)
|
||||
!(text.nil? || text.empty? || !text.index("<"))
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue