| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
|
| 4 |
<head> |
| 5 |
<title>CSS Cascade Layers: test buckets of specificity created by the plugin</title> |
| 6 |
</head> |
| 7 |
|
| 8 |
<body id="body"> |
| 9 |
<target id="first" class="first"></target> |
| 10 |
<target id="second" class="second"></target> |
| 11 |
|
| 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: '1 ID specificity', |
| 30 |
style: ` |
| 31 |
@layer { target#first { color: red; } } |
| 32 |
target { color: green; } |
| 33 |
`, |
| 34 |
}, |
| 35 |
{ |
| 36 |
title: '2 ID specificity', |
| 37 |
style: ` |
| 38 |
@layer { #body target#first { color: red; } } |
| 39 |
target { color: green; } |
| 40 |
`, |
| 41 |
}, |
| 42 |
{ |
| 43 |
title: '3 ID specificity', |
| 44 |
style: ` |
| 45 |
@layer { #body#body target#first { color: red; } } |
| 46 |
target { color: green; } |
| 47 |
`, |
| 48 |
}, |
| 49 |
{ |
| 50 |
title: '4 ID specificity', |
| 51 |
style: ` |
| 52 |
@layer { #body#body#body target#first { color: red; } } |
| 53 |
target { color: green; } |
| 54 |
`, |
| 55 |
}, |
| 56 |
{ |
| 57 |
title: '5 ID specificity', |
| 58 |
style: ` |
| 59 |
@layer { #body#body#body#body target#first { color: red; } } |
| 60 |
target { color: green; } |
| 61 |
`, |
| 62 |
}, |
| 63 |
]; |
| 64 |
|
| 65 |
for (let testCase of testCases) { |
| 66 |
{ |
| 67 |
const styleElement = document.createElement('style'); |
| 68 |
|
| 69 |
const testCaseStyle = await (fetch(`/test/styles.css`, { |
| 70 |
method: 'POST', |
| 71 |
body: testCase['style'], |
| 72 |
}).then((response) => response.text())); |
| 73 |
|
| 74 |
styleElement.textContent = testCaseStyle; |
| 75 |
document.head.append(styleElement); |
| 76 |
|
| 77 |
var targets = document.querySelectorAll('target'); |
| 78 |
for (let target of targets) { |
| 79 |
assert_equals( |
| 80 |
window.getComputedStyle(target).color, |
| 81 |
'rgb(0, 128, 0)', |
| 82 |
testCase['title'] + " (processed), target '" + target.classList[0] + "'" |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
styleElement.remove(); |
| 87 |
} |
| 88 |
|
| 89 |
{ |
| 90 |
const styleElement = document.createElement('style'); |
| 91 |
|
| 92 |
styleElement.textContent = testCase['style']; |
| 93 |
document.head.append(styleElement); |
| 94 |
|
| 95 |
var targets = document.querySelectorAll('target'); |
| 96 |
for (let target of targets) { |
| 97 |
assert_equals( |
| 98 |
window.getComputedStyle(target).color, |
| 99 |
'rgb(0, 128, 0)', |
| 100 |
testCase['title'] + " (native), target '" + target.classList[0] + "'" |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
styleElement.remove(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
</script> |
| 111 |
</body> |
| 112 |
|
| 113 |
</html> |