From 81a287a79afc16d43c0fd24d7aea54be4414940a Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 16 Oct 2023 10:30:32 +0200 Subject: [PATCH] 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 --- packages/compiler/src/ml_parser/html_tags.ts | 6 +++--- packages/compiler/test/ml_parser/html_parser_spec.ts | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/compiler/src/ml_parser/html_tags.ts b/packages/compiler/src/ml_parser/html_tags.ts index bcc4da9e1e942..2a4c6253ad760 100644 --- a/packages/compiler/src/ml_parser/html_tags.ts +++ b/packages/compiler/src/ml_parser/html_tags.ts @@ -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}), @@ -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}); } }); diff --git a/packages/compiler/test/ml_parser/html_parser_spec.ts b/packages/compiler/test/ml_parser/html_parser_spec.ts index c048aba5bcedd..8dc7e45c4f4d5 100644 --- a/packages/compiler/test/ml_parser/html_parser_spec.ts +++ b/packages/compiler/test/ml_parser/html_parser_spec.ts @@ -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('', 'TestComp'))).toEqual([ + [html.Element, 'constructor', 0] + ]); + }); }); describe('attributes', () => {