Fernando Tiberti
Fernando Tiberti

Reputation: 1322

How to use the dart:html library to write html files?

I want to make a program that prepares an HTML file. It would either be on the server side or just running in my local machine.

I think it would be nice to be able to use the dart:html library since it has a lot of methods for manipulating html (obviously). But it is thought to be used dynamically on the client side, and I want to use it like this: manipulate an html DOM tree with dart:html, and when its ready, write a static html file. For instance using query('body').innerHtml

The problem I'm running into is that I if start a project with the "console application" template, I am not able to make dart:html talk to an html file. And if I choose "web application", in which I am able to do this, I cannot load the dart:io library, maybe it has to do with it being tagged as [server] in the SDK?

Of course I could just do:

print(query('body').innerHtml);

and manually copying the output to a file, but I thought maybe there is a more elegant solution.

Upvotes: 4

Views: 697

Answers (1)

Greg Lowe
Greg Lowe

Reputation: 16261

See html5lib.

html5lib in Pure Dart

This is a pure Dart html5 parser. It's a port of html5lib from Python. Since it's 100% Dart you can use it safely from a script or server side app.

Eventually the parse tree API will be compatible with dart:html, so the same code will work on the client or the server.

It doesn't support much in the way of queries yet.

Upvotes: 3

Related Questions