-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.dart
57 lines (49 loc) · 1.15 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import 'package:html/dom.dart';
import 'package:html/dom_parsing.dart';
import 'package:html/parser.dart';
void main(List<String> args) {
var document = parse('''
<body>
<h2>Header 1</h2>
<p>Text.</p>
<h2>Header 2</h2>
More text.
<br/>
</body>''');
// outerHtml output
print('outer html:');
print(document.outerHtml);
print('');
// visitor output
print('html visitor:');
_Visitor().visit(document);
}
// Note: this example visitor doesn't handle things like printing attributes and
// such.
class _Visitor extends TreeVisitor {
String indent = '';
@override
void visitText(Text node) {
if (node.data.trim().isNotEmpty) {
print('$indent${node.data.trim()}');
}
}
@override
void visitElement(Element node) {
if (isVoidElement(node.localName)) {
print('$indent<${node.localName}/>');
} else {
print('$indent<${node.localName}>');
indent += ' ';
visitChildren(node);
indent = indent.substring(0, indent.length - 2);
print('$indent</${node.localName}>');
}
}
@override
void visitChildren(Node node) {
for (var child in node.nodes) {
visit(child);
}
}
}