In dart,
I want to parse a string "<!DOCTYPE HTMT><html><head></head><body>....</body></html>
" to a DOM so that I can manipulate element in the generated DOM. I know in JQuery, there is $.parseHTML to deal with it. But I can not find anything similar in dart.
Thank you.
(I have tried html2lib, but the output Document cannot use query(".classname")
to select.)
To display valid HTML you can set the src field to the following: _src = "data:text/html;charset=utf-8," + Uri. encodeComponent("HTML_CONTENT_HERE"); For the package you can also pass markdown to the src and it will convert it for you.
HTML parsing involves tokenization and tree construction. HTML tokens include start and end tags, as well as attribute names and values. If the document is well-formed, parsing it is straightforward and faster. The parser parses tokenized input into the document, building up the document tree.
You can create an element by parsing HTML text:
new Element.html("YOUR HTML STRING HERE");
see Dart: Up and Running CH03
EDIT
You may need to pass a NodeValidator to get the entire text rendered like:
NodeValidator nodeValidator = new NodeValidatorBuilder()
..allowTextElements();
// ..allowHtml5()
// ..allowElement('Button', attributes: ['ng-disabled']);
new Element.html("YOUR HTML STRING HERE", validator: nodeValidator);
If you run the application you get log messages like "element/attribute xxx removed from ..." if some of the text was not accepted by the NodeValidator.
Try html. It's 100% pure Dart, so you should be set.
You could also consider an XML parser for Dart like xml depending on the nature of your HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With