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

Published by publish-to-git

1a268df55b (HEAD -> refs/heads/master, tag: refs/tags/v0.1.3) 0.1.3
tags/v0.1.3
Gerben 4 years ago
commit
4a0ce2acc2
3 changed files with 81 additions and 0 deletions
  1. +23
    -0
      Readme.md
  2. +19
    -0
      lib/index.js
  3. +39
    -0
      package.json

+ 23
- 0
Readme.md View File

@@ -0,0 +1,23 @@
# 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.:
- `<!DOCTYPE html>`
- `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">`

## Install

npm install "git+https://code.treora.com/gerben/doctype-to-string#latest"

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.

+ 19
- 0
lib/index.js View File

@@ -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 = '<!DOCTYPE ' + doctype.name + (doctype.publicId ? ' PUBLIC "' + doctype.publicId + '"' : '') + (doctype.systemId ? (doctype.publicId ? '' : ' SYSTEM') + (' "' + doctype.systemId + '"') : '') + '>';
return doctypeString;
}

+ 39
- 0
package.json View File

@@ -0,0 +1,39 @@
{
"name": "doctype-to-string",
"version": "0.1.3",
"description": "Convert a DOM DocumentType into a string, e.g. \"<!DOCTYPE html>\"",
"repository": {
"type": "git",
"url": "https://code.treora.com/gerben/doctype-to-string"
},
"main": "lib",
"scripts": {
"build": "babel -d lib src",
"postpublish": "publish-to-git --force && publish-to-git --force --tag latest",
"prepublish": "npm run build",
"test": "ava"
},
"author": "Gerben <gerben@treora.com>",
"license": "CC0-1.0",
"files": [
"lib"
],
"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"
]
}
}

Loading…
Cancel
Save