Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!DOCTYPE html>
<title>CSS @private: shared stylesheet name mappings</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
function loadFrame(t) {
return new Promise(resolve => {
const frame = document.createElement('iframe');
t.add_cleanup(() => frame.remove());
frame.onload = () => resolve(frame);
frame.srcdoc = `
<link rel="stylesheet" href="resources/shared-private.css">
<div class="target"></div>
`;
document.body.append(frame);
});
}
function getPrivateRule(frame) {
const sheet = frame.contentDocument.styleSheets[0];
assert_equals(sheet.cssRules.length, 1);
const styleRule = sheet.cssRules[0];
assert_equals(styleRule.cssRules.length, 1);
return styleRule.cssRules[0];
}
promise_test(async t => {
const firstFrame = await loadFrame(t);
const secondFrame = await loadFrame(t);
const firstRule = getPrivateRule(firstFrame);
const secondRule = getPrivateRule(secondFrame);
const expected = '@private { --x <length>: 10px; }';
assert_equals(firstRule.cssText, expected);
assert_equals(secondRule.cssText, expected);
firstRule.parentRule.deleteRule(0);
assert_equals(firstRule.cssText, expected);
}, 'Shared stylesheets and detached rules preserve authored private names');
promise_test(async t => {
const firstFrame = await loadFrame(t);
const secondFrame = await loadFrame(t);
const firstRule = getPrivateRule(firstFrame);
const secondRule = getPrivateRule(secondFrame);
firstRule.parentRule.style.setProperty('width', 'var(--x)');
secondRule.parentRule.style.setProperty('width', 'var(--x)');
const firstTarget = firstFrame.contentDocument.querySelector('.target');
const secondTarget = secondFrame.contentDocument.querySelector('.target');
assert_equals(firstFrame.contentWindow.getComputedStyle(firstTarget).width,
'10px');
assert_equals(secondFrame.contentWindow.getComputedStyle(secondTarget).width,
'10px');
secondRule.styleMap.set('--x', '25px');
assert_equals(secondFrame.contentWindow.getComputedStyle(secondTarget).width,
'25px');
assert_equals(firstFrame.contentWindow.getComputedStyle(firstTarget).width,
'10px');
}, 'Private values remain isolated after a shared stylesheet is mutated');
promise_test(async t => {
const frame = await loadFrame(t);
const doc = frame.contentDocument;
const firstLink = doc.querySelector('link');
const secondLink = firstLink.cloneNode();
await new Promise(resolve => {
secondLink.onload = resolve;
firstLink.after(secondLink);
});
const secondRule = secondLink.sheet.cssRules[0];
const target = doc.querySelector('.target');
secondRule.style.width = 'var(--x, 20px)';
assert_equals(frame.contentWindow.getComputedStyle(target).width, '10px');
secondRule.cssRules[0].style.removeProperty('--x');
assert_equals(frame.contentWindow.getComputedStyle(target).width, '20px');
assert_equals(getPrivateRule(frame).style.getPropertyValue('--x'), '10px');
}, 'A copied stylesheet cannot capture private values from the original sheet');
</script>