Skip to content

Commit c2d30d3

Browse files
committed
Setup Rollup to prettify html.js
1 parent 68ca399 commit c2d30d3

File tree

5 files changed

+306
-183
lines changed

5 files changed

+306
-183
lines changed

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
/browser
22
/ssr/tests/demo.html
3-
/html.js

html.js

Lines changed: 175 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createElement } from './src/core.js';
1+
import { createElement } from "./src/core.js";
22

33
const MODE_SLASH = 0;
44
const MODE_TEXT = 1;
@@ -16,189 +16,182 @@ const PROP_SET = MODE_PROP_SET;
1616
const PROP_APPEND = MODE_PROP_APPEND;
1717

1818
const evaluate = (h, built, fields, args) => {
19-
let tmp;
20-
21-
// `build()` used the first element of the operation list as
22-
// temporary workspace. Now that `build()` is done we can use
23-
// that space to track whether the current element is "dynamic"
24-
// (i.e. it or any of its descendants depend on dynamic values).
25-
built[0] = 0;
26-
27-
for (let i = 1; i < built.length; i++) {
28-
const type = built[i++];
29-
30-
// Set `built[0]`'s appropriate bits if this element depends on a dynamic value.
31-
const value = built[i] ? ((built[0] |= type ? 1 : 2), fields[built[i++]]) : built[++i];
32-
33-
if (type === TAG_SET) {
34-
args[0] = value;
35-
}
36-
else if (type === PROPS_ASSIGN) {
37-
args[1] = Object.assign(args[1] || {}, value);
38-
}
39-
else if (type === PROP_SET) {
40-
(args[1] = args[1] || {})[built[++i]] = value;
41-
}
42-
else if (type === PROP_APPEND) {
43-
args[1][built[++i]] += (value + '');
44-
}
45-
else if (type) { // type === CHILD_RECURSE
46-
// Set the operation list (including the staticness bits) as
47-
// `this` for the `h` call.
48-
tmp = h.apply(value, evaluate(h, value, fields, ['', null]));
49-
args.push(tmp);
50-
51-
if (value[0]) {
52-
// Set the 2nd lowest bit it the child element is dynamic.
53-
built[0] |= 2;
54-
}
55-
else {
56-
// Rewrite the operation list in-place if the child element is static.
57-
// The currently evaluated piece `CHILD_RECURSE, 0, [...]` becomes
58-
// `CHILD_APPEND, 0, tmp`.
59-
// Essentially the operation list gets optimized for potential future
60-
// re-evaluations.
61-
built[i-2] = CHILD_APPEND;
62-
built[i] = tmp;
63-
}
64-
}
65-
else { // type === CHILD_APPEND
66-
args.push(value);
67-
}
68-
}
69-
70-
return args;
19+
let tmp;
20+
21+
// `build()` used the first element of the operation list as
22+
// temporary workspace. Now that `build()` is done we can use
23+
// that space to track whether the current element is "dynamic"
24+
// (i.e. it or any of its descendants depend on dynamic values).
25+
built[0] = 0;
26+
27+
for (let i = 1; i < built.length; i++) {
28+
const type = built[i++];
29+
30+
// Set `built[0]`'s appropriate bits if this element depends on a dynamic value.
31+
const value = built[i]
32+
? ((built[0] |= type ? 1 : 2), fields[built[i++]])
33+
: built[++i];
34+
35+
if (type === TAG_SET) {
36+
args[0] = value;
37+
} else if (type === PROPS_ASSIGN) {
38+
args[1] = Object.assign(args[1] || {}, value);
39+
} else if (type === PROP_SET) {
40+
(args[1] = args[1] || {})[built[++i]] = value;
41+
} else if (type === PROP_APPEND) {
42+
args[1][built[++i]] += value + "";
43+
} else if (type) {
44+
// type === CHILD_RECURSE
45+
// Set the operation list (including the staticness bits) as
46+
// `this` for the `h` call.
47+
tmp = h.apply(value, evaluate(h, value, fields, ["", null]));
48+
args.push(tmp);
49+
50+
if (value[0]) {
51+
// Set the 2nd lowest bit it the child element is dynamic.
52+
built[0] |= 2;
53+
} else {
54+
// Rewrite the operation list in-place if the child element is static.
55+
// The currently evaluated piece `CHILD_RECURSE, 0, [...]` becomes
56+
// `CHILD_APPEND, 0, tmp`.
57+
// Essentially the operation list gets optimized for potential future
58+
// re-evaluations.
59+
built[i - 2] = CHILD_APPEND;
60+
built[i] = tmp;
61+
}
62+
} else {
63+
// type === CHILD_APPEND
64+
args.push(value);
65+
}
66+
}
67+
68+
return args;
7169
};
7270

