File size: 7,450 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseAttributes = parseAttributes;
const espree_1 = require("./espree");
const RE_IS_SPACE = /^\s$/u;
class State {
    constructor(code, index) {
        this.code = code;
        this.index = index;
    }
    getCurr() {
        return this.code[this.index];
    }
    skipSpaces() {
        while (this.currIsSpace()) {
            this.advance();
            if (this.eof())
                break;
        }
    }
    currIsSpace() {
        return RE_IS_SPACE.test(this.getCurr() || "");
    }
    currIs(expect) {
        return this.code.startsWith(expect, this.index);
    }
    eof() {
        return this.index >= this.code.length;
    }
    eat(expect) {
        if (!this.currIs(expect)) {
            return null;
        }
        this.index += expect.length;
        return expect;
    }
    advance() {
        this.index++;
        return this.getCurr();
    }
}
/** Parse HTML attributes */
function parseAttributes(code, startIndex) {
    const attributes = [];
    const state = new State(code, startIndex);
    while (!state.eof()) {
        state.skipSpaces();
        if (state.currIs(">") || state.currIs("/>") || state.eof())
            break;
        attributes.push(parseAttribute(state));
    }
    return { attributes, index: state.index };
}
/** Parse HTML attribute */
function parseAttribute(state) {
    const start = state.index;
    // parse key
    const key = parseAttributeKey(state);
    const keyEnd = state.index;
    state.skipSpaces();
    if (!state.eat("=")) {
        return {
            type: "Attribute",
            name: key,
            value: true,
            start,
            end: keyEnd,
            metadata: null,
            parent: null,
        };
    }
    state.skipSpaces();
    if (state.eof()) {
        return {
            type: "Attribute",
            name: key,
            value: true,
            start,
            end: keyEnd,
            metadata: null,
            parent: null,
        };
    }
    // parse value
    const value = parseAttributeValue(state);
    return {
        type: "Attribute",
        name: key,
        value: [value],
        start,
        end: state.index,
        metadata: null,
        parent: null,
    };
}
/** Parse HTML attribute key */
function parseAttributeKey(state) {
    const start = state.index;
    while (state.advance()) {
        if (state.currIs("=") ||
            state.currIs(">") ||
            state.currIs("/>") ||
            state.currIsSpace()) {
            break;
        }
    }
    const end = state.index;
    return state.code.slice(start, end);
}
/** Parse HTML attribute value */
function parseAttributeValue(state) {
    const start = state.index;
    const quote = state.eat('"') || state.eat("'");
    const startBk = state.index;
    const expression = parseAttributeMustache(state);
    if (expression) {
        if (!quote || state.eat(quote)) {
            const end = state.index;
            return {
                type: "ExpressionTag",
                expression,
                start,
                end,
                metadata: null,
                parent: null,
            };
        }
    }
    state.index = startBk;
    if (quote) {
        if (state.eof()) {
            return {
                type: "Text",
                data: quote,
                raw: quote,
                start,
                end: state.index,
                parent: null,
            };
        }
        let c;
        while ((c = state.getCurr())) {
            state.advance();
            if (c === quote) {
                const end = state.index;
                const data = state.code.slice(start + 1, end - 1);
                return {
                    type: "Text",
                    data,
                    raw: data,
                    start: start + 1,
                    end: end - 1,
                    parent: null,
                };
            }
        }
    }
    else {
        while (state.advance()) {
            if (state.currIsSpace() || state.currIs(">") || state.currIs("/>")) {
                break;
            }
        }
    }
    const end = state.index;
    const data = state.code.slice(start, end);
    return {
        type: "Text",
        data,
        raw: data,
        start,
        end,
        parent: null,
    };
}
/** Parse mustache */
function parseAttributeMustache(state) {
    if (!state.eat("{")) {
        return null;
    }
    // parse simple expression
    const leadingComments = [];
    const startBk = state.index;
    state.skipSpaces();
    let start = state.index;
    while (!state.eof()) {
        if (state.eat("//")) {
            leadingComments.push(parseInlineComment(state.index - 2));
            state.skipSpaces();
            start = state.index;
            continue;
        }
        if (state.eat("/*")) {
            leadingComments.push(parseBlockComment(state.index - 2));
            state.skipSpaces();
            start = state.index;
            continue;
        }
        const stringQuote = state.eat('"') || state.eat("'");
        if (stringQuote) {
            skipString(stringQuote);
            state.skipSpaces();
            continue;
        }
        const endCandidate = state.index;
        state.skipSpaces();
        if (state.eat("}")) {
            const end = endCandidate;
            try {
                const espree = (0, espree_1.getEspree)();
                const expression = espree.parse(state.code.slice(start, end), {
                    ecmaVersion: espree.latestEcmaVersion,
                }).body[0].expression;
                delete expression.range;
                return Object.assign(Object.assign({}, expression), { leadingComments,
                    start,
                    end });
            }
            catch (_a) {
                break;
            }
        }
        state.advance();
    }
    state.index = startBk;
    return null;
    function parseInlineComment(tokenStart) {
        const valueStart = state.index;
        let valueEnd = null;
        while (!state.eof()) {
            if (state.eat("\n")) {
                valueEnd = state.index - 1;
                break;
            }
            state.advance();
        }
        if (valueEnd == null) {
            valueEnd = state.index;
        }
        return {
            type: "Line",
            value: state.code.slice(valueStart, valueEnd),
            start: tokenStart,
            end: state.index,
        };
    }
    function parseBlockComment(tokenStart) {
        const valueStart = state.index;
        let valueEnd = null;
        while (!state.eof()) {
            if (state.eat("*/")) {
                valueEnd = state.index - 2;
                break;
            }
            state.advance();
        }
        if (valueEnd == null) {
            valueEnd = state.index;
        }
        return {
            type: "Block",
            value: state.code.slice(valueStart, valueEnd),
            start: tokenStart,
            end: state.index,
        };
    }
    function skipString(stringQuote) {
        while (!state.eof()) {
            if (state.eat(stringQuote)) {
                break;
            }
            if (state.eat("\\")) {
                // escape
                state.advance();
            }
            state.advance();
        }
    }
}