-
Notifications
You must be signed in to change notification settings - Fork 2k
/
styling.html
195 lines (173 loc) · 6.28 KB
/
styling.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../lib/style-util.html">
<link rel="import" href="../lib/resolve-url.html">
<link rel="import" href="../lib/style-transformer.html">
<link rel="import" href="../lib/settings.html">
<script>
(function() {
var prepTemplate = Polymer.Base._prepTemplate;
var prepElement = Polymer.Base._prepElement;
var baseStampTemplate = Polymer.Base._stampTemplate;
var nativeShadow = Polymer.Settings.useNativeShadow;
Polymer.Base._addFeature({
// declaration-y
_prepTemplate: function() {
prepTemplate.call(this);
var port = Polymer.DomModule.import(this.is);
if (this._encapsulateStyle === undefined) {
this._encapsulateStyle =
Boolean(port && !nativeShadow);
}
// scope css
// NOTE: dom scoped via annotations
if (nativeShadow || this._encapsulateStyle) {
this._scopeCss();
}
},
_prepElement: function(element) {
if (this._encapsulateStyle) {
Polymer.StyleTransformer.element(element, this.is,
this._scopeCssViaAttr);
}
prepElement.call(this, element);
},
_scopeCss: function() {
this._styles = this._prepareStyles();
this._scopeStyles(this._styles);
},
// search for extra style modules via `styleModules`
_prepareStyles: function() {
var cssText = '', m$ = this.styleModules;
if (m$) {
for (var i=0, l=m$.length, m; (i<l) && (m=m$[i]); i++) {
cssText += this._cssFromModule(m);
}
}
cssText += this._cssFromModule(this.is);
var styles = [];
if (cssText) {
var s = document.createElement('style');
s.textContent = cssText;
styles.push(s);
}
return styles;
},
// 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('style'));
this._unapplyStyles(e$);
e$ = e$.concat(Array.prototype.map.call(
m.querySelectorAll(REMOTE_SHEET_SELECTOR), function(l) {
return l.import.body;
}));
m._cssText = this._cssFromStyles(e$);
}
return m && m._cssText || '';
},
_cssFromStyles: function(styles) {
var cssText = '';
for (var i=0, l=styles.length, s; (i<l) && (s = styles[i]); i++) {
if (s && s.textContent) {
cssText +=
Polymer.ResolveUrl.resolveCss(s.textContent, s.ownerDocument);
}
}
return cssText;
},
_unapplyStyles: function(styles) {
for (var i=0, l=styles.length, s; (i<l) && (s = styles[i]); i++) {
s = s.__appliedElement || s;
s.parentNode.removeChild(s);
}
},
_scopeStyles: function(styles) {
for (var i=0, l=styles.length, s; (i<l) && (s=styles[i]); i++) {
// transform style if necessary and place in correct place
if (nativeShadow) {
if (this._template) {
this._template.content.appendChild(s);
}
} else {
var rules = this._rulesForStyle(s);
Polymer.StyleUtil.applyCss(
Polymer.StyleTransformer.css(rules, this.is, this.extends,
null, this._scopeCssViaAttr),
this.is, null, true);
}
}
},
_rulesForStyle: function(style) {
if (!style.__cssRules) {
style.__cssRules = Polymer.StyleUtil.parser.parse(style.textContent);
}
return style.__cssRules;
},
// instance-y
_stampTemplate: function() {
if (this._encapsulateStyle) {
Polymer.StyleTransformer.host(this, this.is);
}
baseStampTemplate.call(this);
},
// add scoping class whenever an element is added to localDOM
_elementAdd: function(node) {
if (this._encapsulateStyle && !node.__styleScoped) {
Polymer.StyleTransformer.dom(node, this.is, this._scopeCssViaAttr);
}
},
// remove scoping class whenever an element is removed from localDOM
_elementRemove: function(node) {
if (this._encapsulateStyle) {
Polymer.StyleTransformer.dom(node, this.is, this._scopeCssViaAttr, true);
}
},
/**
* Apply style scoping to the specified `container` and all its
* descendants. If `shoudlObserve` is true, changes to the container are
* monitored via mutation observer and scoping is applied.
*/
scopeSubtree: function(container, shouldObserve) {
if (nativeShadow) {
return;
}
var self = this;
var scopify = function(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
node.className = self._scopeElementClass(node, node.className);
var n$ = node.querySelectorAll('*');
Array.prototype.forEach.call(n$, function(n) {
n.className = self._scopeElementClass(n, n.className);
});
}
};
scopify(container);
if (shouldObserve) {
var mo = new MutationObserver(function(mxns) {
mxns.forEach(function(m) {
if (m.addedNodes) {
for (var i=0; i < m.addedNodes.length; i++) {
scopify(m.addedNodes[i]);
}
}
});
});
mo.observe(container, {childList: true, subtree: true});
return mo;
}
}
});
var REMOTE_SHEET_SELECTOR = 'link[rel=import][type~=css]';
})();
</script>