blob: b2e3a2c1b1306bdcc69ab93218c0a954b17024a2 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<title>Shadow DOM: Link and style elements inside a shadow tree should not affect the preferred stylesheet</title>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="assert" content="Link and style elements inside a shadow tree should not affect the preferred stylesheet">
<script src='../../resources/testharness.js'></script>
<script src='../../resources/testharnessreport.js'></script>
</head>
<body>
<script>
function make_iframe(markup, testFunction) {
return () => {
const iframe = document.createElement('iframe');
return new Promise(function (resolve) {
iframe.onload = () => { resolve(iframe.contentDocument); };
document.body.appendChild(iframe);
}).then(function (doc) {
doc.open();
doc.write(markup);
doc.close();
return testFunction(doc, doc.querySelector('div'));
}).then(() => iframe.remove(), error => {
iframe.remove();
return Promise.reject(error);
});
};
}
// Some browsers don't synchronously update the selected stylesheet.
function wait_for_stylesheet_to_be_selected() {
return new Promise(function (resolve) {
setTimeout(resolve, 0);
});
}
promise_test(make_iframe(`<!DOCTYPE html><body>`, (doc) => {
doc.body.innerHTML = `<style title="foo">div { color: red; }</style>`;
return wait_for_stylesheet_to_be_selected().then(() => {
assert_equals(doc.preferredStylesheetSet, 'foo');
assert_equals(doc.selectedStylesheetSet, 'foo');
});
}), 'The title attribute on the style element in a document must set the preferred stylesheet.');
promise_test(make_iframe(`<!DOCTYPE html><body><div>`, (doc) => {
doc.querySelector('div').attachShadow({mode: 'closed'}).innerHTML = `<style title="foo"></style>`;
return wait_for_stylesheet_to_be_selected().then(() => {
assert_equals(doc.preferredStylesheetSet, null);
assert_equals(doc.selectedStylesheetSet, null);
});
}), 'The title attribute on the style element inside a closed shadow tree must not set the preferred stylesheet.');
promise_test(make_iframe(`<!DOCTYPE html><body><div>`, (doc) => {
doc.querySelector('div').attachShadow({mode: 'open'}).innerHTML = `<style title="foo"></style>`;
return wait_for_stylesheet_to_be_selected().then(() => {
assert_equals(doc.preferredStylesheetSet, null);
assert_equals(doc.selectedStylesheetSet, null);
});
}), 'The title attribute on the style element inside an open shadow tree must not set the preferred stylesheet.');
function insert_link_and_wait(parent, title) {
return new Promise((resolve, reject) => {
const link = parent.ownerDocument.createElement('link');
link.rel = 'stylesheet';
link.title = title;
link.href = 'resources/green-host.css';
link.onload = resolve;
parent.appendChild(link);
setTimeout(() => reject('Failed to load the stylesheet'), 1000);
}).then(() => wait_for_stylesheet_to_be_selected());
}
promise_test(make_iframe(`<!DOCTYPE html><body>`, (doc) => {
return insert_link_and_wait(doc.body, 'foo').then(() => {
assert_equals(doc.preferredStylesheetSet, 'foo');
assert_equals(doc.selectedStylesheetSet, 'foo');
});
}), 'The title attribute on the link element in a document must set the preferred stylesheet.');
promise_test(make_iframe(`<!DOCTYPE html><body><div>`, (doc) => {
const root = doc.querySelector('div').attachShadow({mode: 'closed'});
return insert_link_and_wait(root, 'foo').then(() => {
assert_equals(doc.preferredStylesheetSet, null);
assert_equals(doc.selectedStylesheetSet, null);
});
}), 'The title attribute on the link element inside a closed shadow tree must not set the preferred stylesheet.');
promise_test(make_iframe(`<!DOCTYPE html><body><div>`, (doc) => {
const root = doc.querySelector('div').attachShadow({mode: 'open'});
return insert_link_and_wait(root, 'foo').then(() => {
assert_equals(doc.preferredStylesheetSet, null);
assert_equals(doc.selectedStylesheetSet, null);
});
}), 'The title attribute on the link element inside an open shadow tree must not set the preferred stylesheet.');
</script>
</html>
</body>