-
Notifications
You must be signed in to change notification settings - Fork 27.5k
refactor(core): align namespaced attribute validation and security schema contexts #68686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b01dbd3
f2e501e
5fa9201
1e73927
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /** | ||
| * @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 | ||
| */ | ||
|
|
||
| export const SVG_NAMESPACE = 'svg'; | ||
| export const MATH_ML_NAMESPACE = 'math'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,12 +31,13 @@ import { | |
| VariableBinding, | ||
| } from '../expression_parser/ast'; | ||
| import {Parser} from '../expression_parser/parser'; | ||
| import {mergeNsAndName} from '../ml_parser/tags'; | ||
| import {mergeNsAndName, splitNsName} from '../ml_parser/tags'; | ||
| import {InterpolatedAttributeToken, InterpolatedTextToken} from '../ml_parser/tokens'; | ||
| import {ParseError, ParseErrorLevel, ParseSourceSpan} from '../parse_util'; | ||
| import {ElementSchemaRegistry} from '../schema/element_schema_registry'; | ||
| import {CssSelector} from '../directive_matching'; | ||
| import {splitAtColon, splitAtPeriod} from '../util'; | ||
| import {MATH_ML_NAMESPACE, SVG_NAMESPACE} from '../template/pipeline/src/namespaces'; | ||
|
|
||
| const PROPERTY_PARTS_SEPARATOR = '.'; | ||
| const ATTRIBUTE_PREFIX = 'attr'; | ||
|
|
@@ -878,20 +879,42 @@ export function calcPossibleSecurityContexts( | |
| isAttribute: boolean, | ||
| ): SecurityContext[] { | ||
| let ctxs: SecurityContext[]; | ||
| const nameToContext = (elName: string) => registry.securityContext(elName, propName, isAttribute); | ||
|
|
||
| if (selector === null) { | ||
| ctxs = registry.allKnownElementNames().map(nameToContext); | ||
| const [namespaceKey, baseSelector] = selector ? splitNsName(selector, false) : [null, selector]; | ||
| const nameToContext = (elName: string) => { | ||
| const [nsStr, name] = splitNsName(elName, false); | ||
| const ns = nsStr ?? namespaceKey; | ||
| const fullName = ns ? `:${ns}:${name}` : name; | ||
| return registry.securityContext(fullName, propName, isAttribute); | ||
| }; | ||
|
|
||
| const allKnownElements = registry.allKnownElementNames(); | ||
| if (baseSelector === null) { | ||
| ctxs = allKnownElements.map(nameToContext); | ||
| } else { | ||
| ctxs = []; | ||
| CssSelector.parse(selector).forEach((selector) => { | ||
| const elementNames = selector.element ? [selector.element] : registry.allKnownElementNames(); | ||
| CssSelector.parse(baseSelector).forEach((selector) => { | ||
| let elementNames = selector.element ? [selector.element] : allKnownElements; | ||
| if (selector.element && !registry.hasElement(selector.element, [])) { | ||
| const svgElement = `:${SVG_NAMESPACE}:${selector.element}`; | ||
| const mathElement = `:${MATH_ML_NAMESPACE}:${selector.element}`; | ||
| if (registry.hasElement(svgElement, [])) { | ||
| elementNames = [svgElement]; | ||
| } else if (registry.hasElement(mathElement, [])) { | ||
| elementNames = [mathElement]; | ||
| } | ||
|
Comment on lines
+900
to
+904
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Were these intended to be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to keep the existing behaviour, the array is initialized with |
||
| } | ||
| const notElementNames = new Set( | ||
| selector.notSelectors | ||
| .filter((selector) => selector.isElementSelector()) | ||
| .map((selector) => selector.element), | ||
| .map((selector) => selector.element?.toLowerCase()), | ||
| ); | ||
| const possibleElementNames = elementNames.filter((elName) => !notElementNames.has(elName)); | ||
| const possibleElementNames = elementNames.filter((elName) => { | ||
| const elNameLowerCase = elName.toLowerCase(); | ||
| return ( | ||
| !notElementNames.has(elNameLowerCase) && | ||
| !notElementNames.has(splitNsName(elNameLowerCase)[1]) | ||
| ); | ||
| }); | ||
|
|
||
| ctxs.push(...possibleElementNames.map(nameToContext)); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider: The constant splitting / joining of the namespace makes me worried we can easily introduce a bug here. Especially with namespace being optional and easily omitted.
Would it be worth moving away from the "stringly" types here and make something like:
And then we can use this type in the relevant functions with consistent serialize / deserialize functions to make sure we never drop the namespace and are always checking it as expected?
I have a similar concern with casing, where we might forget a
.toLowerCasecall, maybe there's a way to standardize that part too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could definitely do that, but that's quite a chunky refactor. Since the elementName as a string is used in a lot of places.