commit 7be83031d5dc6cf7a48be31976eb30153b4238c8 Author: Gerben Date: Tue Mar 17 03:16:33 2020 +0100 Published by publish-to-git e6d7afec25d63f4dd10f2cc1e61732c31e24636e (HEAD -> refs/heads/master, tag: refs/tags/v0.1.3) 0.1.3 diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..a29a1f2 --- /dev/null +++ b/Readme.md @@ -0,0 +1,32 @@ +# document-outerhtml + +Like [Element.outerHTML][], but for the whole [Document][]. + +This means it returns a string containing the `.....` with all the content between, plus the `` 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 diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..037c95a --- /dev/null +++ b/lib/index.js @@ -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 ''; + case node.DOCUMENT_TYPE_NODE: + return (0, _doctypeToString2.default)(node); + default: + throw new TypeError('Unexpected node type: ' + node.nodeType); + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..8264398 --- /dev/null +++ b/package.json @@ -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 ", + "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" + } +}