File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/node_modules/safevalues/dom/elements/element.js
"use strict";
/**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setPrefixedAttribute = exports.buildPrefixedAttributeSetter = exports.insertAdjacentHtml = exports.setCssText = exports.setOuterHtml = exports.setInnerHtml = void 0;
/**
* @fileoverview This contains safe wrappers for properties that aren't specific
* to one kind of HTMLElement (like innerHTML), plus other setters and functions
* that are not tied to elements (like location.href or Worker constructor).
*/
var attribute_impl_1 = require("../../internals/attribute_impl");
var html_impl_1 = require("../../internals/html_impl");
var style_impl_1 = require("../../internals/style_impl");
/**
* Safely set {@link Element.innerHTML} on a given ShadowRoot or Element which
* may not be a `<script>` element or a `<style>` element.
*/
function setInnerHtml(elOrRoot, v) {
if (isElement(elOrRoot)) {
throwIfScriptOrStyle(elOrRoot);
}
elOrRoot.innerHTML = (0, html_impl_1.unwrapHtml)(v);
}
exports.setInnerHtml = setInnerHtml;
/**
* Safely set {@link Element.outerHTML} for the given Element.
*/
function setOuterHtml(e, v) {
var parent = e.parentElement;
if (parent !== null) {
throwIfScriptOrStyle(parent);
}
e.outerHTML = (0, html_impl_1.unwrapHtml)(v);
}
exports.setOuterHtml = setOuterHtml;
/**
* Set `ElementCSSInlineStyle.cssText` for the given `ElementCSSInlineStyle`.
*/
function setCssText(e, v) {
e.style.cssText = (0, style_impl_1.unwrapStyle)(v);
}
exports.setCssText = setCssText;
/**
* Safely call {@link Element.insertAdjacentHTML} for the given Element.
*/
function insertAdjacentHtml(element, position, v) {
var tagContext = (position === 'beforebegin' || position === 'afterend') ?
element.parentElement :
element;
if (tagContext !== null) {
throwIfScriptOrStyle(tagContext);
}
element.insertAdjacentHTML(position, (0, html_impl_1.unwrapHtml)(v));
}
exports.insertAdjacentHtml = insertAdjacentHtml;
/**
* Given a set of known-to-be-safe prefixes (e.g., "data-", "aria-", "js"),
* return a setter function that allows you to set attributes on an element,
* as long as the names of the attributes to be set has one of the prefixes.
*
* The returned setter ensures that setting any dangerous attribute, e.g.,
* "src", "href" will cause an exception. This is intended to be used as the
* safe alterantive of `Element#setAttribute`, when applications need to set
* attributes that do not have security implications and do not have a
* corresponding DOM property.
*/
function buildPrefixedAttributeSetter(prefix) {
var otherPrefixes = [];
for (var _i = 1; _i < arguments.length; _i++) {
otherPrefixes[_i - 1] = arguments[_i];
}
var prefixes = __spreadArray([prefix], __read(otherPrefixes), false);
return function (e, attr, value) {
setPrefixedAttribute(prefixes, e, attr, value);
};
}
exports.buildPrefixedAttributeSetter = buildPrefixedAttributeSetter;
/**
* The safe alternative to Element#setAttribute. The function takes a list of
* `SafeAttributePrefix`, making developer intention explicit. The attribute
* to be set must has one of the safe prefixes, otherwise the function throws
* an Error.
*/
function setPrefixedAttribute(attrPrefixes, e, attr, value) {
if (attrPrefixes.length === 0) {
throw new Error('No prefixes are provided');
}
var prefixes = attrPrefixes.map(function (s) { return (0, attribute_impl_1.unwrapAttributePrefix)(s); });
var attrLower = attr.toLowerCase();
if (prefixes.every(function (p) { return attrLower.indexOf(p) !== 0; })) {
throw new Error("Attribute \"".concat(attr, "\" does not match any of the allowed prefixes."));
}
e.setAttribute(attr, value);
}
exports.setPrefixedAttribute = setPrefixedAttribute;
function throwIfScriptOrStyle(element) {
if (element.tagName.toLowerCase() === 'script') {
throw new Error('Use setTextContent with a SafeScript.');
}
else if (element.tagName.toLowerCase() === 'style') {
throw new Error('Use setTextContent with a SafeStyleSheet.');
}
}
function isElement(elOrRoot) {
return elOrRoot.tagName !== undefined;
}