Repository navigation
Fix offsetLeft/offsetTop to match major browsers - #32761
Conversation
|
🔨 Triggering try run (#9900895406) for Linux WPT |
|
Test results for linux-wpt-layout-2020 from try job (#9900895406): Flaky unexpected result (25)
Stable unexpected results that are known to be intermittent (21)
Stable unexpected results (21)
|
|
|
c93b22d to
da40418
Compare
|
🔨 Triggering try run (#9901399968) for Linux WPT |
|
Test results for linux-wpt-layout-2020 from try job (#9901399968): Flaky unexpected result (19)
Stable unexpected results that are known to be intermittent (18)
|
|
✨ Try run (#9901399968) succeeded. |
da40418 to
83d5b4e
Compare
atbrakhi
left a comment
There was a problem hiding this comment.
So after going through the zulip discussion as well as the PR description, am I understanding it correctly that in this PR, we are moving away from what the specification says to match the behavior of other browser engine that is not correct according to the specification?
Yes, that is correct. I am assuming that if all major browser agree and WPT tests rely on a behaviour then it's likely to be the spec that ends up changing. |
Loirooriol
left a comment
There was a problem hiding this comment.
I think the specified behavior makes more sense but since nobody follows it...
| // In practice this is not true in major browsers in the case that the offsetParent is the body | ||
| // element and the body element is position:static. In that case offsetLeft/offsetTop are computed | ||
| // relative to the root nodes border box. |
There was a problem hiding this comment.
It seems that other browsers aren't interoperable either, I'm checking
<!DOCTYPE html>
<body id="body" onload="recompute()" style="border: solid">
<div id="target" style="height: 40px; width: 40px; background: red;"></div>
<div id="output" style="text-wrap: nowrap"></div>
<button onclick="recompute()">recompute</button>
<script type="text/javascript">
function recompute() {
let targetNode = document.getElementById("target");
let outputNode = document.getElementById("output");
outputNode.innerHTML = `
offsetParent: ${targetNode.offsetParent.id}<br />
offsetTop: ${targetNode.offsetTop}<br />
offsetLeft: ${targetNode.offsetLeft}<br />
`;
}
</script>
</body>and Firefox says 8, Chromium says 11.
There was a problem hiding this comment.
Hmm... interesting, so:
- I get 10 in Chromium (macOS) for your HTML above
- 1px of the difference just seems to be a different default border thickness. Pinning that to 1px gives me 9px in Chromium.
- Firefox's output of 8px looks like a bug to me. There is nothing that the node is offset 8px from.
But I guess the fact that there isn't perfect interoperability here might mean there is more scope for changing the behaviour.
There was a problem hiding this comment.
You getting 10 seems strange because the body should get a margin of 8px (https://html.spec.whatwg.org/multipage/rendering.html#the-page), and a border width defaults to 3px (https://drafts.csswg.org/css-backgrounds-3/#border-width). I guess that if your device has e.g. 0.5dppx, then the 3px would be 1.5 device pixels, which would be snapped to 1 device pixel, i.e. 2px. But I think 0.5dppx is weird.
Getting 8px on Firefox seems a bug indeed. This is what the offset includes:
| Firefox | Blink, WebKit | |
|---|---|---|
<html> margin |
❌ | ❌ |
<html> border |
✅ | ✅ |
<html> padding |
✅ | ✅ |
<body> margin |
✅ | ✅ |
<body> border |
❌ | ✅ |
<body> padding |
✅ | ✅ |
Note this happens even if the <body> in nested deeper in the tree (so it's not the body element), using its parent (probably containing block?) instead of the <html> (if it has no parent, then the margin,border,padding of the parent are skipped).
There was a problem hiding this comment.
a border width defaults to 3px (https://drafts.csswg.org/css-backgrounds-3/#border-width)
So the default border width in Chrome is weird on my machine (Chrome 130.0.6723.59 on macOS arm64). It is affected by both dppx and zoom level. And it seems that I am not the only one.
At 100% zoom (unzoomed), the default appears to be 3 device pixels. Which comes out at 1.5px at my machine's default resolution which has 2dppx. If I set my machine to it's native resolution then I get a 3px default border width.
If I zoom IN then default border width is DECREASED. If I zoom OUT then default border width is INCREASED.
A dppx of 2 (haven't tested others) does NOT affect an explicitly set medium size, and neither does zooming in, but zooming out does (but less than the default size - seems to be a rounding thing rather than a linear scaling as it is for the default border size).
Not sure if this is a bug in Chrome (using device pixels somewhere they should be using logical pixels?), or an intentional behaviour to have a "better default" for border widths.
There was a problem hiding this comment.
Looks like Firefox is tracking some of these issues, and that they're following an old IE model for offsetLeft/offsetTop that IE moved away from in IE8! https://bugzilla.mozilla.org/show_bug.cgi?id=255754 and https://bugzilla.mozilla.org/show_bug.cgi?id=1309427
And also just generally have a bunch of bugs in their offsetTop/offsetLeft code https://bugzilla.mozilla.org/buglist.cgi?quicksearch=offsetTop
| Fragment::Text(_) | | ||
| Fragment::Image(_) | | ||
| Fragment::IFrame(_) | | ||
| Fragment::Positioning(_) => unreachable!(), |
There was a problem hiding this comment.
This is not unreachable, e.g. this crashes
<!DOCTYPE html>
<html style="position: absolute">
<body id="body" onload="recompute()" style="border: solid">
<div id="target" style="height: 40px; width: 40px; background: red;"></div>
<div id="output" style="text-wrap: nowrap"></div>
<button onclick="recompute()">recompute</button>
<script type="text/javascript">
function recompute() {
let targetNode = document.getElementById("target");
let outputNode = document.getElementById("output");
outputNode.innerHTML = `
offsetParent: ${targetNode.offsetParent.id}<br />
offsetTop: ${targetNode.offsetTop}<br />
offsetLeft: ${targetNode.offsetLeft}<br />
`;
}
</script>
</body>There was a problem hiding this comment.
I have fixed this particular crash. Not sure if the root element can be any of the other variants.
83d5b4e to
a650ad8
Compare
80bd13e to
b712cd6
Compare
|
🔨 Triggering try run (#11427731040) for Linux WPT |
b712cd6 to
7a0659a
Compare
|
Test results for linux-wpt-layout-2020 from try job (#11427731040): Flaky unexpected result (15)
Stable unexpected results that are known to be intermittent (8)
Stable unexpected results (8)
|
|
|
|
🔨 Triggering try run (#11427926429) for Linux WPT |
0288668 to
a3d3d65
Compare
|
🔨 Triggering try run (#11552441587) for Linux WPT |
1b87323 to
52f10e2
Compare
|
🔨 Triggering try run (#11553188857) for Linux WPT |
|
🔨 Triggering try run (#11553906754) for Linux WPT |
|
Test results for linux-wpt-layout-2020 from try job (#11553906754): Flaky unexpected result (16)
Stable unexpected results that are known to be intermittent (7)
|
|
✨ Try run (#11553906754) succeeded. |
|
I think I've addressed all requested changes now |
Loirooriol
left a comment
There was a problem hiding this comment.
Thanks, just some nits for the comment.
See: w3c/csswg-drafts#10549 Signed-off-by: Nico Burns <[email protected]> Fix crash when <html> element is absolutely positioned Signed-off-by: Nico Burns <[email protected]>
Signed-off-by: Nico Burns <[email protected]>
Signed-off-by: Nico Burns <[email protected]>
Signed-off-by: Nico Burns <[email protected]>
Signed-off-by: Nico Burns <[email protected]>
Signed-off-by: Nico Burns <[email protected]>
Co-authored-by: Oriol Brufau <[email protected]> Signed-off-by: Nico Burns <[email protected]>
c669ee8 to
4fdfa69
Compare
|
🔨 Triggering try run (#11848983799) for Linux WPT |
|
Test results for linux-wpt-layout-2020 from try job (#11848983799): Flaky unexpected result (21)
Stable unexpected results that are known to be intermittent (12)
|
|
✨ Try run (#11848983799) succeeded. |
Although the spec (https://www.w3.org/TR/cssom-view-1/#extensions-to-the-htmlelement-interface) says that offsetTop/offsetLeft are always relative to the padding box of the offsetParent. In practice this is not true in major browsers in the case that the offsetParent is the body element and the body element is position:static. In that case offsetLeft/offsetTop are computed relative to the root nodes border box.
See: w3c/csswg-drafts#10549
This PR updates Servo's behaviour to match that of major browsers. Which causes a number of "false fails" in the WPT suite to become passes.
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsdata-offset-xanddata-offset-yproperties)