Open Bug 1465960 Opened 7 years ago Updated 2 years ago

document.documentElement.offsetLeft and document.documentElement.offsetTop return zero even when html has margin

Categories

(Core :: DOM: CSS Object Model, defect, P3)

60 Branch
defect

Tracking

()

Tracking Status
firefox60 --- affected
firefox61 --- affected
firefox62 --- affected

People

(Reporter: nmrock, Unassigned)

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36 Steps to reproduce: Set css: html{ margin-left: 50px; margin-top:300px;} Checked values of: document.documentElement.offsetTop document.documentElement.offsetLeft Actual results: Got zero for both. Expected results: document.documentElement.offsetLeft should have returned: 50 document.documentElement.offsetTop should have returned: 300
Hi nmrock, Please create a test page for this issue and past it here in order to be able to test it. Thanks
Flags: needinfo?(nmrock)
Attached file Test page
In Chrome/Safari, the circle tracks the user's cursor. In Firefox, document.documentElement.offsetLeft and document.documentElement.offsetTop do not return the correct values. So the circle is offset from the cursor by 300px on top, 50px on the left.
Flags: needinfo?(nmrock)
Hi nmrock, Thanks for your test page. I tested this issue on Mac OS X 10.12 and Windows 10, with FF NIghtly 62.0a1(2018-06-05) and with FF release 60 and I can reproduce it.
Status: UNCONFIRMED → NEW
Component: Untriaged → CSS Parsing and Computation
Ever confirmed: true
OS: Unspecified → All
Product: Firefox → Core
Hardware: Unspecified → All
I suspect there needs to be a dupe for this one, but I didn't find it from a quick search... The spec says to return 0 for the <body> element, and we have similar logic for the document element: https://searchfox.org/mozilla-central/rev/3737701cfab93ccea04c0e9cab211ad10f931d87/dom/html/nsGenericHTMLElement.cpp#247 Maybe Boris knows more about the context here.
Component: CSS Parsing and Computation → DOM: CSS Object Model
The context is probably "this was all reverse-engineered at some point and no one trusts the spec because there is no indication (e.g. tests) that it's web-compatible". In general, the spec says that offsetParent is null for both the body and the root. I think that at one point it said that offsetLeft/Top was offset from parent and 0 if no parent. That's clearly not what it says now, though. So we should probably fix that.
That said, Edge and IE do the same thing as Gecko. So this is the historical behavior of these properties, WebKit just implemented something different, and whoever was writing the spec specced the WebKit behavior (why? because he was working on WebKit...) So there's a question about compat fallout here for sure, since it's not just us that has the behavior and it's the long-standing behavior ever since these properties were introduced.
Priority: -- → P3

I am able to reproduce this bug in Firefox Nightly 105 using attached test case and it shows:

document.documentElement.offsetTop
-1
document.documentElement.offsetLeft
-1

While both other browsers (Safari 15.6 & Chrome Canary 106) show same expected output:

document.documentElement.offsetLeft should have returned: 50
document.documentElement.offsetTop should have returned: 300

Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: