1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 20:46:39 +02:00

domRectTest: added IntersectionObserverEntry tests

This commit is contained in:
kkapsner 2020-11-02 13:07:19 +01:00
parent 902d31b643
commit ef69e1dcb5
2 changed files with 41 additions and 2 deletions

View file

@ -31,11 +31,13 @@
const properties = ["x", "y", "width", "height", "top", "left", "right", "bottom"];
async function performTest(output, callback, useIframe = true){
const rects = getElements(useIframe).map(function(element){
const rects = (await Promise.all(getElements(useIframe).map(async function(element){
return {
name: element.dataset.name,
data: callback(element)
data: await callback(element)
};
}))).filter(function(rect){
return rect.data;
});
const data = new Float64Array(rects.length * properties.length);
rects.forEach(function(rect, i){
@ -134,6 +136,25 @@
container.appendChild(output);
performTest(output, callback, useIframe);
}
async function getIntersectionEntryValue(parentElement, element, property){
if (!(element instanceof Element)){
return null;
}
return new Promise(function(resolve){
const timeout = window.setTimeout(function(){
resolve(null);
}, 1000);
const observer = new IntersectionObserver(function(entries){
window.clearTimeout(timeout);
resolve(entries[0][property]);
}, {
root: parentElement,
rootMargin: "1000px",
});
observer.observe(element);
});
}
iframe.addEventListener("load", function(){
[true, false].forEach(function(useIframe){
createTest("Element.getClientRects", function(element){
@ -146,6 +167,15 @@
const quad = element.getBoxQuads();
return quad[0].getBounds();
}, useIframe);
createTest("IntersectionObserverEntry.intersectionRect", function(element){
return getIntersectionEntryValue(element.parentElement, element, "intersectionRect");
}, useIframe);
createTest("IntersectionObserverEntry.boundingClientRect", function(element){
return getIntersectionEntryValue(element.parentElement, element, "boundingClientRect");
}, useIframe);
createTest("IntersectionObserverEntry.rootBounds", function(element){
return getIntersectionEntryValue(element, element.firstChild, "rootBounds");
}, useIframe);
createTest("Range.getClientRects", function(element){
const range = document.createRange();
range.selectNode(element);