Skip to content

Commit 78c7e74

Browse files
author
Rafael Mendonça França
committed
Allow data attributes
Closes #57
1 parent 825d715 commit 78c7e74

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/loofah/html5/scrub.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ def scrub_attributes node
2222
else
2323
attr_node.node_name
2424
end
25+
26+
if attr_name =~ /\Adata-\w+\z/
27+
next
28+
end
29+
2530
unless WhiteList::ALLOWED_ATTRIBUTES.include?(attr_name)
2631
attr_node.remove
2732
next
2833
end
34+
2935
if WhiteList::ATTR_VAL_IS_URI.include?(attr_name)
3036
# this block lifted nearly verbatim from HTML5 sanitization
3137
val_unescaped = CGI.unescapeHTML(attr_node.value).gsub(CONTROL_CHARACTERS,'').downcase

test/html5/test_sanitizer.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ def check_sanitization(input, htmloutput, xhtmloutput, rexmloutput)
8888
end
8989
end
9090

91+
def test_should_allow_data_attributes
92+
input = "<p data-foo='foo'>foo <bad>bar</bad> baz</p>"
93+
94+
output = "<p data-foo='foo'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"
95+
htmloutput = "<p data-foo='foo'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"
96+
97+
check_sanitization(input, htmloutput, output, output)
98+
end
99+
91100
##
92101
## libxml2 downcases attributes, so this is moot.
93102
##

0 commit comments

Comments
 (0)