|
1 | 1 | import { KEYS } from '../util'; |
2 | 2 |
|
3 | | -// Keep track of prepared ShadyDOM templates |
4 | | -const SHADY_TEMPLATES = {}; |
| 3 | +// Keep track of prepared templates |
| 4 | +const TEMPLATE_CACHE = {}; |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * @external HTMLElement |
@@ -316,45 +316,49 @@ export class HXElement extends HTMLElement { |
316 | 316 | /** |
317 | 317 | * @private |
318 | 318 | * @description |
319 | | - * If the browser doesn't have native ShadowDOM, this method |
320 | | - * will ensure that the ShadyDOM template is prepared no more |
321 | | - * than once, and applies ShadyDOM styling to the element. |
322 | | - * |
323 | | - * @param {HTMLTemplate} template |
| 319 | + * Prepares a template for injection into the shadow root |
| 320 | + * @param {String} strTemplate - HTML template content |
| 321 | + * @returns {HTMLTemplate} |
324 | 322 | */ |
325 | | - _$setupShadyDOM (template) { |
| 323 | + _$prepareTemplate (strTemplate) { |
326 | 324 | let _elementName = this.constructor.is; |
327 | 325 |
|
| 326 | + if (TEMPLATE_CACHE[_elementName]) { |
| 327 | + return TEMPLATE_CACHE[_elementName]; |
| 328 | + } |
| 329 | + |
| 330 | + let _template = document.createElement('template'); |
| 331 | + _template.innerHTML = strTemplate; |
| 332 | + |
328 | 333 | if (window.ShadyCSS) { |
329 | | - // check to see if the ShadyDOM template has already been prepared |
330 | | - if (!SHADY_TEMPLATES[_elementName]) { |
331 | | - // modifies 'template' variable in-place |
332 | | - ShadyCSS.prepareTemplate(template, _elementName); |
333 | | - // memoize prepared template, so it isn't prepared more than once |
334 | | - SHADY_TEMPLATES[_elementName] = template; |
335 | | - } |
336 | | - // Apply ShadyDOM styling (rewrites Light DOM) |
337 | | - ShadyCSS.styleElement(this); |
| 334 | + // modifies 'template' variable in-place |
| 335 | + ShadyCSS.prepareTemplate(_template, _elementName); |
338 | 336 | } |
339 | | - }//_$setupShadyDOM |
| 337 | + |
| 338 | + // cache prepared template, so it isn't prepared more than once |
| 339 | + TEMPLATE_CACHE[_elementName] = _template; |
| 340 | + |
| 341 | + return _template; |
| 342 | + }//_$prepareTemplate() |
340 | 343 |
|
341 | 344 | /** |
342 | 345 | * @private |
343 | 346 | * @description |
344 | 347 | * If a ShadowDOM needs to be setup, this method handles: |
345 | 348 | * |
346 | | - * 1. creating the <template> element |
| 349 | + * 1. preparing the <template> element |
347 | 350 | * 2. attaching a shadow root |
348 | 351 | * 3. applying ShadyDOM styling (if needed) |
349 | 352 | * 4. stamping the template |
350 | 353 | */ |
351 | 354 | _$setupShadowDOM () { |
352 | 355 | // Don't do anything unless the "template" class property is defined. |
353 | 356 | if (this.constructor.template) { |
354 | | - let _template = document.createElement('template'); |
355 | | - _template.innerHTML = this.constructor.template; |
| 357 | + let _template = this._$prepareTemplate(this.constructor.template); |
356 | 358 | this.attachShadow({ mode: 'open' }); |
357 | | - this._$setupShadyDOM(_template); |
| 359 | + if (window.ShadyCSS) { |
| 360 | + ShadyCSS.styleElement(this); |
| 361 | + } |
358 | 362 | this.shadowRoot.appendChild(_template.content.cloneNode(true)); |
359 | 363 | } |
360 | 364 | }//_$setupShadowDOM() |
|
0 commit comments