Skip to content

Commit

Permalink
fix: Add doctype when parsing from string (#301)
Browse files Browse the repository at this point in the history
* fix: Add doctype when parsing from string

fixes #277

* Update test/parse/doctype.test.js

Co-authored-by: Chris Brody <chris.brody+brodybits@gmail.com>

* Update test/parse/doctype.test.js

Co-authored-by: Chris Brody <chris.brody+brodybits@gmail.com>

Co-authored-by: Chris Brody <chris.brody+brodybits@gmail.com>
  • Loading branch information
karfau and brodybits committed Aug 26, 2021
1 parent dc429ae commit 566a8d4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/dom-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ DOMHandler.prototype = {
var dt = impl.createDocumentType(name, publicId, systemId);
this.locator && position(this.locator,dt)
appendElement(this, dt);
this.doc.doctype = dt;
}
},
/**
Expand Down
6 changes: 6 additions & 0 deletions lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,12 @@ Document.prototype = {
//implementation : null,
nodeName : '#document',
nodeType : DOCUMENT_NODE,
/**
* The DocumentType node of the document.
*
* @readonly
* @type DocumentType
*/
doctype : null,
documentElement : null,
_inc : 1,
Expand Down
12 changes: 12 additions & 0 deletions test/dom/document.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { getTestParser } = require('../get-test-parser')
const { DOMImplementation } = require('../../lib/dom')

const INPUT = (first = '', second = '', third = '', fourth = '') => `
<html >
Expand Down Expand Up @@ -71,4 +72,15 @@ describe('Document.prototype', () => {
})
})
})
describe('doctype', () => {
it('should be added when passed to createDocument', () => {
const impl = new DOMImplementation()
const doctype = impl.createDocumentType('name')
const doc = impl.createDocument(null, undefined, doctype)

expect(doc.doctype === doctype).toBe(true)
expect(doctype.ownerDocument === doc).toBe(true)
expect(doc.firstChild === doctype).toBe(true)
})
})
})
3 changes: 1 addition & 2 deletions test/dom/ns-test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ describe('XML Namespace Parse', () => {
)
})

//ignore default prefix xml attribute
it('test', () => {
it('should ignore default prefix xml attribute', () => {
const w3 = 'http://www.w3.org/1999/xhtml'
const n1 = 'http://www.frankston.com/public'
const n2 = 'http://rmf.vc/n2'
Expand Down
27 changes: 27 additions & 0 deletions test/parse/doctype.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const { getTestParser } = require('../get-test-parser')
const { MIME_TYPE } = require('../../lib/conventions')

describe('doctype', () => {
describe.each(['SYSTEM', 'PUBLIC'])('%s', (idType) => {
test.each([
Expand All @@ -24,4 +26,29 @@ describe('doctype', () => {
}
)
})

describe('sets Document.doctype', () => {
it('should set it for XML documents', () => {
const { parser } = getTestParser()
const doc = parser.parseFromString('<!DOCTYPE name><X/>')

expect(doc.doctype).toBeTruthy()
expect(doc.doctype.ownerDocument === doc).toBe(true)
expect(doc.firstChild === doc.doctype).toBe(true)
expect(doc.childNodes.length).toBe(2)

})
it('should set it for HTML documents', () => {
const { parser } = getTestParser()
const doc = parser.parseFromString(
'<!DOCTYPE html><body></body>',
MIME_TYPE.HTML
)

expect(doc.doctype).toBeTruthy()
expect(doc.doctype.ownerDocument === doc).toBe(true)
expect(doc.firstChild === doc.doctype).toBe(true)
expect(doc.childNodes.length).toBe(2)
})
})
})

0 comments on commit 566a8d4

Please sign in to comment.