Ticket #5467 (closed bug: fixed)
Patch for jQuery offset method for PlayStation 3 Silk browser
| Reported by: | ktrott | Owned by: | brandon |
|---|---|---|---|
| Priority: | major | Milestone: | 1.4 |
| Component: | offset | Version: | 1.3.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
The PlayStation 3 Silk browser likes to pretend it's a modern browser but it certainly isn't in all ways.
In jquery 1.3.2 which our site is using in production, the offset method that the silk browser uses is the non-getBoundingClientRect one.
This method assumes that document.defaultView exists, however, the Silk browser does not offer this and therefore does not have a method getComputedStyle. This causes an exception to be raised which Silk silently swallows.
I found that in another jQuery method: curCSS, there is proper detection logic to see if document.defaultView exists and if it doesn't it falls back to using the "currentStyle" property on the DOM element.
My patch essentially does the same fallback logic for the offset method.
Here it is:
jQuery.fn.offset = function() {
if ( !this[0] ) return { top: 0, left: 0 }; if ( this[0] === this[0].ownerDocument.body ) return jQuery.offset.bodyOffset( this[0] );
jQuery.offset.initialized jQuery.offset.initialize();
var elem = this[0], offsetParent = elem.offsetParent, prevOffsetParent = elem,
doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement, body = doc.body, defaultView = doc.defaultView,
prevComputedStyle = (defaultView) ? defaultView.getComputedStyle(elem, null) : elem.currentStyle, top = elem.offsetTop, left = elem.offsetLeft;
while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
computedStyle = (defaultView) ? defaultView.getComputedStyle(elem, null) : elem.currentStyle; top -= elem.scrollTop, left -= elem.scrollLeft; if ( elem === offsetParent ) {
top += elem.offsetTop, left += elem.offsetLeft; if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /t(able|d|h)$/i.test(elem.tagName)) )
top += parseInt( computedStyle.borderTopWidth, 10) 0, left += parseInt( computedStyle.borderLeftWidth, 10) 0; prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
} if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" )
top += parseInt( computedStyle.borderTopWidth, 10) 0, left += parseInt( computedStyle.borderLeftWidth, 10) 0; prevComputedStyle = computedStyle;
}
if ( prevComputedStyle.position === "relative" prevComputedStyle.position === "static" ) top += body.offsetTop, left += body.offsetLeft;
if ( prevComputedStyle.position === "fixed" )
top += Math.max(docElem.scrollTop, body.scrollTop), left += Math.max(docElem.scrollLeft, body.scrollLeft);
return { top: top, left: left };
};
Change History
comment:2 follow-up: ↓ 4 Changed 4 years ago by john
- Status changed from new to closed
- Resolution set to fixed
Landed this fix in the Git repo: http://github.com/jquery/jquery/commit/9a371e2d93d6e0a70b4c99e2ca44cbe14017fe5c
Thanks so much for the patch!
comment:4 in reply to: ↑ 2 Changed 2 years ago by anonymous
Replying to john:
Landed this fix in the Git repo: http://github.com/jquery/jquery/commit/9a371e2d93d6e0a70b4c99e2ca44cbe14017fe5c
Thanks so much for the patch!
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

That pasted quite horribly but essentially, the fix is the following:
line 8 of the method: old: prevComputedStyle = defaultView.getComputedStyle(elem, null),
new: prevComputedStyle = (defaultView) ? defaultView.getComputedStyle(elem, null) : elem.currentStyle,
line 12 of the method: old: computedStyle = defaultView.getComputedStyle(elem, null);
new: computedStyle = (defaultView) ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;