Skip to content

Commit b2f623f

Browse files
authored
Merge pull request #4892 from Polymer/2.x-shady-unscoped-style
Add support for `shady-unscoped` attribute
2 parents 4559830 + 15d8c6a commit b2f623f

10 files changed

+504
-55
lines changed

bower.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"url": "https://github.com/Polymer/polymer.git"
2121
},
2222
"dependencies": {
23-
"shadycss": "webcomponents/shadycss#^1.0.0",
24-
"webcomponentsjs": "webcomponents/webcomponentsjs#^1.0.2"
23+
"shadycss": "webcomponents/shadycss#^v1.1.0",
24+
"webcomponentsjs": "webcomponents/webcomponentsjs#^v1.0.19"
2525
},
2626
"devDependencies": {
2727
"web-component-tester": "^6.0.0",

externs/closure-types.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -749,17 +749,15 @@ Polymer_ElementMixin._parseTemplateContent = function(template, templateInfo, no
749749
*/
750750
Polymer_ElementMixin.finalize = function(){};
751751
/**
752-
* @param {string} is Tag name for this element
753-
* @param {!HTMLTemplateElement} template Template to process
752+
* @param {string} cssText Text containing styling to process
754753
* @param {string} baseURI Base URI to rebase CSS paths against
755754
* @return {string}
756755
*/
757-
Polymer_ElementMixin._processStyleText = function(is, template, baseURI){};
756+
Polymer_ElementMixin._processStyleText = function(cssText, baseURI){};
758757
/**
759758
* @param {string} is Tag name (or type extension name) for this element
760-
* @param {string=} ext For type extensions, the tag name that was extended
761759
*/
762-
Polymer_ElementMixin._finalizeTemplate = function(is, ext){};
760+
Polymer_ElementMixin._finalizeTemplate = function(is){};
763761
/**
764762
* @interface
765763
*/
@@ -799,7 +797,7 @@ Polymer_DirMixin.prototype.disconnectedCallback = function(){};
799797
/**
800798
* @override
801799
*/
802-
Polymer_DirMixin._processStyleText = function(is, template, baseURI){};
800+
Polymer_DirMixin._processStyleText = function(cssText, baseURI){};
803801
/**
804802
* @param {string} text CSS text to replace DIR
805803
* @return {string}

lib/mixins/dir-mixin.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@
104104
* @override
105105
* @suppress {missingProperties} Interfaces in closure do not inherit statics, but classes do
106106
*/
107-
static _processStyleText(is, template, baseURI) {
108-
let cssText = super._processStyleText(is, template, baseURI);
107+
static _processStyleText(cssText, baseURI) {
108+
cssText = super._processStyleText(cssText, baseURI);
109109
cssText = this._replaceDirInCssText(cssText);
110110
return cssText;
111111
}

lib/mixins/element-mixin.html

+39-19
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,37 @@
399399
}
400400
}
401401

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+
402433
/**
403434
* @polymer
404435
* @mixinClass
@@ -578,17 +609,15 @@
578609
}
579610

580611
/**
581-
* Gather style text for the template
612+
* Gather style text for a style element in the template.
582613
*
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
585615
* @param {string} baseURI Base URI to rebase CSS paths against
586-
* @return {string} The combined CSS text
616+
* @return {string} The processed CSS text
587617
* @protected
588618
*/
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);
592621
}
593622

594623
/**
@@ -597,26 +626,17 @@
597626
* style scoping.
598627
*
599628
* @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
601629
* @protected
602630
*/
603-
static _finalizeTemplate(is, ext) {
631+
static _finalizeTemplate(is) {
604632
/** @const {HTMLTemplateElement} */
605633
const template = this.prototype._template;
606634
if (template && !template.__polymerFinalized) {
607635
template.__polymerFinalized = true;
608636
const importPath = this.importPath;
609637
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);
620640
this.prototype._bindTemplate(template);
621641
}
622642
}

0 commit comments

Comments
 (0)