Create element instances, then resolve to strings #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Updated with new approach
This PR changes the behavior of
h()
from immediately executed functions, to a function that first creates element instances, and then later resolves them to a string. This means that JSX is now executed in a top-down order, instead of executing the most deeply nested function first. This also makesprops.children
an array of elements (instead of resolved strings), that can be manipulated similar to React.Children.Consider this JSX:
The current execution order is:
Image(), Heading(), Body(), Article()
. This PR switches that to the "expected"Article(), Heading(), Body(), Image()
.An example using new
props.children
:Output:
Semi-breaking Changes
The public JSX/vhtml API must be explicitly cast to a string. The element object uses
toString
to output the HTML.innerHTML = <Article>
still works because innerHTML casts to a string.Size: + 56 bytes, from 580 bytes to 636.
I believe this makes vhtml more consistent with other JSX implementations like React/Preact. It's certainly much more powerful, at the cost of 56 bytes.