summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/selectors/invalidation/has-sibling.html
blob: 7c56b2e7b3340f9cba724475751051fe284b44b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Selector Invalidation: :has() with sibling combinator argument</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 }
#subject:has(~ .test) { color: red }
#subject:has(+ .test) { color: green }
#subject:has(~ div .test) { color: blue }
#subject:has(~ div > .test) { color: purple }
#subject:has(+ div .test) { color: yellow }
#subject:has(+ div > .test) { color: pink }
</style>

<main id=main>
    <div id=subject></div>
    <div id=first_sibling>
        <div id=first_sibling_child>
            <div id=first_sibling_descendant></div>
        </div>
    </div>
    <div id=second_sibling></div>
    <div id=third_sibling>
        <div id=third_sibling_child>
            <div id=third_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.remove();
    testColor(`remove element div.test 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.remove();
    testColor(`remove element div.test 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);

    newElement.remove();
    testColor(`remove tree div>div.test 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);

    newElement.remove();
    testColor(`remove tree div>div.test after ${afterElement.id}`, grey);
}

testColor('initial_color', grey);

testClassChange(first_sibling, green);
testClassChange(second_sibling, red);
testClassChange(third_sibling, red);
testClassChange(first_sibling_child, pink);
testClassChange(first_sibling_descendant, yellow);
testClassChange(third_sibling_child, purple);
testClassChange(third_sibling_descendant, blue);

testElementInsertionBefore(first_sibling, green);
testElementInsertionBefore(second_sibling, red);
testElementInsertionBefore(third_sibling, red);
testElementInsertionBefore(first_sibling_child, pink);
testElementInsertionBefore(first_sibling_descendant, yellow);
testElementInsertionBefore(third_sibling_child, purple);
testElementInsertionBefore(third_sibling_descendant, blue);

testElementInsertionAfter(first_sibling, red);
testElementInsertionAfter(second_sibling, red);
testElementInsertionAfter(third_sibling, red);
testElementInsertionAfter(first_sibling_child, pink);
testElementInsertionAfter(first_sibling_descendant, yellow);
testElementInsertionAfter(third_sibling_child, purple);
testElementInsertionAfter(third_sibling_descendant, blue);

testTreeInsertionBefore(first_sibling, pink);
testTreeInsertionBefore(second_sibling, purple);
testTreeInsertionBefore(third_sibling, purple);
testTreeInsertionBefore(first_sibling_child, yellow);
testTreeInsertionBefore(first_sibling_descendant, yellow);
testTreeInsertionBefore(third_sibling_child, blue);
testTreeInsertionBefore(third_sibling_descendant, blue);

testTreeInsertionAfter(first_sibling, purple);
testTreeInsertionAfter(second_sibling, purple);
testTreeInsertionAfter(third_sibling, purple);
testTreeInsertionAfter(first_sibling_child, yellow);
testTreeInsertionAfter(first_sibling_descendant, yellow);
testTreeInsertionAfter(third_sibling_child, blue);
testTreeInsertionAfter(third_sibling_descendant, blue);
</script>