From d796267ca92c7a3b432da71ae2dbf9b68ddf09d4 Mon Sep 17 00:00:00 2001 From: JounQin Date: Fri, 2 Jul 2021 12:36:04 +0800 Subject: [PATCH] fix(angular-parser): parse tags with namespace correctly related https://github.com/ikatyang/angular-html-parser/issues/22 --- packages/angular-parser/src/index.ts | 3 + .../test/__snapshots__/parser.spec.ts.snap | 151 ++++++++++++++++++ packages/angular-parser/test/parser.spec.ts | 9 ++ 3 files changed, 163 insertions(+) diff --git a/packages/angular-parser/src/index.ts b/packages/angular-parser/src/index.ts index d695780d..839bc77e 100644 --- a/packages/angular-parser/src/index.ts +++ b/packages/angular-parser/src/index.ts @@ -129,6 +129,9 @@ const visitor = { const attributes: MLASTAttr[] = [] const childNodes: MLASTNode[] = [] + // https://github.com/ikatyang/angular-html-parser/issues/22 + nodeName = nodeName.startsWith(':') ? nodeName.slice(1) : nodeName + namespace = attrs.find(attr => attr.name === 'xmlns')?.value || namespace || diff --git a/packages/angular-parser/test/__snapshots__/parser.spec.ts.snap b/packages/angular-parser/test/__snapshots__/parser.spec.ts.snap index 7a49bb19..2b186a27 100644 --- a/packages/angular-parser/test/__snapshots__/parser.spec.ts.snap +++ b/packages/angular-parser/test/__snapshots__/parser.spec.ts.snap @@ -1874,6 +1874,157 @@ Object { } `; +exports[`parser tag namespace 1`] = ` +Object { + "isFragment": true, + "nodeList": Array [ + Object { + "attributes": Array [], + "endCol": 11, + "endLine": 1, + "endOffset": 10, + "endSpace": Object { + "endCol": 10, + "endLine": 1, + "endOffset": 9, + "raw": "", + "startCol": 10, + "startLine": 1, + "startOffset": 9, + }, + "hasSpreadAttr": false, + "isCustomElement": false, + "isFragment": false, + "isGhost": false, + "namespace": "http://www.w3.org/1999/xhtml", + "nodeName": "svg:defs", + "raw": "", + "selfClosingSolidus": Object { + "endCol": 10, + "endLine": 1, + "endOffset": 9, + "raw": "", + "startCol": 10, + "startLine": 1, + "startOffset": 9, + }, + "startCol": 1, + "startLine": 1, + "startOffset": 0, + "tagCloseChar": ">", + "tagOpenChar": "<", + "type": "starttag", + }, + Object { + "endCol": 7, + "endLine": 2, + "endOffset": 17, + "isFragment": false, + "isGhost": false, + "nodeName": "#text", + "raw": " + ", + "startCol": 11, + "startLine": 1, + "startOffset": 10, + "type": "text", + }, + Object { + "attributes": Array [], + "endCol": 27, + "endLine": 2, + "endOffset": 37, + "endSpace": Object { + "endCol": 26, + "endLine": 2, + "endOffset": 36, + "raw": "", + "startCol": 26, + "startLine": 2, + "startOffset": 36, + }, + "hasSpreadAttr": false, + "isCustomElement": false, + "isFragment": false, + "isGhost": false, + "namespace": "http://www.w3.org/1999/xhtml", + "nodeName": "svg:linearGradient", + "raw": "", + "selfClosingSolidus": Object { + "endCol": 26, + "endLine": 2, + "endOffset": 36, + "raw": "", + "startCol": 26, + "startLine": 2, + "startOffset": 36, + }, + "startCol": 7, + "startLine": 2, + "startOffset": 17, + "tagCloseChar": ">", + "tagOpenChar": "<", + "type": "starttag", + }, + Object { + "attributes": Array [], + "endCol": 48, + "endLine": 2, + "endOffset": 58, + "isCustomElement": false, + "isFragment": false, + "isGhost": false, + "namespace": "http://www.w3.org/1999/xhtml", + "nodeName": "svg:linearGradient", + "raw": "", + "startCol": 27, + "startLine": 2, + "startOffset": 37, + "tagCloseChar": ">", + "tagOpenChar": "", + "startCol": 5, + "startLine": 3, + "startOffset": 63, + "tagCloseChar": ">", + "tagOpenChar": " { expect(doc).toMatchSnapshot() expect(snapshotDiff(cleanParse(parse(html)), doc)).toMatchSnapshot() }) + + test('tag namespace', () => { + const html = /* HTML */ ` + + ` + const doc = cleanParse(html) + expect(doc).toMatchSnapshot() + expect(snapshotDiff(cleanParse(parse(html)), doc)).toMatchSnapshot() + }) })