File tree 4 files changed +39
-4
lines changed
4 files changed +39
-4
lines changed Original file line number Diff line number Diff line change
1
+ ## v1.3.0
2
+ * Only print self-closing tags for
3
+ [ void-elements] ( https://www.w3.org/TR/html5/syntax.html#void-elements ) .
4
+ This could cause ` <strong /> ` in HTML documents, which is can be interpreted
5
+ as an opening tag by HTML5 parsers, causing the HTML structure to break.
6
+
1
7
## v1.2.0
2
8
* Does not depend on ` universal_html ` , uses custom HTML rendering for the output.
3
9
* Allowed classes are kept, even if there are non-allowed classes present on the same element.
Original file line number Diff line number Diff line change @@ -19,6 +19,26 @@ import 'package:html/dom.dart';
19
19
final _attrEscape = HtmlEscape (HtmlEscapeMode .attribute);
20
20
final _textEscape = HtmlEscape (HtmlEscapeMode .element);
21
21
22
+ // Set of HTML5 VOID-elements.
23
+ //
24
+ // See: https://www.w3.org/TR/html5/syntax.html#void-elements
25
+ final _voidElements = < String > {
26
+ 'area' ,
27
+ 'base' ,
28
+ 'br' ,
29
+ 'col' ,
30
+ 'embed' ,
31
+ 'hr' ,
32
+ 'img' ,
33
+ 'input' ,
34
+ 'link' ,
35
+ 'meta' ,
36
+ 'param' ,
37
+ 'source' ,
38
+ 'track' ,
39
+ 'wbr' ,
40
+ };
41
+
22
42
String formatHtmlNode (Node node) {
23
43
return _HtmlFormatter ()._format (node);
24
44
}
@@ -63,10 +83,13 @@ class _HtmlFormatter {
63
83
_sb.write ('>' );
64
84
_writeNodes (elem.nodes);
65
85
_sb.write ('</$tagName >' );
66
- } else if (tagName.toLowerCase () == 'script' ) {
67
- _sb. write ( '></$ tagName >' );
68
- } else {
86
+ } else if (_voidElements. contains ( tagName.toLowerCase ()) ) {
87
+ // Only VOID-elements cannot have a closing tag.
88
+ // https://www.w3.org/TR/html5/syntax.html#writing-html-documents-elements
69
89
_sb.write (' />' );
90
+ } else {
91
+ // If not a VOID-element, it is always safe to have a closing tag.
92
+ _sb.write ('></$tagName >' );
70
93
}
71
94
}
72
95
}
Original file line number Diff line number Diff line change 1
1
name : sanitize_html
2
- version : 1.2 .0
2
+ version : 1.3 .0
3
3
authors :
4
4
-
Jonas Finnemann Jensen <[email protected] >
5
5
description : |
Original file line number Diff line number Diff line change @@ -111,4 +111,10 @@ void main() {
111
111
testContains ('<div><div id="x">a</div></div>' , '<div><div>a</div></div>' );
112
112
testContains ('<a href="a.html">a</a><a href="b.html">b</a>' ,
113
113
'<a href="a.html">a</a><a href="b.html">b</a>' );
114
+
115
+ // test void elements
116
+ testContains ('<strong></strong> hello' , '<strong>' );
117
+ testContains ('<strong></strong> hello' , '</strong>' );
118
+ testNotContains ('<strong></strong> hello' , '<strong />' );
119
+ testContains ('<br>hello</br>' , '<br />' );
114
120
}
You can’t perform that action at this time.
0 commit comments