commit 010edc8e5872b125d0da160dbf3957c92680e485 Author: Gerben Date: Sat Apr 25 11:39:04 2020 +0200 Published by publish-to-git b452d34d7929867af9909eae83ab9cb4a37291ba (HEAD -> refs/heads/master, tag: refs/tags/v0.1.4) 0.1.4 diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..8f2cd80 --- /dev/null +++ b/Readme.md @@ -0,0 +1,25 @@ +# doctype-to-string + +Convert a DOM [DocumentType](https://developer.mozilla.org/en-US/docs/Web/API/DocumentType) (usually obtained via `document.doctype`) into a string, e.g.: + - `` + - `` + +## Install + + npm install "git+https://code.treora.com/gerben/doctype-to-string#latest" + +..or + + npm install doctype-to-string@latest + +..or equivalent + +## Usage + + import doctypeToString from 'doctype-to-string' + + const string = doctypeToString(document.doctype) + +## Licence + +[CC0](https://creativecommons.org/publicdomain/zero/1.0/); do whatever you want with this code. diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..8c86218 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,19 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = doctypeToString; +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'); + } + var doctypeString = ''; + return doctypeString; +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..bf8d3e5 --- /dev/null +++ b/package.json @@ -0,0 +1,46 @@ +{ + "name": "doctype-to-string", + "version": "0.1.4", + "description": "Convert a DOM DocumentType into a string, e.g. \"\"", + "keywords": [ + "dom" + ], + "homepage": "https://code.treora.com/gerben/doctype-to-string", + "repository": { + "type": "git", + "url": "https://code.treora.com/gerben/doctype-to-string" + }, + "main": "lib/index.js", + "module": "src/index.js", + "types": "src/index.d.ts", + "scripts": { + "build": "babel -d lib src", + "postpublish": "publish-to-git --force", + "prepublish": "npm run build", + "test": "ava" + }, + "author": "Gerben ", + "license": "CC0-1.0", + "files": [ + "lib", + "src" + ], + "devDependencies": { + "ava": "^1.4.1", + "babel-cli": "^6.26.0", + "babel-preset-env": "^1.7.0", + "babel-register": "^6.26.0", + "publish-to-git": "^1.1.7", + "window": "^4.2.6" + }, + "babel": { + "presets": [ + "env" + ] + }, + "ava": { + "require": [ + "babel-register" + ] + } +} diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..719a55c --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,3 @@ +declare module 'doctype-to-string' { + export default function(doctype: DocumentType | null): string +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..26043e7 --- /dev/null +++ b/src/index.js @@ -0,0 +1,23 @@ +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 = `` + return doctypeString +}