Skip to content

Commit bcd229a

Browse files
committed
Backport optimizations from the htmini branch
1 parent bd4572a commit bcd229a

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

src/build.mjs

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ const MODE_WHITESPACE = 3;
1010
const MODE_TAGNAME = 4;
1111
const MODE_ATTRIBUTE = 5;
1212

13-
export const evaluate = (h, current, fields) => {
14-
const args = ['', null];
15-
13+
export const evaluate = (h, current, fields, args) => {
1614
for (let i = 1; i < current.length; i++) {
1715
const field = current[i++];
1816
const value = field ? fields[field] : current[i];
19-
17+
2018
const code = current[++i];
2119
if (code === TAG_SET) {
2220
args[0] = value;
@@ -28,49 +26,42 @@ export const evaluate = (h, current, fields) => {
2826
args[1] = Object.assign(args[1] || {}, value);
2927
}
3028
else if (code === CHILD_RECURSE) {
31-
args.push(evaluate(h, value, fields));
29+
// eslint-disable-next-line prefer-spread
30+
args.push(h.apply(null, evaluate(h, value, fields, ['', null])));
3231
}
3332
else { // code === CHILD_APPEND
3433
args.push(value);
3534
}
3635
}
3736

38-
// eslint-disable-next-line prefer-spread
39-
return h.apply(null, args);
37+
return args;
4038
};
4139

42-
/** Create a template function given strings from a tagged template. */
40+
4341
export const build = (statics) => {
4442
let mode = MODE_TEXT;
4543
let buffer = '';
4644
let quote = '';
47-
let fallbackPropValue = true;
48-
let current = [];
45+
let current = [0];
4946
let char, propName, idx;
5047

5148
const commit = field => {
52-
if (mode === MODE_TEXT) {
53-
if (field || (buffer = buffer.replace(/^\s*\n\s*|\s*\n\s*$/g,''))) {
54-
current.push(field, buffer, CHILD_APPEND);
55-
}
49+
if (mode === MODE_TEXT && (field || (buffer = buffer.replace(/^\s*\n\s*|\s*\n\s*$/g,'')))) {
50+
current.push(field, buffer, CHILD_APPEND);
5651
}
5752
else if (mode === MODE_TAGNAME && (field || buffer)) {
5853
current.push(field, buffer, TAG_SET);
5954
mode = MODE_WHITESPACE;
6055
}
61-
else if (mode === MODE_WHITESPACE && buffer === '...') {
56+
else if (mode === MODE_WHITESPACE && buffer === '...' && field) {
6257
current.push(field, 0, PROPS_ASSIGN);
6358
}
64-
else if (mode) { // mode === MODE_ATTRIBUTE || mode === MODE_WHITESPACE
65-
if (mode === MODE_WHITESPACE) {
66-
propName = buffer;
67-
buffer = '';
68-
}
69-
if (propName) {
70-
current.push(field, buffer || fallbackPropValue, PROPS_SET, propName);
71-
propName = '';
72-
}
73-
fallbackPropValue = true;
59+
else if (mode === MODE_WHITESPACE && buffer && !field) {
60+
current.push(0, true, PROPS_SET, buffer);
61+
}
62+
else if (mode === MODE_ATTRIBUTE && propName) {
63+
current.push(field, buffer, PROPS_SET, propName);
64+
propName = '';
7465
}
7566
buffer = '';
7667
};
@@ -82,7 +73,7 @@ export const build = (statics) => {
8273
}
8374
commit(i);
8475
}
85-
76+
8677
for (let j=0; j<statics[i].length; j++) {
8778
char = statics[i][j];
8879

@@ -100,31 +91,27 @@ export const build = (statics) => {
10091
else if (idx === 2) {
10192
// char === '>'
10293
commit();
103-
104-
if (!mode) {
105-
// encountered a slash in current tag
106-
107-
if (current.length === 1) {
108-
// no tag name or attributes before the slash
109-
current = current[0];
110-
}
111-
current[0].push(0, current, CHILD_RECURSE);
112-
current = current[0];
113-
}
11494
mode = MODE_TEXT;
11595
}
11696
else if (idx === 3) {
11797
// char === '='
11898
if (mode) {
11999
mode = MODE_ATTRIBUTE;
120100
propName = buffer;
121-
buffer = fallbackPropValue = '';
101+
buffer = '';
122102
}
123103
}
124104
else if (idx === 4) {
125105
// char === '/'
126-
commit();
127-
mode = MODE_SLASH;
106+
if (mode) {
107+
commit();
108+
if (mode === MODE_TAGNAME) {
109+
current = current[0];
110+
}
111+
mode = current;
112+
(current = current[0]).push(0, mode, CHILD_RECURSE);
113+
mode = MODE_SLASH;
114+
}
128115
}
129116
else if (mode) {
130117
// char is a whitespace

src/index.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import { build, evaluate } from './build.mjs';
1515

1616
const CACHE = {};
1717

18-
export default function html(statics) {
18+
export default function html() {
19+
// eslint-disable-next-line prefer-rest-params
20+
const statics = arguments[0];
21+
1922
let key = '.';
2023
for (let i=0; i<statics.length; i++) {
2124
key += statics[i].length + ',' + statics[i];
2225
}
2326
const tpl = CACHE[key] || (CACHE[key] = build(statics));
2427

25-
const res = [];
26-
for (let i=1; i<tpl.length; i+=3) {
27-
// eslint-disable-next-line prefer-rest-params
28-
res.push(evaluate(this, tpl[i], arguments));
29-
}
28+
// eslint-disable-next-line prefer-rest-params
29+
const res = evaluate(this, tpl, arguments, []);
3030
return res.length > 1 ? res : res[0];
3131
}

0 commit comments

Comments
 (0)