cssstyle 4.0.1 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,737 +0,0 @@
1
- 'use strict';
2
-
3
- var { CSSStyleDeclaration } = require('./CSSStyleDeclaration');
4
-
5
- var allProperties = require('./allProperties');
6
- var allExtraProperties = require('./allExtraProperties');
7
- var implementedProperties = require('./implementedProperties');
8
- var parsers = require('./parsers');
9
-
10
- var dashedProperties = [...allProperties, ...allExtraProperties];
11
- var allowedProperties = dashedProperties.map(parsers.dashedToCamelCase);
12
- implementedProperties = Array.from(implementedProperties).map(parsers.dashedToCamelCase);
13
- var invalidProperties = implementedProperties.filter((prop) => !allowedProperties.includes(prop));
14
-
15
- describe('CSSStyleDeclaration', () => {
16
- test('has only valid properties implemented', () => {
17
- expect(invalidProperties.length).toEqual(0);
18
- });
19
-
20
- test('has all properties', () => {
21
- var style = new CSSStyleDeclaration();
22
- allProperties.forEach((property) => {
23
- expect(style.__lookupGetter__(property)).toBeTruthy();
24
- expect(style.__lookupSetter__(property)).toBeTruthy();
25
- });
26
- });
27
-
28
- test('has dashed properties', () => {
29
- var style = new CSSStyleDeclaration();
30
- dashedProperties.forEach((property) => {
31
- expect(style.__lookupGetter__(property)).toBeTruthy();
32
- expect(style.__lookupSetter__(property)).toBeTruthy();
33
- });
34
- });
35
-
36
- test('has all functions', () => {
37
- var style = new CSSStyleDeclaration();
38
-
39
- expect(typeof style.item).toEqual('function');
40
- expect(typeof style.getPropertyValue).toEqual('function');
41
- expect(typeof style.setProperty).toEqual('function');
42
- expect(typeof style.getPropertyPriority).toEqual('function');
43
- expect(typeof style.removeProperty).toEqual('function');
44
-
45
- // TODO - deprecated according to MDN and not implemented at all, can we remove?
46
- expect(typeof style.getPropertyCSSValue).toEqual('function');
47
- });
48
-
49
- test('has special properties', () => {
50
- var style = new CSSStyleDeclaration();
51
-
52
- expect(style.__lookupGetter__('cssText')).toBeTruthy();
53
- expect(style.__lookupSetter__('cssText')).toBeTruthy();
54
- expect(style.__lookupGetter__('length')).toBeTruthy();
55
- expect(style.__lookupSetter__('length')).toBeTruthy();
56
- expect(style.__lookupGetter__('parentRule')).toBeTruthy();
57
- });
58
-
59
- test('from style string', () => {
60
- var style = new CSSStyleDeclaration();
61
- style.cssText = 'color: blue; background-color: red; width: 78%; height: 50vh;';
62
- expect(style.length).toEqual(4);
63
- expect(style.cssText).toEqual('color: blue; background-color: red; width: 78%; height: 50vh;');
64
- expect(style.getPropertyValue('color')).toEqual('blue');
65
- expect(style.item(0)).toEqual('color');
66
- expect(style[1]).toEqual('background-color');
67
- expect(style.backgroundColor).toEqual('red');
68
- style.cssText = '';
69
- expect(style.cssText).toEqual('');
70
- expect(style.length).toEqual(0);
71
- });
72
-
73
- test('from properties', () => {
74
- var style = new CSSStyleDeclaration();
75
- style.color = 'blue';
76
- expect(style.length).toEqual(1);
77
- expect(style[0]).toEqual('color');
78
- expect(style.cssText).toEqual('color: blue;');
79
- expect(style.item(0)).toEqual('color');
80
- expect(style.color).toEqual('blue');
81
- style.backgroundColor = 'red';
82
- expect(style.length).toEqual(2);
83
- expect(style[0]).toEqual('color');
84
- expect(style[1]).toEqual('background-color');
85
- expect(style.cssText).toEqual('color: blue; background-color: red;');
86
- expect(style.backgroundColor).toEqual('red');
87
- style.removeProperty('color');
88
- expect(style[0]).toEqual('background-color');
89
- });
90
-
91
- test('shorthand properties', () => {
92
- var style = new CSSStyleDeclaration();
93
- style.background = 'blue url(http://www.example.com/some_img.jpg)';
94
- expect(style.backgroundColor).toEqual('blue');
95
- expect(style.backgroundImage).toEqual('url(http://www.example.com/some_img.jpg)');
96
- expect(style.background).toEqual('blue url(http://www.example.com/some_img.jpg)');
97
- style.border = '0 solid black';
98
- expect(style.borderWidth).toEqual('0px');
99
- expect(style.borderStyle).toEqual('solid');
100
- expect(style.borderColor).toEqual('black');
101
- expect(style.borderTopWidth).toEqual('0px');
102
- expect(style.borderLeftStyle).toEqual('solid');
103
- expect(style.borderBottomColor).toEqual('black');
104
- style.font = '12em monospace';
105
- expect(style.fontSize).toEqual('12em');
106
- expect(style.fontFamily).toEqual('monospace');
107
- });
108
-
109
- test('width and height properties and null and empty strings', () => {
110
- var style = new CSSStyleDeclaration();
111
- style.height = 6;
112
- expect(style.height).toEqual('');
113
- style.width = 0;
114
- expect(style.width).toEqual('0px');
115
- style.height = '34%';
116
- expect(style.height).toEqual('34%');
117
- style.height = '100vh';
118
- expect(style.height).toEqual('100vh');
119
- style.height = '100vw';
120
- expect(style.height).toEqual('100vw');
121
- style.height = '';
122
- expect(1).toEqual(style.length);
123
- expect(style.cssText).toEqual('width: 0px;');
124
- style.width = null;
125
- expect(0).toEqual(style.length);
126
- expect(style.cssText).toEqual('');
127
- });
128
-
129
- test('implicit properties', () => {
130
- var style = new CSSStyleDeclaration();
131
- style.borderWidth = 0;
132
- expect(style.length).toEqual(1);
133
- expect(style.borderWidth).toEqual('0px');
134
- expect(style.borderTopWidth).toEqual('0px');
135
- expect(style.borderBottomWidth).toEqual('0px');
136
- expect(style.borderLeftWidth).toEqual('0px');
137
- expect(style.borderRightWidth).toEqual('0px');
138
- expect(style.cssText).toEqual('border-width: 0px;');
139
- });
140
-
141
- test('top, left, right, bottom properties', () => {
142
- var style = new CSSStyleDeclaration();
143
- style.top = 0;
144
- style.left = '0%';
145
- style.right = '5em';
146
- style.bottom = '12pt';
147
- expect(style.top).toEqual('0px');
148
- expect(style.left).toEqual('0%');
149
- expect(style.right).toEqual('5em');
150
- expect(style.bottom).toEqual('12pt');
151
- expect(style.length).toEqual(4);
152
- expect(style.cssText).toEqual('top: 0px; left: 0%; right: 5em; bottom: 12pt;');
153
- });
154
-
155
- test('clear and clip properties', () => {
156
- var style = new CSSStyleDeclaration();
157
- style.clear = 'none';
158
- expect(style.clear).toEqual('none');
159
- style.clear = 'lfet';
160
- expect(style.clear).toEqual('none');
161
- style.clear = 'left';
162
- expect(style.clear).toEqual('left');
163
- style.clear = 'right';
164
- expect(style.clear).toEqual('right');
165
- style.clear = 'both';
166
- expect(style.clear).toEqual('both');
167
- style.clip = 'elipse(5px, 10px)';
168
- expect(style.clip).toEqual('');
169
- expect(style.length).toEqual(1);
170
- style.clip = 'rect(0, 3Em, 2pt, 50%)';
171
- expect(style.clip).toEqual('rect(0px, 3em, 2pt, 50%)');
172
- expect(style.length).toEqual(2);
173
- expect(style.cssText).toEqual('clear: both; clip: rect(0px, 3em, 2pt, 50%);');
174
- });
175
-
176
- test('colors', () => {
177
- var style = new CSSStyleDeclaration();
178
- style.color = 'rgba(0,0,0,0)';
179
- expect(style.color).toEqual('rgba(0, 0, 0, 0)');
180
- style.color = 'rgba(5%, 10%, 20%, 0.4)';
181
- expect(style.color).toEqual('rgba(12, 25, 51, 0.4)');
182
- style.color = 'rgb(33%, 34%, 33%)';
183
- expect(style.color).toEqual('rgb(84, 86, 84)');
184
- style.color = 'rgba(300, 200, 100, 1.5)';
185
- expect(style.color).toEqual('rgb(255, 200, 100)');
186
- style.color = 'hsla(0, 1%, 2%, 0.5)';
187
- expect(style.color).toEqual('rgba(5, 5, 5, 0.5)');
188
- style.color = 'hsl(0, 1%, 2%)';
189
- expect(style.color).toEqual('rgb(5, 5, 5)');
190
- style.color = 'rebeccapurple';
191
- expect(style.color).toEqual('rebeccapurple');
192
- style.color = 'transparent';
193
- expect(style.color).toEqual('transparent');
194
- style.color = 'currentcolor';
195
- expect(style.color).toEqual('currentcolor');
196
- style.color = '#ffffffff';
197
- expect(style.color).toEqual('rgba(255, 255, 255, 1)');
198
- style.color = '#fffa';
199
- expect(style.color).toEqual('rgba(255, 255, 255, 0.667)');
200
- style.color = '#ffffff66';
201
- expect(style.color).toEqual('rgba(255, 255, 255, 0.4)');
202
- });
203
-
204
- test('short hand properties with embedded spaces', () => {
205
- var style = new CSSStyleDeclaration();
206
- style.background = 'rgb(0, 0, 0) url(/something/somewhere.jpg)';
207
- expect(style.backgroundColor).toEqual('rgb(0, 0, 0)');
208
- expect(style.backgroundImage).toEqual('url(/something/somewhere.jpg)');
209
- expect(style.cssText).toEqual('background: rgb(0, 0, 0) url(/something/somewhere.jpg);');
210
- style = new CSSStyleDeclaration();
211
- style.border = ' 1px solid black ';
212
- expect(style.border).toEqual('1px solid black');
213
- });
214
-
215
- test('setting shorthand properties to an empty string should clear all dependent properties', () => {
216
- var style = new CSSStyleDeclaration();
217
- style.borderWidth = '1px';
218
- expect(style.cssText).toEqual('border-width: 1px;');
219
- style.border = '';
220
- expect(style.cssText).toEqual('');
221
- });
222
-
223
- test('setting implicit properties to an empty string should clear all dependent properties', () => {
224
- var style = new CSSStyleDeclaration();
225
- style.borderTopWidth = '1px';
226
- expect(style.cssText).toEqual('border-top-width: 1px;');
227
- style.borderWidth = '';
228
- expect(style.cssText).toEqual('');
229
- });
230
-
231
- test('setting a shorthand property, whose shorthands are implicit properties, to an empty string should clear all dependent properties', () => {
232
- var style = new CSSStyleDeclaration();
233
- style.borderTopWidth = '1px';
234
- expect(style.cssText).toEqual('border-top-width: 1px;');
235
- style.border = '';
236
- expect(style.cssText).toEqual('');
237
- style.borderTop = '1px solid black';
238
- expect(style.cssText).toEqual('border-top: 1px solid black;');
239
- style.border = '';
240
- expect(style.cssText).toEqual('');
241
- });
242
-
243
- test('setting border values to "none" should clear dependent values', () => {
244
- var style = new CSSStyleDeclaration();
245
- style.borderTopWidth = '1px';
246
- expect(style.cssText).toEqual('border-top-width: 1px;');
247
- style.border = 'none';
248
- expect(style.cssText).toEqual('');
249
- style.borderTopWidth = '1px';
250
- expect(style.cssText).toEqual('border-top-width: 1px;');
251
- style.borderTopStyle = 'none';
252
- expect(style.cssText).toEqual('');
253
- style.borderTopWidth = '1px';
254
- expect(style.cssText).toEqual('border-top-width: 1px;');
255
- style.borderTop = 'none';
256
- expect(style.cssText).toEqual('');
257
- style.borderTopWidth = '1px';
258
- style.borderLeftWidth = '1px';
259
- expect(style.cssText).toEqual('border-top-width: 1px; border-left-width: 1px;');
260
- style.borderTop = 'none';
261
- expect(style.cssText).toEqual('border-left-width: 1px;');
262
- });
263
-
264
- test('setting border to 0 should be okay', () => {
265
- var style = new CSSStyleDeclaration();
266
- style.border = 0;
267
- expect(style.cssText).toEqual('border: 0px;');
268
- });
269
-
270
- test('setting values implicit and shorthand properties via csstext and setproperty should propagate to dependent properties', () => {
271
- var style = new CSSStyleDeclaration();
272
- style.cssText = 'border: 1px solid black;';
273
- expect(style.cssText).toEqual('border: 1px solid black;');
274
- expect(style.borderTop).toEqual('1px solid black');
275
- style.border = '';
276
- expect(style.cssText).toEqual('');
277
- style.setProperty('border', '1px solid black');
278
- expect(style.cssText).toEqual('border: 1px solid black;');
279
- });
280
-
281
- test('setting opacity should work', () => {
282
- var style = new CSSStyleDeclaration();
283
- style.setProperty('opacity', 0.75);
284
- expect(style.cssText).toEqual('opacity: 0.75;');
285
- style.opacity = '0.50';
286
- expect(style.cssText).toEqual('opacity: 0.5;');
287
- style.opacity = 1;
288
- expect(style.cssText).toEqual('opacity: 1;');
289
- });
290
-
291
- test('width and height of auto should work', () => {
292
- var style = new CSSStyleDeclaration();
293
- style.width = 'auto';
294
- expect(style.cssText).toEqual('width: auto;');
295
- expect(style.width).toEqual('auto');
296
- style = new CSSStyleDeclaration();
297
- style.height = 'auto';
298
- expect(style.cssText).toEqual('height: auto;');
299
- expect(style.height).toEqual('auto');
300
- });
301
-
302
- test('padding and margin should set/clear shorthand properties', () => {
303
- var style = new CSSStyleDeclaration();
304
- var parts = ['Top', 'Right', 'Bottom', 'Left'];
305
- var testParts = function (name, v, V) {
306
- style[name] = v;
307
- for (var i = 0; i < 4; i++) {
308
- var part = name + parts[i];
309
- expect(style[part]).toEqual(V[i]);
310
- }
311
-
312
- expect(style[name]).toEqual(v);
313
- style[name] = '';
314
- };
315
- testParts('padding', '1px', ['1px', '1px', '1px', '1px']);
316
- testParts('padding', '1px 2%', ['1px', '2%', '1px', '2%']);
317
- testParts('padding', '1px 2px 3px', ['1px', '2px', '3px', '2px']);
318
- testParts('padding', '1px 2px 3px 4px', ['1px', '2px', '3px', '4px']);
319
- style.paddingTop = style.paddingRight = style.paddingBottom = style.paddingLeft = '1px';
320
- testParts('padding', '', ['', '', '', '']);
321
- testParts('margin', '1px', ['1px', '1px', '1px', '1px']);
322
- testParts('margin', '1px auto', ['1px', 'auto', '1px', 'auto']);
323
- testParts('margin', '1px 2% 3px', ['1px', '2%', '3px', '2%']);
324
- testParts('margin', '1px 2px 3px 4px', ['1px', '2px', '3px', '4px']);
325
- style.marginTop = style.marginRight = style.marginBottom = style.marginLeft = '1px';
326
- testParts('margin', '', ['', '', '', '']);
327
- });
328
-
329
- test('padding and margin shorthands should set main properties', () => {
330
- var style = new CSSStyleDeclaration();
331
- var parts = ['Top', 'Right', 'Bottom', 'Left'];
332
- var testParts = function (name, v, V) {
333
- var expected;
334
- for (var i = 0; i < 4; i++) {
335
- style[name] = v;
336
- style[name + parts[i]] = V;
337
- expected = v.split(/ /);
338
- expected[i] = V;
339
- expected = expected.join(' ');
340
-
341
- expect(style[name]).toEqual(expected);
342
- }
343
- };
344
- testParts('padding', '1px 2px 3px 4px', '10px');
345
- testParts('margin', '1px 2px 3px 4px', '10px');
346
- testParts('margin', '1px 2px 3px 4px', 'auto');
347
- });
348
-
349
- test('setting individual padding and margin properties to an empty string should clear them', () => {
350
- var style = new CSSStyleDeclaration();
351
-
352
- var properties = ['padding', 'margin'];
353
- var parts = ['Top', 'Right', 'Bottom', 'Left'];
354
- for (var i = 0; i < properties.length; i++) {
355
- for (var j = 0; j < parts.length; j++) {
356
- var property = properties[i] + parts[j];
357
- style[property] = '12px';
358
- expect(style[property]).toEqual('12px');
359
-
360
- style[property] = '';
361
- expect(style[property]).toEqual('');
362
- }
363
- }
364
- });
365
-
366
- test('removing and setting individual margin properties updates the combined property accordingly', () => {
367
- var style = new CSSStyleDeclaration();
368
- style.margin = '1px 2px 3px 4px';
369
-
370
- style.marginTop = '';
371
- expect(style.margin).toEqual('');
372
- expect(style.marginRight).toEqual('2px');
373
- expect(style.marginBottom).toEqual('3px');
374
- expect(style.marginLeft).toEqual('4px');
375
-
376
- style.marginBottom = '';
377
- expect(style.margin).toEqual('');
378
- expect(style.marginRight).toEqual('2px');
379
- expect(style.marginLeft).toEqual('4px');
380
-
381
- style.marginBottom = '5px';
382
- expect(style.margin).toEqual('');
383
- expect(style.marginRight).toEqual('2px');
384
- expect(style.marginBottom).toEqual('5px');
385
- expect(style.marginLeft).toEqual('4px');
386
-
387
- style.marginTop = '6px';
388
- expect(style.cssText).toEqual('margin: 6px 2px 5px 4px;');
389
- });
390
-
391
- test.each(['padding', 'margin'])(
392
- 'removing an individual %s property should remove the combined property and replace it with the remaining individual ones',
393
- (property) => {
394
- var style = new CSSStyleDeclaration();
395
- var parts = ['Top', 'Right', 'Bottom', 'Left'];
396
- var partValues = ['1px', '2px', '3px', '4px'];
397
-
398
- for (var j = 0; j < parts.length; j++) {
399
- var partToRemove = parts[j];
400
- style[property] = partValues.join(' ');
401
- style[property + partToRemove] = '';
402
-
403
- // Main property should have been removed
404
- expect(style[property]).toEqual('');
405
-
406
- // Expect other parts to still be there
407
- for (var k = 0; k < parts.length; k++) {
408
- var propertyCss = property + '-' + parts[k].toLowerCase() + ': ' + partValues[k] + ';';
409
- if (k === j) {
410
- expect(style[property + parts[k]]).toEqual('');
411
- expect(style.cssText).not.toContain(propertyCss);
412
- } else {
413
- expect(style[property + parts[k]]).toEqual(partValues[k]);
414
- expect(style.cssText).toContain(propertyCss);
415
- }
416
- }
417
- }
418
- }
419
- );
420
-
421
- test.each(['margin', 'padding'])(
422
- 'setting additional %s properties keeps important status of others',
423
- (property) => {
424
- var style = new CSSStyleDeclaration();
425
- var importantProperty = property + '-top: 3px !important;';
426
- style.cssText = importantProperty;
427
- expect(style.cssText).toContain(importantProperty);
428
-
429
- style[property + 'Right'] = '4px';
430
- style[property + 'Bottom'] = '5px';
431
- style[property + 'Left'] = '6px';
432
-
433
- expect(style.cssText).toContain(importantProperty);
434
- expect(style.cssText).toContain(property + '-right: 4px;');
435
- expect(style.cssText).toContain(property + '-bottom: 5px;');
436
- expect(style.cssText).toContain(property + '-left: 6px;');
437
- expect(style.cssText).not.toContain('margin:');
438
- }
439
- );
440
-
441
- test.each(['margin', 'padding'])(
442
- 'setting individual %s keeps important status of others',
443
- (property) => {
444
- var style = new CSSStyleDeclaration();
445
- style.cssText = property + ': 3px !important;';
446
-
447
- style[property + 'Top'] = '4px';
448
-
449
- expect(style.cssText).toContain(property + '-top: 4px;');
450
- expect(style.cssText).toContain(property + '-right: 3px !important;');
451
- expect(style.cssText).toContain(property + '-bottom: 3px !important;');
452
- expect(style.cssText).toContain(property + '-left: 3px !important;');
453
- expect(style.cssText).not.toContain('margin:');
454
- }
455
- );
456
-
457
- test('setting a value to 0 should return the string value', () => {
458
- var style = new CSSStyleDeclaration();
459
- style.setProperty('fill-opacity', 0);
460
- expect(style.fillOpacity).toEqual('0');
461
- });
462
-
463
- test('onchange callback should be called when the csstext changes', () => {
464
- var called = 0;
465
- var style = new CSSStyleDeclaration(function (cssText) {
466
- called++;
467
- expect(cssText).toEqual('opacity: 0;');
468
- });
469
- style.cssText = 'opacity: 0;';
470
- expect(called).toEqual(1);
471
- style.cssText = 'opacity: 0;';
472
- expect(called).toEqual(2);
473
- });
474
-
475
- test('onchange callback should be called only once when multiple properties were added', () => {
476
- var called = 0;
477
- var style = new CSSStyleDeclaration(function (cssText) {
478
- called++;
479
- expect(cssText).toEqual('width: 100px; height: 100px;');
480
- });
481
- style.cssText = 'width: 100px;height:100px;';
482
- expect(called).toEqual(1);
483
- });
484
-
485
- test('onchange callback should not be called when property is set to the same value', () => {
486
- var called = 0;
487
- var style = new CSSStyleDeclaration(function () {
488
- called++;
489
- });
490
-
491
- style.setProperty('opacity', 0);
492
- expect(called).toEqual(1);
493
- style.setProperty('opacity', 0);
494
- expect(called).toEqual(1);
495
- });
496
-
497
- test('onchange callback should not be called when removeProperty was called on non-existing property', () => {
498
- var called = 0;
499
- var style = new CSSStyleDeclaration(function () {
500
- called++;
501
- });
502
- style.removeProperty('opacity');
503
- expect(called).toEqual(0);
504
- });
505
-
506
- test('setting float should work the same as cssfloat', () => {
507
- var style = new CSSStyleDeclaration();
508
- style.float = 'left';
509
- expect(style.cssFloat).toEqual('left');
510
- });
511
-
512
- test('setting improper css to csstext should not throw', () => {
513
- var style = new CSSStyleDeclaration();
514
- style.cssText = 'color: ';
515
- expect(style.cssText).toEqual('');
516
- style.color = 'black';
517
- style.cssText = 'float: ';
518
- expect(style.cssText).toEqual('');
519
- });
520
-
521
- test('url parsing works with quotes', () => {
522
- var style = new CSSStyleDeclaration();
523
- style.backgroundImage = 'url(http://some/url/here1.png)';
524
- expect(style.backgroundImage).toEqual('url(http://some/url/here1.png)');
525
- style.backgroundImage = "url('http://some/url/here2.png')";
526
- expect(style.backgroundImage).toEqual('url(http://some/url/here2.png)');
527
- style.backgroundImage = 'url("http://some/url/here3.png")';
528
- expect(style.backgroundImage).toEqual('url(http://some/url/here3.png)');
529
- });
530
-
531
- test('setting 0 to a padding or margin works', () => {
532
- var style = new CSSStyleDeclaration();
533
- style.padding = 0;
534
- expect(style.cssText).toEqual('padding: 0px;');
535
- style.margin = '1em';
536
- style.marginTop = '0';
537
- expect(style.marginTop).toEqual('0px');
538
- });
539
-
540
- test('setting ex units to a padding or margin works', () => {
541
- var style = new CSSStyleDeclaration();
542
- style.padding = '1ex';
543
- expect(style.cssText).toEqual('padding: 1ex;');
544
- style.margin = '1em';
545
- style.marginTop = '0.5ex';
546
- expect(style.marginTop).toEqual('0.5ex');
547
- });
548
-
549
- test('setting empty string and null to a padding or margin works', () => {
550
- var style = new CSSStyleDeclaration();
551
- var parts = ['Top', 'Right', 'Bottom', 'Left'];
552
- function testParts(base, nullValue) {
553
- var props = [base].concat(parts.map((part) => base + part));
554
- for (let prop of props) {
555
- expect(style[prop]).toEqual('');
556
- style[prop] = '10px';
557
- expect(style[prop]).toEqual('10px');
558
- style[prop] = nullValue;
559
- expect(style[prop]).toEqual('');
560
- }
561
- }
562
-
563
- testParts('margin', '');
564
- testParts('margin', null);
565
- testParts('padding', '');
566
- testParts('padding', null);
567
- });
568
-
569
- test('setting undefined to a padding or margin does nothing', () => {
570
- var style = new CSSStyleDeclaration();
571
- var parts = ['Top', 'Right', 'Bottom', 'Left'];
572
- function testParts(base) {
573
- var props = [base].concat(parts.map((part) => base + part));
574
- for (let prop of props) {
575
- style[prop] = '10px';
576
- expect(style[prop]).toEqual('10px');
577
- style[prop] = undefined;
578
- expect(style[prop]).toEqual('10px');
579
- }
580
- }
581
-
582
- testParts('margin');
583
- testParts('padding');
584
- });
585
-
586
- test('setting null to background works', () => {
587
- var style = new CSSStyleDeclaration();
588
- style.background = 'red';
589
- expect(style.cssText).toEqual('background: red;');
590
- style.background = null;
591
- expect(style.cssText).toEqual('');
592
- });
593
-
594
- test('flex properties should keep their values', () => {
595
- var style = new CSSStyleDeclaration();
596
- style.flexDirection = 'column';
597
- expect(style.cssText).toEqual('flex-direction: column;');
598
- style.flexDirection = 'row';
599
- expect(style.cssText).toEqual('flex-direction: row;');
600
- });
601
-
602
- test('camelcase properties are not assigned with `.setproperty()`', () => {
603
- var style = new CSSStyleDeclaration();
604
- style.setProperty('fontSize', '12px');
605
- expect(style.cssText).toEqual('');
606
- });
607
-
608
- test('casing is ignored in `.setproperty()`', () => {
609
- var style = new CSSStyleDeclaration();
610
- style.setProperty('FoNt-SiZe', '12px');
611
- expect(style.fontSize).toEqual('12px');
612
- expect(style.getPropertyValue('font-size')).toEqual('12px');
613
- });
614
-
615
- test('support non string entries in border-spacing', () => {
616
- var style = new CSSStyleDeclaration();
617
- style.borderSpacing = 0;
618
- expect(style.cssText).toEqual('border-spacing: 0px;');
619
- });
620
-
621
- test('float should be valid property for `.setproperty()`', () => {
622
- var style = new CSSStyleDeclaration();
623
- style.setProperty('float', 'left');
624
- expect(style.float).toEqual('left');
625
- expect(style.getPropertyValue('float')).toEqual('left');
626
- });
627
-
628
- test('flex-shrink works', () => {
629
- var style = new CSSStyleDeclaration();
630
- style.setProperty('flex-shrink', 0);
631
- expect(style.getPropertyValue('flex-shrink')).toEqual('0');
632
- style.setProperty('flex-shrink', 1);
633
- expect(style.getPropertyValue('flex-shrink')).toEqual('1');
634
- expect(style.cssText).toEqual('flex-shrink: 1;');
635
- });
636
-
637
- test('flex-grow works', () => {
638
- var style = new CSSStyleDeclaration();
639
- style.setProperty('flex-grow', 2);
640
- expect(style.getPropertyValue('flex-grow')).toEqual('2');
641
- expect(style.cssText).toEqual('flex-grow: 2;');
642
- });
643
-
644
- test('flex-basis works', () => {
645
- var style = new CSSStyleDeclaration();
646
- style.setProperty('flex-basis', 0);
647
- expect(style.getPropertyValue('flex-basis')).toEqual('0px');
648
- style.setProperty('flex-basis', '250px');
649
- expect(style.getPropertyValue('flex-basis')).toEqual('250px');
650
- style.setProperty('flex-basis', '10em');
651
- expect(style.getPropertyValue('flex-basis')).toEqual('10em');
652
- style.setProperty('flex-basis', '30%');
653
- expect(style.getPropertyValue('flex-basis')).toEqual('30%');
654
- expect(style.cssText).toEqual('flex-basis: 30%;');
655
- });
656
-
657
- test('shorthand flex works', () => {
658
- var style = new CSSStyleDeclaration();
659
- style.setProperty('flex', 'none');
660
- expect(style.getPropertyValue('flex-grow')).toEqual('0');
661
- expect(style.getPropertyValue('flex-shrink')).toEqual('0');
662
- expect(style.getPropertyValue('flex-basis')).toEqual('auto');
663
- style.removeProperty('flex');
664
- style.removeProperty('flex-basis');
665
- style.setProperty('flex', 'auto');
666
- expect(style.getPropertyValue('flex-grow')).toEqual('');
667
- expect(style.getPropertyValue('flex-shrink')).toEqual('');
668
- expect(style.getPropertyValue('flex-basis')).toEqual('auto');
669
- style.removeProperty('flex');
670
- style.setProperty('flex', '0 1 250px');
671
- expect(style.getPropertyValue('flex')).toEqual('0 1 250px');
672
- expect(style.getPropertyValue('flex-grow')).toEqual('0');
673
- expect(style.getPropertyValue('flex-shrink')).toEqual('1');
674
- expect(style.getPropertyValue('flex-basis')).toEqual('250px');
675
- style.removeProperty('flex');
676
- style.setProperty('flex', '2');
677
- expect(style.getPropertyValue('flex-grow')).toEqual('2');
678
- expect(style.getPropertyValue('flex-shrink')).toEqual('');
679
- expect(style.getPropertyValue('flex-basis')).toEqual('');
680
- style.removeProperty('flex');
681
- style.setProperty('flex', '20%');
682
- expect(style.getPropertyValue('flex-grow')).toEqual('');
683
- expect(style.getPropertyValue('flex-shrink')).toEqual('');
684
- expect(style.getPropertyValue('flex-basis')).toEqual('20%');
685
- style.removeProperty('flex');
686
- style.setProperty('flex', '2 2');
687
- expect(style.getPropertyValue('flex-grow')).toEqual('2');
688
- expect(style.getPropertyValue('flex-shrink')).toEqual('2');
689
- expect(style.getPropertyValue('flex-basis')).toEqual('');
690
- style.removeProperty('flex');
691
- });
692
-
693
- test('font-size get a valid value', () => {
694
- var style = new CSSStyleDeclaration();
695
- const invalidValue = '1r5px';
696
- style.cssText = 'font-size: 15px';
697
- expect(1).toEqual(style.length);
698
- style.cssText = `font-size: ${invalidValue}`;
699
- expect(0).toEqual(style.length);
700
- expect(undefined).toEqual(style[0]);
701
- });
702
-
703
- test('getPropertyValue for custom properties in cssText', () => {
704
- const style = new CSSStyleDeclaration();
705
- style.cssText = '--foo: red';
706
-
707
- expect(style.getPropertyValue('--foo')).toEqual('red');
708
- });
709
-
710
- test('getPropertyValue for custom properties with setProperty', () => {
711
- const style = new CSSStyleDeclaration();
712
- style.setProperty('--bar', 'blue');
713
-
714
- expect(style.getPropertyValue('--bar')).toEqual('blue');
715
- });
716
-
717
- test('getPropertyValue for custom properties with object setter', () => {
718
- const style = new CSSStyleDeclaration();
719
- style['--baz'] = 'yellow';
720
-
721
- expect(style.getPropertyValue('--baz')).toEqual('');
722
- });
723
-
724
- test('custom properties are case-sensitive', () => {
725
- const style = new CSSStyleDeclaration();
726
- style.cssText = '--fOo: purple';
727
-
728
- expect(style.getPropertyValue('--foo')).toEqual('');
729
- expect(style.getPropertyValue('--fOo')).toEqual('purple');
730
- });
731
-
732
- test('supports calc', () => {
733
- const style = new CSSStyleDeclaration();
734
- style.setProperty('width', 'calc(100% - 100px)');
735
- expect(style.getPropertyValue('width')).toEqual('calc(100% - 100px)');
736
- });
737
- });