73-
const build = function(statics) {
74-
75-
let mode = MODE_TEXT;
76-
let buffer = '';
77-
let quote = '';
78-
let current = [0];
79-
let char, propName;
80-
81-
const commit = field => {
82-
if (mode === MODE_TEXT && (field || (buffer = buffer.replace(/^\s*\n\s*|\s*\n\s*$/g,'')))) {
83-
{
84-
current.push(CHILD_APPEND, field, buffer);
85-
}
86-
}
87-
else if (mode === MODE_TAGNAME && (field || buffer)) {
88-
{
89-
current.push(TAG_SET, field, buffer);
90-
}
91-
mode = MODE_WHITESPACE;
92-
}
93-
else if (mode === MODE_WHITESPACE && buffer === '...' && field) {
94-
{
95-
current.push(PROPS_ASSIGN, field, 0);
96-
}
97-
}
98-
else if (mode === MODE_WHITESPACE && buffer && !field) {
99-
{
100-
current.push(PROP_SET, 0, true, buffer);
101-
}
102-
}
103-
else if (mode >= MODE_PROP_SET) {
104-
{
105-
if (buffer || (!field && mode === MODE_PROP_SET)) {
106-
current.push(mode, 0, buffer, propName);
107-
mode = MODE_PROP_APPEND;
108-
}
109-
if (field) {
110-
current.push(mode, field, 0, propName);
111-
mode = MODE_PROP_APPEND;
112-
}
113-
}
114-
}
115-
116-
buffer = '';
117-
};
118-
119-
for (let i=0; i<statics.length; i++) {
120-
if (i) {
121-
if (mode === MODE_TEXT) {
122-
commit();
123-
}
124-
commit(i);
125-
}
126-
127-
for (let j=0; j<statics[i].length;j++) {
128-
char = statics[i][j];
129-
130-
if (mode === MODE_TEXT) {
131-
if (char === '<') {
132-
// commit buffer
133-
commit();
134-
{
135-
current = [current];
136-
}
137-
mode = MODE_TAGNAME;
138-
}
139-
else {
140-
buffer += char;
141-
}
142-
}
143-
else if (mode === MODE_COMMENT) {
144-
// Ignore everything until the last three characters are '-', '-' and '>'
145-
if (buffer === '--' && char === '>') {
146-
mode = MODE_TEXT;
147-
buffer = '';
148-
}
149-
else {
150-
buffer = char + buffer[0];
151-
}
152-
}
153-
else if (quote) {
154-
if (char === quote) {
155-
quote = '';
156-
}
157-
else {
158-
buffer += char;
159-
}
160-
}
161-
else if (char === '"' || char === "'") {
162-
quote = char;
163-
}
164-
else if (char === '>') {
165-
commit();
166-
mode = MODE_TEXT;
167-
}
168-
else if (!mode) ;
169-
else if (char === '=') {
170-
mode = MODE_PROP_SET;
171-
propName = buffer;
172-
buffer = '';
173-
}
174-
else if (char === '/' && (mode < MODE_PROP_SET || statics[i][j+1] === '>')) {
175-
commit();
176-
if (mode === MODE_TAGNAME) {
177-
current = current[0];
178-
}
179-
mode = current;
180-
{
181-
(current = current[0]).push(CHILD_RECURSE, 0, mode);
182-
}
183-
mode = MODE_SLASH;
184-
}
185-
else if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
186-
// <a disabled>
187-
commit();
188-
mode = MODE_WHITESPACE;
189-
}
190-
else {
191-
buffer += char;
192-
}
193-
194-
if (mode === MODE_TAGNAME && buffer === '!--') {
195-
mode = MODE_COMMENT;
196-
current = current[0];
197-
}
198-
}
199-
}
200-
commit();
201-
return current;
71+
const build = function (statics) {
72+
let mode = MODE_TEXT;
73+
let buffer = "";
74+
let quote = "";
75+
let current = [0];
76+
let char, propName;
77+
78+
const commit = (field) => {
79+
if (
80+
mode === MODE_TEXT &&
81+
(field || (buffer = buffer.replace(/^\s*\n\s*|\s*\n\s*$/g, "")))
82+
) {
83+
{
84+
current.push(CHILD_APPEND, field, buffer);
85+
}
86+
} else if (mode === MODE_TAGNAME && (field || buffer)) {
87+
{
88+
current.push(TAG_SET, field, buffer);
89+
}
90+
mode = MODE_WHITESPACE;
91+
} else if (mode === MODE_WHITESPACE && buffer === "..." && field) {
92+
{
93+
current.push(PROPS_ASSIGN, field, 0);
94+
}
95+
} else if (mode === MODE_WHITESPACE && buffer && !field) {
96+
{
97+
current.push(PROP_SET, 0, true, buffer);
98+
}
99+
} else if (mode >= MODE_PROP_SET) {
100+
{
101+
if (buffer || (!field && mode === MODE_PROP_SET)) {
102+
current.push(mode, 0, buffer, propName);
103+
mode = MODE_PROP_APPEND;
104+
}
105+
if (field) {
106+
current.push(mode, field, 0, propName);
107+
mode = MODE_PROP_APPEND;
108+
}
109+
}
110+
}
111+
112+
buffer = "";
113+
};
114+
115+
for (let i = 0; i < statics.length; i++) {
116+
if (i) {
117+
if (mode === MODE_TEXT) {
118+
commit();
119+
}
120+
commit(i);
121+
}
122+
123+
for (let j = 0; j < statics[i].length; j++) {
124+
char = statics[i][j];
125+
126+
if (mode === MODE_TEXT) {
127+
if (char === "<") {
128+
// commit buffer
129+
commit();
130+
{
131+
current = [current];
132+
}
133+
mode = MODE_TAGNAME;
134+
} else {
135+
buffer += char;
136+
}
137+
} else if (mode === MODE_COMMENT) {
138+
// Ignore everything until the last three characters are '-', '-' and '>'
139+
if (buffer === "--" && char === ">") {
140+
mode = MODE_TEXT;
141+
buffer = "";
142+
} else {
143+
buffer = char + buffer[0];
144+
}
145+
} else if (quote) {
146+
if (char === quote) {
147+
quote = "";
148+
} else {
149+
buffer += char;
150+
}
151+
} else if (char === '"' || char === "'") {
152+
quote = char;
153+
} else if (char === ">") {
154+
commit();
155+
mode = MODE_TEXT;
156+
} else if (!mode);
157+
else if (char === "=") {
158+
mode = MODE_PROP_SET;
159+
propName = buffer;
160+
buffer = "";
161+
} else if (
162+
char === "/" &&
163+
(mode < MODE_PROP_SET || statics[i][j + 1] === ">")
164+
) {
165+
commit();
166+
if (mode === MODE_TAGNAME) {
167+
current = current[0];
168+
}
169+
mode = current;
170+
{
171+
(current = current[0]).push(CHILD_RECURSE, 0, mode);
172+
}
173+
mode = MODE_SLASH;
174+
} else if (
175+
char === " " ||
176+
char === "\t" ||
177+
char === "\n" ||
178+
char === "\r"
179+
) {
180+
// <a disabled>
181+
commit();
182+
mode = MODE_WHITESPACE;
183+
} else {
184+
buffer += char;
185+
}
186+
187+
if (mode === MODE_TAGNAME && buffer === "!--") {
188+
mode = MODE_COMMENT;
189+
current = current[0];
190+
}
191+
}
192+
}
193+
commit();
194+
return current;
202195
};
203196

204197
const CACHE = new Map();

0 commit comments

Comments
 (0)