MCPcopy
hub / github.com/angular/angular / parse

Method parse

packages/compiler/src/directive_matching.ts:58–121  ·  view source on GitHub ↗
(selector: string)

Source from the content-addressed store, hash-verified

56 notSelectors: CssSelector[] = [];
57
58 static parse(selector: string): CssSelector[] {
59 const results: CssSelector[] = [];
60 const _addResult = (res: CssSelector[], cssSel: CssSelector) => {
61 if (
62 cssSel.notSelectors.length > 0 &&
63 !cssSel.element &&
64 cssSel.classNames.length == 0 &&
65 cssSel.attrs.length == 0
66 ) {
67 cssSel.element = '*';
68 }
69 res.push(cssSel);
70 };
71 let cssSelector = new CssSelector();
72 let match: string[] | null;
73 let current = cssSelector;
74 let inNot = false;
75 _SELECTOR_REGEXP.lastIndex = 0;
76 while ((match = _SELECTOR_REGEXP.exec(selector))) {
77 if (match[SelectorRegexp.NOT]) {
78 if (inNot) {
79 throw new Error('Nesting :not in a selector is not allowed');
80 }
81 inNot = true;
82 current = new CssSelector();
83 cssSelector.notSelectors.push(current);
84 }
85 const tag = match[SelectorRegexp.TAG];
86 if (tag) {
87 const prefix = match[SelectorRegexp.PREFIX];
88 if (prefix === '#') {
89 // #hash
90 current.addAttribute('id', tag.slice(1));
91 } else if (prefix === '.') {
92 // Class
93 current.addClassName(tag.slice(1));
94 } else {
95 // Element
96 current.setElement(tag);
97 }
98 }
99 const attribute = match[SelectorRegexp.ATTRIBUTE];
100
101 if (attribute) {
102 current.addAttribute(
103 current.unescapeAttribute(attribute),
104 match[SelectorRegexp.ATTRIBUTE_VALUE],
105 );
106 }
107 if (match[SelectorRegexp.NOT_END]) {
108 inNot = false;
109 current = cssSelector;
110 }
111 if (match[SelectorRegexp.SEPARATOR]) {
112 if (inNot) {
113 throw new Error('Multiple selectors in :not are not supported');
114 }
115 _addResult(results, cssSelector);

Callers

nothing calls this directly

Calls 5

addAttributeMethod · 0.80
addClassNameMethod · 0.80
setElementMethod · 0.80
unescapeAttributeMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected