diff --git a/src/lib/custom-style.html b/src/lib/custom-style.html index 1eb97416ac..30225dc4d9 100644 --- a/src/lib/custom-style.html +++ b/src/lib/custom-style.html @@ -79,7 +79,11 @@ is: 'custom-style', extends: 'style', - created: function() { + properties: { + module: {} + }, + + ready: function() { // NOTE: we cannot just check attached because custom elements in // HTMLImports do not get attached. this._tryApply(); @@ -100,7 +104,7 @@ styleDefaults.addStyle(e); // we may not have any textContent yet due to parser yielding // if so, wait until we do... - if (e.textContent) { + if (e.textContent || this.module) { this._apply(); } else { var observer = new MutationObserver(function() { @@ -118,6 +122,13 @@ _apply: function() { // used applied element from HTMLImports polyfill or this var e = this.__appliedElement || this; + // path fixup iff necessary + if (!this.__appliedElement) { + e.textContent = styleUtil.resolveCss(e.textContent, e.ownerDocument); + } + if (this.module) { + e.textContent += styleUtil.cssFromModules(this.module); + } this._computeStyleProperties(); var props = this._styleProperties; var self = this; diff --git a/src/lib/style-util.html b/src/lib/style-util.html index 8f3e829a4c..43f2fe219c 100644 --- a/src/lib/style-util.html +++ b/src/lib/style-util.html @@ -16,7 +16,7 @@ return { - MODULE_STYLES_SELECTOR: 'style, link[rel=import][type~=css]', + MODULE_STYLES_SELECTOR: 'style, link[rel=import][type~=css], template', toCssText: function(rules, callback, preserveProperties) { if (typeof rules === 'string') { @@ -81,16 +81,36 @@ return style; }, + cssFromModules: function(moduleIds) { + var modules = moduleIds.trim().split(' '); + var cssText = ''; + for (var i=0; i < modules.length; i++) { + cssText += this.cssFromModule(modules[i]); + } + return cssText; + }, + // returns cssText of styles in a given module; also un-applies any // styles that apply to the document. cssFromModule: function(moduleId) { var m = Polymer.DomModule.import(moduleId); if (m && !m._cssText) { - var cssText = ''; - var e$ = Array.prototype.slice.call( - m.querySelectorAll(this.MODULE_STYLES_SELECTOR)); - for (var i=0, e; i < e$.length; i++) { - e = e$[i]; + m._cssText = this._cssFromElement(m); + } + return m && m._cssText || ''; + }, + + // support lots of ways to discover css... + _cssFromElement: function(element) { + var cssText = ''; + var e$ = Array.prototype.slice.call( + element.querySelectorAll(this.MODULE_STYLES_SELECTOR)); + for (var i=0, e; i < e$.length; i++) { + e = e$[i]; + // look inside templates for elements + if (e.localName === 'template') { + cssText += this._cssFromElement(e.content); + } else { // style elements inside dom-modules will apply to the main document // we don't want this, so we remove them here. if (e.localName === 'style') { @@ -103,15 +123,19 @@ } // adjust paths in css. if (e) { - cssText += - Polymer.ResolveUrl.resolveCss(e.textContent, e.ownerDocument); + cssText += this.resolveCss(e.textContent, e.ownerDocument); } } - m._cssText = cssText; + // now support module refs on 'styling' elements + var module = e.getAttribute('module'); + if (module) { + cssText += this.cssFromModules(module); + } } - return m && m._cssText || ''; + return cssText; }, + resolveCss: Polymer.ResolveUrl.resolveCss, parser: Polymer.CssParse, ruleTypes: Polymer.CssParse.types diff --git a/test/smoke/style-sharing/index.html b/test/smoke/style-sharing/index.html new file mode 100644 index 0000000000..ef56ef0b6f --- /dev/null +++ b/test/smoke/style-sharing/index.html @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + +
.bar from custom-style with module shared-styles
+
.foo should not be colored
+
.zot from custom-style using var in custom-style with module shared-styles
+ +
+ + + + + + diff --git a/test/smoke/style-sharing/shared-styles.html b/test/smoke/style-sharing/shared-styles.html new file mode 100644 index 0000000000..437f48254b --- /dev/null +++ b/test/smoke/style-sharing/shared-styles.html @@ -0,0 +1,11 @@ + + + \ No newline at end of file diff --git a/test/smoke/style-sharing/x-foo.html b/test/smoke/style-sharing/x-foo.html new file mode 100644 index 0000000000..6b243508d7 --- /dev/null +++ b/test/smoke/style-sharing/x-foo.html @@ -0,0 +1,31 @@ + + + + + + \ No newline at end of file