File: offsetParent-across-shadow-boundaries.html

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (256 lines) | stat: -rw-r--r-- 13,115 bytes parent folder | download | duplicates (4)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE html>
<html>
<head>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="assert" content="offsetParent should only return nodes that are shadow including ancestor">
<link rel="help" href="https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetparent">
<link rel="help" href="https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/event-path-test-helpers.js"></script>
</head>
<body>
<div id="log"></div>
<div id="container" style="position: relative"></div>
<script>

const container = document.getElementById('container');

function testOffsetParentInShadowTree(mode) {
    test(function () {
        const host = document.createElement('div');
        container.appendChild(host);
        this.add_cleanup(() => host.remove());
        const shadowRoot = host.attachShadow({mode});
        shadowRoot.innerHTML = '<div id="relativeParent" style="position: relative; padding-left: 100px; padding-top: 70px;"><div id="target"></div></div>';
        const relativeParent = shadowRoot.getElementById('relativeParent');

        assert_true(relativeParent instanceof HTMLDivElement);
        const target = shadowRoot.getElementById('target');
        assert_equals(target.offsetParent, relativeParent);
        assert_equals(target.offsetLeft, 100);
        assert_equals(target.offsetTop, 70);
    }, `offsetParent must return the offset parent in the same shadow tree of ${mode} mode`);
}

testOffsetParentInShadowTree('open');
testOffsetParentInShadowTree('closed');

function testOffsetParentInNestedShadowTrees(mode) {
    test(function () {
        const outerHost = document.createElement('section');
        container.appendChild(outerHost);
        this.add_cleanup(() => outerHost.remove());
        const outerShadow = outerHost.attachShadow({mode});
        outerShadow.innerHTML = '<section id="outerParent" style="position: absolute; top: 50px; left: 50px;"></section>';

        const innerHost = document.createElement('div');
        outerShadow.firstChild.appendChild(innerHost);
        const innerShadow = innerHost.attachShadow({mode});
        innerShadow.innerHTML = '<div id="innerParent" style="position: relative; padding-left: 60px; padding-top: 40px;"><div id="target"></div></div>';
        const innerParent = innerShadow.getElementById('innerParent');

        const target = innerShadow.getElementById('target');
        assert_true(innerParent instanceof HTMLDivElement);
        assert_equals(target.offsetParent, innerParent);
        assert_equals(target.offsetLeft, 60);
        assert_equals(target.offsetTop, 40);

        outerHost.remove();
    }, `offsetParent must return the offset parent in the same shadow tree of ${mode} mode even when nested`);
}

testOffsetParentInNestedShadowTrees('open');
testOffsetParentInNestedShadowTrees('closed');

function testOffsetParentOnElementAssignedToSlotInsideOffsetParent(mode) {
    test(function () {
        const host = document.createElement('div');
        host.innerHTML = '<div id="target"></div>'
        container.appendChild(host);
        this.add_cleanup(() => host.remove());
        const shadowRoot = host.attachShadow({mode});
        shadowRoot.innerHTML = '<div style="position: relative; padding-left: 85px; padding-top: 45px;"><slot></slot></div>';
        const target = host.querySelector('#target');
        assert_equals(target.offsetParent, container);
        assert_equals(target.offsetLeft, 85);
        assert_equals(target.offsetTop, 45);
    }, `offsetParent must skip offset parents of an element when the context object is assigned to a slot in a shadow tree of ${mode} mode`);
}

testOffsetParentOnElementAssignedToSlotInsideOffsetParent('open');
testOffsetParentOnElementAssignedToSlotInsideOffsetParent('closed');

function testOffsetParentOnElementAssignedToSlotInsideFixedPositionWithContainingBlock(mode) {
    test(function () {
        const host = document.createElement('div');
        host.innerHTML = '<div id="target"></div>';
        container.appendChild(host);
        this.add_cleanup(() => host.remove());
        const shadowRoot = host.attachShadow({mode});
        shadowRoot.innerHTML = [
            '<div style="transform: translate(10px, 10px);" id="wrapper">',
            '<div style="position: fixed; padding-left: 85px; padding-top: 45px;">',
                '<slot></slot>',
            '</div></div>'].join('');
        const target = host.querySelector('#target');
        assert_equals(target.offsetParent, container);
        assert_equals(target.offsetLeft, 85);
        assert_equals(target.offsetTop, 45);
    }, `offsetParent must return the fixed position containing block of an element when the context object is assigned to a slot within a fixed containing block in shadow tree of ${mode} mode`);
}

