| 1 |
<!DOCTYPE html> |
| 2 |
<title>Resolving @property name conflicts with cascade layers</title> |
| 3 |
<link rel="help" href="https://drafts.csswg.org/css-cascade-5/#layering"> |
| 4 |
<link rel="author" href="mailto:[email protected]"> |
| 5 |
<!-- Source : https://github.com/web-platform-tests/wpt/blob/master/css/css-cascade/layer-property-override.html --> |
| 6 |
|
| 7 |
<style> |
| 8 |
#target, |
| 9 |
#reference { |
| 10 |
width: 100px; |
| 11 |
height: 100px; |
| 12 |
} |
| 13 |
|
| 14 |
#reference { |
| 15 |
background-color: green; |
| 16 |
} |
| 17 |
</style> |
| 18 |
|
| 19 |
<div id="target"></div> |
| 20 |
<div id="reference"></div> |
| 21 |
<div id="log"></div> |
| 22 |
|
| 23 |
<script type="module"> |
| 24 |
window.addEventListener('click', () => { |
| 25 |
window.runTest(); |
| 26 |
}); |
| 27 |
|
| 28 |
window.runTest = async function runTest() { |
| 29 |
function assert_equals(actual, expected, message) { |
| 30 |
if (actual != expected) { |
| 31 |
log.innerHTML = message + ": expected " + expected + ", got " + actual; |
| 32 |
throw new Error(message + ": expected " + expected + ", got " + actual); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// In all tests, background color of #target should be green, same as #reference |
| 37 |
|
| 38 |
const testCases = [ |
| 39 |
{ |
| 40 |
title: '@property unlayered overrides layered', |
| 41 |
style: ` |
| 42 |
#target { |
| 43 |
background-color: var(--foo); |
| 44 |
} |
| 45 |
|
| 46 |
@property --foo { |
| 47 |
syntax: '<color>'; |
| 48 |
inherits: false; |
| 49 |
initial-value: green; |
| 50 |
} |
| 51 |
|
| 52 |
@layer { |
| 53 |
@property --foo { |
| 54 |
syntax: '<color>'; |
| 55 |
inherits: false; |
| 56 |
initial-value: red; |
| 57 |
} |
| 58 |
} |
| 59 |
` |
| 60 |
}, |
| 61 |
|
| 62 |
{ |
| 63 |
title: '@property override between layers', |
| 64 |
style: ` |
| 65 |
@layer base, override; |
| 66 |
|
| 67 |
#target { |
| 68 |
background-color: var(--foo); |
| 69 |
} |
| 70 |
|
| 71 |
@layer override { |
| 72 |
@property --foo { |
| 73 |
syntax: '<color>'; |
| 74 |
inherits: false; |
| 75 |
initial-value: green; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
@layer base { |
| 80 |
@property --foo { |
| 81 |
syntax: '<color>'; |
| 82 |
inherits: false; |
| 83 |
initial-value: red; |
| 84 |
} |
| 85 |
} |
| 86 |
` |
| 87 |
}, |
| 88 |
|
| 89 |
{ |
| 90 |
title: '@property override update with appended sheet 1', |
| 91 |
style: ` |
| 92 |
@layer base, override; |
| 93 |
|
| 94 |
#target { |
| 95 |
background-color: var(--foo); |
| 96 |
} |
| 97 |
|
| 98 |
@layer override { |
| 99 |
@property --foo { |
| 100 |
syntax: '<color>'; |
| 101 |
inherits: false; |
| 102 |
initial-value: green; |
| 103 |
} |
| 104 |
} |
| 105 |
`, |
| 106 |
append: ` |
| 107 |
@layer base { |
| 108 |
@property --foo { |
| 109 |
syntax: '<color>'; |
| 110 |
inherits: false; |
| 111 |
initial-value: red; |
| 112 |
} |
| 113 |
} |
| 114 |
` |
| 115 |
}, |
| 116 |
|
| 117 |
{ |
| 118 |
title: '@property override update with appended sheet 2', |
| 119 |
style: ` |
| 120 |
@layer base, override; |
| 121 |
|
| 122 |
#target { |
| 123 |
background-color: var(--foo); |
| 124 |
} |
| 125 |
|
| 126 |
@layer base { |
| 127 |
@property --foo { |
| 128 |
syntax: '<color>'; |
| 129 |
inherits: false; |
| 130 |
initial-value: red; |
| 131 |
} |
| 132 |
} |
| 133 |
`, |
| 134 |
append: ` |
| 135 |
@layer override { |
| 136 |
@property --foo { |
| 137 |
syntax: '<color>'; |
| 138 |
inherits: false; |
| 139 |
initial-value: green; |
| 140 |
} |
| 141 |
} |
| 142 |
` |
| 143 |
}, |
| 144 |
]; |
| 145 |
|
| 146 |
for (let testCase of testCases) { |
| 147 |
var documentStyle = document.createElement('style'); |
| 148 |
|
| 149 |
const testCaseStyle = await (fetch(`/test/styles.css`, { |
| 150 |
method: 'POST', |
| 151 |
body: testCase['style'], |
| 152 |
}).then((response) => response.text())); |
| 153 |
|
| 154 |
documentStyle.appendChild(document.createTextNode(testCaseStyle)); |
| 155 |
document.head.appendChild(documentStyle); |
| 156 |
|
| 157 |
var appendedStyle; |
| 158 |
if (testCase['append']) { |
| 159 |
document.body.offsetLeft; // Force style update |
| 160 |
appendedStyle = document.createElement('style'); |
| 161 |
|
| 162 |
const testCaseAppend = await (fetch(`/test/styles.css`, { |
| 163 |
method: 'POST', |
| 164 |
body: testCase['style'] + "\n" + testCase['append'], |
| 165 |
}).then((response) => response.text())); |
| 166 |
|
| 167 |
appendedStyle.appendChild(document.createTextNode(testCaseAppend)); |
| 168 |
document.head.appendChild(appendedStyle); |
| 169 |
} |
| 170 |
|
| 171 |
assert_equals( |
| 172 |
getComputedStyle(target).backgroundColor, |
| 173 |
getComputedStyle(reference).backgroundColor, |
| 174 |
testCase['title'] + ': background color' |
| 175 |
); |
| 176 |
|
| 177 |
if (appendedStyle) { |
| 178 |
appendedStyle.remove(); |
| 179 |
} |
| 180 |
|
| 181 |
documentStyle.remove(); |
| 182 |
} |
| 183 |
|
| 184 |
return true; |
| 185 |
} |
| 186 |
</script> |