This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f118e00
commit 0b8025c
Showing
3 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} | ||
} |