testOffsetParentOnElementAssignedToSlotInsideFixedPositionWithContainingBlock('open');
testOffsetParentOnElementAssignedToSlotInsideFixedPositionWithContainingBlock('closed');

function testOffsetParentOnFixedElementAssignedToSlotInsideFixedPositionWithContainingBlock(mode) {
    test(function () {
        const host = document.createElement('div');
        host.innerHTML = '<div id="target" style="position: fixed;"></div>';
        container.appendChild(host);
        this.add_cleanup(() => host.remove());
        const shadowRoot = host.attachShadow({mode});
        shadowRoot.innerHTML = [
            '<div style="transform: translate(10px, 10px);" id="wrapper">',
            '<div style="position: fixed; padding-left: 85px; padding-top: 45px;">',
                '<slot></slot>',
            '</div></div>'].join('');
        const target = host.querySelector('#target');
        assert_equals(target.offsetParent, container);
        assert_equals(target.offsetLeft, 85);
        assert_equals(target.offsetTop, 45);
    }, `offsetParent must return the fixed position containing block of a fixed element when the context object is assigned to a slot within a fixed containing block in shadow tree of ${mode} mode`);
}

testOffsetParentOnFixedElementAssignedToSlotInsideFixedPositionWithContainingBlock('open');
testOffsetParentOnFixedElementAssignedToSlotInsideFixedPositionWithContainingBlock('closed');

function testOffsetParentOnElementAssignedToSlotInsideFixedPosition(mode) {
    test(function () {
        const host = document.createElement('div');
        host.innerHTML = '<div id="target"></div>';
        container.appendChild(host);
        this.add_cleanup(() => host.remove());
        const shadowRoot = host.attachShadow({mode});
        shadowRoot.innerHTML = [
            '<div id="fixed" style="position: fixed; padding-left: 85px; padding-top: 45px;">',
                '<slot></slot>',
            '</div>'].join('');
        const target = host.querySelector('#target');
        const fixed = shadowRoot.querySelector('#fixed');
        assert_equals(target.offsetParent, null);
        assert_equals(target.offsetLeft, 85 + fixed.offsetLeft);
        assert_equals(target.offsetTop, 45 + fixed.offsetTop);
    }, `offsetParent must return null when the context object is assigned to a slot without a fixed containing block in shadow tree of ${mode} mode`);
}

testOffsetParentOnElementAssignedToSlotInsideFixedPosition('open');
testOffsetParentOnElementAssignedToSlotInsideFixedPosition('closed');

function testOffsetParentOnElementAssignedToSlotInsideNestedOffsetParents(mode) {
    test(function () {
        const host = document.createElement('div');
        host.innerHTML = '<div id="target" style="border:solid 1px blue;">hi</div>';
        const previousBlock = document.createElement('div');
        previousBlock.style.height = '12px';
        container.append(previousBlock, host);
        this.add_cleanup(() => { container.innerHTML = ''; });
        const shadowRoot = host.attachShadow({mode});
        shadowRoot.innerHTML = '<section style="position: relative; margin-left: 20px; margin-top: 100px; background: #ccc"><div style="position: absolute; top: 10px; left: 10px;"><slot></slot></div></section>';
        const target = host.querySelector('#target');
        assert_equals(target.offsetParent, container);
        assert_equals(target.offsetLeft, 30);
        assert_equals(target.offsetTop, 122);
    }, `offsetParent must skip multiple offset parents of an element when the context object is assigned to a slot in a shadow tree of ${mode} mode`);
}

testOffsetParentOnElementAssignedToSlotInsideNestedOffsetParents('open');
testOffsetParentOnElementAssignedToSlotInsideNestedOffsetParents('closed');

