Skip to main content

Bug Tracker

Side navigation

#5467 closed bug (fixed)

Opened November 07, 2009 03:58AM UTC

Closed November 07, 2009 01:58PM UTC

Last modified February 01, 2011 12:56AM UTC

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:
Blocked by: Blocking:
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 };

};

Attachments (0)
Change History (4)

Changed November 07, 2009 04:00AM UTC by ktrott comment:1

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;

Changed November 07, 2009 01:58PM UTC by john comment:2

resolution: → fixed
status: newclosed

Landed this fix in the Git repo:

http://github.com/jquery/jquery/commit/9a371e2d93d6e0a70b4c99e2ca44cbe14017fe5c

Thanks so much for the patch!

Changed February 01, 2011 12:54AM UTC by anonymous comment:3

Changed February 01, 2011 12:56AM UTC by anonymous comment:4

Replying to [comment:2 john]:

Landed this fix in the Git repo: http://github.com/jquery/jquery/commit/9a371e2d93d6e0a70b4c99e2ca44cbe14017fe5c Thanks so much for the patch!