forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
772 lines (690 loc) · 20.9 KB
/
parser.ts
File metadata and controls
772 lines (690 loc) · 20.9 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
import { Color } from '../color';
import { getKnownColor } from '../color/known-colors';
export type Parsed<V> = { start: number; end: number; value: V };
// Values
export type URL = string;
export type Angle = number;
export interface Unit<T> {
value: number;
unit: string;
}
export type Length = Unit<'px' | 'dip'>;
export type Percentage = Unit<'%'>;
export type LengthPercentage = Length | Percentage;
export type Keyword = string;
export interface ColorStop {
color: Color;
offset?: LengthPercentage;
}
export interface LinearGradient {
angle: number;
colors: ColorStop[];
}
export interface Background {
readonly color?: number | Color;
readonly image?: URL | LinearGradient;
readonly repeat?: BackgroundRepeat;
readonly position?: BackgroundPosition;
readonly size?: BackgroundSize;
}
export type BackgroundRepeat = 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';
export type BackgroundSize =
| 'auto'
| 'cover'
| 'contain'
| {
x: LengthPercentage;
y: 'auto' | LengthPercentage;
};
export type HorizontalAlign = 'left' | 'center' | 'right';
export type VerticalAlign = 'top' | 'center' | 'bottom';
export interface HorizontalAlignWithOffset {
readonly align: 'left' | 'right';
readonly offset: LengthPercentage;
}
export interface VerticalAlignWithOffset {
readonly align: 'top' | 'bottom';
readonly offset: LengthPercentage;
}
export interface BackgroundPosition {
readonly x: HorizontalAlign | HorizontalAlignWithOffset;
readonly y: VerticalAlign | VerticalAlignWithOffset;
text?: string;
}
const urlRegEx = /\s*url\((?:(['"])([^\1]*)\1|([^)]*))\)\s*/gy;
export function parseURL(text: string, start = 0): Parsed<URL> {
urlRegEx.lastIndex = start;
const result = urlRegEx.exec(text);
if (!result) {
return null;
}
const end = urlRegEx.lastIndex;
const value: URL = result[2] || result[3];
return { start, end, value };
}
const hexColorRegEx = /\s*#((?:[0-9A-F]{8})|(?:[0-9A-F]{6})|(?:[0-9A-F]{4})|(?:[0-9A-F]{3}))\s*/giy;
export function parseHexColor(text: string, start = 0): Parsed<Color> {
hexColorRegEx.lastIndex = start;
const result = hexColorRegEx.exec(text);
if (!result) {
return null;
}
const end = hexColorRegEx.lastIndex;
return { start, end, value: new Color('#' + result[1]) };
}
const cssColorRegEx = /\s*((?:rgb|rgba|hsl|hsla|hsv|hsva)\([^\(\)]*\))/gy;
export function parseCssColor(text: string, start = 0): Parsed<Color> {
cssColorRegEx.lastIndex = start;
const result = cssColorRegEx.exec(text);
if (!result) {
return null;
}
const end = cssColorRegEx.lastIndex;
try {
return { start, end, value: new Color(result[1]) };
} catch {
return null;
}
}
export function convertHSLToRGBColor(hue: number, saturation: number, lightness: number): { r: number; g: number; b: number } {
// Per formula it will be easier if hue is divided to 60° and saturation to 100 beforehand
// https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB
hue /= 60;
lightness /= 100;
const chroma = ((1 - Math.abs(2 * lightness - 1)) * saturation) / 100,
X = chroma * (1 - Math.abs((hue % 2) - 1));
// Add lightness match to all RGB components beforehand
let { m: r, m: g, m: b } = { m: lightness - chroma / 2 };
if (0 <= hue && hue < 1) {
r += chroma;
g += X;
} else if (hue < 2) {
r += X;
g += chroma;
} else if (hue < 3) {
g += chroma;
b += X;
} else if (hue < 4) {
g += X;
b += chroma;
} else if (hue < 5) {
r += X;
b += chroma;
} else if (hue < 6) {
r += chroma;
b += X;
}
return {
r: Math.round(r * 0xff),
g: Math.round(g * 0xff),
b: Math.round(b * 0xff),
};
}
export function parseColorKeyword(value, start: number, keyword = parseKeyword(value, start)): Parsed<Color> {
const parseColor = keyword && getKnownColor(keyword.value);
if (parseColor != null) {
const end = keyword.end;
return { start, end, value: new Color(parseColor) };
}
return null;
}
export function parseColor(value: string, start = 0, keyword = parseKeyword(value, start)): Parsed<Color> {
return parseHexColor(value, start) || parseColorKeyword(value, start, keyword) || parseCssColor(value, start);
}
const keywordRegEx = /\s*([a-z][\w\-]*)\s*/giy;
function parseKeyword(text: string, start = 0): Parsed<Keyword> {
keywordRegEx.lastIndex = start;
const result = keywordRegEx.exec(text);
if (!result) {
return null;
}
const end = keywordRegEx.lastIndex;
const value = result[1];
return { start, end, value };
}
const backgroundRepeatKeywords = new Set(['repeat', 'repeat-x', 'repeat-y', 'no-repeat']);
export function parseRepeat(value: string, start = 0, keyword = parseKeyword(value, start)): Parsed<BackgroundRepeat> {
if (keyword && backgroundRepeatKeywords.has(keyword.value)) {
const end = keyword.end;
const value = <BackgroundRepeat>keyword.value;
return { start, end, value };
}
return null;
}
const unitRegEx = /\s*([+\-]?(?:\d+\.\d+|\d+|\.\d+)(?:[eE][+\-]?\d+)?)([a-zA-Z]+|%)?\s*/gy;
export function parseUnit(text: string, start = 0): Parsed<Unit<string>> {
unitRegEx.lastIndex = start;
const result = unitRegEx.exec(text);
if (!result) {
return null;
}
const end = unitRegEx.lastIndex;
const value = parseFloat(result[1]);
const unit = <any>result[2] || 'dip';
return { start, end, value: { value, unit } };
}
export function parsePercentageOrLength(text: string, start = 0): Parsed<LengthPercentage> {
const unitResult = parseUnit(text, start);
if (unitResult) {
const { start, end } = unitResult;
const value = <LengthPercentage>unitResult.value;
if (value.unit === '%') {
value.value /= 100;
} else if (!value.unit) {
value.unit = 'dip';
} else if (value.unit === 'px' || value.unit === 'dip') {
// same
} else {
return null;
}
return { start, end, value };
}
return null;
}
const angleUnitsToRadMap: {
[unit: string]: (start: number, end: number, value: number) => Parsed<Angle>;
} = {
deg: (start: number, end: number, deg: number) => ({
start,
end,
value: (deg / 180) * Math.PI,
}),
rad: (start: number, end: number, rad: number) => ({
start,
end,
value: rad,
}),
grad: (start: number, end: number, grad: number) => ({
start,
end,
value: (grad / 200) * Math.PI,
}),
turn: (start: number, end: number, turn: number) => ({
start,
end,
value: turn * Math.PI * 2,
}),
};
export function parseAngle(value: string, start = 0): Parsed<Angle> {
const angleResult = parseUnit(value, start);
if (angleResult) {
const { start, end, value } = angleResult;
return (angleUnitsToRadMap[value.unit] || ((_, __, ___) => null))(start, end, value.value);
}
return null;
}
const backgroundSizeKeywords = new Set(['auto', 'contain', 'cover']);
export function parseBackgroundSize(value: string, start = 0, keyword = parseKeyword(value, start)): Parsed<BackgroundSize> {
let end = start;
if (keyword && backgroundSizeKeywords.has(keyword.value)) {
end = keyword.end;
const value = <'auto' | 'cover' | 'contain'>keyword.value;
return { start, end, value };
}
// Parse one or two lengths... the other will be "auto"
const firstLength = parsePercentageOrLength(value, end);
if (firstLength) {
end = firstLength.end;
const secondLength = parsePercentageOrLength(value, firstLength.end);
if (secondLength) {
end = secondLength.end;
return {
start,
end,
value: { x: firstLength.value, y: secondLength.value },
};
} else {
return { start, end, value: { x: firstLength.value, y: 'auto' } };
}
}
return null;
}
const backgroundPositionKeywords = Object.freeze(new Set(['left', 'right', 'top', 'bottom', 'center']));
const backgroundPositionKeywordsDirection: {
[align: string]: 'x' | 'center' | 'y';
} = {
left: 'x',
right: 'x',
center: 'center',
top: 'y',
bottom: 'y',
};
export function parseBackgroundPosition(text: string, start = 0, keyword = parseKeyword(text, start)): Parsed<BackgroundPosition> {
function formatH(align: Parsed<HorizontalAlign>, offset: Parsed<LengthPercentage>) {
if (align.value === 'center') {
return 'center';
}
if (offset && offset.value.value !== 0) {
return { align: align.value, offset: offset.value };
}
return align.value;
}
function formatV(align: Parsed<VerticalAlign>, offset: Parsed<LengthPercentage>) {
if (align.value === 'center') {
return 'center';
}
if (offset && offset.value.value !== 0) {
return { align: align.value, offset: offset.value };
}
return align.value;
}
let end = start;
if (keyword && backgroundPositionKeywords.has(keyword.value)) {
end = keyword.end;
const firstDirection = backgroundPositionKeywordsDirection[keyword.value];
const firstLength = firstDirection !== 'center' && parsePercentageOrLength(text, end);
if (firstLength) {
end = firstLength.end;
}
const secondKeyword = parseKeyword(text, end);
if (secondKeyword && backgroundPositionKeywords.has(secondKeyword.value)) {
end = secondKeyword.end;
const secondDirection = backgroundPositionKeywordsDirection[secondKeyword.end];
if (firstDirection === secondDirection && firstDirection !== 'center') {
return null; // Reject pair of both horizontal or both vertical alignments.
}
const secondLength = secondDirection !== 'center' && parsePercentageOrLength(text, end);
if (secondLength) {
end = secondLength.end;
}
if ((firstDirection === secondDirection && secondDirection === 'center') || firstDirection === 'x' || secondDirection === 'y') {
return {
start,
end,
value: {
x: formatH(<Parsed<HorizontalAlign>>keyword, firstLength),
y: formatV(<Parsed<VerticalAlign>>secondKeyword, secondLength),
},
};
} else {
return {
start,
end,
value: {
x: formatH(<Parsed<HorizontalAlign>>secondKeyword, secondLength),
y: formatV(<Parsed<VerticalAlign>>keyword, firstLength),
},
};
}
} else {
if (firstDirection === 'center') {
return { start, end, value: { x: 'center', y: 'center' } };
} else if (firstDirection === 'x') {
return {
start,
end,
value: {
x: formatH(<Parsed<HorizontalAlign>>keyword, firstLength),
y: 'center',
},
};
} else {
return {
start,
end,
value: {
x: 'center',
y: formatV(<Parsed<VerticalAlign>>keyword, firstLength),
},
};
}
}
} else {
const firstLength = parsePercentageOrLength(text, end);
if (firstLength) {
end = firstLength.end;
const secondLength = parsePercentageOrLength(text, end);
if (secondLength) {
end = secondLength.end;
return {
start,
end,
value: {
x: { align: 'left', offset: firstLength.value },
y: { align: 'top', offset: secondLength.value },
},
};
} else {
return {
start,
end,
value: {
x: { align: 'left', offset: firstLength.value },
y: 'center',
},
};
}
} else {
return null;
}
}
}
const directionRegEx = /\s*to\s*(left|right|top|bottom)\s*(left|right|top|bottom)?\s*/gy;
const sideDirections = {
top: (Math.PI * 0) / 2,
right: (Math.PI * 1) / 2,
bottom: (Math.PI * 2) / 2,
left: (Math.PI * 3) / 2,
};
const cornerDirections = {
top: {
right: (Math.PI * 1) / 4,
left: (Math.PI * 7) / 4,
},
right: {
top: (Math.PI * 1) / 4,
bottom: (Math.PI * 3) / 4,
},
bottom: {
right: (Math.PI * 3) / 4,
left: (Math.PI * 5) / 4,
},
left: {
top: (Math.PI * 7) / 4,
bottom: (Math.PI * 5) / 4,
},
};
function parseDirection(text: string, start = 0): Parsed<Angle> {
directionRegEx.lastIndex = start;
const result = directionRegEx.exec(text);
if (!result) {
return null;
}
const end = directionRegEx.lastIndex;
const firstDirection = result[1];
if (result[2]) {
const secondDirection = result[2];
const value = cornerDirections[firstDirection][secondDirection];
return value === undefined ? null : { start, end, value };
} else {
return { start, end, value: sideDirections[firstDirection] };
}
}
const openingBracketRegEx = /\s*\(\s*/gy;
const closingBracketRegEx = /\s*\)\s*/gy;
const closingBracketOrCommaRegEx = /\s*([),])\s*/gy;
function parseArgumentsList<T>(text: string, start: number, argument: (value: string, lastIndex: number, index: number) => Parsed<T>): Parsed<Parsed<T>[]> {
openingBracketRegEx.lastIndex = start;
const openingBracket = openingBracketRegEx.exec(text);
if (!openingBracket) {
return null;
}
let end = openingBracketRegEx.lastIndex;
const value: Parsed<T>[] = [];
closingBracketRegEx.lastIndex = end;
const closingBracket = closingBracketRegEx.exec(text);
if (closingBracket) {
return { start, end, value };
}
// eslint-disable-next-line no-constant-condition
for (let index = 0; true; index++) {
const arg = argument(text, end, index);
if (!arg) {
return null;
}
end = arg.end;
value.push(arg);
closingBracketOrCommaRegEx.lastIndex = end;
const closingBracketOrComma = closingBracketOrCommaRegEx.exec(text);
if (closingBracketOrComma) {
end = closingBracketOrCommaRegEx.lastIndex;
if (closingBracketOrComma[1] === ',') {
// noinspection UnnecessaryContinueJS
continue;
} else if (closingBracketOrComma[1] === ')') {
return { start, end, value };
}
} else {
return null;
}
}
}
export function parseColorStop(text: string, start = 0): Parsed<ColorStop> {
const color = parseColor(text, start);
if (!color) {
return null;
}
let end = color.end;
const offset = parsePercentageOrLength(text, end);
if (offset) {
end = offset.end;
return {
start,
end,
value: { color: color.value, offset: offset.value },
};
}
return { start, end, value: { color: color.value } };
}
const linearGradientStartRegEx = /\s*linear-gradient\s*/gy;
export function parseLinearGradient(text: string, start = 0): Parsed<LinearGradient> {
linearGradientStartRegEx.lastIndex = start;
const lgs = linearGradientStartRegEx.exec(text);
if (!lgs) {
return null;
}
let end = linearGradientStartRegEx.lastIndex;
let angle = Math.PI;
const colors: ColorStop[] = [];
const parsedArgs = parseArgumentsList<Angle | ColorStop>(text, end, (text, start, index) => {
if (index === 0) {
// First arg can be gradient direction
const angleArg = parseAngle(text, start) || parseDirection(text, start);
if (angleArg) {
angle = angleArg.value;
return angleArg;
}
}
const colorStop = parseColorStop(text, start);
if (colorStop) {
colors.push(colorStop.value);
return colorStop;
}
return null;
});
if (!parsedArgs) {
return null;
}
end = parsedArgs.end;
return { start, end, value: { angle, colors } };
}
const slashRegEx = /\s*(\/)\s*/gy;
function parseSlash(text: string, start: number): Parsed<'/'> {
slashRegEx.lastIndex = start;
const slash = slashRegEx.exec(text);
if (!slash) {
return null;
}
const end = slashRegEx.lastIndex;
return { start, end, value: '/' };
}
export function parseBackground(text: string, start = 0): Parsed<Background> {
const value: any = {};
let end = start;
while (end < text.length) {
const keyword = parseKeyword(text, end);
const color = parseColor(text, end, keyword);
if (color) {
value.color = color.value;
end = color.end;
continue;
}
const repeat = parseRepeat(text, end, keyword);
if (repeat) {
value.repeat = repeat.value;
end = repeat.end;
continue;
}
const position = parseBackgroundPosition(text, end, keyword);
if (position) {
position.value.text = text.substring(position.start, position.end);
value.position = position.value;
end = position.end;
const slash = parseSlash(text, end);
if (slash) {
end = slash.end;
const size = parseBackgroundSize(text, end);
if (!size) {
// Found / but no proper size following
return null;
}
value.size = size.value;
end = size.end;
}
continue;
}
const url = parseURL(text, end);
if (url) {
value.image = url.value;
end = url.end;
continue;
}
const gradient = parseLinearGradient(text, end);
if (gradient) {
value.image = gradient.value;
end = gradient.end;
continue;
}
return null;
}
return { start, end, value };
}
// Selectors
export type Combinator = '+' | '~' | '>' | ' ';
export interface UniversalSelector {
type: '*';
}
export interface TypeSelector {
type: '';
identifier: string;
}
export interface ClassSelector {
type: '.';
identifier: string;
}
export interface IdSelector {
type: '#';
identifier: string;
}
export interface PseudoClassSelector {
type: ':';
identifier: string;
}
export type AttributeSelectorTest = '=' | '^=' | '$=' | '*=' | '~=' | '|=';
export interface AttributeSelector {
type: '[]';
property: string;
test?: AttributeSelectorTest;
value?: string;
}
export type SimpleSelector = UniversalSelector | TypeSelector | ClassSelector | IdSelector | PseudoClassSelector | AttributeSelector;
export type SimpleSelectorSequence = SimpleSelector[];
export type SelectorCombinatorPair = [SimpleSelectorSequence, Combinator];
export type Selector = SelectorCombinatorPair[];
const universalSelectorRegEx = /\*/gy;
export function parseUniversalSelector(text: string, start = 0): Parsed<UniversalSelector> {
universalSelectorRegEx.lastIndex = start;
const result = universalSelectorRegEx.exec(text);
if (!result) {
return null;
}
const end = universalSelectorRegEx.lastIndex;
return { start, end, value: { type: '*' } };
}
const simpleIdentifierSelectorRegEx = /(#|\.|:|\b)((?:[\w_-]|\\.)(?:[\w\d_-]|\\.)*)/guy;
const unicodeEscapeRegEx = /\\([0-9a-fA-F]{1,5}\s|[0-9a-fA-F]{6})/g;
export function parseSimpleIdentifierSelector(text: string, start = 0): Parsed<TypeSelector | ClassSelector | IdSelector | PseudoClassSelector> {
simpleIdentifierSelectorRegEx.lastIndex = start;
const result = simpleIdentifierSelectorRegEx.exec(text.replace(unicodeEscapeRegEx, (_, c) => '\\' + String.fromCodePoint(parseInt(c.trim(), 16))));
if (!result) {
return null;
}
const end = simpleIdentifierSelectorRegEx.lastIndex;
const type = <'#' | '.' | ':' | ''>result[1];
const identifier: string = result[2].replace(/\\/g, '');
const value = <TypeSelector | ClassSelector | IdSelector | PseudoClassSelector>{ type, identifier };
return { start, end, value };
}
const attributeSelectorRegEx = /\[\s*([_\-\w][_\-\w\d]*)\s*(?:(=|\^=|\$=|\*=|\~=|\|=)\s*(?:([_\-\w][_\-\w\d]*)|"((?:[^\\"]|\\(?:"|n|r|f|\\|0-9a-f))*)"|'((?:[^\\']|\\(?:'|n|r|f|\\|0-9a-f))*)')\s*)?\]/gy;
export function parseAttributeSelector(text: string, start: number): Parsed<AttributeSelector> {
attributeSelectorRegEx.lastIndex = start;
const result = attributeSelectorRegEx.exec(text);
if (!result) {
return null;
}
const end = attributeSelectorRegEx.lastIndex;
const property = result[1];
if (result[2]) {
const test = <AttributeSelectorTest>result[2];
const value = result[3] || result[4] || result[5];
return { start, end, value: { type: '[]', property, test, value } };
}
return { start, end, value: { type: '[]', property } };
}
export function parseSimpleSelector(text: string, start = 0): Parsed<SimpleSelector> {
return parseUniversalSelector(text, start) || parseSimpleIdentifierSelector(text, start) || parseAttributeSelector(text, start);
}
export function parseSimpleSelectorSequence(text: string, start: number): Parsed<SimpleSelector[]> {
let simpleSelector = parseSimpleSelector(text, start);
if (!simpleSelector) {
return null;
}
let end = simpleSelector.end;
const value = <SimpleSelectorSequence>[];
while (simpleSelector) {
value.push(simpleSelector.value);
end = simpleSelector.end;
simpleSelector = parseSimpleSelector(text, end);
}
return { start, end, value };
}
const combinatorRegEx = /\s*([+~>])?\s*/gy;
export function parseCombinator(text: string, start = 0): Parsed<Combinator> {
combinatorRegEx.lastIndex = start;
const result = combinatorRegEx.exec(text);
if (!result) {
return null;
}
const end = combinatorRegEx.lastIndex;
const value = <Combinator>result[1] || ' ';
return { start, end, value };
}
const whiteSpaceRegEx = /\s*/gy;
export function parseSelector(text: string, start = 0): Parsed<Selector> {
let end = start;
whiteSpaceRegEx.lastIndex = end;
const leadingWhiteSpace = whiteSpaceRegEx.exec(text);
if (leadingWhiteSpace) {
end = whiteSpaceRegEx.lastIndex;
}
const value = <Selector>[];
let combinator: Parsed<Combinator>;
let expectSimpleSelector = true; // Must have at least one
let pair: SelectorCombinatorPair;
do {
const simpleSelectorSequence = parseSimpleSelectorSequence(text, end);
if (!simpleSelectorSequence) {
if (expectSimpleSelector) {
return null;
} else {
break;
}
}
end = simpleSelectorSequence.end;
if (combinator) {
// This logic looks weird; this `if` statement would occur on the next LOOP, so it effects the prior `pair`
// variable which is already pushed into the `value` array is going to have its `undefined` set to this
// value before the following statement creates a new `pair` memory variable.
// noinspection JSUnusedAssignment
pair[1] = combinator.value;
}
pair = [simpleSelectorSequence.value, undefined];
value.push(pair);
combinator = parseCombinator(text, end);
if (combinator) {
end = combinator.end;
}
expectSimpleSelector = combinator && combinator.value !== ' '; // Simple selector must follow non trailing white space combinator
} while (combinator);
return { start, end, value };
}