-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathshorthand-testcommon.js
106 lines (93 loc) · 4.32 KB
/
shorthand-testcommon.js
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
'use strict';
function test_shorthand_value(property, value, longhands) {
const stringifiedValue = JSON.stringify(value);
for (let longhand of Object.keys(longhands).sort()) {
test(function(){
var div = document.getElementById('target') || document.createElement('div');
div.style[property] = "";
try {
div.style[property] = value;
const readValue = div.style[longhand];
assert_equals(readValue, longhands[longhand], longhand + " should be canonical");
div.style[longhand] = "";
div.style[longhand] = readValue;
assert_equals(div.style[longhand], readValue, "serialization should round-trip");
} finally {
div.style[property] = "";
}
}, "e.style['" + property + "'] = " + stringifiedValue + " should set " + longhand);
}
test(function(){
var div = document.getElementById('target') || document.createElement('div');
div.style[property] = "";
try {
const expectedLength = div.style.length;
div.style[property] = value;
assert_true(CSS.supports(property, value));
for (let longhand of Object.keys(longhands).sort()) {
div.style[longhand] = "";
}
assert_equals(div.style.length, expectedLength);
} finally {
div.style[property] = "";
}
}, "e.style['" + property + "'] = " + stringifiedValue + " should not set unrelated longhands");
}
/**
* Helper to be called from inside test().
*/
function is_property_in_longhands(t, property_name) {
let e = document.createElement("div");
document.body.append(e);
t.add_cleanup(() => e.remove());
let cs = getComputedStyle(e);
return Array.from(cs).includes(property_name);
}
/**
* This function is designed mainly to test the distinction between
* legacy name aliases and legacy shorthands.
*/
function test_is_legacy_name_alias(old_name, new_name) {
test(t => {
let e = document.createElement("div");
e.style.setProperty(old_name, "inherit");
assert_equals(e.style.getPropertyValue(old_name), "inherit",
`${old_name} is supported`);
assert_equals(e.style.getPropertyValue(new_name), "inherit",
`${old_name} is an alias for ${new_name}`);
assert_equals(e.style.cssText, `${new_name}: inherit;`,
`declarations serialize using new name ${new_name}`);
e = document.createElement("div");
e.style.setProperty(old_name, "var(--v)");
assert_equals(e.style.getPropertyValue(new_name), "var(--v)",
`${old_name} is a legacy name alias rather than a shorthand`)
e = document.createElement("div");
e.style.setProperty(new_name, "var(--w)");
assert_equals(e.style.getPropertyValue(old_name), "var(--w)",
`${old_name} is a legacy name alias rather than a shorthand`)
assert_false(is_property_in_longhands(t, old_name),
`${old_name} is not in getComputedStyle() list of longhands`);
}, `${old_name} is a legacy name alias for ${new_name}`);
}
/**
* This function is designed mainly to test the distinction between
* legacy name aliases and legacy shorthands.
*/
function test_is_legacy_shorthand(old_name, new_name) {
test(t => {
let e = document.createElement("div");
e.style.setProperty(old_name, "inherit");
assert_equals(e.style.getPropertyValue(old_name), "inherit",
`${old_name} is supported`);
assert_equals(e.style.getPropertyValue(new_name), "inherit",
`${old_name} is an alias for ${new_name}`);
assert_equals(e.style.cssText, `${new_name}: inherit;`,
`declarations serialize using new name ${new_name}`);
e = document.createElement("div");
e.style.setProperty(old_name, "var(--v)");
assert_equals(e.style.getPropertyValue(new_name), "",
`${old_name} is a shorthand rather than a legacy name alias`)
assert_false(is_property_in_longhands(t, old_name),
`${old_name} is not in getComputedStyle() list of longhands`);
}, `${old_name} is a legacy name alias for ${new_name}`);
}