Skip to content

Commit

Permalink
fix(compiler): avoid error in template parser for tag names that can …
Browse files Browse the repository at this point in the history
…occur in object prototype (#52225)

Fixes that the compiler was throwing an error if an element tag name is the same as a built-in prototype property (e.g. `constructor` or `toString`). The problem was that we were storing the tag names in an object literal with the `Object` prototype. These changes resolve the issue by creating an object without a prototype.

Fixes #52224.

PR Close #52225
  • Loading branch information
crisbeto authored and pkozlowski-opensource committed Oct 16, 2023
1 parent 2fa5e70 commit 81a287a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/compiler/src/ml_parser/html_tags.ts
Expand Up @@ -75,7 +75,7 @@ let TAG_DEFINITIONS!: {[key: string]: HtmlTagDefinition};
export function getHtmlTagDefinition(tagName: string): HtmlTagDefinition {
if (!TAG_DEFINITIONS) {
DEFAULT_TAG_DEFINITION = new HtmlTagDefinition({canSelfClose: true});
TAG_DEFINITIONS = {
TAG_DEFINITIONS = Object.assign(Object.create(null), {
'base': new HtmlTagDefinition({isVoid: true}),
'meta': new HtmlTagDefinition({isVoid: true}),
'area': new HtmlTagDefinition({isVoid: true}),
Expand Down Expand Up @@ -142,10 +142,10 @@ export function getHtmlTagDefinition(tagName: string): HtmlTagDefinition {
}),
'textarea': new HtmlTagDefinition(
{contentType: TagContentType.ESCAPABLE_RAW_TEXT, ignoreFirstLf: true}),
};
});

new DomElementSchemaRegistry().allKnownElementNames().forEach(knownTagName => {
if (!TAG_DEFINITIONS.hasOwnProperty(knownTagName) && getNsPrefix(knownTagName) === null) {
if (!TAG_DEFINITIONS[knownTagName] && getNsPrefix(knownTagName) === null) {
TAG_DEFINITIONS[knownTagName] = new HtmlTagDefinition({canSelfClose: false});
}
});
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/test/ml_parser/html_parser_spec.ts
Expand Up @@ -240,6 +240,12 @@ import {humanizeDom, humanizeDomSourceSpans, humanizeLineColumn, humanizeNodes}
]);
expect(parsed.errors).toEqual([]);
});

it('should parse element with JavaScript keyword tag name', () => {
expect(humanizeDom(parser.parse('<constructor></constructor>', 'TestComp'))).toEqual([
[html.Element, 'constructor', 0]
]);
});
});

describe('attributes', () => {
Expand Down

0 comments on commit 81a287a

Please sign in to comment.