Browse Source

Published by publish-to-git

e6d7afec25 (HEAD -> refs/heads/master, tag: refs/tags/v0.1.3) 0.1.3
tags/v0.1.3
Gerben 4 years ago
commit
7be83031d5
3 changed files with 117 additions and 0 deletions
  1. +32
    -0
      Readme.md
  2. +39
    -0
      lib/index.js
  3. +46
    -0
      package.json

+ 32
- 0
Readme.md View File

@@ -0,0 +1,32 @@
# 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"

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

+ 46
- 0
package.json View File

@@ -0,0 +1,46 @@
{
"name": "document-outerhtml",
"version": "0.1.3",
"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",
"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"
],
"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.3"
}
}

Loading…
Cancel
Save