/** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ var _a; // Scopes that have had styling prepared. Note, must only be done once per // scope. var styledScopes = new Set(); // Map of css per scope. This is collected during first scope render, used when // styling is prepared, and then discarded. var scopeCssStore = new Map(); var ENABLE_SHADYDOM_NOPATCH = true; /** * lit-html patches. These properties cannot be renamed. * * ChildPart.prototype._$getTemplate * * ChildPart.prototype._$setValue */ var polyfillSupport = function (Template, ChildPart) { var _a, _b; // polyfill-support is only needed if ShadyCSS or the ApplyShim is in use // We test at the point of patching, which makes it safe to load // webcomponentsjs and polyfill-support in either order if (window.ShadyCSS === undefined || (window.ShadyCSS.nativeShadow && !window.ShadyCSS.ApplyShim)) { return; } // console.log( // '%c Making lit-html compatible with ShadyDOM/CSS.', // 'color: lightgreen; font-style: italic' // ); var wrap = ((_a = window.ShadyDOM) === null || _a === void 0 ? void 0 : _a.inUse) && ((_b = window.ShadyDOM) === null || _b === void 0 ? void 0 : _b.noPatch) === true ? window.ShadyDOM.wrap : function (node) { return node; }; var needsPrepareStyles = function (name) { return name !== undefined && !styledScopes.has(name); }; var cssForScope = function (name) { var scopeCss = scopeCssStore.get(name); if (scopeCss === undefined) { scopeCssStore.set(name, (scopeCss = [])); } return scopeCss; }; var prepareStyles = function (name, template) { // Get styles var scopeCss = cssForScope(name); var hasScopeCss = scopeCss.length !== 0; if (hasScopeCss) { var style = document.createElement('style'); style.textContent = scopeCss.join('\n'); // Note, it's important to add the style to the *end* of the template so // it doesn't mess up part indices. template.content.appendChild(style); } // Mark this scope as styled. styledScopes.add(name); // Remove stored data since it's no longer needed. scopeCssStore.delete(name); // ShadyCSS removes scopes and removes the style under ShadyDOM and leaves // it under native Shadow DOM window.ShadyCSS.prepareTemplateStyles(template, name); // Note, under native Shadow DOM, the style is added to the beginning of the // template. It must be moved to the *end* of the template so it doesn't // mess up part indices. if (hasScopeCss && window.ShadyCSS.nativeShadow) { // If there were styles but the CSS text was empty, ShadyCSS will // eliminate the style altogether, so the style here could be null var style = template.content.querySelector('style'); if (style !== null) { template.content.appendChild(style); } } }; var scopedTemplateCache = new Map(); /** * Override to extract style elements from the template * and store all style.textContent in the shady scope data. * Note, it's ok to patch Template since it's only used via ChildPart. */ var originalCreateElement = Template.createElement; Template.createElement = function (html, options) { var element = originalCreateElement.call(Template, html, options); var scope = options === null || options === void 0 ? void 0 : options.scope; if (scope !== undefined) { if (!window.ShadyCSS.nativeShadow) { window.ShadyCSS.prepareTemplateDom(element, scope); } // Process styles only if this scope is being prepared. Otherwise, // leave styles as is for back compat with Lit1. if (needsPrepareStyles(scope)) { var scopeCss = cssForScope(scope); // Remove styles and store textContent. var styles = element.content.querySelectorAll('style'); // Store the css in this template in the scope css and remove the