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
190 lines (175 loc) · 4.26 KB
/
index.mjs
File metadata and controls
190 lines (175 loc) · 4.26 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
185
186
187
188
189
190
/**
* 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 = {};
export default function html(statics) {
const str = statics.join('\0');
const tpl = CACHE[str] || (CACHE[str] = build(str));
// 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. */
function build(input) {
let out = 'return ';
let buffer = '';
let mode = MODE_WHITESPACE;
let fieldIndex = 1;
let field = '';
let hasChildren = 0;
let propCount = 0;
let spreads = 0;
let quote = 0;
let spread, slash, charCode, inTag, propName, propHasValue;
function commit() {
if (!inTag) {
if (field || (buffer = buffer.replace(/^\s*\n+\s*|\s*\n+\s*$/g,''))) {
if (hasChildren++) out += ',';
out += field || JSON.stringify(buffer);
}
}
else if (mode === MODE_TAGNAME) {
if (hasChildren++) out += ',';
out += 'h(' + (field || JSON.stringify(buffer));
mode = MODE_WHITESPACE;
}
else if (mode === MODE_ATTRIBUTE || (mode === MODE_WHITESPACE && buffer === '...')) {
if (mode === MODE_WHITESPACE) {
spread = true;
if (!spreads++) {
if (propCount === 0) out += ',Object.assign({},';
else out = out.replace(/,\(\{(.*?)$/, ',Object.assign({},{$1') + '},';
}
out += field + ',{';
propCount++;
}
else if (propName) {
if (!spread) out += ',';
if (propCount === 0) out += '({';
out += propName + ':';
out += field || ((propHasValue || buffer) && JSON.stringify(buffer)) || 'true';
propName = '';
spread = false;
propCount++;
}
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<input.length; i++) {
charCode = input.charCodeAt(i);
field = '';
if (charCode === QUOTE_SINGLE || charCode === QUOTE_DOUBLE) {
if (quote === charCode) {
quote = 0;
continue;
}
if (quote === 0) {
quote = charCode;
continue;
}
}
if (charCode === 0) {
if (!inTag) commit();
field = `$_h[${fieldIndex++}]`;
commit();
continue;
}
if (quote === 0) {
switch (charCode) {
case TAG_START:
if (!inTag) {
// commit buffer
commit();
inTag = true;
propCount = 0;
slash = spread = propHasValue = false;
mode = MODE_TAGNAME;
continue;
}
case TAG_END:
if (inTag) {
commit();
if (mode !== MODE_SKIP) {
if (propCount === 0) {
out += ',null';
}
else {
out += '})';
}
}
if (slash) {
out += ')';
}
inTag = false;
propCount = 0;
mode = MODE_TEXT;
continue;
}
case EQUALS:
if (inTag) {
mode = MODE_ATTRIBUTE;
propHasValue = true;
propName = buffer;
buffer = '';
continue;
}
case SLASH:
if (inTag) {
if (!slash) {
slash = true;
// </foo>
if (mode === MODE_TAGNAME && !field && !buffer.trim().length) {
buffer = field = '';
mode = MODE_SKIP;
}
}
continue;
}
case TAB:
case NEWLINE:
case RETURN:
case SPACE:
// <a disabled>
if (inTag) {
commit();
continue;
}
}
}
buffer += input.charAt(i);
}
commit();
return Function('h', '$_h', out);
}