Skip to content

Fix corner cases by unifying escaping/unescaping - #21

Closed
jviide wants to merge 3 commits into
developit:masterfrom
jviide:patch-01
Closed

jviide wants to merge 3 commits into
developit:masterfrom
jviide:patch-01

Conversation

@jviide

@jviide jviide commented Nov 23, 2018 •

Copy link
Copy Markdown
Collaborator

This pull request addresses some corner cases and adds tests for those cases:

  • <div>$_h[1]</div> gets transformed to h("div",{},[]);
  • Static attribute values that contain upper-case characters get mangled, e.g. <div id=Hello> gets transformed to h(\"div\",{id:\":::Hello\"},[]);
  • I'm not too sure about this, but based on this HTML spec attribute names can theoretically contain characters such as \, which can get transformed into malformed code.

These problems were partially caused by the way how e.g. upper-case characters in attributes are escaped before being fed to the browser's HTML parser and how they couldn't be unambigiously unescaped back later. Therefore this patch modifies placeholder handling and escaping:

  1. In statics underscore and upper-case characters get prefixed with $_ (so e.g. A becomes $_A).
  2. Variables get transformed to $_[<number>] (e.g. $_[1]) instead of $_h[<number>] to separate them from potentially lower-cased $_H.
  3. Attribute names containing a placeholder tag name is $_ instead of c@ so the complete string would be $_=<something>.

Cases 2. and 3. are handled as previously, and case 1. gets reversed in the end - the idea being that in the end every substring that matches /\$_[_A-Z]/i can be safely and unambiguously turned back into an underscore or upper-case character. Attribute names also get escaped with JSON.stringify.

These changes do not seem to materially affect the performance in my environment, but of course YMMV. As a bonus the gzipped builds seem to be a tad smaller (671 B for htm.mjs.gz, down from 688 B).

There's also a minor change to preact.test.js, as the new code assumes that the statics that get passed in are always strings. I think this is a valid assumption..?

@jviide

jviide commented Nov 23, 2018

Copy link
Copy Markdown
Collaborator Author

I have signed the CLA.

@jviide

jviide commented Nov 23, 2018 •

Copy link
Copy Markdown
Collaborator Author

Spotted one more case: the Babel plugin transforming <div>$$$_h_[1]</div> to h("div",{},[]);. This was due to similar escaping problems, but this time in the Babel plugin's index.mjs file. Solved it by partially reusing the same trick, namely escaping all underscores in statics to $__ and all variables to $_[<number>] and unescaping in the end accordingly.

Also reorganized the tests to run through potential problems with underscores and $_ placeholders.

Update: Separated the Babel plugin specific fixes to PR #35.

@developit

Copy link
Copy Markdown
Owner

I'm worried $_ isn't a unique enough prefix. Thoughts?

@jviide

jviide commented Nov 23, 2018

Copy link
Copy Markdown
Collaborator Author

Yes, $_ may well appear in the input statics, but escaping should take care of that. Those $_s get escaped to $$__ (notice the two underscores). Therefore they won't be confused with placeholders, as the placeholders are always prefixed with $_A ... $_Z, $_[ or $_=.

In the escaped text the placeholders will be the only appearances of $_ that are immediately followed some other character than an underscore. This also allows us unescape things prefixed with$_ back unambiguously.

@jviide

jviide commented Nov 26, 2018

Copy link
Copy Markdown
Collaborator Author

I forgot to mention: one of the added tests checks that $_s in different parts of the source template aren't confused with placeholders and are preserved in the result as-is.

@jviide

jviide commented Dec 4, 2018

Copy link
Copy Markdown
Collaborator Author

To make reviewing easier I separated the Babel plugin specific changes to pull request #35 and added a new test file htm.test.js for htm-specific tests that don't depend on the Babel plugin.

@jviide

jviide commented Dec 18, 2018

Copy link
Copy Markdown
Collaborator Author

Closing this in preparation to #38.

@jviide jviide closed this Dec 18, 2018
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.

2 participants