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

Published by publish-to-git

b452d34d79 (HEAD -> refs/heads/master, tag: refs/tags/v0.1.4) 0.1.4
tags/latest
Gerben 4 years ago
commit
010edc8e58
5 changed files with 116 additions and 0 deletions
  1. +25
    -0
      Readme.md
  2. +19
    -0
      lib/index.js
  3. +46
    -0
      package.json
  4. +3
    -0
      src/index.d.ts
  5. +23
    -0
      src/index.js

+ 25
- 0
Readme.md View File

@@ -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.:
- `<!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"

..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.

+ 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;
}

+ 46
- 0
package.json View File

@@ -0,0 +1,46 @@
{
"name": "doctype-to-string",
"version": "0.1.4",
"description": "Convert a DOM DocumentType into a string, e.g. \"<!DOCTYPE html>\"",
"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 <gerben@treora.com>",
"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"
]
}
}

+ 3
- 0
src/index.d.ts View File

@@ -0,0 +1,3 @@
declare module 'doctype-to-string' {
export default function(doctype: DocumentType | null): string
}

+ 23
- 0
src/index.js View File

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

Loading…
Cancel
Save