Skip to content

Commit

Permalink
fix(angular-parser): parse tags with namespace correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 2, 2021
1 parent 717e97b commit d796267
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/angular-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
151 changes: 151 additions & 0 deletions packages/angular-parser/test/__snapshots__/parser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<svg:defs>",
"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": "<svg:linearGradient>",
"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": "</svg:linearGradient>",
"startCol": 27,
"startLine": 2,
"startOffset": 37,
"tagCloseChar": ">",
"tagOpenChar": "</",
"type": "endtag",
},
Object {
"endCol": 5,
"endLine": 3,
"endOffset": 63,
"isFragment": false,
"isGhost": false,
"nodeName": "#text",
"raw": "
",
"startCol": 48,
"startLine": 2,
"startOffset": 58,
"type": "text",
},
Object {
"attributes": Array [],
"endCol": 16,
"endLine": 3,
"endOffset": 74,
"isCustomElement": false,
"isFragment": false,
"isGhost": false,
"namespace": "http://www.w3.org/1999/xhtml",
"nodeName": "svg:defs",
"raw": "</svg:defs>",
"startCol": 5,
"startLine": 3,
"startOffset": 63,
"tagCloseChar": ">",
"tagOpenChar": "</",
"type": "endtag",
},
],
}
`;
exports[`parser tag namespace 2`] = `
"Snapshot Diff:
Compared values have no visual difference."
`;
exports[`parser void element 1`] = `
Object {
"isFragment": true,
Expand Down
9 changes: 9 additions & 0 deletions packages/angular-parser/test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,13 @@ describe('parser', () => {
expect(doc).toMatchSnapshot()
expect(snapshotDiff(cleanParse(parse(html)), doc)).toMatchSnapshot()
})

test('tag namespace', () => {
const html = /* HTML */ `<svg:defs>
<svg:linearGradient></svg:linearGradient>
</svg:defs>`
const doc = cleanParse(html)
expect(doc).toMatchSnapshot()
expect(snapshotDiff(cleanParse(parse(html)), doc)).toMatchSnapshot()
})
})

0 comments on commit d796267

Please sign in to comment.