Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fabc9f7

Browse files
committed
feat(sanitizer): add html5 elements to the whitelist
Closes #89
1 parent c17c731 commit fabc9f7

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/sanitizer.js

+31-17
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,39 @@ var START_TAG_REGEXP = /^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:
2727
URI_REGEXP = /^((ftp|https?):\/\/|mailto:|#)/,
2828
NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g; // Match everything outside of normal chars and " (quote character)
2929

30-
// Empty Elements - HTML 4.01
31-
var emptyElements = makeMap("area,br,col,hr,img");
32-
33-
// Block Elements - HTML 4.01
34-
var blockElements = makeMap("address,blockquote,center,dd,del,dir,div,dl,dt,"+
35-
"hr,ins,li,map,menu,ol,p,pre,script,table,tbody,td,tfoot,th,thead,tr,ul");
36-
37-
// Inline Elements - HTML 4.01
38-
var inlineElements = makeMap("a,abbr,acronym,b,bdo,big,br,cite,code,del,dfn,em,font,i,img,"+
39-
"ins,kbd,label,map,q,s,samp,small,span,strike,strong,sub,sup,tt,u,var");
40-
// Elements that you can, intentionally, leave open
41-
// (and which close themselves)
42-
var closeSelfElements = makeMap("colgroup,dd,dt,li,p,td,tfoot,th,thead,tr");
30+
31+
// Good source of info about elements and attributes
32+
// http://dev.w3.org/html5/spec/Overview.html#semantics
33+
// http://simon.html5.org/html-elements
34+
35+
// Safe Void Elements - HTML5
36+
// http://dev.w3.org/html5/spec/Overview.html#void-elements
37+
var voidElements = makeMap("area,br,col,hr,img,wbr");
38+
39+
// Elements that you can, intentionally, leave open (and which close themselves)
40+
// http://dev.w3.org/html5/spec/Overview.html#optional-tags
41+
var optionalEndTagBlockElements = makeMap("colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr"),
42+
optionalEndTagInlineElements = makeMap("rp,rt"),
43+
optionalEndTagElements = extend({}, optionalEndTagInlineElements, optionalEndTagBlockElements);
44+
45+
// Safe Block Elements - HTML5
46+
var blockElements = extend({}, optionalEndTagBlockElements, makeMap("address,article,aside," +
47+
"blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5,h6," +
48+
"header,hgroup,hr,ins,map,menu,nav,ol,pre,script,section,table,ul"));
49+
50+
// Inline Elements - HTML5
51+
var inlineElements = extend({}, optionalEndTagInlineElements, makeMap("a,abbr,acronym,b,bdi,bdo," +
52+
"big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s,samp,small," +
53+
"span,strike,strong,sub,sup,time,tt,u,var"));
54+
55+
4356
// Special Elements (can contain anything)
4457
var specialElements = makeMap("script,style");
45-
var validElements = extend({}, emptyElements, blockElements, inlineElements, closeSelfElements);
58+
59+
var validElements = extend({}, voidElements, blockElements, inlineElements, optionalEndTagElements);
4660

4761
//Attributes that have href and hence need to be sanitized
48-
var uriAttrs = makeMap("background,href,longdesc,src,usemap");
62+
var uriAttrs = makeMap("background,cite,href,longdesc,src,usemap");
4963
var validAttrs = extend({}, uriAttrs, makeMap(
5064
'abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,'+
5165
'color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,'+
@@ -146,11 +160,11 @@ function htmlParser( html, handler ) {
146160
}
147161
}
148162

149-
if ( closeSelfElements[ tagName ] && stack.last() == tagName ) {
163+
if ( optionalEndTagElements[ tagName ] && stack.last() == tagName ) {
150164
parseEndTag( "", tagName );
151165
}
152166

153-
unary = emptyElements[ tagName ] || !!unary;
167+
unary = voidElements[ tagName ] || !!unary;
154168

155169
if ( !unary )
156170
stack.push( tagName );

0 commit comments

Comments
 (0)