Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Migrate from document.defaultView => window. Fixes #10373 #524

Closed
wants to merge 4 commits into from

7 participants

Rick Waldron Paul Irish David Murdoch Mike Sherov Timmy Willison John-David Dalton Corey Frang
Rick Waldron
Collaborator

No description provided.

Paul Irish

link to ticket where i brought this up: http://bugs.jquery.com/ticket/10373

Paul Irish

You're aliasing window.getComputedStyle as a local getComputedStyle variable but that seems needless.
Instead, just check (window.getComputedStyle) in the conditionals and use it straight out within the block.

doing this you'd save another 4 (OMG FOUR!) bytes

Rick Waldron
Collaborator
247971   (-266) jquery.js
 93914   (-103) jquery.min.js
 33197    (-16) jquery.min.js.gz         
John-David Dalton

this breaks cross-iframe happiness cuz before it would use the element's ownerDocument.

David Murdoch

@jdalton, how's so? Are you making jQuery document aware? (http://bugs.jquery.com/ticket/8405#comment:10)

Mike Sherov
Collaborator

@jdalton, just curious... what difference does it make if we're using the getComputedStyle function of the window instead of the same function on the ownerDocument?

Timmy Willison
Collaborator

I share the same concern as @jdalton.

Rick Waldron
Collaborator

@jdalton thanks for the heads up, ping me on IRC :)

John-David Dalton

@davidmurdoch @mikesherov One of the cool things about being document agnostic is that you can have jQuery in one (i)frame/primary document and script elements in other (i)frames w/o also having jQuery loaded in all (i)frames. I'm not sure how much compat jQuery has for this, but I thought it was a rad enough feature to add to my own lib. So basically before:

  // elem is from another iframe so its document, window, & defaultView are different
  elem.ownerDocument.defaultView

Using the primary window's method kills that functionality.
The use of ownerDocument and the private getWindow() function makes me think that at one time cross-frame scripting was considered a feature.

Rick Waldron
Collaborator

This test confirms that it is iframe safe: http://jsfiddle.net/rwaldron/4NWcY/

Corey Frang
Owner

@jdalton - Little bit of research here - getComputedStyle doesn't seem to care what window its on...

John-David Dalton

It doesn't get the same result in FF 3.6; see this test.
The reason this raised red flags for me is normally foreign nodes cause errors to be thrown when inserting/attempting-to-mix-with the primary document's nodes.

Rick Waldron
Collaborator

Thanks @jdalton ... I just confirmed that FF3.6 is indeed unable to get the computed style unless called from the correct context

Perhaps when FF3.6 is EOL'ed, we can re-open this

Rick Waldron rwaldron closed this
Corey Frang
Owner

lame - but awesome - thanks for finding the hole @jdalton

John-David Dalton

It doesn't work in Fennec either (produces the same result as FF 3.6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 17 deletions.
  1. +8 −12 src/css.js
  2. +2 −3 src/offset.js
  3. +2 −2 src/support.js
20 src/css.js
View
@@ -13,8 +13,7 @@ var ralpha = /alpha\([^)]*\)/i,
cssHeight = [ "Top", "Bottom" ],
curCSS,
- getComputedStyle,
- currentStyle;
+ computedStyle, currentStyle;
jQuery.fn.css = function( name, value ) {
// Setting 'undefined' is a no-op
@@ -262,18 +261,15 @@ jQuery(function() {
}
});
-if ( document.defaultView && document.defaultView.getComputedStyle ) {
- getComputedStyle = function( elem, name ) {
- var ret, defaultView, computedStyle;
+if ( window.getComputedStyle ) {
+ computedStyle = function( elem, name ) {
+ var ret,
+ computed = getComputedStyle( elem, null );
name = name.replace( rupper, "-$1" ).toLowerCase();
- if ( !(defaultView = elem.ownerDocument.defaultView) ) {
- return undefined;
- }
-
- if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) {
- ret = computedStyle.getPropertyValue( name );
+ if ( computed ) {
+ ret = computed.getPropertyValue( name );
if ( ret === "" && !jQuery.contains( elem.ownerDocument.documentElement, elem ) ) {
ret = jQuery.style( elem, name );
}
@@ -317,7 +313,7 @@ if ( document.documentElement.currentStyle ) {
};
}
-curCSS = getComputedStyle || currentStyle;
+curCSS = computedStyle || currentStyle;
function getWH( elem, name, extra ) {
5 src/offset.js
View
@@ -69,8 +69,7 @@ if ( "getBoundingClientRect" in document.documentElement ) {
doc = elem.ownerDocument,
docElem = doc.documentElement,
body = doc.body,
- defaultView = doc.defaultView,
- prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
+ prevComputedStyle = window.getComputedStyle ? getComputedStyle( elem, null ) : elem.currentStyle,
top = elem.offsetTop,
left = elem.offsetLeft;
@@ -79,7 +78,7 @@ if ( "getBoundingClientRect" in document.documentElement ) {
break;
}
- computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
+ computedStyle = window.getComputedStyle ? getComputedStyle(elem, null) : elem.currentStyle;
top -= elem.scrollTop;
left -= elem.scrollLeft;
4 src/support.js
View
@@ -219,13 +219,13 @@ jQuery.support = (function() {
// info see bug #3333
// Fails in WebKit before Feb 2011 nightlies
// WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
- if ( document.defaultView && document.defaultView.getComputedStyle ) {
+ if ( window.getComputedStyle ) {
marginDiv = document.createElement( "div" );
marginDiv.style.width = "0";
marginDiv.style.marginRight = "0";
div.appendChild( marginDiv );
support.reliableMarginRight =
- ( parseInt( ( document.defaultView.getComputedStyle( marginDiv, null ) || { marginRight: 0 } ).marginRight, 10 ) || 0 ) === 0;
+ ( parseInt( ( getComputedStyle( marginDiv, null ) || { marginRight: 0 } ).marginRight, 10 ) || 0 ) === 0;
}
// Remove the body element we added
Something went wrong with that request. Please try again.