|
399 | 399 | }
|
400 | 400 | }
|
401 | 401 |
|
| 402 | + /** |
| 403 | + * Process all style elements in the element template. Styles with the |
| 404 | + * `include` attribute are processed such that any styles in |
| 405 | + * the associated "style modules" are included in the element template. |
| 406 | + * @param {PolymerElementConstructor} klass Element class |
| 407 | + * @param {!HTMLTemplateElement} template Template to process |
| 408 | + * @param {string} is Name of element |
| 409 | + * @param {string} baseURI Base URI for element |
| 410 | + * @private |
| 411 | + */ |
| 412 | + function processElementStyles(klass, template, is, baseURI) { |
| 413 | + const styles = Polymer.StyleGather.stylesFromModuleImports(is).concat( |
| 414 | + Polymer.StyleGather.stylesFromTemplate(template)); |
| 415 | + let templateStyles = template.content.querySelectorAll('style'); |
| 416 | + let lastStyle = templateStyles[templateStyles.length-1]; |
| 417 | + // ensure all gathered styles are actually in this template. |
| 418 | + for (let i=0; i < styles.length; i++) { |
| 419 | + let s = styles[i]; |
| 420 | + // if the style is not in this template, it's been "included" and |
| 421 | + // we put a clone of it in the template. |
| 422 | + if (s.getRootNode() != template.content) { |
| 423 | + s = s.cloneNode(true); |
| 424 | + template.content.insertBefore(s, lastStyle); |
| 425 | + } |
| 426 | + s.textContent = klass._processStyleText(s.textContent, baseURI); |
| 427 | + } |
| 428 | + if (window.ShadyCSS) { |
| 429 | + window.ShadyCSS.prepareTemplate(template, is); |
| 430 | + } |
| 431 | + } |
| 432 | + |
402 | 433 | /**
|
403 | 434 | * @polymer
|
404 | 435 | * @mixinClass
|
|
578 | 609 | }
|
579 | 610 |
|
580 | 611 | /**
|
581 |
| - * Gather style text for the template |
| 612 | + * Gather style text for a style element in the template. |
582 | 613 | *
|
583 |
| - * @param {string} is Tag name for this element |
584 |
| - * @param {!HTMLTemplateElement} template Template to process |
| 614 | + * @param {string} cssText Text containing styling to process |
585 | 615 | * @param {string} baseURI Base URI to rebase CSS paths against
|
586 |
| - * @return {string} The combined CSS text |
| 616 | + * @return {string} The processed CSS text |
587 | 617 | * @protected
|
588 | 618 | */
|
589 |
| - static _processStyleText(is, template, baseURI) { |
590 |
| - return Polymer.StyleGather.cssFromModuleImports(is) + |
591 |
| - Polymer.StyleGather.cssFromTemplate(template, baseURI); |
| 619 | + static _processStyleText(cssText, baseURI) { |
| 620 | + return Polymer.ResolveUrl.resolveCss(cssText, baseURI); |
592 | 621 | }
|
593 | 622 |
|
594 | 623 | /**
|
|
597 | 626 | * style scoping.
|
598 | 627 | *
|
599 | 628 | * @param {string} is Tag name (or type extension name) for this element
|
600 |
| - * @param {string=} ext For type extensions, the tag name that was extended |
601 | 629 | * @protected
|
602 | 630 | */
|
603 |
| - static _finalizeTemplate(is, ext) { |
| 631 | + static _finalizeTemplate(is) { |
604 | 632 | /** @const {HTMLTemplateElement} */
|
605 | 633 | const template = this.prototype._template;
|
606 | 634 | if (template && !template.__polymerFinalized) {
|
607 | 635 | template.__polymerFinalized = true;
|
608 | 636 | const importPath = this.importPath;
|
609 | 637 | const baseURI = importPath ? Polymer.ResolveUrl.resolveUrl(importPath) : '';
|
610 |
| - // support `include="module-name"` |
611 |
| - let cssText = this._processStyleText(is, template, baseURI); |
612 |
| - if (cssText) { |
613 |
| - let style = document.createElement('style'); |
614 |
| - style.textContent = cssText; |
615 |
| - template.content.insertBefore(style, template.content.firstChild); |
616 |
| - } |
617 |
| - if (window.ShadyCSS) { |
618 |
| - window.ShadyCSS.prepareTemplate(template, is, ext); |
619 |
| - } |
| 638 | + // e.g. support `include="module-name"`, and ShadyCSS |
| 639 | + processElementStyles(this, template, is, baseURI); |
620 | 640 | this.prototype._bindTemplate(template);
|
621 | 641 | }
|
622 | 642 | }
|
|
0 commit comments