Skip to content

Commit

Permalink
fix: preserve doctype with sysid (#143) (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmc24 committed Oct 8, 2020
1 parent 73cd2d4 commit 738fb3e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/sax.js
Expand Up @@ -531,8 +531,16 @@ function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
var len = matchs.length;
if(len>1 && /!doctype/i.test(matchs[0][0])){
var name = matchs[1][0];
var pubid = len>3 && /^public$/i.test(matchs[2][0]) && matchs[3][0]
var sysid = len>4 && matchs[4][0];
var pubid = false;
var sysid = false;
if(len>3){
if(/^public$/i.test(matchs[2][0])){
pubid = matchs[3][0];
sysid = len>4 && matchs[4][0];
}else if(/^system$/i.test(matchs[2][0])){
sysid = matchs[3][0];
}
}
var lastMatch = matchs[len-1]
domBuilder.startDTD(name,pubid && pubid.replace(/^(['"])(.*?)\1$/,'$2'),
sysid && sysid.replace(/^(['"])(.*?)\1$/,'$2'));
Expand Down
10 changes: 10 additions & 0 deletions test/parse/node.test.js
Expand Up @@ -195,4 +195,14 @@ describe('XML Node Parse', () => {
`${DOCTYPE}<html xmlns="http://www.w3.org/1999/xhtml"></html>`
)
})

it('preserves doctype with sysid', () => {
const DOCTYPE = '<!DOCTYPE custom SYSTEM "custom.dtd">'

const actual = new DOMParser()
.parseFromString(`${DOCTYPE}<custom/>`, 'text/xml')
.toString()

expect(actual).toStrictEqual(`${DOCTYPE}<custom/>`)
})
})
4 changes: 2 additions & 2 deletions test/xmltest/__snapshots__/not-wf.test.js.snap
Expand Up @@ -442,7 +442,7 @@ Object {
exports[`xmltest/not-wellformed standalone should match 056.xml with snapshot 1`] = `
Object {
"actual": "<!DOCTYPE doc SYSTEM \\"comment\\">
"actual": "<!DOCTYPE doc>
<doc/>",
}
`;
Expand Down Expand Up @@ -1387,7 +1387,7 @@ Object {
exports[`xmltest/not-wellformed standalone should match 185.xml with snapshot 1`] = `
Object {
"actual": "<?xml version=\\"1.0\\" standalone=\\"yes\\"?>
<!DOCTYPE doc SYSTEM \\">\\">
<!DOCTYPE doc SYSTEM \\"185.ent\\">
<doc>&amp;e;</doc>",
"error": Array [
"[xmldom error] entity not found:&e;
Expand Down

0 comments on commit 738fb3e

Please sign in to comment.