|
72 | 72 | var propertyUtils = Polymer.StyleProperties;
|
73 | 73 | var styleUtil = Polymer.StyleUtil;
|
74 | 74 | var styleDefaults = Polymer.StyleDefaults;
|
| 75 | + var styleTransformer = Polymer.StyleTransformer; |
75 | 76 |
|
76 | 77 | Polymer({
|
77 | 78 |
|
78 | 79 | is: 'custom-style',
|
79 | 80 | extends: 'style',
|
80 | 81 |
|
81 | 82 | created: function() {
|
82 |
| - this._appliesToDocument = (this.parentNode.localName !== 'dom-module'); |
83 |
| - if (this._appliesToDocument) { |
84 |
| - // used applied element from HTMLImports polyfill or this |
85 |
| - var e = this.__appliedElement || this; |
86 |
| - var rules = styleUtil.rulesForStyle(e); |
87 |
| - propertyUtils.decorateStyles([e]); |
88 |
| - this._rulesToDefaultProperties(rules); |
89 |
| - // NOTE: go async to give a chance to collect properties into |
90 |
| - // the StyleDefaults before applying |
91 |
| - this.async(this._applyStyle); |
| 83 | + // NOTE: we cannot just check attached because custom elements in |
| 84 | + // HTMLImports do not get attached. |
| 85 | + this._tryApply(); |
| 86 | + }, |
| 87 | + |
| 88 | + attached: function() { |
| 89 | + this._tryApply(); |
| 90 | + }, |
| 91 | + |
| 92 | + _tryApply: function() { |
| 93 | + if (!this._appliesToDocument) { |
| 94 | + // only apply variables iff this style is not inside a dom-module |
| 95 | + if (this.parentNode && |
| 96 | + (this.parentNode.localName !== 'dom-module')) { |
| 97 | + this._appliesToDocument = true; |
| 98 | + // used applied element from HTMLImports polyfill or this |
| 99 | + var e = this.__appliedElement || this; |
| 100 | + styleDefaults.addStyle(e); |
| 101 | + // we may not have any textContent yet due to parser yielding |
| 102 | + // if so, wait until we do... |
| 103 | + if (e.textContent) { |
| 104 | + this._apply(); |
| 105 | + } else { |
| 106 | + var observer = new MutationObserver(function() { |
| 107 | + observer.disconnect(); |
| 108 | + this._apply(); |
| 109 | + }.bind(this)); |
| 110 | + observer.observe(e, {childList: true}); |
| 111 | + } |
| 112 | + } |
92 | 113 | }
|
93 | 114 | },
|
94 | 115 |
|
95 | 116 | // polyfill this style with root scoping and
|
96 | 117 | // apply custom properties!
|
97 |
| - _applyStyle: function() { |
| 118 | + _apply: function() { |
98 | 119 | // used applied element from HTMLImports polyfill or this
|
99 | 120 | var e = this.__appliedElement || this;
|
100 | 121 | this._computeStyleProperties();
|
101 | 122 | var props = this._styleProperties;
|
102 | 123 | var self = this;
|
103 | 124 | e.textContent = styleUtil.toCssText(styleUtil.rulesForStyle(e),
|
104 | 125 | function(rule) {
|
105 |
| - // polyfill lack of support for :root |
106 |
| - if (rule.selector === ':root') { |
107 |
| - rule.selector = 'body'; |
108 |
| - } |
109 | 126 | var css = rule.cssText = rule.parsedCssText;
|
110 | 127 | if (rule.propertyInfo && rule.propertyInfo.cssText) {
|
111 | 128 | // TODO(sorvell): factor better
|
|
114 | 131 | // replace with reified properties, scenario is same as mixin
|
115 | 132 | rule.cssText = propertyUtils.valueForProperties(css, props);
|
116 | 133 | }
|
117 |
| - if (!nativeShadow) { |
118 |
| - Polymer.StyleTransformer.rootRule(rule); |
119 |
| - } |
| 134 | + styleTransformer.documentRule(rule); |
120 | 135 | }
|
121 | 136 | );
|
122 |
| - }, |
123 |
| - |
124 |
| - _rulesToDefaultProperties: function(rules) { |
125 |
| - // produce css containing only property assignments. |
126 |
| - styleUtil.forEachStyleRule(rules, function(rule) { |
127 |
| - if (!rule.propertyInfo.properties) { |
128 |
| - rule.cssText = ''; |
129 |
| - } |
130 |
| - }); |
131 |
| - // tell parser to emit css that includes properties. |
132 |
| - var cssText = styleUtil.parser.stringify(rules, true); |
133 |
| - if (cssText) { |
134 |
| - styleDefaults.applyCss(cssText); |
135 |
| - } |
136 | 137 | }
|
137 | 138 |
|
138 | 139 | });
|
|
0 commit comments