Skip to content

Commit

Permalink
Allow data attributes
Browse files Browse the repository at this point in the history
Closes #57
  • Loading branch information
Rafael Mendonça França committed Oct 13, 2013
1 parent 825d715 commit 78c7e74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/loofah/html5/scrub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ def scrub_attributes node
else
attr_node.node_name
end

if attr_name =~ /\Adata-\w+\z/
next
end

unless WhiteList::ALLOWED_ATTRIBUTES.include?(attr_name)
attr_node.remove
next
end

if WhiteList::ATTR_VAL_IS_URI.include?(attr_name)
# this block lifted nearly verbatim from HTML5 sanitization
val_unescaped = CGI.unescapeHTML(attr_node.value).gsub(CONTROL_CHARACTERS,'').downcase
Expand Down
9 changes: 9 additions & 0 deletions test/html5/test_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ def check_sanitization(input, htmloutput, xhtmloutput, rexmloutput)
end
end

def test_should_allow_data_attributes
input = "<p data-foo='foo'>foo <bad>bar</bad> baz</p>"

output = "<p data-foo='foo'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"
htmloutput = "<p data-foo='foo'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"

check_sanitization(input, htmloutput, output, output)
end

##
## libxml2 downcases attributes, so this is moot.
##
Expand Down

0 comments on commit 78c7e74

Please sign in to comment.