UNPKG

es5-lit-element

Version:
114 lines (92 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.css = exports.unsafeCSS = exports.CSSResult = exports.supportsAdoptingStyleSheets = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } /** @license Copyright (c) 2019 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 */ var supportsAdoptingStyleSheets = 'adoptedStyleSheets' in Document.prototype && 'replace' in CSSStyleSheet.prototype; exports.supportsAdoptingStyleSheets = supportsAdoptingStyleSheets; var constructionToken = Symbol(); var CSSResult = /*#__PURE__*/ function () { function CSSResult(cssText, safeToken) { _classCallCheck(this, CSSResult); if (safeToken !== constructionToken) { throw new Error('CSSResult is not constructable. Use `unsafeCSS` or `css` instead.'); } this.cssText = cssText; } // Note, this is a getter so that it's lazy. In practice, this means // stylesheets are not created until the first element instance is made. _createClass(CSSResult, [{ key: "toString", value: function toString() { return this.cssText; } }, { key: "styleSheet", get: function get() { if (this._styleSheet === undefined) { // Note, if `adoptedStyleSheets` is supported then we assume CSSStyleSheet // is constructable. if (supportsAdoptingStyleSheets) { this._styleSheet = new CSSStyleSheet(); this._styleSheet.replaceSync(this.cssText); } else { this._styleSheet = null; } } return this._styleSheet; } }]); return CSSResult; }(); /** * Wrap a value for interpolation in a css tagged template literal. * * This is unsafe because untrusted CSS text can be used to phone home * or exfiltrate data to an attacker controlled site. Take care to only use * this with trusted input. */ exports.CSSResult = CSSResult; var unsafeCSS = function unsafeCSS(value) { return new CSSResult(String(value), constructionToken); }; exports.unsafeCSS = unsafeCSS; var textFromCSSResult = function textFromCSSResult(value) { if (value instanceof CSSResult) { return value.cssText; } else if (typeof value === 'number') { return value; } else { throw new Error("Value passed to 'css' function must be a 'css' function result: ".concat(value, ". Use 'unsafeCSS' to pass non-literal values, but\n take care to ensure page security.")); } }; /** * Template tag which which can be used with LitElement's `style` property to * set element styles. For security reasons, only literal string values may be * used. To incorporate non-literal values `unsafeCSS` may be used inside a * template string part. */ var css = function css(strings) { for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { values[_key - 1] = arguments[_key]; } var cssText = values.reduce(function (acc, v, idx) { return acc + textFromCSSResult(v) + strings[idx + 1]; }, strings[0]); return new CSSResult(cssText, constructionToken); }; exports.css = css;