1
YUI.add('stylesheet-tests', function(Y) {
3
// Set up the document for testing
5
// May need setAttribute?
6
//Y.one("link:not([href^=http]),link[href=^http://localhost/]").set('id', 'locallink');
7
Y.one("link").set('id', 'locallink');
8
if (!Y.one("#testbed")) {
9
Y.one("body").append('<div id="testbed"></div>');
15
StyleSheet = Y.StyleSheet,
16
suite = new Y.Test.Suite("Y.StyleSheet"),
18
testbed = d.getElementById('testbed'),
19
style = Y.Node.create('<style type="text/css" id="styleblock"></style>'),
20
cssText = 'h1 { font: normal 125%/1.4 Arial, sans-serif; }';
22
if (!Y.one("#styleblock")) {
23
Y.one("head").append(style);
25
if (style._node.styleSheet) {
26
style._node.styleSheet.cssText = cssText;
28
style.append(cssText);
32
StyleAssert.normalizeColor = function (c) {
34
replace(/#([0-9a-f])([0-9a-f])([0-9a-f])(?![0-9a-f])/i,'#$1$1$2$2$3$3').
35
replace(/#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})(?![0-9a-f])/i,
37
return "rgb("+parseInt(r,16)+", "+
42
StyleAssert.areEqual = function (a,b,msg) {
43
var aa = StyleAssert.normalizeColor(a),
44
bb = StyleAssert.normalizeColor(b);
46
if (aa === 'bold') { aa = '700'; }
47
if (bb === 'bold') { bb = '700'; }
50
throw new Assert.ComparisonFailure(
51
Assert._formatMessage(msg, "Values should be equal."), a, b);
55
Dom.add = function (el,tag,conf) {
56
var child = d.createElement(tag);
58
Y.mix(child,conf,true);
60
return el.appendChild(child);
62
Dom.getNodeCount = function (tag,root) {
63
return (root||d).getElementsByTagName(tag||'*').length;
65
Dom.getSheet = function (yuid) {
66
var nodes = d.getElementsByTagName('style'),
69
for (i = nodes.length - 1; i >= 0; --i) {
70
if (Y.stamp(nodes[i]) === yuid) {
71
return (nodes[i].sheet || nodes[i].styleSheet);
76
suite.add(new Y.Test.Case({
77
name : "Test <style> node creation",
80
this.styleNodeCount = Dom.getNodeCount('style');
81
this.linkNodeCount = Dom.getNodeCount('link');
83
this.testNode = Dom.add(testbed,'div',{id:'target'});
86
tearDown : function () {
87
testbed.innerHTML = '';
90
test_createNew : function () {
93
Assert.areSame(this.styleNodeCount + 1, Dom.getNodeCount('style'));
96
test_createFromExistingStyle : function () {
97
Y.StyleSheet('styleblock');
99
Assert.areSame(this.styleNodeCount, Dom.getNodeCount('style'));
102
test_createFromExistingLink : function () {
103
Y.StyleSheet('locallink');
105
Assert.areSame(this.styleNodeCount, Dom.getNodeCount('style'),"style");
106
Assert.areSame(this.linkNodeCount, Dom.getNodeCount('link'),"link");
109
test_createEntireSheet : function () {
110
Y.StyleSheet("#target { font-weight: bold; }");
112
Assert.areSame(this.styleNodeCount + 1, Dom.getNodeCount('style'));
114
StyleAssert.areEqual('bold',Y.DOM.getStyle(this.testNode,'fontWeight'));
117
test_gettingFromCache : function () {
119
var a = new StyleSheet('test'),
120
b = new StyleSheet('test');
122
Assert.areSame(this.styleNodeCount, Dom.getNodeCount('style'));
123
Assert.areSame(a,b,"From cache by name");
126
b = new StyleSheet(a.getId());
128
Assert.areSame(this.styleNodeCount, Dom.getNodeCount('style'));
129
Assert.areSame(a,b,"From cache by yuiSSID");
132
a = new StyleSheet(d.getElementById('styleblock'));
133
b = new StyleSheet('styleblock');
135
Assert.areSame(this.styleNodeCount, Dom.getNodeCount('style'));
136
Assert.areSame(a,b,"From cache by node vs id");
140
suite.add(new Y.Test.Case({
141
name : "Test xdomain stylesheet access",
143
setUp : function () {
144
this.remoteLink = Dom.add(
145
d.getElementsByTagName('head')[0],'link',{
148
href : 'http://yui.yahooapis.com/2.6.0/build/base/base-min.css'
152
tearDown : function () {
153
this.remoteLink.parentNode.removeChild(this.remoteLink);
158
"StyleSheet seeded with remote link should fail" : true,
159
"getCssText on a remote StyleSheet should throw an error" : true,
160
"set(..) on a remote StyleSheet should throw an error" : true,
161
"disabling a remote StyleSheet should throw an error" : true
165
"StyleSheet seeded with remote link should fail" : function () {
166
// Each line should throw an exception
167
Y.StyleSheet(this.remoteLink);
169
Y.log("StyleSheet creation allowed from remote file", "warn", "TestRunner");
170
throw Error("This is an informative test only");
173
"getCssText on a remote StyleSheet should throw an error" : function () {
174
// Each line should throw an exception
175
var sheet = Y.StyleSheet(this.remoteLink);
179
Y.log("Getting cssText of a remote StyleSheet allowed", "warn", "TestRunner");
180
throw Error("This is an informative test only");
183
"set(..) on a remote StyleSheet should throw an error" : function () {
184
// Each line should throw an exception
185
var sheet = Y.StyleSheet(this.remoteLink);
187
sheet.set('#target', { color: '#f00' });
189
Y.log("Creating rules in a remote StyleSheet allowed", "warn", "TestRunner");
190
throw Error("This is an informative test only");
193
"disabling a remote StyleSheet should throw an error" : function () {
194
// Each line should throw an exception
195
var sheet = Y.StyleSheet(this.remoteLink);
199
Y.log("Disabling a remote StyleSheet allowed", "warn", "TestRunner");
200
throw Error("This is an informative test only");
204
suite.add(new Y.Test.Case({
209
test_important: 2528707 // bug
213
setUp : function () {
214
this.stylesheet = new StyleSheet('test');
216
this.testNode = Dom.add(testbed,'div',{
218
innerHTML:'<p>1</p><p>2</p><pre>pre</pre>'
221
tearDown : function () {
222
testbed.innerHTML = '';
223
this.stylesheet.unset('#target');
224
this.stylesheet.unset('#target p');
225
this.stylesheet.unset('#target pre');
226
// This should be unnecessary, but for the sake of cleanliness...
227
this.stylesheet.unset('#target, #target p, #target pre');
230
test_addSimpleSelector : function () {
231
this.stylesheet.set('#target',{
233
backgroundColor : '#eef',
234
border : '1px solid #ccc'
237
StyleAssert.areEqual('#123456',
238
Y.DOM.getStyle(this.testNode,'color'),
240
StyleAssert.areEqual('#eef',
241
Y.DOM.getStyle(this.testNode,'backgroundColor'),
243
StyleAssert.areEqual('#ccc',
244
Y.DOM.getStyle(this.testNode,'borderLeftColor'),
248
test_addRuleWithInvalidValue : function () {
249
// This would throw an exception in IE if anywhere
250
this.stylesheet.set('#target .foo .bar', { color : 'invalid-value' });
253
test_descendantSelector : function () {
254
var before = Y.DOM.getStyle(
255
testbed.getElementsByTagName('pre')[0],'textAlign');
257
this.stylesheet.set('#target p', { textAlign: 'right' });
259
StyleAssert.areEqual('right',
261
testbed.getElementsByTagName('p')[0],'textAlign'),
262
"#target p { text-align: right; }");
264
StyleAssert.areEqual(before,
266
testbed.getElementsByTagName('pre')[0],'textAlign'),
267
"#target pre should not be set (maybe auto/inherit?)");
270
test_setCommaSelector : function () {
271
var sheet = Dom.getSheet(this.stylesheet.getId());
274
Assert.fail("Could not find this StyleSheet's node or sheet");
277
this.stylesheet.set('#target, #target p, #target pre', {
281
Assert.areEqual(3,(sheet.cssRules || sheet.rules).length, "Comma selector split failure");
283
StyleAssert.areEqual('16px', Y.DOM.getStyle(this.testNode,'paddingLeft'));
284
StyleAssert.areEqual('16px',
286
testbed.getElementsByTagName('p')[0],'paddingLeft'),
288
StyleAssert.areEqual('16px',
290
testbed.getElementsByTagName('pre')[0],'paddingLeft'),
294
test_important: function () {
295
var target = Y.one('#target'),
296
sheet = Dom.getSheet(this.stylesheet.getId()),
297
original = target.get('offsetHeight');
300
Assert.fail("Could not find this StyleSheet's node or sheet");
303
this.stylesheet.set('#target p', {
304
paddingBottom: '10px !important'
307
Assert.areEqual(1,(sheet.cssRules || sheet.rules).length, "!important rule not added to the sheet");
309
Assert.areNotEqual(original, target.get('offsetHeight'));
311
Assert.fail(); // remove when the bug is fixed
315
suite.add(new Y.Test.Case({
316
name : "Test Enable/Disable sheet",
318
setUp : function () {
319
this.stylesheet = new StyleSheet('test');
321
this.stylesheet.enable();
323
this.testNode = Dom.add(testbed,'div',{id:'target'});
326
color : Y.DOM.getStyle(this.testNode,'color'),
327
backgroundColor : Y.DOM.getStyle(this.testNode,'backgroundColor'),
328
borderLeftColor : Y.DOM.getStyle(this.testNode,'borderLeftColor')
333
tearDown : function () {
334
testbed.innerHTML = '';
335
this.stylesheet.enable();
336
this.stylesheet.unset('#target');
337
this.stylesheet.unset('#target p');
340
test_disableSheet : function () {
341
this.stylesheet.set('#target',{
343
backgroundColor : '#eef',
344
border : '1px solid #ccc'
347
StyleAssert.areEqual('#123456',
348
Y.DOM.getStyle(this.testNode,'color'),
350
StyleAssert.areEqual('#eef',
351
Y.DOM.getStyle(this.testNode,'backgroundColor'),
352
"backgroundColor (enabled)");
353
StyleAssert.areEqual('#ccc',
354
Y.DOM.getStyle(this.testNode,'borderLeftColor'),
357
this.stylesheet.disable();
359
StyleAssert.areEqual(this.before.color,
360
Y.DOM.getStyle(this.testNode,'color'),
362
StyleAssert.areEqual(this.before.backgroundColor,
363
Y.DOM.getStyle(this.testNode,'backgroundColor'),
364
"backgroundColor (disabled)");
365
StyleAssert.areEqual(this.before.borderLeftColor,
366
Y.DOM.getStyle(this.testNode,'borderLeftColor'),
367
"border (disabled)");
370
test_enableSheet : function () {
371
this.stylesheet.disable();
373
this.stylesheet.set('#target',{
375
backgroundColor : '#eef',
376
border : '1px solid #ccc'
379
StyleAssert.areEqual(this.before.color,
380
Y.DOM.getStyle(this.testNode,'color'),
382
StyleAssert.areEqual(this.before.backgroundColor,
383
Y.DOM.getStyle(this.testNode,'backgroundColor'),
384
"backgroundColor (disabled)");
385
StyleAssert.areEqual(this.before.borderLeftColor,
386
Y.DOM.getStyle(this.testNode,'borderLeftColor'),
387
"border (disabled)");
389
this.stylesheet.enable();
391
StyleAssert.areEqual('#123456',
392
Y.DOM.getStyle(this.testNode,'color'),
394
StyleAssert.areEqual('#eef',
395
Y.DOM.getStyle(this.testNode,'backgroundColor'),
396
"backgroundColor (enabled)");
397
StyleAssert.areEqual('#ccc',
398
Y.DOM.getStyle(this.testNode,'borderLeftColor'),
403
suite.add(new Y.Test.Case({
406
setUp : function () {
407
this.stylesheet = new StyleSheet('test');
409
this.testNode = Dom.add(testbed,'div',{
411
innerHTML:'<p>1</p><p>2</p><pre>pre</pre>'
415
color : Y.DOM.getStyle(this.testNode,'color'),
416
backgroundColor : Y.DOM.getStyle(this.testNode,'backgroundColor'),
417
borderLeftColor : Y.DOM.getStyle(this.testNode,'borderLeftColor'),
418
textAlign : Y.DOM.getStyle(this.testNode,'textAlign')
422
tearDown : function () {
423
testbed.innerHTML = '';
424
this.stylesheet.unset('#target');
425
this.stylesheet.unset('#target p');
426
this.stylesheet.unset('#target pre');
427
// This should be unnecessary, but for the sake of cleanliness...
428
this.stylesheet.unset('#target, #target p, #target pre');
431
test_unset : function () {
432
this.stylesheet.set('#target',{
434
backgroundColor : '#eef',
435
border : '1px solid #ccc'
438
StyleAssert.areEqual('#f00',
439
Y.DOM.getStyle(this.testNode,'color'),
440
"color (before unset)");
441
StyleAssert.areEqual('#eef',
442
Y.DOM.getStyle(this.testNode,'backgroundColor'),
443
"backgroundColor (before unset)");
444
StyleAssert.areEqual('#ccc',
445
Y.DOM.getStyle(this.testNode,'borderLeftColor'),
446
"border (before unset)");
448
this.stylesheet.unset('#target', 'color');
450
StyleAssert.areEqual(this.before.color,
451
Y.DOM.getStyle(this.testNode,'color'),
452
"color (after unset)");
454
this.stylesheet.unset('#target', ['backgroundColor','border']);
456
StyleAssert.areEqual(this.before.backgroundColor,
457
Y.DOM.getStyle(this.testNode,'backgroundColor'),
458
"backgroundColor (after unset)");
459
StyleAssert.areEqual(this.before.borderLeftColor,
460
Y.DOM.getStyle(this.testNode,'borderLeftColor'),
461
"border (after unset)");
464
test_removeRule : function () {
465
this.stylesheet.set('#target', { textAlign: 'right' });
467
StyleAssert.areEqual('right',
468
Y.DOM.getStyle(this.testNode,'textAlign'),
469
"#target { text-align: right; }");
471
this.stylesheet.unset('#target');
472
StyleAssert.areEqual(this.before.textAlign,
473
Y.DOM.getStyle(this.testNode,'textAlign'),
474
"#target text-align still in place");
477
test_unsetCommaSelector : function () {
478
var p = this.testNode.getElementsByTagName('p')[0],
479
pre = this.testNode.getElementsByTagName('pre')[0],
482
Y.DOM.getStyle(this.testNode,'paddingLeft'),
483
Y.DOM.getStyle(p,'paddingLeft'),
484
Y.DOM.getStyle(pre,'paddingLeft')
487
Y.DOM.getStyle(this.testNode,'marginRight'),
488
Y.DOM.getStyle(p,'marginRight'),
489
Y.DOM.getStyle(pre,'marginRight')
493
sheet = Dom.getSheet(this.stylesheet.getId());
496
Assert.fail("Could not find this StyleSheet's node or sheet");
499
this.stylesheet.set('#target, #target p, #target pre', {
505
Assert.areEqual(3,(sheet.cssRules || sheet.rules).length,
506
"Comma selector split failure");
509
this.stylesheet.unset('#target, #target p, #target pre','paddingLeft');
512
Y.DOM.getStyle(this.testNode,'paddingLeft'),
513
Y.DOM.getStyle(p,'paddingLeft'),
514
Y.DOM.getStyle(pre,'paddingLeft')
517
Assert.areEqual(3,(sheet.cssRules || sheet.rules).length,
518
"Should still be 3 rules");
520
Y.ArrayAssert.itemsAreEqual(before.paddingLeft,after);
523
Y.DOM.getStyle(this.testNode,'marginRight'),
524
Y.DOM.getStyle(p,'marginRight'),
525
Y.DOM.getStyle(pre,'marginRight')
527
Y.ArrayAssert.itemsAreEqual(['30px','30px','30px'],after);
530
test_removeCommaSelector : function () {
531
var /*p = this.testNode.getElementsByTagName('p')[0],
532
pre = this.testNode.getElementsByTagName('pre')[0],
535
Y.DOM.getStyle(this.testNode,'paddingLeft'),
536
Y.DOM.getStyle(p,'paddingLeft'),
537
Y.DOM.getStyle(pre,'paddingLeft')
541
sheet = Dom.getSheet(this.stylesheet.getId());
544
Assert.fail("Could not capture this StyleSheet's node or sheet");
547
this.stylesheet.set('#target, #target p, #target pre', {
551
Assert.areEqual(3,(sheet.cssRules || sheet.rules).length,
552
"Comma selector split failure");
554
this.stylesheet.unset('#target, #target pre','paddingLeft');
556
Assert.areEqual(1,(sheet.cssRules || sheet.rules).length);
560
suite.add(new Y.Test.Case({
561
name : "Test getCssText",
569
setUp : function () {
570
this.stylesheet = new StyleSheet('test');
572
this.testNode = Dom.add(testbed,'div',{
574
innerHTML:'<p>1</p><p>2</p><pre>pre</pre>'
577
this.stylesheet.set('#target, #target p', {
581
tearDown : function () {
582
testbed.innerHTML = '';
583
this.stylesheet.unset('#target');
584
this.stylesheet.unset('#target p');
587
test_getRuleCSS : function () {
588
var css = this.stylesheet.getCssText('#target p');
589
Y.log(css, 'info','TestLogger');
590
Assert.isString(css);
591
Assert.areSame(true, /padding/i.test(css));
594
test_getSheetCSS : function () {
595
var css = this.stylesheet.getCssText();
597
Y.log(css, 'info','TestLogger');
599
Assert.isString(css);
600
Assert.areSame(true, /padding/i.test(css));
601
Assert.areSame(true, /#target/i.test(css));
602
Assert.areSame(true, /#target\s+p\s+\{/i.test(css));
605
test_important: function () {
606
this.stylesheet.set('#target p', {
607
paddingBottom: '10px !important'
610
var css = this.stylesheet.getCssText();
612
if (/important/i.test(css)) {
613
Y.log("!important not found in cssText", "warn", "TestRunner");
616
Assert.fail(); // remove when the bug is fixed
620
suite.add(new Y.Test.Case({
621
name : "Test float/opacity",
623
setUp : function () {
624
this.stylesheet = new StyleSheet('test');
626
if (!d.getElementById('target')) {
627
this.testNode = Dom.add(testbed,'div',{
629
innerHTML:'<p id="p1">1</p><p id="p2">2</p><p id="p3">3</p>'
634
test_float : function () {
635
var p1 = Y.DOM.byId('p1'),
636
p2 = Y.DOM.byId('p2'),
637
p3 = Y.DOM.byId('p3');
639
this.stylesheet.set('#target',{
647
border: '5px solid #ccc',
651
.set('#p1',{ 'float': 'left' })
652
.set('#p2',{ cssFloat: 'left' })
653
.set('#p3',{ styleFloat: 'left' });
655
Assert.areEqual('left', Y.DOM.getStyle(p1,'float'));
656
Assert.areEqual('left', Y.DOM.getStyle(p2,'float'));
657
Assert.areEqual('left', Y.DOM.getStyle(p3,'float'));
660
test_opacity : function () {
661
var p1 = Y.DOM.byId('p1'),
662
p2 = Y.DOM.byId('p2'),
663
p3 = Y.DOM.byId('p3');
665
this.stylesheet.set('#p1',{ opacity: 0.5 }).
666
set('#p2',{ opacity: ".2" }).
667
set('#p3',{ opacity: 1 });
669
Assert.areEqual(0.5,Y.DOM.getStyle(p1,'opacity'));
670
Assert.areEqual(0.2,Y.DOM.getStyle(p2,'opacity'));
671
Assert.areEqual(1,Y.DOM.getStyle(p3,'opacity'));
675
suite.add(new Y.Test.Case({
676
name: "Testbed Cleanup",
678
testbedCleanup: function () {
679
Y.all('#testbed,style').remove(true);
683
Y.Test.Runner.add(suite);
686
}, '@VERSION@' ,{requires:['stylesheet', 'test']});