-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathnode_selector_matcher.ts
472 lines (439 loc) Β· 17.1 KB
/
node_selector_matcher.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
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import '../util/ng_dev_mode';
import {assertDefined, assertEqual, assertNotEqual} from '../util/assert';
import {AttributeMarker} from './interfaces/attribute_marker';
import {TAttributes, TNode, TNodeType} from './interfaces/node';
import {CssSelector, CssSelectorList, SelectorFlags} from './interfaces/projection';
import {classIndexOf} from './styling/class_differ';
import {isNameOnlyAttributeMarker} from './util/attrs_utils';
const NG_TEMPLATE_SELECTOR = 'ng-template';
/**
* Search the `TAttributes` to see if it contains `cssClassToMatch` (case insensitive)
*
* @param tNode static data of the node to match
* @param attrs `TAttributes` to search through.
* @param cssClassToMatch class to match (lowercase)
* @param isProjectionMode Whether or not class matching should look into the attribute `class` in
* addition to the `AttributeMarker.Classes`.
*/
function isCssClassMatching(
tNode: TNode,
attrs: TAttributes,
cssClassToMatch: string,
isProjectionMode: boolean,
): boolean {
ngDevMode &&
assertEqual(
cssClassToMatch,
cssClassToMatch.toLowerCase(),
'Class name expected to be lowercase.',
);
let i = 0;
if (isProjectionMode) {
for (; i < attrs.length && typeof attrs[i] === 'string'; i += 2) {
// Search for an implicit `class` attribute and check if its value matches `cssClassToMatch`.
if (
attrs[i] === 'class' &&
classIndexOf((attrs[i + 1] as string).toLowerCase(), cssClassToMatch, 0) !== -1
) {
return true;
}
}
} else if (isInlineTemplate(tNode)) {
// Matching directives (i.e. when not matching for projection mode) should not consider the
// class bindings that are present on inline templates, as those class bindings only target
// the root node of the template, not the template itself.
return false;
}
// Resume the search for classes after the `Classes` marker.
i = attrs.indexOf(AttributeMarker.Classes, i);
if (i > -1) {
// We found the classes section. Start searching for the class.
let item: TAttributes[number];
while (++i < attrs.length && typeof (item = attrs[i]) === 'string') {
if (item.toLowerCase() === cssClassToMatch) {
return true;
}
}
}
return false;
}
/**
* Checks whether the `tNode` represents an inline template (e.g. `*ngFor`).
*
* @param tNode current TNode
*/
export function isInlineTemplate(tNode: TNode): boolean {
return tNode.type === TNodeType.Container && tNode.value !== NG_TEMPLATE_SELECTOR;
}
/**
* Function that checks whether a given tNode matches tag-based selector and has a valid type.
*
* Matching can be performed in 2 modes: projection mode (when we project nodes) and regular
* directive matching mode:
* - in the "directive matching" mode we do _not_ take TContainer's tagName into account if it is
* different from NG_TEMPLATE_SELECTOR (value different from NG_TEMPLATE_SELECTOR indicates that a
* tag name was extracted from * syntax so we would match the same directive twice);
* - in the "projection" mode, we use a tag name potentially extracted from the * syntax processing
* (applicable to TNodeType.Container only).
*/
function hasTagAndTypeMatch(
tNode: TNode,
currentSelector: string,
isProjectionMode: boolean,
): boolean {
const tagNameToCompare =
tNode.type === TNodeType.Container && !isProjectionMode ? NG_TEMPLATE_SELECTOR : tNode.value;
return currentSelector === tagNameToCompare;
}
/**
* A utility function to match an Ivy node static data against a simple CSS selector
*
* @param tNode static data of the node to match
* @param selector The selector to try matching against the node.
* @param isProjectionMode if `true` we are matching for content projection, otherwise we are doing
* directive matching.
* @returns true if node matches the selector.
*/
export function isNodeMatchingSelector(
tNode: TNode,
selector: CssSelector,
isProjectionMode: boolean,
): boolean {
ngDevMode && assertDefined(selector[0], 'Selector should have a tag name');
let mode: SelectorFlags = SelectorFlags.ELEMENT;
const nodeAttrs = tNode.attrs;
// Find the index of first attribute that has no value, only a name.
const nameOnlyMarkerIdx = nodeAttrs !== null ? getNameOnlyMarkerIndex(nodeAttrs) : 0;
// When processing ":not" selectors, we skip to the next ":not" if the
// current one doesn't match
let skipToNextSelector = false;
for (let i = 0; i < selector.length; i++) {
const current = selector[i];
if (typeof current === 'number') {
// If we finish processing a :not selector and it hasn't failed, return false
if (!skipToNextSelector && !isPositive(mode) && !isPositive(current)) {
return false;
}
// If we are skipping to the next :not() and this mode flag is positive,
// it's a part of the current :not() selector, and we should keep skipping
if (skipToNextSelector && isPositive(current)) continue;
skipToNextSelector = false;
mode = (current as number) | (mode & SelectorFlags.NOT);
continue;
}
if (skipToNextSelector) continue;
if (mode & SelectorFlags.ELEMENT) {
mode = SelectorFlags.ATTRIBUTE | (mode & SelectorFlags.NOT);
if (
(current !== '' && !hasTagAndTypeMatch(tNode, current, isProjectionMode)) ||
(current === '' && selector.length === 1)
) {
if (isPositive(mode)) return false;
skipToNextSelector = true;
}
} else if (mode & SelectorFlags.CLASS) {
if (nodeAttrs === null || !isCssClassMatching(tNode, nodeAttrs, current, isProjectionMode)) {
if (isPositive(mode)) return false;
skipToNextSelector = true;
}
} else {
const selectorAttrValue = selector[++i];
const attrIndexInNode = findAttrIndexInNode(
current,
nodeAttrs,
isInlineTemplate(tNode),
isProjectionMode,
);
if (attrIndexInNode === -1) {
if (isPositive(mode)) return false;
skipToNextSelector = true;
continue;
}
if (selectorAttrValue !== '') {
let nodeAttrValue: string;
if (attrIndexInNode > nameOnlyMarkerIdx) {
nodeAttrValue = '';
} else {
ngDevMode &&
assertNotEqual(
nodeAttrs![attrIndexInNode],
AttributeMarker.NamespaceURI,
'We do not match directives on namespaced attributes',
);
// we lowercase the attribute value to be able to match
// selectors without case-sensitivity
// (selectors are already in lowercase when generated)
nodeAttrValue = (nodeAttrs![attrIndexInNode + 1] as string).toLowerCase();
}
if (mode & SelectorFlags.ATTRIBUTE && selectorAttrValue !== nodeAttrValue) {
if (isPositive(mode)) return false;
skipToNextSelector = true;
}
}
}
}
return isPositive(mode) || skipToNextSelector;
}
function isPositive(mode: SelectorFlags): boolean {
return (mode & SelectorFlags.NOT) === 0;
}
/**
* Examines the attribute's definition array for a node to find the index of the
* attribute that matches the given `name`.
*
* NOTE: This will not match namespaced attributes.
*
* Attribute matching depends upon `isInlineTemplate` and `isProjectionMode`.
* The following table summarizes which types of attributes we attempt to match:
*
* ===========================================================================================================
* Modes | Normal Attributes | Bindings Attributes | Template Attributes | I18n
* Attributes
* ===========================================================================================================
* Inline + Projection | YES | YES | NO | YES
* -----------------------------------------------------------------------------------------------------------
* Inline + Directive | NO | NO | YES | NO
* -----------------------------------------------------------------------------------------------------------
* Non-inline + Projection | YES | YES | NO | YES
* -----------------------------------------------------------------------------------------------------------
* Non-inline + Directive | YES | YES | NO | YES
* ===========================================================================================================
*
* @param name the name of the attribute to find
* @param attrs the attribute array to examine
* @param isInlineTemplate true if the node being matched is an inline template (e.g. `*ngFor`)
* rather than a manually expanded template node (e.g `<ng-template>`).
* @param isProjectionMode true if we are matching against content projection otherwise we are
* matching against directives.
*/
function findAttrIndexInNode(
name: string,
attrs: TAttributes | null,
isInlineTemplate: boolean,
isProjectionMode: boolean,
): number {
if (attrs === null) return -1;
let i = 0;
if (isProjectionMode || !isInlineTemplate) {
let bindingsMode = false;
while (i < attrs.length) {
const maybeAttrName = attrs[i];
if (maybeAttrName === name) {
return i;
} else if (
maybeAttrName === AttributeMarker.Bindings ||
maybeAttrName === AttributeMarker.I18n
) {
bindingsMode = true;
} else if (
maybeAttrName === AttributeMarker.Classes ||
maybeAttrName === AttributeMarker.Styles
) {
let value = attrs[++i];
// We should skip classes here because we have a separate mechanism for
// matching classes in projection mode.
while (typeof value === 'string') {
value = attrs[++i];
}
continue;
} else if (maybeAttrName === AttributeMarker.Template) {
// We do not care about Template attributes in this scenario.
break;
} else if (maybeAttrName === AttributeMarker.NamespaceURI) {
// Skip the whole namespaced attribute and value. This is by design.
i += 4;
continue;
}
// In binding mode there are only names, rather than name-value pairs.
i += bindingsMode ? 1 : 2;
}
// We did not match the attribute
return -1;
} else {
return matchTemplateAttribute(attrs, name);
}
}
export function isNodeMatchingSelectorList(
tNode: TNode,
selector: CssSelectorList,
isProjectionMode: boolean = false,
): boolean {
for (let i = 0; i < selector.length; i++) {
if (isNodeMatchingSelector(tNode, selector[i], isProjectionMode)) {
return true;
}
}
return false;
}
export function getProjectAsAttrValue(tNode: TNode): CssSelector | null {
const nodeAttrs = tNode.attrs;
if (nodeAttrs != null) {
const ngProjectAsAttrIdx = nodeAttrs.indexOf(AttributeMarker.ProjectAs);
// only check for ngProjectAs in attribute names, don't accidentally match attribute's value
// (attribute names are stored at even indexes)
if ((ngProjectAsAttrIdx & 1) === 0) {
return nodeAttrs[ngProjectAsAttrIdx + 1] as CssSelector;
}
}
return null;
}
function getNameOnlyMarkerIndex(nodeAttrs: TAttributes) {
for (let i = 0; i < nodeAttrs.length; i++) {
const nodeAttr = nodeAttrs[i];
if (isNameOnlyAttributeMarker(nodeAttr)) {
return i;
}
}
return nodeAttrs.length;
}
function matchTemplateAttribute(attrs: TAttributes, name: string): number {
let i = attrs.indexOf(AttributeMarker.Template);
if (i > -1) {
i++;
while (i < attrs.length) {
const attr = attrs[i];
// Return in case we checked all template attrs and are switching to the next section in the
// attrs array (that starts with a number that represents an attribute marker).
if (typeof attr === 'number') return -1;
if (attr === name) return i;
i++;
}
}
return -1;
}
/**
* Checks whether a selector is inside a CssSelectorList
* @param selector Selector to be checked.
* @param list List in which to look for the selector.
*/
export function isSelectorInSelectorList(selector: CssSelector, list: CssSelectorList): boolean {
selectorListLoop: for (let i = 0; i < list.length; i++) {
const currentSelectorInList = list[i];
if (selector.length !== currentSelectorInList.length) {
continue;
}
for (let j = 0; j < selector.length; j++) {
if (selector[j] !== currentSelectorInList[j]) {
continue selectorListLoop;
}
}
return true;
}
return false;
}
function maybeWrapInNotSelector(isNegativeMode: boolean, chunk: string): string {
return isNegativeMode ? ':not(' + chunk.trim() + ')' : chunk;
}
function stringifyCSSSelector(selector: CssSelector): string {
let result = selector[0] as string;
let i = 1;
let mode = SelectorFlags.ATTRIBUTE;
let currentChunk = '';
let isNegativeMode = false;
while (i < selector.length) {
let valueOrMarker = selector[i];
if (typeof valueOrMarker === 'string') {
if (mode & SelectorFlags.ATTRIBUTE) {
const attrValue = selector[++i] as string;
currentChunk +=
'[' + valueOrMarker + (attrValue.length > 0 ? '="' + attrValue + '"' : '') + ']';
} else if (mode & SelectorFlags.CLASS) {
currentChunk += '.' + valueOrMarker;
} else if (mode & SelectorFlags.ELEMENT) {
currentChunk += ' ' + valueOrMarker;
}
} else {
//
// Append current chunk to the final result in case we come across SelectorFlag, which
// indicates that the previous section of a selector is over. We need to accumulate content
// between flags to make sure we wrap the chunk later in :not() selector if needed, e.g.
// ```
// ['', Flags.CLASS, '.classA', Flags.CLASS | Flags.NOT, '.classB', '.classC']
// ```
// should be transformed to `.classA :not(.classB .classC)`.
//
// Note: for negative selector part, we accumulate content between flags until we find the
// next negative flag. This is needed to support a case where `:not()` rule contains more than
// one chunk, e.g. the following selector:
// ```
// ['', Flags.ELEMENT | Flags.NOT, 'p', Flags.CLASS, 'foo', Flags.CLASS | Flags.NOT, 'bar']
// ```
// should be stringified to `:not(p.foo) :not(.bar)`
//
if (currentChunk !== '' && !isPositive(valueOrMarker)) {
result += maybeWrapInNotSelector(isNegativeMode, currentChunk);
currentChunk = '';
}
mode = valueOrMarker;
// According to CssSelector spec, once we come across `SelectorFlags.NOT` flag, the negative
// mode is maintained for remaining chunks of a selector.
isNegativeMode = isNegativeMode || !isPositive(mode);
}
i++;
}
if (currentChunk !== '') {
result += maybeWrapInNotSelector(isNegativeMode, currentChunk);
}
return result;
}
/**
* Generates string representation of CSS selector in parsed form.
*
* ComponentDef and DirectiveDef are generated with the selector in parsed form to avoid doing
* additional parsing at runtime (for example, for directive matching). However in some cases (for
* example, while bootstrapping a component), a string version of the selector is required to query
* for the host element on the page. This function takes the parsed form of a selector and returns
* its string representation.
*
* @param selectorList selector in parsed form
* @returns string representation of a given selector
*/
export function stringifyCSSSelectorList(selectorList: CssSelectorList): string {
return selectorList.map(stringifyCSSSelector).join(',');
}
/**
* Extracts attributes and classes information from a given CSS selector.
*
* This function is used while creating a component dynamically. In this case, the host element
* (that is created dynamically) should contain attributes and classes specified in component's CSS
* selector.
*
* @param selector CSS selector in parsed form (in a form of array)
* @returns object with `attrs` and `classes` fields that contain extracted information
*/
export function extractAttrsAndClassesFromSelector(selector: CssSelector): TAttributes {
const attrs: TAttributes = [];
const classes: string[] = [];
let i = 1;
let mode = SelectorFlags.ATTRIBUTE;
while (i < selector.length) {
let valueOrMarker = selector[i];
if (typeof valueOrMarker === 'string') {
if (mode === SelectorFlags.ATTRIBUTE) {
if (valueOrMarker !== '') {
attrs.push(valueOrMarker, selector[++i] as string);
}
} else if (mode === SelectorFlags.CLASS) {
classes.push(valueOrMarker);
}
} else {
// According to CssSelector spec, once we come across `SelectorFlags.NOT` flag, the negative
// mode is maintained for remaining chunks of a selector. Since attributes and classes are
// extracted only for "positive" part of the selector, we can stop here.
if (!isPositive(mode)) break;
mode = valueOrMarker;
}
i++;
}
if (classes.length) {
attrs.push(AttributeMarker.Classes, ...classes);
}
return attrs;
}