Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect highlight bounds checks #420

Merged
merged 2 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 16 additions & 26 deletions src/adapter/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,20 @@ export interface Measurements {
};
}

function getBoundsState(rect: {
top: number;
height: number;
left: number;
width: number;
}) {
return {
top: rect.top + window.pageYOffset < window.scrollY,
bottom: rect.top + rect.height > window.innerHeight + scrollY,
left: rect.left + window.pageXOffset < window.scrollX,
right: rect.left + rect.width > window.scrollX + window.innerWidth,
};
}

export function measureNode(dom: Element): Measurements {
const s = window.getComputedStyle(dom);
const r = dom.getBoundingClientRect();

const top = r.top + window.pageYOffset;
const left = r.left + window.pageXOffset;

return {
top,
left,
bounds: getBoundsState(r),
top: r.top + window.scrollY,
left: r.left + window.scrollX,
// Attention: getBoundingClientRect() is relative to the viewport.
bounds: {
top: r.top + r.height <= 0,
bottom: r.top >= window.innerHeight,
left: r.left + r.width <= 0,
right: r.left >= window.innerWidth,
},
boxSizing: s.boxSizing,

// Round to at most 2 decimals. This is not 100% accurate,
Expand Down Expand Up @@ -90,12 +79,13 @@ export function mergeMeasure(a: Measurements, b: Measurements): Measurements {
boxSizing: a.boxSizing,
top,
left,
bounds: getBoundsState({
height,
left,
top,
width,
}),
// Attention: We're dealing with absolute coordinates here
bounds: {
top: top + height <= window.scrollY,
bottom: top >= window.scrollY + window.innerHeight,
left: left + width <= window.scrollX,
right: left >= window.scrollX + window.innerWidth,
},
width,
height,

Expand Down
2 changes: 1 addition & 1 deletion test-e2e/tests/highlight-iframe.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from "@playwright/test";
import { gotoTest, wait } from "../pw-utils";

test.only("Highlight iframe nodes", async ({ page }) => {
test("Highlight iframe nodes", async ({ page }) => {
const { devtools } = await gotoTest(page, "iframe");

await devtools
Expand Down