Skip to content

Commit 3ad2f8c

Browse files
committed
Reorganize code to optimize size
1 parent 077a065 commit 3ad2f8c

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

src/build.mjs

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const build = (h, fields) => {
1111
let buffer = '';
1212
let quote = '';
1313
let args = root;
14-
let char, propName, idx;
14+
let char, propName;
1515

1616
const commit = () => {
1717
if (mode === MODE_TEXT && (buffer = buffer.replace(/^\s*\n\s*|\s*\n\s*$/g, ''))) {
@@ -54,53 +54,58 @@ export const build = (h, fields) => {
5454
for (let j=0; j<fields[0][i].length; j++) {
5555
char = fields[0][i][j];
5656

57-
if (mode === MODE_TEXT && char === '<') {
58-
// commit buffer
59-
commit();
60-
stack.push(args);
61-
args = ['', null];
62-
buffer = '';
63-
mode = MODE_TAGNAME;
64-
}
65-
else if (mode !== MODE_TEXT && (char === quote || !quote) && (idx = '\'">=/\t\n\r '.indexOf(char)) >= 0) {
66-
if (idx < 2) {
67-
// char === '"' || char === "'"
68-
quote = quote ? '' : char;
69-
}
70-
else if (idx === 2) {
71-
// char === '>'
57+
if (mode === MODE_TEXT) {
58+
if (char === '<') {
59+
// commit buffer
7260
commit();
73-
mode = MODE_TEXT;
61+
stack.push(args);
62+
args = ['', null];
63+
buffer = '';
64+
mode = MODE_TAGNAME;
7465
}
75-
else if (idx === 3) {
76-
// char === '='
77-
if (mode) {
78-
mode = MODE_ATTRIBUTE;
79-
propName = buffer;
80-
buffer = '';
81-
}
66+
else {
67+
buffer += char;
8268
}
83-
else if (idx === 4) {
84-
// char === '/'
85-
if (mode) {
86-
commit();
87-
if (mode === MODE_TAGNAME) {
88-
// no tag name before the slash
89-
args = stack.pop();
90-
}
91-
// eslint-disable-next-line prefer-spread
92-
mode = h.apply(null, args); // Use 'mode' as a temporary variable
93-
(args = stack.pop()).push(mode);
94-
mode = MODE_SLASH;
95-
}
69+
}
70+
else if (quote) {
71+
if (char === quote) {
72+
quote = '';
9673
}
97-
else if (mode) {
98-
// char is a whitespace
99-
// <a disabled>
100-
commit();
101-
mode = MODE_WHITESPACE;
74+
else {
75+
buffer += char;
10276
}
10377
}
78+
else if (char === '"' || char === "'") {
79+
quote = char;
80+
}
81+
else if (char === '>') {
82+
commit();
83+
mode = MODE_TEXT;
84+
}
85+
else if (!mode) {
86+
// Ignore everything until the tag ends
87+
}
88+
else if (char === '=') {
89+
mode = MODE_ATTRIBUTE;
90+
propName = buffer;
91+
buffer = '';
92+
}
93+
else if (char === '/') {
94+
commit();
95+
if (mode === MODE_TAGNAME) {
96+
// no tag name before the slash
97+
args = stack.pop();
98+
}
99+
// eslint-disable-next-line prefer-spread
100+
mode = h.apply(null, args); // Use 'mode' as a temporary variable
101+
(args = stack.pop()).push(mode);
102+
mode = MODE_SLASH;
103+
}
104+
else if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
105+
// <a disabled>
106+
commit();
107+
mode = MODE_WHITESPACE;
108+
}
104109
else {
105110
buffer += char;
106111
}

0 commit comments

Comments
 (0)