UNPKG

@web/dev-server-core

Version:
159 lines 5.08 kB
"use strict"; /** * @license * Copyright (c) 2018 The Polymer Project Authors. All rights reserved. * This code may only be used under the BSD style license found at * http://polymer.github.io/LICENSE.txt * The complete set of authors may be found at * http://polymer.github.io/AUTHORS.txt * The complete set of contributors may be found at * http://polymer.github.io/CONTRIBUTORS.txt * Code distributed by Google as part of the polymer project is also * subject to an additional IP rights grant found at * http://polymer.github.io/PATENTS.txt */ Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultChildNodes = exports.setTextContent = exports.normalize = exports.removeAttribute = exports.setAttribute = exports.hasAttribute = exports.getAttributeIndex = exports.getAttribute = exports.getTextContent = void 0; const modification_js_1 = require("./modification.js"); const predicates_js_1 = require("./predicates.js"); const walking_js_1 = require("./walking.js"); /** * Return the text value of a node or element * * Equivalent to `node.textContent` in the browser */ function getTextContent(node) { if ((0, predicates_js_1.isCommentNode)(node)) { return node.data || ''; } if ((0, predicates_js_1.isTextNode)(node)) { return node.value || ''; } const subtree = (0, walking_js_1.nodeWalkAll)(node, predicates_js_1.isTextNode); return subtree.map(getTextContent).join(''); } exports.getTextContent = getTextContent; /** * @returns The string value of attribute `name`, or `null`. */ function getAttribute(element, name) { const i = getAttributeIndex(element, name); if (i > -1) { return element.attrs[i].value; } return null; } exports.getAttribute = getAttribute; function getAttributeIndex(element, name) { if (!element.attrs) { return -1; } const n = name.toLowerCase(); for (let i = 0; i < element.attrs.length; i++) { if (element.attrs[i].name.toLowerCase() === n) { return i; } } return -1; } exports.getAttributeIndex = getAttributeIndex; /** * @returns `true` iff [element] has the attribute [name], `false` otherwise. */ function hasAttribute(element, name) { return getAttributeIndex(element, name) !== -1; } exports.hasAttribute = hasAttribute; function setAttribute(element, name, value) { const i = getAttributeIndex(element, name); if (i > -1) { element.attrs[i].value = value; } else { element.attrs.push({ name: name, value: value }); } } exports.setAttribute = setAttribute; function removeAttribute(element, name) { const i = getAttributeIndex(element, name); if (i > -1) { element.attrs.splice(i, 1); } } exports.removeAttribute = removeAttribute; function collapseTextRange(parent, start, end) { if (!parent.childNodes) { return; } let text = ''; for (let i = start; i <= end; i++) { text += getTextContent(parent.childNodes[i]); } parent.childNodes.splice(start, end - start + 1); if (text) { const tn = modification_js_1.constructors.text(text); tn.parentNode = parent; parent.childNodes.splice(start, 0, tn); } } /** * Normalize the text inside an element * * Equivalent to `element.normalize()` in the browser * See https://developer.mozilla.org/en-US/docs/Web/API/Node/normalize */ function normalize(node) { if (!((0, predicates_js_1.isElement)(node) || (0, predicates_js_1.isDocument)(node) || (0, predicates_js_1.isDocumentFragment)(node))) { return; } if (!node.childNodes) { return; } let textRangeStart = -1; for (let i = node.childNodes.length - 1, n; i >= 0; i--) { n = node.childNodes[i]; if ((0, predicates_js_1.isTextNode)(n)) { if (textRangeStart === -1) { textRangeStart = i; } if (i === 0) { // collapse leading text nodes collapseTextRange(node, 0, textRangeStart); } } else { // recurse normalize(n); // collapse the range after this node if (textRangeStart > -1) { collapseTextRange(node, i + 1, textRangeStart); textRangeStart = -1; } } } } exports.normalize = normalize; /** * Set the text value of a node or element * * Equivalent to `node.textContent = value` in the browser */ function setTextContent(node, value) { if ((0, predicates_js_1.isCommentNode)(node)) { node.data = value; } else if ((0, predicates_js_1.isTextNode)(node)) { node.value = value; } else { const tn = modification_js_1.constructors.text(value); tn.parentNode = node; node.childNodes = [tn]; } } exports.setTextContent = setTextContent; const defaultChildNodes = function defaultChildNodes(node) { return node.childNodes; }; exports.defaultChildNodes = defaultChildNodes; //# sourceMappingURL=util.js.map