-
Notifications
You must be signed in to change notification settings - Fork 54
/
cross-origin-subframe.html
141 lines (130 loc) · 3.73 KB
/
cross-origin-subframe.html
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
<!DOCTYPE html>
<div style="height: 200px; width: 100px;"></div>
<div id="target" style="background-color: green; width:100px; height:100px"></div>
<div id="empty-target" style="width: 100px"></div>
<div style="height: 200px; width: 100px;"></div>
<script>
var port;
var entries = [];
var target = document.getElementById("target");
var emptyTarget = document.getElementById("empty-target");
var scroller = document.scrollingElement;
var nextStep;
function clientRectToJson(rect) {
if (!rect)
return "null";
return {
top: rect.top,
right: rect.right,
bottom: rect.bottom,
left: rect.left
};
}
function entryToJson(entry) {
return {
boundingClientRect: clientRectToJson(entry.boundingClientRect),
intersectionRect: clientRectToJson(entry.intersectionRect),
rootBounds: clientRectToJson(entry.rootBounds),
isIntersecting: entry.isIntersecting,
target: entry.target.id
};
}
// Note that we never use RAF in this code, because this frame might get render-throttled.
// Instead of RAF-ing, we just post an empty message to the parent window, which will
// RAF when it is received, and then send us a message to cause the next step to run.
// Use a rootMargin here, and verify it does NOT get applied for the cross-origin case.
var observer = new IntersectionObserver(function(changes) {
entries = entries.concat(changes)
}, { rootMargin: "7px" });
observer.observe(target);
observer.observe(emptyTarget);
function step0() {
entries = entries.concat(observer.takeRecords());
nextStep = step1;
var expected = [{
boundingClientRect: [8, 108, 208, 308],
intersectionRect: [0, 0, 0, 0],
rootBounds: "null",
isIntersecting: false,
target: target.id
}, {
boundingClientRect: [8, 108, 308, 308],
intersectionRect: [0, 0, 0, 0],
rootBounds: "null",
isIntersecting: false,
target: emptyTarget.id
}];
port.postMessage({
actual: entries.map(entryToJson),
expected: expected,
description: "First rAF"
}, "*");
entries = [];
port.postMessage({scrollTo: 200}, "*");
}
function step1() {
entries = entries.concat(observer.takeRecords());
port.postMessage({
actual: entries.map(entryToJson),
expected: [],
description: "topDocument.scrollingElement.scrollTop = 200"
}, "*");
entries = [];
scroller.scrollTop = 250;
nextStep = step2;
port.postMessage({}, "*");
}
function step2() {
entries = entries.concat(observer.takeRecords());
var expected = [{
boundingClientRect: [8, 108, -42, 58],
intersectionRect: [8, 108, 0, 58],
rootBounds: "null",
isIntersecting: true,
target: target.id
}, {
boundingClientRect: [8, 108, 58, 58],
intersectionRect: [8, 108, 58, 58],
rootBounds: "null",
isIntersecting: true,
target: emptyTarget.id
}];
port.postMessage({
actual: entries.map(entryToJson),
expected: expected,
description: "iframeDocument.scrollingElement.scrollTop = 250"
}, "*");
entries = [];
nextStep = step3;
port.postMessage({scrollTo: 100}, "*");
}
function step3() {
entries = entries.concat(observer.takeRecords());
var expected = [{
boundingClientRect: [8, 108, -42, 58],
intersectionRect: [0, 0, 0, 0],
rootBounds: "null",
isIntersecting: false,
target: target.id
}, {
boundingClientRect: [8, 108, 58, 58],
intersectionRect: [0, 0, 0, 0],
rootBounds: "null",
isIntersecting: false,
target: emptyTarget.id
}];
port.postMessage({
actual: entries.map(entryToJson),
expected: expected,
description: "topDocument.scrollingElement.scrollTop = 100"
}, "*");
port.postMessage({DONE: 1}, "*");
}
function handleMessage(event)
{
port = event.source;
nextStep();
}
nextStep = step0;
window.addEventListener("message", handleMessage);
</script>