Skip to content

Commit 81a287a

Browse files
crisbetopkozlowski-opensource
authored andcommitted
fix(compiler): avoid error in template parser for tag names that can 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
1 parent 2fa5e70 commit 81a287a

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

packages/compiler/src/ml_parser/html_tags.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ let TAG_DEFINITIONS!: {[key: string]: HtmlTagDefinition};
7575
export function getHtmlTagDefinition(tagName: string): HtmlTagDefinition {
7676
if (!TAG_DEFINITIONS) {
7777
DEFAULT_TAG_DEFINITION = new HtmlTagDefinition({canSelfClose: true});
78-
TAG_DEFINITIONS = {
78+
TAG_DEFINITIONS = Object.assign(Object.create(null), {
7979
'base': new HtmlTagDefinition({isVoid: true}),
8080
'meta': new HtmlTagDefinition({isVoid: true}),
8181
'area': new HtmlTagDefinition({isVoid: true}),
@@ -142,10 +142,10 @@ export function getHtmlTagDefinition(tagName: string): HtmlTagDefinition {
142142
}),
143143
'textarea': new HtmlTagDefinition(
144144
{contentType: TagContentType.ESCAPABLE_RAW_TEXT, ignoreFirstLf: true}),
145-
};
145+
});
146146

147147
new DomElementSchemaRegistry().allKnownElementNames().forEach(knownTagName => {
148-
if (!TAG_DEFINITIONS.hasOwnProperty(knownTagName) && getNsPrefix(knownTagName) === null) {
148+
if (!TAG_DEFINITIONS[knownTagName] && getNsPrefix(knownTagName) === null) {
149149
TAG_DEFINITIONS[knownTagName] = new HtmlTagDefinition({canSelfClose: false});
150150
}
151151
});

packages/compiler/test/ml_parser/html_parser_spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ import {humanizeDom, humanizeDomSourceSpans, humanizeLineColumn, humanizeNodes}
240240
]);
241241
expect(parsed.errors).toEqual([]);
242242
});
243+
244+
it('should parse element with JavaScript keyword tag name', () => {
245+
expect(humanizeDom(parser.parse('<constructor></constructor>', 'TestComp'))).toEqual([
246+
[html.Element, 'constructor', 0]
247+
]);
248+
});
243249
});
244250

245251
describe('attributes', () => {

0 commit comments

Comments
 (0)