function testOffsetParentOnElementAssignedToSlotInsideNestedShadowTrees(mode) {
    test(function () {
        const outerHost = document.createElement('section');
        outerHost.innerHTML = '<div id="target"></div>';
        container.appendChild(outerHost);
        this.add_cleanup(() => outerHost.remove());
        const outerShadow = outerHost.attachShadow({mode});
        outerShadow.innerHTML = '<section style="position: absolute; top: 40px; left: 50px;"><div id="innerHost"><slot></slot></div></section>';

        const innerShadow = outerShadow.getElementById('innerHost').attachShadow({mode});
        innerShadow.innerHTML = '<div style="position: absolute; top: 200px; margin-left: 100px;"><slot></slot></div>';

        const target = outerHost.querySelector('#target');
        assert_equals(target.offsetParent, container);
        assert_equals(target.offsetLeft, 150);
        assert_equals(target.offsetTop, 240);
        outerHost.remove();
    }, `offsetParent must skip offset parents of an element when the context object is assigned to a slot in nested shadow trees of ${mode} mode`);
}

testOffsetParentOnElementAssignedToSlotInsideNestedShadowTrees('open');
testOffsetParentOnElementAssignedToSlotInsideNestedShadowTrees('closed');

function testOffsetParentOnElementInsideShadowTreeWithoutOffsetParent(mode) {
    test(function () {
        const outerHost = document.createElement('section');
        container.appendChild(outerHost);
        this.add_cleanup(() => outerHost.remove());
        const outerShadow = outerHost.attachShadow({mode});
        outerShadow.innerHTML = '<div id="innerHost"><div id="target"></div></div>';

        const innerShadow = outerShadow.getElementById('innerHost').attachShadow({mode});
        innerShadow.innerHTML = '<div style="position: absolute; top: 23px; left: 24px;"><slot></slot></div>';

        const target = outerShadow.querySelector('#target');
        assert_equals(target.offsetParent, container);
        assert_equals(target.offsetLeft, 24);
        assert_equals(target.offsetTop, 23);
    }, `offsetParent must find the first offset parent which is a shadow-including ancestor of the context object even some shadow tree of ${mode} mode did not have any offset parent`);
}

testOffsetParentOnElementInsideShadowTreeWithoutOffsetParent('open');
testOffsetParentOnElementInsideShadowTreeWithoutOffsetParent('closed');

function testOffsetParentOnUnassignedChild(mode) {
    test(function () {
        const host = document.createElement('section');
        host.innerHTML = '<div id="target"></div>';
        this.add_cleanup(() => host.remove());
        container.appendChild(host);
        const shadowRoot = host.attachShadow({mode});
        shadowRoot.innerHTML = '<section style="position: absolute; top: 50px; left: 50px;">content</section>';
        const target = host.querySelector('#target');
        assert_equals(target.offsetParent, null);
        assert_equals(target.offsetLeft, 0);
        assert_equals(target.offsetTop, 0);
    }, `offsetParent must return null on a child element of a shadow host for the shadow tree in ${mode} mode which is not assigned to any slot`);
}

testOffsetParentOnUnassignedChild('open');
testOffsetParentOnUnassignedChild('closed');

function testOffsetParentOnAssignedChildNotInFlatTree(mode) {
    test(function () {
        const outerHost = document.createElement('section');
        outerHost.innerHTML = '<div id="target"></div>';
        container.appendChild(outerHost);
        this.add_cleanup(() => outerHost.remove());
        const outerShadow = outerHost.attachShadow({mode});
        outerShadow.innerHTML = '<div id="innerHost"><div style="position: absolute; top: 50px; left: 50px;"><slot></slot></div></div>';

        const innerShadow = outerShadow.getElementById('innerHost').attachShadow({mode});
        innerShadow.innerHTML = '<div>content</div>';

        const target = outerHost.querySelector('#target');
        assert_equals(target.offsetParent, null);
        assert_equals(target.offsetLeft, 0);
        assert_equals(target.offsetTop, 0);
    }, `offsetParent must return null on a child element of a shadow host for the shadow tree in ${mode} mode which is not in the flat tree`);
}

testOffsetParentOnAssignedChildNotInFlatTree('open');
testOffsetParentOnAssignedChildNotInFlatTree('closed');

</script>
</body>
</html>