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
|
<!DOCTYPE html>
<meta charset="utf-8">
<title>Common serialization checks for all properties</title>
<link rel="author" title="Oriol Brufau" href="mailto:obrufau@igalia.com" />
<link rel="help" href="https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-getpropertyvalue">
<div id="element"></div>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
const element = document.getElementById("element");
const { style } = element;
const computedStyle = getComputedStyle(element);
const cssProperties = new Set();
const cssShorthands = new Map();
const cssShorthandsForLonghand = new Map();
const cssLonghands = new Set();
const cssAliases = new Map();
const initialValues = new Map();
setup(function() {
for (let obj = style; obj; obj = Reflect.getPrototypeOf(obj)) {
for (let name of Object.getOwnPropertyNames(obj)) {
const property = name.replace(/[A-Z]/g, c => "-" + c.toLowerCase());
if (CSS.supports(property, "initial")) {
cssProperties.add(property);
}
}
}
for (let property of cssProperties) {
style.cssText = "";
style.setProperty(property, "initial");
if (style.length > 1) {
cssShorthands.set(property, [...style]);
for (let longhand of style) {
if (cssShorthandsForLonghand.has(longhand)) {
cssShorthandsForLonghand.get(longhand).add(property);
} else {
cssShorthandsForLonghand.set(longhand, new Set([property]));
}
}
} else if (style.length === 1) {
if (property === style[0]) {
cssLonghands.add(property);
} else {
cssAliases.set(property, style[0]);
}
}
}
});
test(function() {
const bad = [];
for (let property of cssProperties) {
style.cssText = "";
style.setProperty(property, "initial");
const result = style.getPropertyValue(property);
if (result !== "initial") {
bad.push([property, result]);
}
}
assert_array_equals(bad, []);
}, "All properties can serialize 'initial'");
test(function() {
for (let longhand of cssLonghands) {
element.style.setProperty(longhand, "initial");
}
const bad = [];
for (let property of cssProperties) {
const result = computedStyle.getPropertyValue(property);
if (CSS.supports(property, result)) {
initialValues.set(property, result);
} else if (property !== "all") {
bad.push([property, result]);
}
}
assert_array_equals(bad, []);
}, "All properties (except 'all') can serialize their initial value (computed)");
test(function() {
const bad = [];
for (let [property, value] of initialValues) {
style.cssText = "";
style.setProperty(property, value);
const result = style.getPropertyValue(property);
if (!CSS.supports(property, result) && property !== "all") {
bad.push([property, value, result]);
}
}
assert_array_equals(bad, []);
}, "All properties (except 'all') can serialize their initial value (specified)");
test(function() {
const bad = [];
for (let [shorthand, longhands] of cssShorthands) {
style.cssText = "";
for (let longhand of longhands) {
style.setProperty(longhand, "initial");
}
const result = style.getPropertyValue(shorthand);
if (result !== "initial") {
bad.push([shorthand, result]);
}
}
assert_array_equals(bad, []);
}, "All shorthands can serialize their longhands set to 'initial'");
test(function() {
const bad = [];
outerloop:
for (let [shorthand, longhands] of cssShorthands) {
style.cssText = "";
for (let longhand of longhands) {
if (!initialValues.has(longhand)) {
continue outerloop;
}
style.setProperty(longhand, initialValues.get(longhand));
}
const result = style.getPropertyValue(shorthand);
if (!CSS.supports(shorthand, result) && shorthand !== "all") {
bad.push([shorthand, result]);
}
}
assert_array_equals(bad, []);
}, "All shorthands (except 'all') can serialize their longhands set to their initial value");
test(function() {
const bad = [];
for (let [alias, target] of cssAliases) {
style.cssText = "";
style.setProperty(target, "initial");
const result = style.getPropertyValue(alias);
if (result !== "initial") {
bad.push([alias, result]);
}
}
assert_array_equals(bad, []);
}, "All aliases can serialize target property set to 'initial'");
test(function() {
const bad = [];
for (let [alias, target] of cssAliases) {
if (!initialValues.has(target)) {
continue;
}
style.cssText = "";
style.setProperty(target, initialValues.get(target));
const result = style.getPropertyValue(alias);
if (!CSS.supports(alias, result)) {
bad.push([alias, result]);
}
}
assert_array_equals(bad, []);
}, "All aliases can serialize target property set to its initial value");
test(function() {
const bad = [];
for (let [shorthand, longhands] of cssShorthands) {
for (let longhand of longhands) {
style.cssText = "";
style.setProperty(shorthand, "initial");
style.setProperty(longhand, "inherit");
const result = style.getPropertyValue(shorthand);
if (result !== "") {
bad.push([shorthand, longhand, result]);
}
}
}
assert_array_equals(bad, []);
}, "Can't serialize shorthand when longhands are set to different css-wide keywords");
test(function() {
const bad = [];
for (let [shorthand, longhands] of cssShorthands) {
for (let longhand of longhands) {
style.cssText = "";
style.setProperty(shorthand, "initial");
style.setProperty(longhand, "initial", "important");
const result = style.getPropertyValue(shorthand);
if (result !== "") {
bad.push([shorthand, longhand, result]);
}
}
}
assert_array_equals(bad, []);
}, "Can't serialize shorthand when longhands have different priority");
test(function() {
const bad = [];
for (let [shorthand, longhands] of cssShorthands) {
for (let longhand of longhands) {
style.cssText = "";
style.setProperty(shorthand, "initial");
style.removeProperty(longhand);
const result = style.getPropertyValue(shorthand);
if (result !== "") {
bad.push([shorthand, longhand, result]);
}
}
}
assert_array_equals(bad, []);
}, "Can't serialize shorthand set to 'initial' when some longhand is missing");
test(function() {
const bad = [];
for (let [shorthand, longhands] of cssShorthands) {
if (initialValues.has(shorthand)) {
for (let longhand of longhands) {
style.cssText = "";
style.setProperty(shorthand, initialValues.get(shorthand));
style.removeProperty(longhand);
const result = style.getPropertyValue(shorthand);
if (result !== "") {
bad.push([shorthand, longhand, result]);
}
}
}
}
assert_array_equals(bad, []);
}, "Can't serialize shorthand set to initial value when some longhand is missing");
</script>
|