-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
utils.test.ts
431 lines (397 loc) · 14.3 KB
/
utils.test.ts
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import * as assert from 'assert';
import {
getLineAtPosition,
extractStyleTag,
extractScriptTags,
updateRelativeImport,
getWordAt
} from '../../../src/lib/documents/utils';
import { Position } from 'vscode-languageserver';
describe('document/utils', () => {
describe('extractTag', () => {
it('supports boolean attributes', () => {
const extracted = extractStyleTag('<style test></style>');
assert.deepStrictEqual(extracted?.attributes, { test: 'test' });
});
it('supports unquoted attributes', () => {
const extracted = extractStyleTag('<style type=text/css></style>');
assert.deepStrictEqual(extracted?.attributes, {
type: 'text/css'
});
});
it('does not extract style tag inside comment', () => {
const text = `
<p>bla</p>
<!--<style>h1{ color: blue; }</style>-->
<style>p{ color: blue; }</style>
`;
assert.deepStrictEqual(extractStyleTag(text), {
content: 'p{ color: blue; }',
attributes: {},
start: 108,
end: 125,
startPos: Position.create(3, 23),
endPos: Position.create(3, 40),
container: { start: 101, end: 133 }
});
});
it('does not extract tags starting with style/script', () => {
// https://github.com/sveltejs/language-tools/issues/43
// this would previously match <styles>....</style> due to misconfigured attribute matching regex
const text = `
<styles>p{ color: blue; }</styles>
<p>bla</p>
></style>
`;
assert.deepStrictEqual(extractStyleTag(text), null);
});
it('is canse sensitive to style/script', () => {
const text = `
<Style></Style>
<Script></Script>
`;
assert.deepStrictEqual(extractStyleTag(text), null);
assert.deepStrictEqual(extractScriptTags(text), null);
});
it('only extract attribute until tag ends', () => {
const text = `
<script type="typescript">
() => abc
</script>
`;
const extracted = extractScriptTags(text);
const attributes = extracted?.script?.attributes;
assert.deepStrictEqual(attributes, { type: 'typescript' });
});
it('can extract with self-closing component before it', () => {
const extracted = extractStyleTag('<SelfClosing /><style></style>');
assert.deepStrictEqual(extracted, {
start: 22,
end: 22,
startPos: {
character: 22,
line: 0
},
endPos: {
character: 22,
line: 0
},
attributes: {},
content: '',
container: {
end: 30,
start: 15
}
});
});
it('can extract with unclosed component after it', () => {
const extracted = extractStyleTag('<style></style><C {#if asd}<p>asd</p>{/if}');
assert.deepStrictEqual(extracted, {
start: 7,
end: 7,
startPos: {
character: 7,
line: 0
},
endPos: {
character: 7,
line: 0
},
attributes: {},
content: '',
container: {
start: 0,
end: 15
}
});
});
it('extracts style tag', () => {
const text = `
<p>bla</p>
<style>p{ color: blue; }</style>
`;
assert.deepStrictEqual(extractStyleTag(text), {
content: 'p{ color: blue; }',
attributes: {},
start: 51,
end: 68,
startPos: Position.create(2, 23),
endPos: Position.create(2, 40),
container: { start: 44, end: 76 }
});
});
it('extracts style tag with attributes', () => {
const text = `
<style lang="scss">p{ color: blue; }</style>
`;
assert.deepStrictEqual(extractStyleTag(text), {
content: 'p{ color: blue; }',
attributes: { lang: 'scss' },
start: 36,
end: 53,
startPos: Position.create(1, 35),
endPos: Position.create(1, 52),
container: { start: 17, end: 61 }
});
});
it('extracts style tag with attributes and extra whitespace', () => {
const text = `
<style lang="scss" > p{ color: blue; } </style>
`;
assert.deepStrictEqual(extractStyleTag(text), {
content: ' p{ color: blue; } ',
attributes: { lang: 'scss' },
start: 44,
end: 65,
startPos: Position.create(1, 43),
endPos: Position.create(1, 64),
container: { start: 17, end: 73 }
});
});
it('extracts script tag with attribute with > in it', () => {
const text = `
<script lang="ts" generics="T extends Record<string, any>">content</script>
<p>bla</p>
`;
assert.deepStrictEqual(extractScriptTags(text)?.script, {
content: 'content',
attributes: {
generics: 'T extends Record<string, any>',
lang: 'ts'
},
start: 76,
end: 83,
startPos: Position.create(1, 75),
endPos: Position.create(1, 82),
container: { start: 17, end: 92 }
});
});
it('extracts top level script tag only', () => {
const text = `
{#if name}
<script>
console.log('if not top level')
</script>
{/if}
<ul>
{#each cats as cat}
<script>
console.log('each not top level')
</script>
{/each}
</ul>
{#await promise}
<script>
console.log('await not top level')
</script>
{:then number}
<script>
console.log('then not top level')
</script>
{:catch error}
<script>
console.log('catch not top level')
</script>
{/await}
<p>{@html <script> console.log('html not top level')</script>}</p>
{@html mycontent}
{@debug myvar}
<!-- p{ color: blue; }</script> -->
<!--<script lang="scss">
p{ color: blue; }
</script> -->
<scrit>blah</scrit>
<script>top level script</script>
`;
assert.deepStrictEqual(extractScriptTags(text)?.script, {
content: 'top level script',
attributes: {},
start: 1243,
end: 1259,
startPos: Position.create(34, 24),
endPos: Position.create(34, 40),
container: { start: 1235, end: 1268 }
});
});
it("extracts top level script when there're whitespace before block name", () => {
const text = `
<script>top level script</script>
{ #if myvar } {/if}
`;
assert.deepStrictEqual(extractScriptTags(text)?.script, {
content: 'top level script',
attributes: {},
start: 25,
end: 41,
startPos: Position.create(1, 24),
endPos: Position.create(1, 40),
container: { start: 17, end: 50 }
});
});
it('ignores script tag in svelte:head', () => {
// https://github.com/sveltejs/language-tools/issues/143#issuecomment-636422045
const text = `
<svelte:head>
<link rel="stylesheet" href="/lib/jodit.es2018.min.css" />
<script src="/lib/jodit.es2018.min.js">
</script>
</svelte:head>
<p>jo</p>
<script>top level script</script>
<h1>Hello, world!</h1>
<style>.bla {}</style>
`;
assert.deepStrictEqual(extractScriptTags(text)?.script, {
content: 'top level script',
attributes: {},
start: 254,
end: 270,
startPos: Position.create(7, 20),
endPos: Position.create(7, 36),
container: { start: 246, end: 279 }
});
});
it('extracts script and module script', () => {
const text = `
<script context="module">a</script>
<script>b</script>
`;
assert.deepStrictEqual(extractScriptTags(text), {
moduleScript: {
attributes: {
context: 'module'
},
container: {
end: 48,
start: 13
},
content: 'a',
start: 38,
end: 39,
startPos: {
character: 37,
line: 1
},
endPos: {
character: 38,
line: 1
}
},
script: {
attributes: {},
container: {
end: 79,
start: 61
},
content: 'b',
start: 69,
end: 70,
startPos: {
character: 20,
line: 2
},
endPos: {
character: 21,
line: 2
}
}
});
});
it('extract tag correctly with #if and < operator', () => {
const text = `
{#if value < 3}
<div>
bla
</div>
{:else if value < 4}
{/if}
<script>let value = 2</script>
<div>
{#if value < 3}
<div>
bla
</div>
{:else if value < 4}
{/if}
</div>`;
assert.deepStrictEqual(extractScriptTags(text)?.script, {
content: 'let value = 2',
attributes: {},
start: 159,
end: 172,
startPos: Position.create(7, 18),
endPos: Position.create(7, 31),
container: { start: 151, end: 181 }
});
});
});
describe('#getLineAtPosition', () => {
it('should return line at position (only one line)', () => {
assert.deepStrictEqual(getLineAtPosition(Position.create(0, 1), 'ABC'), 'ABC');
});
it('should return line at position (multiple lines)', () => {
assert.deepStrictEqual(
getLineAtPosition(Position.create(1, 1), 'ABC\nDEF\nGHI'),
'DEF\n'
);
});
});
describe('#updateRelativeImport', () => {
it('should update path of component with ending', () => {
const newPath = updateRelativeImport(
'C:/absolute/path/oldPath',
'C:/absolute/newPath',
'./Component.svelte'
);
assert.deepStrictEqual(newPath, '../path/oldPath/Component.svelte');
});
it('should update path of file without ending', () => {
const newPath = updateRelativeImport(
'C:/absolute/path/oldPath',
'C:/absolute/newPath',
'./someTsFile'
);
assert.deepStrictEqual(newPath, '../path/oldPath/someTsFile');
});
it('should update path of file going one up', () => {
const newPath = updateRelativeImport(
'C:/absolute/path/oldPath',
'C:/absolute/path',
'./someTsFile'
);
assert.deepStrictEqual(newPath, './oldPath/someTsFile');
});
});
describe('#getWordAt', () => {
it('returns word between whitespaces', () => {
assert.equal(getWordAt('qwd asd qwd', 5), 'asd');
});
it('returns word between whitespace and end of string', () => {
assert.equal(getWordAt('qwd asd', 5), 'asd');
});
it('returns word between start of string and whitespace', () => {
assert.equal(getWordAt('asd qwd', 2), 'asd');
});
it('returns word between start of string and end of string', () => {
assert.equal(getWordAt('asd', 2), 'asd');
});
it('returns word with custom delimiters', () => {
assert.equal(
getWordAt('asd on:asd-qwd="asd" ', 10, { left: /\S+$/, right: /[\s=]/ }),
'on:asd-qwd'
);
});
function testEvent(str: string, pos: number, expected: string) {
assert.equal(getWordAt(str, pos, { left: /\S+$/, right: /[^\w$:]/ }), expected);
}
it('returns event #1', () => {
testEvent('<div on:>', 8, 'on:');
});
it('returns event #2', () => {
testEvent('<div on: >', 8, 'on:');
});
it('returns empty string when only whitespace', () => {
assert.equal(getWordAt('a a', 2), '');
});
});
});