This PR rewrites a good bit of the core logic.
Before
Before, every get
on h
would append to this.htmlString: string
internally, and calling the resulting function would return that string:
h.div.id("test")(
h.p`Hello world`
) === `<div id="test"><p>Hello world</p></div>`
After this PR
Now, instead of passing this.htmlString
around and appending to it, we pass around this.tree
, which has the same type signature as https://posthtml.org/#/tree.
Furthermore, calling the resulting function will no longer return an HTML string, but return the h instance itself. Instead, one must manually call toString
on the outermost function to get an HTML string.
h.div.id("test")(
h.p`Hello world`
).toString() === `<div id="test"><p>Hello world</p></div>`
Why make this change? It is more verbose.
In the first code block, hdot functions are stringified from the inside out. h.p
is converted to an HTML string, which is passed as a child to h.div
. This has a major ramification: parent elements can not inspect the content of their children without complex string manipulation.
The new logic builds one tree structure, and then only stringifies once from the top down, when the dev calls toString
.
Plugins
This enables plugins to do a lot more. Since the tree structure is the same as postHTML's, plugins should be mostly interoperable with a few more changes. Plugins will now only run once per tree, which should be a performance boost.
TODO: add more plugin docs