Skip to content

Commit f505287

Browse files
authored
Merge pull request #12 from jonasfj/fix-11
Fix issue #11, only use self-closing tags for void-elements
2 parents df272a5 + a0c0b0b commit f505287

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

sanitize_html/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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+
17
## v1.2.0
28
* Does not depend on `universal_html`, uses custom HTML rendering for the output.
39
* Allowed classes are kept, even if there are non-allowed classes present on the same element.

sanitize_html/lib/src/html_formatter.dart

+26-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ import 'package:html/dom.dart';
1919
final _attrEscape = HtmlEscape(HtmlEscapeMode.attribute);
2020
final _textEscape = HtmlEscape(HtmlEscapeMode.element);
2121

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+
2242
String formatHtmlNode(Node node) {
2343
return _HtmlFormatter()._format(node);
2444
}
@@ -63,10 +83,13 @@ class _HtmlFormatter {
6383
_sb.write('>');
6484
_writeNodes(elem.nodes);
6585
_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
6989
_sb.write(' />');
90+
} else {
91+
// If not a VOID-element, it is always safe to have a closing tag.
92+
_sb.write('></$tagName>');
7093
}
7194
}
7295
}

sanitize_html/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sanitize_html
2-
version: 1.2.0
2+
version: 1.3.0
33
authors:
44
- Jonas Finnemann Jensen <[email protected]>
55
description: |

sanitize_html/test/sanitize_html_test.dart

+6
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,10 @@ void main() {
111111
testContains('<div><div id="x">a</div></div>', '<div><div>a</div></div>');
112112
testContains('<a href="a.html">a</a><a href="b.html">b</a>',
113113
'<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 />');
114120
}

0 commit comments

Comments
 (0)