CSSStyleDeclaration.test.js
21.2 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
'use strict';
var { CSSStyleDeclaration } = require('./CSSStyleDeclaration');
var allProperties = require('./allProperties');
var allExtraProperties = require('./allExtraProperties');
var implementedProperties = require('./implementedProperties');
var parsers = require('./parsers');
var dashedProperties = [...allProperties, ...allExtraProperties];
var allowedProperties = dashedProperties.map(parsers.dashedToCamelCase);
implementedProperties = Array.from(implementedProperties).map(parsers.dashedToCamelCase);
var invalidProperties = implementedProperties.filter(prop => !allowedProperties.includes(prop));
describe('CSSStyleDeclaration', () => {
test('has only valid properties implemented', () => {
expect(invalidProperties.length).toEqual(0);
});
test('has all properties', () => {
var style = new CSSStyleDeclaration();
allProperties.forEach(property => {
expect(style.__lookupGetter__(property)).toBeTruthy();
expect(style.__lookupSetter__(property)).toBeTruthy();
});
});
test('has dashed properties', () => {
var style = new CSSStyleDeclaration();
dashedProperties.forEach(property => {
expect(style.__lookupGetter__(property)).toBeTruthy();
expect(style.__lookupSetter__(property)).toBeTruthy();
});
});
test('has all functions', () => {
var style = new CSSStyleDeclaration();
expect(typeof style.item).toEqual('function');
expect(typeof style.getPropertyValue).toEqual('function');
expect(typeof style.setProperty).toEqual('function');
expect(typeof style.getPropertyPriority).toEqual('function');
expect(typeof style.removeProperty).toEqual('function');
// TODO - deprecated according to MDN and not implemented at all, can we remove?
expect(typeof style.getPropertyCSSValue).toEqual('function');
});
test('has special properties', () => {
var style = new CSSStyleDeclaration();
expect(style.__lookupGetter__('cssText')).toBeTruthy();
expect(style.__lookupSetter__('cssText')).toBeTruthy();
expect(style.__lookupGetter__('length')).toBeTruthy();
expect(style.__lookupSetter__('length')).toBeTruthy();
expect(style.__lookupGetter__('parentRule')).toBeTruthy();
});
test('from style string', () => {
var style = new CSSStyleDeclaration();
style.cssText = 'color: blue; background-color: red; width: 78%; height: 50vh;';
expect(style.length).toEqual(4);
expect(style.cssText).toEqual('color: blue; background-color: red; width: 78%; height: 50vh;');
expect(style.getPropertyValue('color')).toEqual('blue');
expect(style.item(0)).toEqual('color');
expect(style[1]).toEqual('background-color');
expect(style.backgroundColor).toEqual('red');
style.cssText = '';
expect(style.cssText).toEqual('');
expect(style.length).toEqual(0);
});
test('from properties', () => {
var style = new CSSStyleDeclaration();
style.color = 'blue';
expect(style.length).toEqual(1);
expect(style[0]).toEqual('color');
expect(style.cssText).toEqual('color: blue;');
expect(style.item(0)).toEqual('color');
expect(style.color).toEqual('blue');
style.backgroundColor = 'red';
expect(style.length).toEqual(2);
expect(style[0]).toEqual('color');
expect(style[1]).toEqual('background-color');
expect(style.cssText).toEqual('color: blue; background-color: red;');
expect(style.backgroundColor).toEqual('red');
style.removeProperty('color');
expect(style[0]).toEqual('background-color');
});
test('shorthand properties', () => {
var style = new CSSStyleDeclaration();
style.background = 'blue url(http://www.example.com/some_img.jpg)';
expect(style.backgroundColor).toEqual('blue');
expect(style.backgroundImage).toEqual('url(http://www.example.com/some_img.jpg)');
expect(style.background).toEqual('blue url(http://www.example.com/some_img.jpg)');
style.border = '0 solid black';
expect(style.borderWidth).toEqual('0px');
expect(style.borderStyle).toEqual('solid');
expect(style.borderColor).toEqual('black');
expect(style.borderTopWidth).toEqual('0px');
expect(style.borderLeftStyle).toEqual('solid');
expect(style.borderBottomColor).toEqual('black');
style.font = '12em monospace';
expect(style.fontSize).toEqual('12em');
expect(style.fontFamily).toEqual('monospace');
});
test('width and height properties and null and empty strings', () => {
var style = new CSSStyleDeclaration();
style.height = 6;
expect(style.height).toEqual('');
style.width = 0;
expect(style.width).toEqual('0px');
style.height = '34%';
expect(style.height).toEqual('34%');
style.height = '100vh';
expect(style.height).toEqual('100vh');
style.height = '100vw';
expect(style.height).toEqual('100vw');
style.height = '';
expect(1).toEqual(style.length);
expect(style.cssText).toEqual('width: 0px;');
style.width = null;
expect(0).toEqual(style.length);
expect(style.cssText).toEqual('');
});
test('implicit properties', () => {
var style = new CSSStyleDeclaration();
style.borderWidth = 0;
expect(style.length).toEqual(1);
expect(style.borderWidth).toEqual('0px');
expect(style.borderTopWidth).toEqual('0px');
expect(style.borderBottomWidth).toEqual('0px');
expect(style.borderLeftWidth).toEqual('0px');
expect(style.borderRightWidth).toEqual('0px');
expect(style.cssText).toEqual('border-width: 0px;');
});
test('top, left, right, bottom properties', () => {
var style = new CSSStyleDeclaration();
style.top = 0;
style.left = '0%';
style.right = '5em';
style.bottom = '12pt';
expect(style.top).toEqual('0px');
expect(style.left).toEqual('0%');
expect(style.right).toEqual('5em');
expect(style.bottom).toEqual('12pt');
expect(style.length).toEqual(4);
expect(style.cssText).toEqual('top: 0px; left: 0%; right: 5em; bottom: 12pt;');
});
test('clear and clip properties', () => {
var style = new CSSStyleDeclaration();
style.clear = 'none';
expect(style.clear).toEqual('none');
style.clear = 'lfet';
expect(style.clear).toEqual('none');
style.clear = 'left';
expect(style.clear).toEqual('left');
style.clear = 'right';
expect(style.clear).toEqual('right');
style.clear = 'both';
expect(style.clear).toEqual('both');
style.clip = 'elipse(5px, 10px)';
expect(style.clip).toEqual('');
expect(style.length).toEqual(1);
style.clip = 'rect(0, 3Em, 2pt, 50%)';
expect(style.clip).toEqual('rect(0px, 3em, 2pt, 50%)');
expect(style.length).toEqual(2);
expect(style.cssText).toEqual('clear: both; clip: rect(0px, 3em, 2pt, 50%);');
});
test('colors', () => {
var style = new CSSStyleDeclaration();
style.color = 'rgba(0,0,0,0)';
expect(style.color).toEqual('rgba(0, 0, 0, 0)');
style.color = 'rgba(5%, 10%, 20%, 0.4)';
expect(style.color).toEqual('rgba(12, 25, 51, 0.4)');
style.color = 'rgb(33%, 34%, 33%)';
expect(style.color).toEqual('rgb(84, 86, 84)');
style.color = 'rgba(300, 200, 100, 1.5)';
expect(style.color).toEqual('rgb(255, 200, 100)');
style.color = 'hsla(0, 1%, 2%, 0.5)';
expect(style.color).toEqual('rgba(5, 5, 5, 0.5)');
style.color = 'hsl(0, 1%, 2%)';
expect(style.color).toEqual('rgb(5, 5, 5)');
style.color = 'rebeccapurple';
expect(style.color).toEqual('rebeccapurple');
style.color = 'transparent';
expect(style.color).toEqual('transparent');
style.color = 'currentcolor';
expect(style.color).toEqual('currentcolor');
style.color = '#ffffffff';
expect(style.color).toEqual('rgba(255, 255, 255, 1)');
style.color = '#fffa';
expect(style.color).toEqual('rgba(255, 255, 255, 0.667)');
style.color = '#ffffff66';
expect(style.color).toEqual('rgba(255, 255, 255, 0.4)');
});
test('short hand properties with embedded spaces', () => {
var style = new CSSStyleDeclaration();
style.background = 'rgb(0, 0, 0) url(/something/somewhere.jpg)';
expect(style.backgroundColor).toEqual('rgb(0, 0, 0)');
expect(style.backgroundImage).toEqual('url(/something/somewhere.jpg)');
expect(style.cssText).toEqual('background: rgb(0, 0, 0) url(/something/somewhere.jpg);');
style = new CSSStyleDeclaration();
style.border = ' 1px solid black ';
expect(style.border).toEqual('1px solid black');
});
test('setting shorthand properties to an empty string should clear all dependent properties', () => {
var style = new CSSStyleDeclaration();
style.borderWidth = '1px';
expect(style.cssText).toEqual('border-width: 1px;');
style.border = '';
expect(style.cssText).toEqual('');
});
test('setting implicit properties to an empty string should clear all dependent properties', () => {
var style = new CSSStyleDeclaration();
style.borderTopWidth = '1px';
expect(style.cssText).toEqual('border-top-width: 1px;');
style.borderWidth = '';
expect(style.cssText).toEqual('');
});
test('setting a shorthand property, whose shorthands are implicit properties, to an empty string should clear all dependent properties', () => {
var style = new CSSStyleDeclaration();
style.borderTopWidth = '1px';
expect(style.cssText).toEqual('border-top-width: 1px;');
style.border = '';
expect(style.cssText).toEqual('');
style.borderTop = '1px solid black';
expect(style.cssText).toEqual('border-top: 1px solid black;');
style.border = '';
expect(style.cssText).toEqual('');
});
test('setting border values to "none" should clear dependent values', () => {
var style = new CSSStyleDeclaration();
style.borderTopWidth = '1px';
expect(style.cssText).toEqual('border-top-width: 1px;');
style.border = 'none';
expect(style.cssText).toEqual('');
style.borderTopWidth = '1px';
expect(style.cssText).toEqual('border-top-width: 1px;');
style.borderTopStyle = 'none';
expect(style.cssText).toEqual('');
style.borderTopWidth = '1px';
expect(style.cssText).toEqual('border-top-width: 1px;');
style.borderTop = 'none';
expect(style.cssText).toEqual('');
style.borderTopWidth = '1px';
style.borderLeftWidth = '1px';
expect(style.cssText).toEqual('border-top-width: 1px; border-left-width: 1px;');
style.borderTop = 'none';
expect(style.cssText).toEqual('border-left-width: 1px;');
});
test('setting border to 0 should be okay', () => {
var style = new CSSStyleDeclaration();
style.border = 0;
expect(style.cssText).toEqual('border: 0px;');
});
test('setting values implicit and shorthand properties via csstext and setproperty should propagate to dependent properties', () => {
var style = new CSSStyleDeclaration();
style.cssText = 'border: 1px solid black;';
expect(style.cssText).toEqual('border: 1px solid black;');
expect(style.borderTop).toEqual('1px solid black');
style.border = '';
expect(style.cssText).toEqual('');
style.setProperty('border', '1px solid black');
expect(style.cssText).toEqual('border: 1px solid black;');
});
test('setting opacity should work', () => {
var style = new CSSStyleDeclaration();
style.setProperty('opacity', 0.75);
expect(style.cssText).toEqual('opacity: 0.75;');
style.opacity = '0.50';
expect(style.cssText).toEqual('opacity: 0.5;');
style.opacity = 1;
expect(style.cssText).toEqual('opacity: 1;');
});
test('width and height of auto should work', () => {
var style = new CSSStyleDeclaration();
style.width = 'auto';
expect(style.cssText).toEqual('width: auto;');
expect(style.width).toEqual('auto');
style = new CSSStyleDeclaration();
style.height = 'auto';
expect(style.cssText).toEqual('height: auto;');
expect(style.height).toEqual('auto');
});
test('padding and margin should set/clear shorthand properties', () => {
var style = new CSSStyleDeclaration();
var parts = ['Top', 'Right', 'Bottom', 'Left'];
var testParts = function(name, v, V) {
style[name] = v;
for (var i = 0; i < 4; i++) {
var part = name + parts[i];
expect(style[part]).toEqual(V[i]);
}
expect(style[name]).toEqual(v);
style[name] = '';
};
testParts('padding', '1px', ['1px', '1px', '1px', '1px']);
testParts('padding', '1px 2%', ['1px', '2%', '1px', '2%']);
testParts('padding', '1px 2px 3px', ['1px', '2px', '3px', '2px']);
testParts('padding', '1px 2px 3px 4px', ['1px', '2px', '3px', '4px']);
style.paddingTop = style.paddingRight = style.paddingBottom = style.paddingLeft = '1px';
testParts('padding', '', ['', '', '', '']);
testParts('margin', '1px', ['1px', '1px', '1px', '1px']);
testParts('margin', '1px auto', ['1px', 'auto', '1px', 'auto']);
testParts('margin', '1px 2% 3px', ['1px', '2%', '3px', '2%']);
testParts('margin', '1px 2px 3px 4px', ['1px', '2px', '3px', '4px']);
style.marginTop = style.marginRight = style.marginBottom = style.marginLeft = '1px';
testParts('margin', '', ['', '', '', '']);
});
test('padding and margin shorthands should set main properties', () => {
var style = new CSSStyleDeclaration();
var parts = ['Top', 'Right', 'Bottom', 'Left'];
var testParts = function(name, v, V) {
var expected;
for (var i = 0; i < 4; i++) {
style[name] = v;
style[name + parts[i]] = V;
expected = v.split(/ /);
expected[i] = V;
expected = expected.join(' ');
expect(style[name]).toEqual(expected);
}
};
testParts('padding', '1px 2px 3px 4px', '10px');
testParts('margin', '1px 2px 3px 4px', '10px');
testParts('margin', '1px 2px 3px 4px', 'auto');
});
test('setting a value to 0 should return the string value', () => {
var style = new CSSStyleDeclaration();
style.setProperty('fill-opacity', 0);
expect(style.fillOpacity).toEqual('0');
});
test('onchange callback should be called when the csstext changes', () => {
var style = new CSSStyleDeclaration(function(cssText) {
expect(cssText).toEqual('opacity: 0;');
});
style.setProperty('opacity', 0);
});
test('setting float should work the same as cssfloat', () => {
var style = new CSSStyleDeclaration();
style.float = 'left';
expect(style.cssFloat).toEqual('left');
});
test('setting improper css to csstext should not throw', () => {
var style = new CSSStyleDeclaration();
style.cssText = 'color: ';
expect(style.cssText).toEqual('');
style.color = 'black';
style.cssText = 'float: ';
expect(style.cssText).toEqual('');
});
test('url parsing works with quotes', () => {
var style = new CSSStyleDeclaration();
style.backgroundImage = 'url(http://some/url/here1.png)';
expect(style.backgroundImage).toEqual('url(http://some/url/here1.png)');
style.backgroundImage = "url('http://some/url/here2.png')";
expect(style.backgroundImage).toEqual('url(http://some/url/here2.png)');
style.backgroundImage = 'url("http://some/url/here3.png")';
expect(style.backgroundImage).toEqual('url(http://some/url/here3.png)');
});
test('setting 0 to a padding or margin works', () => {
var style = new CSSStyleDeclaration();
style.padding = 0;
expect(style.cssText).toEqual('padding: 0px;');
style.margin = '1em';
style.marginTop = '0';
expect(style.marginTop).toEqual('0px');
});
test('setting ex units to a padding or margin works', () => {
var style = new CSSStyleDeclaration();
style.padding = '1ex';
expect(style.cssText).toEqual('padding: 1ex;');
style.margin = '1em';
style.marginTop = '0.5ex';
expect(style.marginTop).toEqual('0.5ex');
});
test('setting null to background works', () => {
var style = new CSSStyleDeclaration();
style.background = 'red';
expect(style.cssText).toEqual('background: red;');
style.background = null;
expect(style.cssText).toEqual('');
});
test('flex properties should keep their values', () => {
var style = new CSSStyleDeclaration();
style.flexDirection = 'column';
expect(style.cssText).toEqual('flex-direction: column;');
style.flexDirection = 'row';
expect(style.cssText).toEqual('flex-direction: row;');
});
test('camelcase properties are not assigned with `.setproperty()`', () => {
var style = new CSSStyleDeclaration();
style.setProperty('fontSize', '12px');
expect(style.cssText).toEqual('');
});
test('casing is ignored in `.setproperty()`', () => {
var style = new CSSStyleDeclaration();
style.setProperty('FoNt-SiZe', '12px');
expect(style.fontSize).toEqual('12px');
expect(style.getPropertyValue('font-size')).toEqual('12px');
});
test('support non string entries in border-spacing', () => {
var style = new CSSStyleDeclaration();
style.borderSpacing = 0;
expect(style.cssText).toEqual('border-spacing: 0px;');
});
test('float should be valid property for `.setproperty()`', () => {
var style = new CSSStyleDeclaration();
style.setProperty('float', 'left');
expect(style.float).toEqual('left');
expect(style.getPropertyValue('float')).toEqual('left');
});
test('flex-shrink works', () => {
var style = new CSSStyleDeclaration();
style.setProperty('flex-shrink', 0);
expect(style.getPropertyValue('flex-shrink')).toEqual('0');
style.setProperty('flex-shrink', 1);
expect(style.getPropertyValue('flex-shrink')).toEqual('1');
expect(style.cssText).toEqual('flex-shrink: 1;');
});
test('flex-grow works', () => {
var style = new CSSStyleDeclaration();
style.setProperty('flex-grow', 2);
expect(style.getPropertyValue('flex-grow')).toEqual('2');
expect(style.cssText).toEqual('flex-grow: 2;');
});
test('flex-basis works', () => {
var style = new CSSStyleDeclaration();
style.setProperty('flex-basis', 0);
expect(style.getPropertyValue('flex-basis')).toEqual('0px');
style.setProperty('flex-basis', '250px');
expect(style.getPropertyValue('flex-basis')).toEqual('250px');
style.setProperty('flex-basis', '10em');
expect(style.getPropertyValue('flex-basis')).toEqual('10em');
style.setProperty('flex-basis', '30%');
expect(style.getPropertyValue('flex-basis')).toEqual('30%');
expect(style.cssText).toEqual('flex-basis: 30%;');
});
test('shorthand flex works', () => {
var style = new CSSStyleDeclaration();
style.setProperty('flex', 'none');
expect(style.getPropertyValue('flex-grow')).toEqual('0');
expect(style.getPropertyValue('flex-shrink')).toEqual('0');
expect(style.getPropertyValue('flex-basis')).toEqual('auto');
style.removeProperty('flex');
style.removeProperty('flex-basis');
style.setProperty('flex', 'auto');
expect(style.getPropertyValue('flex-grow')).toEqual('');
expect(style.getPropertyValue('flex-shrink')).toEqual('');
expect(style.getPropertyValue('flex-basis')).toEqual('auto');
style.removeProperty('flex');
style.setProperty('flex', '0 1 250px');
expect(style.getPropertyValue('flex')).toEqual('0 1 250px');
expect(style.getPropertyValue('flex-grow')).toEqual('0');
expect(style.getPropertyValue('flex-shrink')).toEqual('1');
expect(style.getPropertyValue('flex-basis')).toEqual('250px');
style.removeProperty('flex');
style.setProperty('flex', '2');
expect(style.getPropertyValue('flex-grow')).toEqual('2');
expect(style.getPropertyValue('flex-shrink')).toEqual('');
expect(style.getPropertyValue('flex-basis')).toEqual('');
style.removeProperty('flex');
style.setProperty('flex', '20%');
expect(style.getPropertyValue('flex-grow')).toEqual('');
expect(style.getPropertyValue('flex-shrink')).toEqual('');
expect(style.getPropertyValue('flex-basis')).toEqual('20%');
style.removeProperty('flex');
style.setProperty('flex', '2 2');
expect(style.getPropertyValue('flex-grow')).toEqual('2');
expect(style.getPropertyValue('flex-shrink')).toEqual('2');
expect(style.getPropertyValue('flex-basis')).toEqual('');
style.removeProperty('flex');
});
test('font-size get a valid value', () => {
var style = new CSSStyleDeclaration();
const invalidValue = '1r5px';
style.cssText = 'font-size: 15px';
expect(1).toEqual(style.length);
style.cssText = `font-size: ${invalidValue}`;
expect(0).toEqual(style.length);
expect(undefined).toEqual(style[0]);
});
test('getPropertyValue for custom properties in cssText', () => {
const style = new CSSStyleDeclaration();
style.cssText = '--foo: red';
expect(style.getPropertyValue('--foo')).toEqual('red');
});
test('getPropertyValue for custom properties with setProperty', () => {
const style = new CSSStyleDeclaration();
style.setProperty('--bar', 'blue');
expect(style.getPropertyValue('--bar')).toEqual('blue');
});
test('getPropertyValue for custom properties with object setter', () => {
const style = new CSSStyleDeclaration();
style['--baz'] = 'yellow';
expect(style.getPropertyValue('--baz')).toEqual('');
});
test('custom properties are case-sensitive', () => {
const style = new CSSStyleDeclaration();
style.cssText = '--fOo: purple';
expect(style.getPropertyValue('--foo')).toEqual('');
expect(style.getPropertyValue('--fOo')).toEqual('purple');
});
test('supports calc', () => {
const style = new CSSStyleDeclaration();
style.setProperty('width', 'calc(100% - 100px)');
expect(style.getPropertyValue('width')).toEqual('calc(100% - 100px)');
});
});