| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>CSS Cascade Layers: !important</title> |
| 5 |
<meta name="assert" content="!important in CSS Cascade Layers"> |
| 6 |
<link rel="help" href="https://www.w3.org/TR/css-cascade-5/#layering"> |
| 7 |
</head> |
| 8 |
|
| 9 |
<body> |
| 10 |
<target class="first"></target> |
| 11 |
<target class="second"></target> |
| 12 |
<div id="log"></div> |
| 13 |
<script type="module"> |
| 14 |
window.addEventListener('click', () => { |
| 15 |
window.runTest(); |
| 16 |
}); |
| 17 |
|
| 18 |
window.runTest = async function runTest() { |
| 19 |
function assert_equals(actual, expected, message) { |
| 20 |
if (actual != expected) { |
| 21 |
log.innerHTML = message + ": expected " + expected + ", got " + actual; |
| 22 |
throw new Error(message + ": expected " + expected + ", got " + actual); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
// In all test cases, the rule specified as "color: green" should win. |
| 27 |
var testCases = [ |
| 28 |
{ |
| 29 |
title: 'Same specificity, layered important first', |
| 30 |
style: ` |
| 31 |
@layer { target { color: green !important; } } |
| 32 |
target { color: red; } |
| 33 |
`, |
| 34 |
}, |
| 35 |
{ |
| 36 |
title: 'Same specificity, layered important second', |
| 37 |
style: ` |
| 38 |
target { color: red; } |
| 39 |
@layer { target { color: green !important; } } |
| 40 |
`, |
| 41 |
}, |
| 42 |
{ |
| 43 |
title: 'Same specificity, all important (order A)', |
| 44 |
style: ` |
| 45 |
@layer { target { color: green !important; } } |
| 46 |
@layer { target { color: red !important; } } |
| 47 |
target { color: red !important; } |
| 48 |
`, |
| 49 |
}, |
| 50 |
{ |
| 51 |
title: 'Same specificity, all important (order B)', |
| 52 |
style: ` |
| 53 |
@layer { target { color: green !important; } } |
| 54 |
target { color: red !important; } |
| 55 |
@layer { target { color: red !important; } } |
| 56 |
`, |
| 57 |
}, |
| 58 |
{ |
| 59 |
title: 'Same specificity, all important (order C)', |
| 60 |
style: ` |
| 61 |
target { color: red !important; } |
| 62 |
@layer { target { color: green !important; } } |
| 63 |
@layer { target { color: red !important; } } |
| 64 |
`, |
| 65 |
}, |
| 66 |
{ |
| 67 |
title: 'Same specificity, all important (order D)', |
| 68 |
style: ` |
| 69 |
@layer A, B; |
| 70 |
@layer B { target { color: red !important; } } |
| 71 |
@layer A { target { color: green !important; } } |
| 72 |
target { color: red !important; } |
| 73 |
`, |
| 74 |
}, |
| 75 |
{ |
| 76 |
title: 'Different specificity (A), all important', |
| 77 |
style: ` |
| 78 |
@layer { target { color: green !important; } } |
| 79 |
@layer { target { color: red !important; } } |
| 80 |
target.first { color: red !important; } |
| 81 |
`, |
| 82 |
}, |
| 83 |
{ |
| 84 |
title: 'Different specificity (B), all important', |
| 85 |
style: ` |
| 86 |
@layer { target { color: green !important; } } |
| 87 |
@layer { target.first { color: red !important; } } |
| 88 |
target { color: red !important; } |
| 89 |
`, |
| 90 |
}, |
| 91 |
]; |
| 92 |
|
| 93 |
for (let testCase of testCases) { |
| 94 |
{ |
| 95 |
const styleElement = document.createElement('style'); |
| 96 |
|
| 97 |
const testCaseStyle = await (fetch(`/test/styles.css`, { |
| 98 |
method: 'POST', |
| 99 |
body: testCase['style'], |
| 100 |
}).then((response) => response.text())); |
| 101 |
|
| 102 |
styleElement.textContent = testCaseStyle; |
| 103 |
document.head.append(styleElement); |
| 104 |
|
| 105 |
var targets = document.querySelectorAll('target'); |
| 106 |
for (let target of targets) { |
| 107 |
assert_equals( |
| 108 |
window.getComputedStyle(target).color, |
| 109 |
'rgb(0, 128, 0)', |
| 110 |
testCase['title'] + " (processed), target '" + target.classList[0] + "'" |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
styleElement.remove(); |
| 115 |
} |
| 116 |
|
| 117 |
{ |
| 118 |
const styleElement = document.createElement('style'); |
| 119 |
|
| 120 |
styleElement.textContent = testCase['style']; |
| 121 |
document.head.append(styleElement); |
| 122 |
|
| 123 |
var targets = document.querySelectorAll('target'); |
| 124 |
for (let target of targets) { |
| 125 |
assert_equals( |
| 126 |
window.getComputedStyle(target).color, |
| 127 |
'rgb(0, 128, 0)', |
| 128 |
testCase['title'] + " (native), target '" + target.classList[0] + "'" |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
styleElement.remove(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
</script> |
| 139 |
</body> |
| 140 |
|
| 141 |
</html> |