forked from developit/htm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
184 lines (172 loc) · 4.39 KB
/
index.mjs
File metadata and controls
184 lines (172 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const CACHE = {};
const stringify = JSON.stringify;
export default function html(statics) {
let key = '.';
for (let i=0; i<statics.length; i++) key += statics[i].length + ',' + statics[i];
const tpl = CACHE[key] || (CACHE[key] = build(statics));
// eslint-disable-next-line prefer-rest-params
return tpl(this, arguments);
}
const TAG_START = 60;
const TAG_END = 62;
const EQUALS = 61;
const QUOTE_DOUBLE = 34;
const QUOTE_SINGLE = 39;
const TAB = 9;
const NEWLINE = 10;
const RETURN = 13;
const SPACE = 32;
const SLASH = 47;
const MODE_WHITESPACE = 0;
const MODE_TEXT = 1;
const MODE_TAGNAME = 9;
const MODE_ATTRIBUTE = 13;
const MODE_SKIP = 47;
/** Create a template function given strings from a tagged template. */
const build = (statics) => {
let mode = MODE_WHITESPACE;
let out = 'return ';
let buffer = '';
let field = '';
let hasChildren = 0;
let props = '';
let propsClose = '';
let spreadClose = '';
let quote = 0;
let slash, charCode, inTag, propName, propHasValue;
const commit = () => {
if (!inTag) {
if (field || (buffer = buffer.replace(/^\s*\n\s*|\s*\n\s*$/g,''))) {
if (hasChildren++) out += ',';
out += field || stringify(buffer);
}
}
else if (mode === MODE_TAGNAME) {
if (hasChildren++) out += ',';
out += 'h(' + (field || stringify(buffer));
mode = MODE_WHITESPACE;
}
else if (mode === MODE_ATTRIBUTE || (mode === MODE_WHITESPACE && buffer === '...')) {
if (mode === MODE_WHITESPACE) {
if (!spreadClose) {
spreadClose = ')';
if (!props) props = 'Object.assign({}';
else props = 'Object.assign(' + props;
}
props += propsClose + ',' + field;
propsClose = '';
}
else if (propName) {
if (!props) props += '{';
else props += ',' + (propsClose ? '' : '{');
propsClose = '}';
props += stringify(propName) + ':';
props += field || ((propHasValue || buffer) && stringify(buffer)) || 'true';
propName = '';
}
propHasValue = false;
}
else if (mode === MODE_WHITESPACE) {
mode = MODE_ATTRIBUTE;
// we're in an attribute name
propName = buffer;
buffer = field = '';
commit();
mode = MODE_WHITESPACE;
}
buffer = field = '';
};
for (let i=0; i<statics.length; i++) {
if (i > 0) {
if (!inTag) commit();
field = `$[${i}]`;
commit();
}
for (let j=0; j<statics[i].length; j++) {
charCode = statics[i].charCodeAt(j);
if (!inTag) {
if (charCode === TAG_START) {
// commit buffer
commit();
inTag = 1;
spreadClose = propsClose = props = '';
slash = propHasValue = false;
mode = MODE_TAGNAME;
continue;
}
}
else {
if (charCode === QUOTE_SINGLE || charCode === QUOTE_DOUBLE) {
if (quote === charCode) {
quote = 0;
continue;
}
if (quote === 0) {
quote = charCode;
continue;
}
}
if (quote === 0) {
switch (charCode) {
case TAG_END:
commit();
if (mode !== MODE_SKIP) {
if (!props) {
out += ',null';
}
else {
out += ',' + props + propsClose + spreadClose;
}
}
if (slash) {
out += ')';
}
inTag = 0;
props = '';
mode = MODE_TEXT;
continue;
case EQUALS:
mode = MODE_ATTRIBUTE;
propHasValue = true;
propName = buffer;
buffer = '';
continue;
case SLASH:
if (!slash) {
slash = true;
// </foo>
if (mode === MODE_TAGNAME && !buffer.trim()) {
buffer = field = '';
mode = MODE_SKIP;
}
}
continue;
case TAB:
case NEWLINE:
case RETURN:
case SPACE:
// <a disabled>
commit();
mode = MODE_WHITESPACE;
continue;
}
}
}
buffer += statics[i].charAt(j);
}
}
commit();
return Function('h', '$', out);
};