Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve doctype with sysid (#143) #144

Merged
merged 1 commit into from Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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