diff --git a/src/index.js b/src/index.js index b27d194..26043e7 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,17 @@ export default function doctypeToString(doctype) { if (doctype === null) { return '' } + // Checking with instanceof DocumentType might be neater, but how to get a + // reference to DocumentType without assuming it to be available globally? + // To play nice with custom DOM implementations, we resort to duck-typing. + if (!doctype + || doctype.nodeType !== doctype.DOCUMENT_TYPE_NODE + || typeof doctype.name !== 'string' + || typeof doctype.publicId !== 'string' + || typeof doctype.systemId !== 'string' + ) { + throw new TypeError('Expected a DocumentType') + } const doctypeString = ` { t.is(doctypeToString(null), '') }) +test('should throw if argument is not a DocumentType', t => { + t.throws(() => doctypeToString('a string, for example.'), TypeError) +}) + test('should work for a HTML5-ish doctype', t => { const doctype = '' const doc = makeStubDocument(doctype)