Convert a DOM DocumentType into a string, e.g. "<!DOCTYPE html>"

doctype-to-string/lib/ index.js
19 lines
941 B

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = doctypeToString;
  6. function doctypeToString(doctype) {
  7. if (doctype === null) {
  8. return '';
  9. }
  10. // Checking with instanceof DocumentType might be neater, but how to get a
  11. // reference to DocumentType without assuming it to be available globally?
  12. // To play nice with custom DOM implementations, we resort to duck-typing.
  13. if (!doctype || doctype.nodeType !== doctype.DOCUMENT_TYPE_NODE || typeof doctype.name !== 'string' || typeof doctype.publicId !== 'string' || typeof doctype.systemId !== 'string') {
  14. throw new TypeError('Expected a DocumentType');
  15. }
  16. var doctypeString = '<!DOCTYPE ' + doctype.name + (doctype.publicId ? ' PUBLIC "' + doctype.publicId + '"' : '') + (doctype.systemId ? (doctype.publicId ? '' : ' SYSTEM') + (' "' + doctype.systemId + '"') : '') + '>';
  17. return doctypeString;
  18. }