Browse Source

Published by publish-to-git

81658e9375 (HEAD -> refs/heads/master, tag: refs/tags/v0.1.4) 0.1.4
tags/latest
Gerben 3 months ago
commit
7595b6f2c5
5 changed files with 154 additions and 0 deletions
  1. +34
    -0
      Readme.md
  2. +39
    -0
      lib/index.js
  3. +49
    -0
      package.json
  4. +3
    -0
      src/index.d.ts
  5. +29
    -0
      src/index.js

+ 34
- 0
Readme.md View File

@@ -0,0 +1,34 @@
# document-outerhtml

Like [Element.outerHTML][], but for the whole [Document][].

This means it returns a string containing the `<html>.....</html>` with all the content between, plus the `<!DOCTYPE ....>` declaration (if present), and any comments and stray elements or text nodes.

## Install

npm install "git+https://code.treora.com/gerben/document-outerhtml#latest"

or

npm install document-outerhtml@latest

..or equivalent

## Usage

import documentOuterHTML from 'document-outerhtml'

const html = documentOuterHTML(document)

## Licence

[CC0](https://creativecommons.org/publicdomain/zero/1.0/); do whatever you want with this code.

## See also

- [XMLSerializer.serializeToString][]; does nearly the same thing, except it creates XML.


[Element.outerHTML]: https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML
[Document]: https://developer.mozilla.org/en-US/docs/Web/API/Document
[XMLSerializer.serializeToString]: https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer/serializeToString

+ 39
- 0
lib/index.js View File

@@ -0,0 +1,39 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = documentOuterHTML;

var _doctypeToString = require('doctype-to-string');

var _doctypeToString2 = _interopRequireDefault(_doctypeToString);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function documentOuterHTML(document) {
if (!document || document.nodeType === undefined || document.nodeType !== document.DOCUMENT_NODE) {
throw new TypeError('Expected a Document');
}
var html = [].concat(_toConsumableArray(document.childNodes)).map(function (node) {
return nodeToString(node);
}).join('\n');
return html;
}

function nodeToString(node) {
switch (node.nodeType) {
case node.ELEMENT_NODE:
return node.outerHTML;
case node.TEXT_NODE:
return node.textContent;
case node.COMMENT_NODE:
return '<!--' + node.textContent + '-->';
case node.DOCUMENT_TYPE_NODE:
return (0, _doctypeToString2.default)(node);
default:
throw new TypeError('Unexpected node type: ' + node.nodeType);
}
}

+ 49
- 0
package.json View File

@@ -0,0 +1,49 @@
{
"name": "document-outerhtml",
"version": "0.1.4",
"description": "Like Element.outerHTML, but for the whole Document.",
"keywords": [
"dom"
],
"homepage": "https://code.treora.com/gerben/document-outerhtml",
"repository": {
"type": "git",
"url": "https://code.treora.com/gerben/document-outerhtml"
},
"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"
]
},
"dependencies": {
"doctype-to-string": "git+https://code.treora.com/gerben/doctype-to-string#semver:^0.1.4"
}
}

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

@@ -0,0 +1,3 @@
declare module 'document-outerhtml' {
export default function(doc: Document): string
}

+ 29
- 0
src/index.js View File

@@ -0,0 +1,29 @@
import doctypeToString from 'doctype-to-string'

export default function documentOuterHTML(document) {
if (!document
|| document.nodeType === undefined
|| document.nodeType !== document.DOCUMENT_NODE
) {
throw new TypeError('Expected a Document')
}
const html = [...document.childNodes]
.map(node => nodeToString(node))
.join('\n')
return html
}

function nodeToString(node) {
switch (node.nodeType) {
case node.ELEMENT_NODE:
return node.outerHTML
case node.TEXT_NODE:
return node.textContent
case node.COMMENT_NODE:
return `<!--${node.textContent}-->`
case node.DOCUMENT_TYPE_NODE:
return doctypeToString(node)
default:
throw new TypeError(`Unexpected node type: ${node.nodeType}`)
}
}

Loading…
Cancel
Save