|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>StyleSheet</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>StyleSheet</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro component"> |
|
|
31 |
<p> |
|
|
32 |
The StyleSheet module normalizes the dynamic creation and modification |
|
|
33 |
of CSS stylesheets on a page. This makes it easy to manage the |
|
|
34 |
development, storage, and reapplication of personalized user |
|
|
35 |
stylesheets. Because it targets styles at the CSS level, it also |
|
|
36 |
allows you to modify styles applied to a CSS pseudo-element such as |
|
|
37 |
<code>p::first-letter</code>, or pseudo-class such as |
|
|
38 |
<code>a:hover</code>. |
|
|
39 |
</p> |
|
|
40 |
|
|
|
41 |
<p> |
|
|
42 |
StyleSheet is capable of creating new stylesheets from scratch or |
|
|
43 |
modifying existing stylesheets held as properties of |
|
|
44 |
<code><link></code> or <code><style></code> elements. It |
|
|
45 |
should be noted that not all browsers support reading or modifying |
|
|
46 |
external stylesheets from other domains. |
|
|
47 |
</p> |
|
|
48 |
</div> |
|
|
49 |
|
|
|
50 |
<h2 id="getting-started">Getting Started</h2> |
|
|
51 |
|
|
|
52 |
<p> |
|
|
53 |
To include the source files for StyleSheet and its dependencies, first load |
|
|
54 |
the YUI seed file if you haven't already loaded it. |
|
|
55 |
</p> |
|
|
56 |
|
|
|
57 |
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script></pre> |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
<p> |
|
|
61 |
Next, create a new YUI instance for your application and populate it with the |
|
|
62 |
modules you need by specifying them as arguments to the <code>YUI().use()</code> method. |
|
|
63 |
YUI will automatically load any dependencies required by the modules you |
|
|
64 |
specify. |
|
|
65 |
</p> |
|
|
66 |
|
|
|
67 |
<pre class="code prettyprint"><script> |
|
|
68 |
// Create a new YUI instance and populate it with the required modules. |
|
|
69 |
YUI().use('stylesheet', function (Y) { |
|
|
70 |
// StyleSheet is available and ready for use. Add implementation |
|
|
71 |
// code here. |
|
|
72 |
}); |
|
|
73 |
</script></pre> |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
<p> |
|
|
77 |
For more information on creating YUI instances and on the |
|
|
78 |
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the |
|
|
79 |
documentation for the <a href="../yui/index.html">YUI Global Object</a>. |
|
|
80 |
</p> |
|
|
81 |
|
|
|
82 |
|
|
|
83 |
<h2 id="using">Using the StyleSheet Utility</h2> |
|
|
84 |
|
|
|
85 |
<h3 id="instantiating">Instantiating a <code>Y.StyleSheet</code></h3> |
|
|
86 |
|
|
|
87 |
<p> |
|
|
88 |
The <code>Y.StyleSheet</code> constructor is written to support both |
|
|
89 |
function syntax and normal constructor syntax making the <code>new</code> |
|
|
90 |
prefix unnecessary (but harmless). |
|
|
91 |
</p> |
|
|
92 |
|
|
|
93 |
<p> |
|
|
94 |
The constructor has no required parameters. Passing no arguments will |
|
|
95 |
create a new, empty StyleSheet instance. |
|
|
96 |
</p> |
|
|
97 |
|
|
|
98 |
<pre class="code prettyprint">// These are equivalent; both create new empty StyleSheets |
|
|
99 |
var myStyleSheet = new Y.StyleSheet(); |
|
|
100 |
|
|
|
101 |
var myOtherStyleSheet = Y.StyleSheet();</pre> |
|
|
102 |
|
|
|
103 |
|
|
|
104 |
<p> |
|
|
105 |
To seed a new StyleSheet with a number of CSS rules, you can pass the |
|
|
106 |
constructor any of the following: |
|
|
107 |
</p> |
|
|
108 |
|
|
|
109 |
<ol> |
|
|
110 |
<li> |
|
|
111 |
a <code><style></code> or <code><link></code> node |
|
|
112 |
reference, |
|
|
113 |
</li> |
|
|
114 |
<li> |
|
|
115 |
the id of a <code><style></code> or <code><link></code> |
|
|
116 |
node, or |
|
|
117 |
</li> |
|
|
118 |
<li>a string of CSS</li> |
|
|
119 |
</ol> |
|
|
120 |
|
|
|
121 |
<pre class="code prettyprint"><link id="local" type="text/css" rel="stylesheet" href="local_file.css"> |
|
|
122 |
<style id="styleblock" type="text/css"> |
|
|
123 |
.some select.or { |
|
|
124 |
margin-right: 2em; |
|
|
125 |
} |
|
|
126 |
</style></pre> |
|
|
127 |
|
|
|
128 |
|
|
|
129 |
<pre class="code prettyprint">YUI().use('node','stylesheet', function (Y) { |
|
|
130 |
|
|
|
131 |
// Node or HTMLElement reference for a style or locally sourced link element |
|
|
132 |
var sheet = Y.StyleSheet(Y.one("#styleblock")); |
|
|
133 |
sheet = Y.StyleSheet(Y.DOM.byId('local')); |
|
|
134 |
|
|
|
135 |
// OR the id of a style element or locally sourced link element |
|
|
136 |
sheet = Y.StyleSheet('#local'); |
|
|
137 |
|
|
|
138 |
// OR string of css text |
|
|
139 |
var css = ".moduleX .alert { background: #fcc; font-weight: bold; } " + |
|
|
140 |
".moduleX .warn { background: #eec; } " + |
|
|
141 |
".hide_messages .moduleX .alert, " + |
|
|
142 |
".hide_messages .moduleX .warn { display: none; }"; |
|
|
143 |
|
|
|
144 |
sheet = new Y.StyleSheet(css); |
|
|
145 |
|
|
|
146 |
//... |
|
|
147 |
});</pre> |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
<p> |
|
|
151 |
Be aware that the <a |
|
|
152 |
href="http://en.wikipedia.org/wiki/Same_origin_policy">Same Origin |
|
|
153 |
policy</a> prevents access in some browsers to the style data of |
|
|
154 |
<code><link></code> elements with <code>href</code>s pointing to |
|
|
155 |
other domains. Attempts to seed a <code>Y.StyleSheet</code> instance with |
|
|
156 |
a cross-domain <code><link></code> may result in a security |
|
|
157 |
error. |
|
|
158 |
</p> |
|
|
159 |
|
|
|
160 |
<pre class="code prettyprint"><link id="remote" type="text/css" rel="stylesheet" href="http://other.domain.com/remote_file.css"></pre> |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
<pre class="code prettyprint">// ERROR - Same Origin Policy prevents access to remote stylesheets |
|
|
164 |
var styleSheet = Y.StyleSheet('remote');</pre> |
|
|
165 |
|
|
|
166 |
|
|
|
167 |
<h3 id="registry">Getting a StyleSheet by registered name</h3> |
|
|
168 |
|
|
|
169 |
<p> |
|
|
170 |
<code>Y.StyleSheet</code> supports registering instances by name allowing |
|
|
171 |
them to be recalled by that same name elsewhere in your code. Internally, |
|
|
172 |
<code>Y.StyleSheet</code> maintains a registry of all created StyleSheet |
|
|
173 |
instances, using a unique generated id that the host node is tagged with. |
|
|
174 |
This allows future attempts to create a StyleSheet instance from the same |
|
|
175 |
node to return the previously created instance associated with that id. |
|
|
176 |
</p> |
|
|
177 |
|
|
|
178 |
<p> |
|
|
179 |
Register a StyleSheet instance manually using the static |
|
|
180 |
<code>register</code> method or pass the desired name as a second parameter |
|
|
181 |
to the constructor. |
|
|
182 |
</p> |
|
|
183 |
|
|
|
184 |
<pre class="code prettyprint">var sheetA = Y.StyleSheet('my_stylesheet'); |
|
|
185 |
|
|
|
186 |
// Create a registry alias to sheetA. We'll call it bob. |
|
|
187 |
Y.StyleSheet.register(sheetA, 'bob'); |
|
|
188 |
|
|
|
189 |
// Create another StyleSheet passing the name as the second parameter |
|
|
190 |
var css = ".some .css { white-space: pre-wrap; color: pink; }"; |
|
|
191 |
var sheetB = Y.StyleSheet(css, 'my sheet'); |
|
|
192 |
|
|
|
193 |
// Meanwhile, elsewhere in your code |
|
|
194 |
|
|
|
195 |
// sheetA is the same instance as sheet1 and sheet2 |
|
|
196 |
var sheet1 = Y.StyleSheet(Y.one('#my_stylesheet')), |
|
|
197 |
sheet2 = Y.StyleSheet('bob'); |
|
|
198 |
|
|
|
199 |
// sheetB is the same instance as sheet3 |
|
|
200 |
var sheet3 = Y.StyleSheet('my sheet');</pre> |
|
|
201 |
|
|
|
202 |
|
|
|
203 |
<p> |
|
|
204 |
If an unregistered name is passed as the <em>first</em> argument to the |
|
|
205 |
constructor, a new empty StyleSheet will be created and registered with |
|
|
206 |
that name. This allows you to use the following code pattern: |
|
|
207 |
</p> |
|
|
208 |
|
|
|
209 |
<pre class="code prettyprint">// Whichever of these executes first, an empty StyleSheet will be created |
|
|
210 |
// and registered as 'MyApp'. |
|
|
211 |
|
|
|
212 |
// In one area of your app |
|
|
213 |
Y.StyleSheet('MyApp').set('.module .messages', { display: 'none' }); |
|
|
214 |
|
|
|
215 |
//... |
|
|
216 |
|
|
|
217 |
// In another area of your app |
|
|
218 |
Y.StyleSheet('MyApp').unset('.module .messages','display');</pre> |
|
|
219 |
|
|
|
220 |
|
|
|
221 |
<h3 id="first_param">Summary of how the constructor handles the first argument</h3> |
|
|
222 |
|
|
|
223 |
<p> |
|
|
224 |
When nothing is passed as the first argument, a new StyleSheet instance is |
|
|
225 |
created. |
|
|
226 |
</p> |
|
|
227 |
|
|
|
228 |
<p> |
|
|
229 |
When a <code><style></code> or <code><link></code> element is |
|
|
230 |
passed as the first argument, it is inspected for the id stamp that |
|
|
231 |
StyleSheet tags known host nodes with. If it finds one, it will return the |
|
|
232 |
associated StyleSheet from the registry. If not, it will stamp the node |
|
|
233 |
and seed the instance from the node's CSS content. |
|
|
234 |
</p> |
|
|
235 |
|
|
|
236 |
<p> |
|
|
237 |
When a string is passed as the first argument, StyleSheet does the |
|
|
238 |
following things in order: |
|
|
239 |
</p> |
|
|
240 |
|
|
|
241 |
<ol> |
|
|
242 |
<li> |
|
|
243 |
Check the registry for an instance associated to that name. If found, |
|
|
244 |
return the instance. |
|
|
245 |
</li> |
|
|
246 |
<li> |
|
|
247 |
Check the DOM for a <code><style></code> or |
|
|
248 |
<code><link></code> node with that id. If found, check the |
|
|
249 |
registry for an instance associated to its tagged id if present. If |
|
|
250 |
found, return that instance. If not, use that node to seed a new |
|
|
251 |
StyleSheet instance. |
|
|
252 |
</li> |
|
|
253 |
<li> |
|
|
254 |
Check the string for a curly brace { character. If found, create a new |
|
|
255 |
instance seeded with the string as initial <code>cssText</code>. |
|
|
256 |
</li> |
|
|
257 |
<li> |
|
|
258 |
Create a new empty StyleSheet and register the instance by the provided |
|
|
259 |
string. |
|
|
260 |
</li> |
|
|
261 |
</ol> |
|
|
262 |
|
|
|
263 |
<h3 id="set">Creating and modifying CSS style rules</h3> |
|
|
264 |
|
|
|
265 |
<p> |
|
|
266 |
The core method of StyleSheet instances is <code>set(selector, |
|
|
267 |
style_properties)</code>. It will create or alter a CSS rule using the |
|
|
268 |
property:value pairs in <code>style_properties</code> targeting the |
|
|
269 |
provided <code>selector</code>. In essence, it looks very much like |
|
|
270 |
natural CSS syntax, <em>except style properties must be in JavaScript's |
|
|
271 |
camelCase</em>. |
|
|
272 |
</p> |
|
|
273 |
|
|
|
274 |
<pre class="code prettyprint">Y.StyleSheet('MyApp').set( |
|
|
275 |
"q.uoted select.or[str=ing]", { |
|
|
276 |
fontSize : "150%", // note the camel casing |
|
|
277 |
background : "#030 url(/images/bg_image.png) scroll repeat-y top left", |
|
|
278 |
cssFloat : "left", |
|
|
279 |
opacity : 0.5 |
|
|
280 |
});</pre> |
|
|
281 |
|
|
|
282 |
|
|
|
283 |
<p> |
|
|
284 |
Rather than continually add new rules that will override one another, |
|
|
285 |
StyleSheet manages one rule per selector and modifies them in place. This |
|
|
286 |
may be relevant if you have two or more rules with selectors of the same |
|
|
287 |
specificity. |
|
|
288 |
</p> |
|
|
289 |
|
|
|
290 |
<p> |
|
|
291 |
As with regular CSS syntax, comma-separated selectors are supported, but |
|
|
292 |
internally StyleSheet splits them up into individual rules because browser |
|
|
293 |
support for multiple selectors is not consistent. This means calling |
|
|
294 |
<code>set(..)</code> with such a selector string <em>will incur multiple |
|
|
295 |
repaints or reflows</em>, but limited to the number of atomic |
|
|
296 |
selectors. |
|
|
297 |
</p> |
|
|
298 |
|
|
|
299 |
<pre class="code prettyprint">// This is valid, but will trigger 3 reflows |
|
|
300 |
Y.StyleSheet('MyApp').set( |
|
|
301 |
'.foo, .bar, .baz', { |
|
|
302 |
borderRight: "1em solid #f00" |
|
|
303 |
});</pre> |
|
|
304 |
|
|
|
305 |
|
|
|
306 |
<h3 id="normalized_properties">Some style properties are normalized</h3> |
|
|
307 |
|
|
|
308 |
<p> |
|
|
309 |
Two style properties have differing implementation between browsers, namely |
|
|
310 |
<code>float</code> and <code>opacity</code>. StyleSheet instances will |
|
|
311 |
normalize these properties for you. |
|
|
312 |
</p> |
|
|
313 |
|
|
|
314 |
<p> |
|
|
315 |
Because "float" is a reserved word in JavaScript, it is supported |
|
|
316 |
by the name <code>cssFloat</code> in W3C compliant browsers and |
|
|
317 |
<code>styleFloat</code> in IE. StyleSheet will accept any of these to set |
|
|
318 |
the <code>float</code> property. |
|
|
319 |
</p> |
|
|
320 |
|
|
|
321 |
<pre class="code prettyprint">// Any of these will work |
|
|
322 |
Y.StyleSheet('MyApp').set('.foo', { |
|
|
323 |
"float" : "left", // "float" must be quoted |
|
|
324 |
cssFloat : "right", |
|
|
325 |
styleFloat : "none" |
|
|
326 |
});</pre> |
|
|
327 |
|
|
|
328 |
|
|
|
329 |
<p> |
|
|
330 |
IE does not support the <code>opacity</code> style property, but has |
|
|
331 |
equivalent functionality offered by its proprietary <code>filter</code> |
|
|
332 |
property, though using a different value syntax. StyleSheet will translate |
|
|
333 |
<code>opacity</code> to <code>filter</code> for IE, but it <em>will |
|
|
334 |
not</em> translate <code>filter</code> to <code>opacity</code> for |
|
|
335 |
W3C-compliant browsers. |
|
|
336 |
</p> |
|
|
337 |
|
|
|
338 |
<h3 id="unset">Removing and resetting CSS style rules</h3> |
|
|
339 |
|
|
|
340 |
<p> |
|
|
341 |
When you want to remove a particular rule or style property from affecting |
|
|
342 |
the cascade, use <code>unset(selector,propert[y|ies])</code>. |
|
|
343 |
</p> |
|
|
344 |
|
|
|
345 |
<p> |
|
|
346 |
<code>unset(..)</code> can be called in any of the following ways, with the |
|
|
347 |
noted result: |
|
|
348 |
</p> |
|
|
349 |
|
|
|
350 |
<ul> |
|
|
351 |
<li> |
|
|
352 |
<code>unset('.foo')</code> — removes the rule associated to the |
|
|
353 |
selector entirely. |
|
|
354 |
</li> |
|
|
355 |
<li> |
|
|
356 |
<code>unset('.foo','font')</code> — unsets the <code>font</code> |
|
|
357 |
property and any child properties (e.g. |
|
|
358 |
'font-weight','font-variant','font-size','line-height', and |
|
|
359 |
'font-family'). If there are no set properties left, the rule is |
|
|
360 |
removed. |
|
|
361 |
</li> |
|
|
362 |
<li> |
|
|
363 |
<code>unset('.foo',['font','border',...])</code> — same as above, |
|
|
364 |
but the rule is modified only once with the final applicable |
|
|
365 |
<code>cssText</code>. |
|
|
366 |
</li> |
|
|
367 |
</ul> |
|
|
368 |
|
|
|
369 |
<p> |
|
|
370 |
It is important to note that there is a difference between setting a style |
|
|
371 |
property to its default value and unsetting it. The former can be achieved |
|
|
372 |
by calling <code>set(selector, { property: "auto" })</code> (or |
|
|
373 |
the respective default value for that property). |
|
|
374 |
</p> |
|
|
375 |
|
|
|
376 |
<p> |
|
|
377 |
However, as the CSS is reapplied to the page, the "auto" value |
|
|
378 |
will override any value for that property that may have cascaded in from |
|
|
379 |
another rule. This is different than removing the property assignment |
|
|
380 |
entirely, as this allows cascading values through. |
|
|
381 |
</p> |
|
|
382 |
|
|
|
383 |
<pre class="code prettyprint">Y.StyleSheet('MyApp').set('.foo', { background: 'auto' }); |
|
|
384 |
|
|
|
385 |
// is NOT the same as |
|
|
386 |
|
|
|
387 |
Y.StyleSheet('MyApp').unset('.foo','background');</pre> |
|
|
388 |
|
|
|
389 |
|
|
|
390 |
<h3 id="not_selector">A note on selector strings</h3> |
|
|
391 |
|
|
|
392 |
<p> |
|
|
393 |
Though the StyleSheet Utility takes selector strings as input to its API, |
|
|
394 |
it <em>does not</em> leverage the YUI selector engine. YUI's selector |
|
|
395 |
functionality supplements native CSS support for DOM access, but |
|
|
396 |
accomplishes this through efficient DOM traversal. Since the StyleSheet |
|
|
397 |
Utility uses the browser's built-in stylesheet and rule objects, it can not |
|
|
398 |
handle selectors that are not supported by the browser's native CSS |
|
|
399 |
parser. |
|
|
400 |
</p> |
|
|
401 |
|
|
|
402 |
<pre class="code prettyprint">// This will not cause a style change in IE 6, for example |
|
|
403 |
Y.StyleSheet('MyApp').set('input[type=checkbox]:checked', { |
|
|
404 |
verticalAlign : 'super' |
|
|
405 |
});</pre> |
|
|
406 |
|
|
|
407 |
|
|
|
408 |
<h3 id="disable">Disabling and enabling a StyleSheet</h3> |
|
|
409 |
|
|
|
410 |
<p> |
|
|
411 |
Disabling a StyleSheet effectively turns it off; no CSS from that |
|
|
412 |
stylesheet is applied to the page. Disabling a StyleSheet does not remove |
|
|
413 |
the host node from the page, and style can be reapplied by enabling the |
|
|
414 |
StyleSheet again. |
|
|
415 |
</p> |
|
|
416 |
|
|
|
417 |
<p> |
|
|
418 |
When StyleSheets are disabled, it is still possible to change their style |
|
|
419 |
rules via <code>set</code> and <code>unset</code>. |
|
|
420 |
</p> |
|
|
421 |
|
|
|
422 |
<pre class="code prettyprint">var sheet = Y.StyleSheet(styleNode); |
|
|
423 |
|
|
|
424 |
sheet.disable(); |
|
|
425 |
sheet.set('.foo', { backgroundColor: '#900', color: '#fff' }); |
|
|
426 |
sheet.set('.bar', { borderBottomColor: '#369' }); |
|
|
427 |
sheet.unset('.baz'); |
|
|
428 |
sheet.enable();</pre> |
|
|
429 |
|
|
|
430 |
|
|
|
431 |
<h3 id="static">Static methods</h3> |
|
|
432 |
|
|
|
433 |
<p> |
|
|
434 |
<code>Y.StyleSheet</code> exposes a few static methods. |
|
|
435 |
</p> |
|
|
436 |
|
|
|
437 |
<div class="apisummary"> |
|
|
438 |
<table> |
|
|
439 |
<thead> |
|
|
440 |
<tr> |
|
|
441 |
<th>Method</th> |
|
|
442 |
<th>Use for</th> |
|
|
443 |
</tr> |
|
|
444 |
</thead> |
|
|
445 |
<tbody> |
|
|
446 |
<tr> |
|
|
447 |
<td> |
|
|
448 |
<pre class="code prettyprint">Y.StyleSheet.register(instance, name)</pre> |
|
|
449 |
|
|
|
450 |
</td> |
|
|
451 |
<td>Use to assign a named registry entry for a StyleSheet instance.</td> |
|
|
452 |
</tr> |
|
|
453 |
<tr> |
|
|
454 |
<td> |
|
|
455 |
<pre class="code prettyprint">Y.StyleSheet.toCssText(property_obj, starting_cssText)</pre> |
|
|
456 |
|
|
|
457 |
</td> |
|
|
458 |
<td>Use to translate an object of style property:value pairs to a single <code>cssText</code> string. The optional second argument is a <code>cssText</code> string of a style's "before" state.</td> |
|
|
459 |
</tr> |
|
|
460 |
</tbody> |
|
|
461 |
</table> |
|
|
462 |
</div> |
|
|
463 |
|
|
|
464 |
<p> |
|
|
465 |
<code>Y.StyleSheet.toCssText</code> is used internally to assemble the |
|
|
466 |
<code>cssText</code> strings for updating the stylesheet rules. However, |
|
|
467 |
it may also be helpful for avoiding reflow overhead when substantially |
|
|
468 |
modifying a single element's style. |
|
|
469 |
</p> |
|
|
470 |
|
|
|
471 |
<pre class="code prettyprint">var el = Y.one('some_element'), |
|
|
472 |
changes = { color : '#684', fontWeight: 'bold', padding: '2em' }, |
|
|
473 |
currentStyle = el.getStyle('cssText'); |
|
|
474 |
|
|
|
475 |
el.setStyle('cssText', |
|
|
476 |
Y.StyleSheet.toCssText(changes, currentStyle));</pre> |
|
|
477 |
|
|
|
478 |
|
|
|
479 |
<h2 id="mechanism">How <code>Y.StyleSheet</code> works</h2> |
|
|
480 |
|
|
|
481 |
<p> |
|
|
482 |
Browsers grant access via the DOM API to stylesheets included in markup as |
|
|
483 |
<code><link></code> or <code><style></code> elements. Despite |
|
|
484 |
differing implementations across the browser spectrum, they all support |
|
|
485 |
adding, removing, and modifying CSS rules. |
|
|
486 |
</p> |
|
|
487 |
|
|
|
488 |
<p> |
|
|
489 |
CSS rules are comprised of a selector and collection of style |
|
|
490 |
property:value pairs enclosed in curly braces. |
|
|
491 |
</p> |
|
|
492 |
|
|
|
493 |
<pre class="code prettyprint">/* | This is a CSS rule | |
|
|
494 |
| selectorText | style properties | */ |
|
|
495 |
div.this-is a .rule { font-color: #f00; }</pre> |
|
|
496 |
|
|
|
497 |
|
|
|
498 |
<p> |
|
|
499 |
In JavaScript, each rule object has a <code>selectorText</code> property |
|
|
500 |
and a <code>style</code> property that operates in the same way as the |
|
|
501 |
<code>style</code> property on regular DOM elements, such as |
|
|
502 |
<code><p></code> or <code><strong></code> elements. |
|
|
503 |
</p> |
|
|
504 |
|
|
|
505 |
<p> |
|
|
506 |
Arguably the most valuable property of the style collection is |
|
|
507 |
<code>cssText</code> which corresponds to the serialized summary of |
|
|
508 |
property:value pairs applied by this collection (e.g. "font-size: 100%; |
|
|
509 |
color: #FF0000;"). The reason this property is important is that |
|
|
510 |
modifications to the string value will cause changes to repopulate the |
|
|
511 |
individual style properties <em>while only triggering a single repaint or |
|
|
512 |
reflow</em> by the browser. |
|
|
513 |
</p> |
|
|
514 |
|
|
|
515 |
<pre class="code prettyprint">var el = document.getElementById('some_element'); |
|
|
516 |
|
|
|
517 |
el.style.borderBottom = '3px solid #eee'; // reflow |
|
|
518 |
el.style.borderTop = '3px solid #ccc'; // another reflow |
|
|
519 |
el.style.fontWeight = 'bold'; // another reflow |
|
|
520 |
|
|
|
521 |
// Vs. three changes in one reflow |
|
|
522 |
el.style.cssText += '; border-bottom: 3px solid #eee; border-top: 3px solid #ccc; font-weight: bold';</pre> |
|
|
523 |
|
|
|
524 |
|
|
|
525 |
<p> |
|
|
526 |
<code>Y.StyleSheet</code> leverages this mechanism in addition to applying |
|
|
527 |
modifications at the CSS rule level rather than modifying each targeted DOM |
|
|
528 |
node directly. This means changing multiple style properties on multiple |
|
|
529 |
elements (that can be identified by a single selector) will only ever incur |
|
|
530 |
one repaint or reflow. |
|
|
531 |
</p> |
|
|
532 |
|
|
|
533 |
<p> |
|
|
534 |
It must be noted that all reflows are not the same. The scope of a reflow |
|
|
535 |
is greatly affected by what element triggered it. For example, changing a |
|
|
536 |
style of an absolutely positioned element will trigger a very limited |
|
|
537 |
reflow because the browser understands that not much <em>could</em> change |
|
|
538 |
as a result. Stylesheet modifications on the other hand are not tied to an |
|
|
539 |
element, but the page as a whole. The CSS cascade must be recalculated and |
|
|
540 |
applied, resulting in a full page reflow. This means it may be more |
|
|
541 |
efficient to individually update many elements than to change the |
|
|
542 |
stylesheet. |
|
|
543 |
</p> |
|
|
544 |
|
|
|
545 |
<h2 id="knownissues">Known Issues</h2> |
|
|
546 |
|
|
|
547 |
<p> |
|
|
548 |
<strong>Unable to set style values with |
|
|
549 |
<code>!important</code></strong>. |
|
|
550 |
</p> |
|
|
551 |
|
|
|
552 |
<p> |
|
|
553 |
CSS syntax for declaring that a style value has <code>important</code> |
|
|
554 |
priority is to include the <code>!important</code> flag after the |
|
|
555 |
value. |
|
|
556 |
</p> |
|
|
557 |
|
|
|
558 |
<pre class="code prettyprint">.some-class { |
|
|
559 |
color: #000 !important; |
|
|
560 |
}</pre> |
|
|
561 |
|
|
|
562 |
|
|
|
563 |
<p> |
|
|
564 |
However, the DOM API for modifying stylesheets does not parse out the |
|
|
565 |
<code>!important</code> flag from the assigned value string, and thus |
|
|
566 |
considers the entire string to be an invalid value. |
|
|
567 |
</p> |
|
|
568 |
|
|
|
569 |
<pre class="code prettyprint">el.style.color = "#000 !important"; // Error</pre> |
|
|
570 |
|
|
|
571 |
|
|
|
572 |
<p> |
|
|
573 |
StyleSheet will support <code>!important</code> in the value string in a |
|
|
574 |
future release, but for now the only way to assign an |
|
|
575 |
<code>!important</code> style is by creating a new StyleSheet, passing a |
|
|
576 |
CSS text string to the constructor. |
|
|
577 |
</p> |
|
|
578 |
|
|
|
579 |
<pre class="code prettyprint">var sheet = new Y.StyleSheet(); |
|
|
580 |
sheet.set(".foo", { color: "#000 !important" }); // FAIL |
|
|
581 |
|
|
|
582 |
new Y.StyleSheet(".foo { color: #000 !important; }"); // WORKS</pre> |
|
|
583 |
|
|
|
584 |
</div> |
|
|
585 |
</div> |
|
|
586 |
</div> |
|
|
587 |
|
|
|
588 |
<div class="yui3-u-1-4"> |
|
|
589 |
<div class="sidebar"> |
|
|
590 |
|
|
|
591 |
<div id="toc" class="sidebox"> |
|
|
592 |
<div class="hd"> |
|
|
593 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
594 |
</div> |
|
|
595 |
|
|
|
596 |
<div class="bd"> |
|
|
597 |
<ul class="toc"> |
|
|
598 |
<li> |
|
|
599 |
<a href="#getting-started">Getting Started</a> |
|
|
600 |
</li> |
|
|
601 |
<li> |
|
|
602 |
<a href="#using">Using the StyleSheet Utility</a> |
|
|
603 |
<ul class="toc"> |
|
|
604 |
<li> |
|
|
605 |
<a href="#instantiating">Instantiating a <code>Y.StyleSheet</code></a> |
|
|
606 |
</li> |
|
|
607 |
<li> |
|
|
608 |
<a href="#registry">Getting a StyleSheet by registered name</a> |
|
|
609 |
</li> |
|
|
610 |
<li> |
|
|
611 |
<a href="#first_param">Summary of how the constructor handles the first argument</a> |
|
|
612 |
</li> |
|
|
613 |
<li> |
|
|
614 |
<a href="#set">Creating and modifying CSS style rules</a> |
|
|
615 |
</li> |
|
|
616 |
<li> |
|
|
617 |
<a href="#normalized_properties">Some style properties are normalized</a> |
|
|
618 |
</li> |
|
|
619 |
<li> |
|
|
620 |
<a href="#unset">Removing and resetting CSS style rules</a> |
|
|
621 |
</li> |
|
|
622 |
<li> |
|
|
623 |
<a href="#not_selector">A note on selector strings</a> |
|
|
624 |
</li> |
|
|
625 |
<li> |
|
|
626 |
<a href="#disable">Disabling and enabling a StyleSheet</a> |
|
|
627 |
</li> |
|
|
628 |
<li> |
|
|
629 |
<a href="#static">Static methods</a> |
|
|
630 |
</li> |
|
|
631 |
</ul> |
|
|
632 |
</li> |
|
|
633 |
<li> |
|
|
634 |
<a href="#mechanism">How <code>Y.StyleSheet</code> works</a> |
|
|
635 |
</li> |
|
|
636 |
<li> |
|
|
637 |
<a href="#knownissues">Known Issues</a> |
|
|
638 |
</li> |
|
|
639 |
</ul> |
|
|
640 |
</div> |
|
|
641 |
</div> |
|
|
642 |
|
|
|
643 |
|
|
|
644 |
|
|
|
645 |
<div class="sidebox"> |
|
|
646 |
<div class="hd"> |
|
|
647 |
<h2 class="no-toc">Examples</h2> |
|
|
648 |
</div> |
|
|
649 |
|
|
|
650 |
<div class="bd"> |
|
|
651 |
<ul class="examples"> |
|
|
652 |
|
|
|
653 |
|
|
|
654 |
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input"> |
|
|
655 |
<a href="stylesheet-theme.html">Adjusting a Page Theme on the Fly</a> |
|
|
656 |
</li> |
|
|
657 |
|
|
|
658 |
|
|
|
659 |
|
|
|
660 |
|
|
|
661 |
</ul> |
|
|
662 |
</div> |
|
|
663 |
</div> |
|
|
664 |
|
|
|
665 |
|
|
|
666 |
|
|
|
667 |
<div class="sidebox"> |
|
|
668 |
<div class="hd"> |
|
|
669 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
670 |
</div> |
|
|
671 |
|
|
|
672 |
<div class="bd"> |
|
|
673 |
<ul class="examples"> |
|
|
674 |
|
|
|
675 |
|
|
|
676 |
|
|
|
677 |
|
|
|
678 |
<li data-description="Example Photo Browser application."> |
|
|
679 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
680 |
</li> |
|
|
681 |
|
|
|
682 |
|
|
|
683 |
</ul> |
|
|
684 |
</div> |
|
|
685 |
</div> |
|
|
686 |
|
|
|
687 |
</div> |
|
|
688 |
</div> |
|
|
689 |
</div> |
|
|
690 |
</div> |
|
|
691 |
|
|
|
692 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
693 |
<script>prettyPrint();</script> |
|
|
694 |
|
|
|
695 |
<script> |
|
|
696 |
YUI.Env.Tests = { |
|
|
697 |
examples: [], |
|
|
698 |
project: '../assets', |
|
|
699 |
assets: '../assets/stylesheet', |
|
|
700 |
name: 'stylesheet', |
|
|
701 |
title: 'StyleSheet', |
|
|
702 |
newWindow: '', |
|
|
703 |
auto: false |
|
|
704 |
}; |
|
|
705 |
YUI.Env.Tests.examples.push('stylesheet-theme'); |
|
|
706 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
707 |
|
|
|
708 |
</script> |
|
|
709 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
710 |
|
|
|
711 |
|
|
|
712 |
|
|
|
713 |
</body> |
|
|
714 |
</html> |