Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concatenate mixed static + dynamic attribute values #93

Merged
merged 8 commits into from
Jul 8, 2019
Merged

Concatenate mixed static + dynamic attribute values #93

merged 8 commits into from
Jul 8, 2019

Conversation

jviide
Copy link
Collaborator

@jviide jviide commented Jun 17, 2019

This pull request implements string concatenation for mixed static + dynamic attributes and adds related tests. As long as there's more than one dynamic and/or static value they get interpreted as strings and concatenated. In practice this means that <div a="${1}2" />, <div a="1${2}" />, <div a="${1}${2}" />, <div a=${1}${2} /> and so on are effectively parsed as <div a=${"1" + "2"} />.

As a side effect HTM now allows empty attribute names like <div =1 /> or <div ""=1 />.

The htm.mjs.br size changes by +26 bytes (540 B -> 566 B) for the default build and +18 bytes (376 B -> 394 B) for the mini build.

Closes #58.

@jviide
Copy link
Collaborator Author

jviide commented Jun 17, 2019

Marked this PR as draft, because it doesn't yet implement the string concatenation for the Babel plugins.

@jviide jviide marked this pull request as ready for review June 17, 2019 23:53
@jviide
Copy link
Collaborator Author

jviide commented Jun 17, 2019

Now babel-plugin-htm also supports concatenating mixed static + dynamic attribute values. Some refactoring was necessary. The build and evaluate functions are in their own internal module now, along with a new treeify function. treeify takes the output of build and produces a tree-like structure that's hopefully bit easier to analyze and transform without needing to know about the gnarly size optimizations inside build/evaluate. The source code contains a small example how such a tree looks like.

The logic tries to keep the resulting code at least somewhat compact. For example <a b=${1}${2}> transforms to h("a",{b:""+1+2}) because the string coercion is needed. However, <a b=${"1"}${2}> transforms to h("a",{b:"1"+2}), as the "" + part would be unnecessary.

@jviide jviide requested a review from developit June 17, 2019 23:59
@jviide
Copy link
Collaborator Author

jviide commented Jul 2, 2019

Pushed a commit that fixed a performance regression in the non-mini version. Now the master and this PR get the same results when running npm run test:perf.

As a nice bonus the build size shrunk by 3 bytes, so the in total this branch adds +23 bytes to the non-mini build.

@@ -1,4 +1,4 @@
import htm from 'htm';
import { build, treeify } from '../../src/build.mjs';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this get bundled? If not the path will fail when published.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, build and treeify get bundled to dist/babel-plugin-htm.js (and the corresponding .mjs file).

(Fixed the weird tab between { build, and treeify } though 🙂)

@jviide jviide merged commit ff8cf6f into developit:master Jul 8, 2019
@jviide jviide deleted the concatenate branch July 8, 2019 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

string interpolation in attribute does not work as expected
2 participants