Skip to content

Commit

Permalink
fix(angular-parser): support parsing void element like <img> (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jul 2, 2021
1 parent ff69c8b commit 717e97b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
11 changes: 7 additions & 4 deletions packages/angular-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ const getSourceSpan = (
: nodeOrSourceSpan

const getRaw = (
nodeOrSourceSpan: ParseSourceSpan | { sourceSpan: ParseSourceSpan },
nodeOrSourceSpan: ParseSourceSpan | { sourceSpan: ParseSourceSpan } | null,
text: string,
) => {
if (!nodeOrSourceSpan) {
return ''
}
const { start, end } = getSourceSpan(nodeOrSourceSpan)
return text.slice(start.offset, end.offset)
}
Expand Down Expand Up @@ -120,8 +123,8 @@ const visitor = {
const partialStartTag = nodeMapper(startSourceSpan!, options)

const { text } = options
const startTagText = getRaw(startSourceSpan!, text)
const endTagText = getRaw(endSourceSpan!, text)
const startTagText = getRaw(startSourceSpan, text)
const endTagText = getRaw(endSourceSpan, text)

const attributes: MLASTAttr[] = []
const childNodes: MLASTNode[] = []
Expand Down Expand Up @@ -176,7 +179,7 @@ const visitor = {

if (startTagText === endTagText) {
startTag.tagCloseChar = '/>'
} else {
} else if (endTagText) {
startTag.pearNode = endTag = {
...nodeMapper(endSourceSpan!, options),
type: MLASTNodeType.EndTag,
Expand Down
50 changes: 50 additions & 0 deletions packages/angular-parser/test/__snapshots__/parser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1873,3 +1873,53 @@ Object {
],
}
`;
exports[`parser void element 1`] = `
Object {
"isFragment": true,
"nodeList": Array [
Object {
"attributes": Array [],
"endCol": 6,
"endLine": 1,
"endOffset": 5,
"endSpace": Object {
"endCol": 5,
"endLine": 1,
"endOffset": 4,
"raw": "",
"startCol": 5,
"startLine": 1,
"startOffset": 4,
},
"hasSpreadAttr": false,
"isCustomElement": false,
"isFragment": false,
"isGhost": false,
"namespace": "http://www.w3.org/1999/xhtml",
"nodeName": "img",
"raw": "<img>",
"selfClosingSolidus": Object {
"endCol": 5,
"endLine": 1,
"endOffset": 4,
"raw": "",
"startCol": 5,
"startLine": 1,
"startOffset": 4,
},
"startCol": 1,
"startLine": 1,
"startOffset": 0,
"tagCloseChar": ">",
"tagOpenChar": "<",
"type": "starttag",
},
],
}
`;
exports[`parser void element 2`] = `
"Snapshot Diff:
Compared values have no visual difference."
`;
7 changes: 7 additions & 0 deletions packages/angular-parser/test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ describe('parser', () => {
expect(snapshotDiff(cleanParse(parse(html)), doc)).toMatchSnapshot()
expect(cleanParse(`<html></html>`)).toMatchSnapshot()
})

test('void element', () => {
const html = `<img>`
const doc = cleanParse(html)
expect(doc).toMatchSnapshot()
expect(snapshotDiff(cleanParse(parse(html)), doc)).toMatchSnapshot()
})
})
4 changes: 2 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"extends": "./node_modules/@1stg/tsconfig/node",
"compilerOptions": {
"baseUrl": ".",
"target": "es2017",
"paths": {
"markuplint-angular-parser": ["./packages/angular-parser/src"],
"markuplint-sync": ["./packages/sync/src"]
}
},
"target": "es2017"
}
}

0 comments on commit 717e97b

Please sign in to comment.