[6a3a178] | 1 | /**
|
---|
| 2 | * Copyright 2018 Google LLC
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
---|
| 5 | * use this file except in compliance with the License. You may obtain a copy of
|
---|
| 6 | * the License at
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
---|
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
---|
| 13 | * License for the specific language governing permissions and limitations under
|
---|
| 14 | * the License.
|
---|
| 15 | */
|
---|
| 16 | import { JSDOM } from 'jsdom';
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Parse HTML into a mutable, serializable DOM Document, provided by JSDOM.
|
---|
| 20 | * @private
|
---|
| 21 | * @param {String} html HTML to parse into a Document instance
|
---|
| 22 | */
|
---|
| 23 | export function createDocument (html) {
|
---|
| 24 | const jsdom = new JSDOM(html, {
|
---|
| 25 | contentType: 'text/html'
|
---|
| 26 | });
|
---|
| 27 | const { window } = jsdom;
|
---|
| 28 | const document = window.document;
|
---|
| 29 | document.$jsdom = jsdom;
|
---|
| 30 | return document;
|
---|
| 31 | }
|
---|
| 32 | /**
|
---|
| 33 | * Serialize a Document to an HTML String
|
---|
| 34 | * @private
|
---|
| 35 | * @param {Document} document A Document, such as one created via `createDocument()`
|
---|
| 36 | */
|
---|
| 37 | export function serializeDocument (document) {
|
---|
| 38 | return document.$jsdom.serialize();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /** Like node.textContent, except it works */
|
---|
| 42 | export function setNodeText (node, text) {
|
---|
| 43 | while (node.lastChild) {
|
---|
| 44 | node.removeChild(node.lastChild);
|
---|
| 45 | }
|
---|
| 46 | node.appendChild(node.ownerDocument.createTextNode(text));
|
---|
| 47 | }
|
---|