-
Notifications
You must be signed in to change notification settings - Fork 179
Implement HTM without eval #53
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ae0f622
Implement HTM without eval
jviide dbdc12e
Optimize build.js size by avoiding comparisons
jviide 6752939
Implement multiple root elements
jviide d060bb8
Optimize size with String#indexOf
jviide 7199d5c
Use property access instead of String#charAt
jviide 80e602c
Remove a String#indexOf call
jviide 0bb9227
Merge branch 'master' into no-eval
developit bd4572a
Merge branch 'master' into no-eval
jviide bcd229a
Backport optimizations from the htmini branch
jviide 1f277a2
Optimize build.mjs size
jviide 1bddae7
Clean up unnecessary code
jviide 35e2821
Reorganize code for smaller size
jviide 0608217
Optimize instruction arrays
jviide 1f992e8
Use Map for caching when available
jviide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| const TAG_SET = 1; | ||
| const PROPS_SET = 2; | ||
| const PROPS_ASSIGN = 3; | ||
| const CHILD_RECURSE = 4; | ||
| const CHILD_APPEND = 0; | ||
|
|
||
| const MODE_SLASH = 0; | ||
| const MODE_TEXT = 1; | ||
| const MODE_WHITESPACE = 2; | ||
| const MODE_TAGNAME = 3; | ||
| const MODE_ATTRIBUTE = 4; | ||
|
|
||
| export const evaluate = (h, current, fields, args) => { | ||
| for (let i = 1; i < current.length; i++) { | ||
| const field = current[i++]; | ||
| const value = typeof field === 'number' ? fields[field] : field; | ||
|
|
||
| if (current[i] === TAG_SET) { | ||
| args[0] = value; | ||
| } | ||
| else if (current[i] === PROPS_SET) { | ||
| (args[1] = args[1] || {})[current[++i]] = value; | ||
| } | ||
| else if (current[i] === PROPS_ASSIGN) { | ||
| args[1] = Object.assign(args[1] || {}, value); | ||
| } | ||
| else if (current[i]) { | ||
| // code === CHILD_RECURSE | ||
| // eslint-disable-next-line prefer-spread | ||
| args.push(h.apply(null, evaluate(h, value, fields, ['', null]))); | ||
| } | ||
| else { | ||
| // code === CHILD_APPEND | ||
| args.push(value); | ||
| } | ||
| } | ||
|
|
||
| return args; | ||
| }; | ||
|
|
||
|
|
||
| export const build = (statics) => { | ||
| let mode = MODE_TEXT; | ||
| let buffer = ''; | ||
| let quote = ''; | ||
| let current = [0]; | ||
| let char, propName; | ||
|
|
||
| const commit = field => { | ||
| if (mode === MODE_TEXT && (field || (buffer = buffer.replace(/^\s*\n\s*|\s*\n\s*$/g,'')))) { | ||
| current.push(field || buffer, CHILD_APPEND); | ||
| } | ||
| else if (mode === MODE_TAGNAME && (field || buffer)) { | ||
| current.push(field || buffer, TAG_SET); | ||
| mode = MODE_WHITESPACE; | ||
| } | ||
| else if (mode === MODE_WHITESPACE && buffer === '...' && field) { | ||
| current.push(field, PROPS_ASSIGN); | ||
| } | ||
| else if (mode === MODE_WHITESPACE && buffer && !field) { | ||
| current.push(true, PROPS_SET, buffer); | ||
| } | ||
| else if (mode === MODE_ATTRIBUTE && propName) { | ||
| current.push(field || buffer, PROPS_SET, propName); | ||
| propName = ''; | ||
| } | ||
| buffer = ''; | ||
| }; | ||
|
|
||
| for (let i=0; i<statics.length; i++) { | ||
| if (i) { | ||
| if (mode === MODE_TEXT) { | ||
| commit(); | ||
| } | ||
| commit(i); | ||
| } | ||
|
|
||
| for (let j=0; j<statics[i].length; j++) { | ||
| char = statics[i][j]; | ||
|
|
||
| if (mode === MODE_TEXT) { | ||
| if (char === '<') { | ||
| // commit buffer | ||
| commit(); | ||
| current = [current]; | ||
| mode = MODE_TAGNAME; | ||
| } | ||
| else { | ||
| buffer += char; | ||
| } | ||
| } | ||
| else if (quote) { | ||
| if (char === quote) { | ||
| quote = ''; | ||
| } | ||
| else { | ||
| buffer += char; | ||
| } | ||
| } | ||
| else if (char === '"' || char === "'") { | ||
| quote = char; | ||
| } | ||
| else if (char === '>') { | ||
| commit(); | ||
| mode = MODE_TEXT; | ||
| } | ||
| else if (!mode) { | ||
| // Ignore everything until the tag ends | ||
| } | ||
| else if (char === '=') { | ||
| mode = MODE_ATTRIBUTE; | ||
| propName = buffer; | ||
| buffer = ''; | ||
| } | ||
| else if (char === '/') { | ||
| commit(); | ||
| if (mode === MODE_TAGNAME) { | ||
| current = current[0]; | ||
| } | ||
| mode = current; | ||
| (current = current[0]).push(mode, CHILD_RECURSE); | ||
| mode = MODE_SLASH; | ||
| } | ||
| else if (char === ' ' || char === '\t' || char === '\n' || char === '\r') { | ||
| // <a disabled> | ||
| commit(); | ||
| mode = MODE_WHITESPACE; | ||
| } | ||
| else { | ||
| buffer += char; | ||
| } | ||
| } | ||
| } | ||
| commit(); | ||
| return current; | ||
| }; | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be benefit to using
= statics[i][j|0]:https://esbench.com/bench/5c181d294cd7e6009ef61e48
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to have a notable effect (in my environment).