Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in dart parse HTML string to DOM

Tags:

html

parsing

dart

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.)

like image 839
xhg Avatar asked Oct 19 '13 14:10

xhg


People also ask

How do I display HTML content in flutter?

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.

What is HTML parser?

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.


2 Answers

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.

like image 107
Eason PI Avatar answered Oct 02 '22 15:10

Eason PI


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.

like image 24
Vidya Avatar answered Oct 02 '22 13:10

Vidya