Skip to content

Commit 1a213f0

Browse files
authored
Merge pull request #246 from rackerlabs/fix-ShadyCSS-0.9.x
fix(HXElement): fix template caching (v0.9.x)
2 parents 406f919 + f8b28c0 commit 1a213f0

File tree

3 files changed

+72
-3920
lines changed

3 files changed

+72
-3920
lines changed

src/helix-ui/elements/HXElement.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { KEYS } from '../util';
22

3-
// Keep track of prepared ShadyDOM templates
4-
const SHADY_TEMPLATES = {};
3+
// Keep track of prepared templates
4+
const TEMPLATE_CACHE = {};
55

66
/**
77
* @external HTMLElement
@@ -316,45 +316,49 @@ export class HXElement extends HTMLElement {
316316
/**
317317
* @private
318318
* @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}
324322
*/
325-
_$setupShadyDOM (template) {
323+
_$prepareTemplate (strTemplate) {
326324
let _elementName = this.constructor.is;
327325

326+
if (TEMPLATE_CACHE[_elementName]) {
327+
return TEMPLATE_CACHE[_elementName];
328+
}
329+
330+
let _template = document.createElement('template');
331+
_template.innerHTML = strTemplate;
332+
328333
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);
338336
}
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()
340343

341344
/**
342345
* @private
343346
* @description
344347
* If a ShadowDOM needs to be setup, this method handles:
345348
*
346-
* 1. creating the <template> element
349+
* 1. preparing the <template> element
347350
* 2. attaching a shadow root
348351
* 3. applying ShadyDOM styling (if needed)
349352
* 4. stamping the template
350353
*/
351354
_$setupShadowDOM () {
352355
// Don't do anything unless the "template" class property is defined.
353356
if (this.constructor.template) {
354-
let _template = document.createElement('template');
355-
_template.innerHTML = this.constructor.template;
357+
let _template = this._$prepareTemplate(this.constructor.template);
356358
this.attachShadow({ mode: 'open' });
357-
this._$setupShadyDOM(_template);
359+
if (window.ShadyCSS) {
360+
ShadyCSS.styleElement(this);
361+
}
358362
this.shadowRoot.appendChild(_template.content.cloneNode(true));
359363
}
360364
}//_$setupShadowDOM()

0 commit comments

Comments
 (0)