blob: 91b1dcf03970fb497eeb8bc6e7d37dc37d8934a3 [file] [log] [blame]
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Selector Invalidation: :has() in sibling position</title>
<link rel="author" title="Antti Koivisto" href="mailto:antti@apple.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://drafts.csswg.org/selectors/#relational">
<style>
div, main { color: grey }
div:has(.test, [test_attr]) ~ #subject { color: red }
div:has(> .test, > [test_attr]) ~ #subject { color: green }
div:has(~ .test, ~ [test_attr]) ~ #subject { color: yellow }
div:has(+ .test, + [test_attr]) ~ #subject { color: blue }
div:has(~ div .test, ~ div [test_attr]) ~ #subject { color: purple }
div:has(+ div .test, + div [test_attr]) ~ #subject { color: pink }
</style>
<main id=main>
<div id=previous_sibling>
<div id=previous_sibling_child>
<div id=previous_sibling_descendant></div>
</div>
</div>
<div id=subject></div>
<div id=next_sibling>
<div id=next_sibling_child>
<div id=next_sibling_descendant></div>
</div>
</div>
</main>
<script>
const grey = 'rgb(128, 128, 128)';
const red = 'rgb(255, 0, 0)';
const green = 'rgb(0, 128, 0)';
const blue = 'rgb(0, 0, 255)';
const yellow = 'rgb(255, 255, 0)';
const purple = 'rgb(128, 0, 128)';
const pink = 'rgb(255, 192, 203)';
function testColor(test_name, color) {
test(function() {
assert_equals(getComputedStyle(subject).color, color);
}, test_name);
}
function testClassChange(element, expectedColor)
{
element.classList.add('test');
testColor(`add .test to ${element.id}`, expectedColor);
element.classList.remove('test');
testColor(`remove .test from ${element.id}`, grey);
}
function testElementInsertionBefore(beforeElement, expectedColor)
{
const newElement = document.createElement('div');
newElement.classList.add('test')
beforeElement.before(newElement);
testColor(`insert element div.test before ${beforeElement.id}`, expectedColor);
newElement.classList.remove('test');
testColor(`remove the class 'test' from the element inserted before ${beforeElement.id}`, grey);
newElement.classList.add('test');
testColor(`add the class 'test' again to the element inserted before ${beforeElement.id}`, expectedColor);
newElement.remove();
testColor(`remove element div.test before ${beforeElement.id}`, grey);
newElement.classList.remove('test')
beforeElement.before(newElement);
testColor(`insert element div before ${beforeElement.id}`, grey);
newElement.classList.add('test');
testColor(`add the class 'test' to the element inserted again before ${beforeElement.id}`, expectedColor);
newElement.classList.remove('test');
testColor(`remove the class 'test' from the element inserted again before ${beforeElement.id}`, grey);
newElement.remove();
testColor(`remove element div before ${beforeElement.id}`, grey);
newElement.setAttribute('test_attr', '');
beforeElement.before(newElement);
testColor(`insert element div[test_attr] before ${beforeElement.id}`, expectedColor);
newElement.remove();
testColor(`remove element div[test_attr] before ${beforeElement.id}`, grey);
}
function testElementInsertionAfter(afterElement, expectedColor)
{
const newElement = document.createElement('div');
newElement.classList.add('test')
afterElement.after(newElement);
testColor(`insert element div.test after ${afterElement.id}`, expectedColor);
newElement.classList.remove('test');
testColor(`remove the class 'test' from the element inserted after ${afterElement.id}`, grey);
newElement.classList.add('test');
testColor(`add the class 'test' again to the element inserted after ${afterElement.id}`, expectedColor);
newElement.remove();
testColor(`remove element div.test after ${afterElement.id}`, grey);
newElement.classList.remove('test');
afterElement.after(newElement);
testColor(`insert element div after ${afterElement.id}`, grey);
newElement.classList.add('test');
testColor(`add the class 'test' to the element inserted again after ${afterElement.id}`, expectedColor);
newElement.classList.remove('test');
testColor(`remove the class 'test' from the element inserted again after ${afterElement.id}`, grey);
newElement.remove();
testColor(`remove element div after ${afterElement.id}`, grey);
newElement.setAttribute('test_attr', '');
afterElement.after(newElement);
testColor(`insert element div[test_attr] after ${afterElement.id}`, expectedColor);
newElement.remove();
testColor(`remove element div[test_attr] after ${afterElement.id}`, grey);
}
function testTreeInsertionBefore(beforeElement, expectedColor)
{
const newElement = document.createElement('div');
const newChild = document.createElement('div');
newChild.classList.add('test');
newElement.appendChild(newChild);
beforeElement.before(newElement);
testColor(`insert tree div>div.test before ${beforeElement.id}`, expectedColor);
newChild.classList.remove('test');
testColor(`remove the class 'test' from the element in the tree inserted before ${beforeElement.id}`, grey);
newChild.classList.add('test');
testColor(`add the class 'test' again to the element in the tree inserted before ${beforeElement.id}`, expectedColor);
newElement.remove();
testColor(`remove tree div>div.test before ${beforeElement.id}`, grey);
newChild.classList.remove('test');
beforeElement.before(newElement);
testColor(`insert tree div>div before ${beforeElement.id}`, grey);
newChild.classList.add('test');
testColor(`add the class 'test' to the element in the tree inserted again before ${beforeElement.id}`, expectedColor);
newChild.classList.remove('test');
testColor(`remove the class 'test' from the element in the tree inserted again before ${beforeElement.id}`, grey);
newElement.remove();
testColor(`remove tree div>div before ${beforeElement.id}`, grey);
newChild.setAttribute('test_attr', '');
beforeElement.before(newElement);
testColor(`insert element div>div[test_attr] before ${beforeElement.id}`, expectedColor);
newElement.remove();
testColor(`remove element div>div[test_attr] before ${beforeElement.id}`, grey);
}
function testTreeInsertionAfter(afterElement, expectedColor)
{
const newElement = document.createElement('div');
const newChild = document.createElement('div');
newChild.classList.add('test');
newElement.appendChild(newChild);
afterElement.after(newElement);
testColor(`insert tree div>div.test after ${afterElement.id}`, expectedColor);
newChild.classList.remove('test');
testColor(`remove the class 'test' from the element in the tree inserted after ${afterElement.id}`, grey);
newChild.classList.add('test');
testColor(`add the class 'test' again to the element in the tree inserted after ${afterElement.id}`, expectedColor);
newElement.remove();
testColor(`remove tree div>div.test after ${afterElement.id}`, grey);
newChild.classList.remove('test');
afterElement.after(newElement);
testColor(`insert tree div>div after ${afterElement.id}`, grey);
newChild.classList.add('test');
testColor(`add the class 'test' to the element in the tree inserted again after ${afterElement.id}`, expectedColor);
newChild.classList.remove('test');
testColor(`remove the class 'test' from the element in the tree inserted again after ${afterElement.id}`, grey);
newElement.remove();
testColor(`remove tree div>div after ${afterElement.id}`, grey);
newChild.setAttribute('test_attr', '');
afterElement.after(newElement);
testColor(`insert element div>div[test_attr] after ${afterElement.id}`, expectedColor);
newElement.remove();
testColor(`remove element div>div[test_attr] after ${afterElement.id}`, grey);
}
testColor('Initial color', grey);
testClassChange(previous_sibling, grey);
testClassChange(previous_sibling_child, green);
testClassChange(previous_sibling_descendant, red);
testClassChange(subject, blue);
testClassChange(next_sibling, yellow);
testClassChange(next_sibling_child, purple);
testClassChange(next_sibling_descendant, purple);
testElementInsertionBefore(previous_sibling, grey);
testElementInsertionBefore(previous_sibling_child, green);
testElementInsertionBefore(previous_sibling_descendant, red);
testElementInsertionBefore(subject, blue);
testElementInsertionBefore(next_sibling, yellow);
testElementInsertionBefore(next_sibling_child, purple);
testElementInsertionBefore(next_sibling_descendant, purple);
testElementInsertionAfter(previous_sibling, blue);
testElementInsertionAfter(previous_sibling_child, green);
testElementInsertionAfter(previous_sibling_descendant, red);
testElementInsertionAfter(subject, yellow);
testElementInsertionAfter(next_sibling, yellow);
testElementInsertionAfter(next_sibling_child, purple);
testElementInsertionAfter(next_sibling_descendant, purple);
testTreeInsertionBefore(previous_sibling, green);
testTreeInsertionBefore(previous_sibling_child, red);
testTreeInsertionBefore(previous_sibling_descendant, red);
testTreeInsertionBefore(subject, pink);
testTreeInsertionBefore(next_sibling, purple);
testTreeInsertionBefore(next_sibling_child, purple);
testTreeInsertionBefore(next_sibling_descendant, purple);
testTreeInsertionAfter(previous_sibling, pink);
testTreeInsertionAfter(previous_sibling_child, red);
testTreeInsertionAfter(previous_sibling_descendant, red);
testTreeInsertionAfter(subject, purple);
testTreeInsertionAfter(next_sibling, purple);
testTreeInsertionAfter(next_sibling_child, purple);
testTreeInsertionAfter(next_sibling_descendant, purple);
</script>