Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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 rwaldron closed this September 29, 2011
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

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.
20  src/css.js
@@ -13,8 +13,7 @@ var ralpha = /alpha\([^)]*\)/i,
13 13
 	cssHeight = [ "Top", "Bottom" ],
14 14
 	curCSS,
15 15
 
16  
-	getComputedStyle,
17  
-	currentStyle;
  16
+	computedStyle, currentStyle;
18 17
 
19 18
 jQuery.fn.css = function( name, value ) {
20 19
 	// Setting 'undefined' is a no-op
@@ -262,18 +261,15 @@ jQuery(function() {
262 261
 	}
263 262
 });
264 263
 
265  
-if ( document.defaultView && document.defaultView.getComputedStyle ) {
266  
-	getComputedStyle = function( elem, name ) {
267  
-		var ret, defaultView, computedStyle;
  264
+if ( window.getComputedStyle ) {
  265
+	computedStyle = function( elem, name ) {
  266
+		var ret,
  267
+		computed = getComputedStyle( elem, null );
268 268
 
269 269
 		name = name.replace( rupper, "-$1" ).toLowerCase();
270 270
 
271  
-		if ( !(defaultView = elem.ownerDocument.defaultView) ) {
272  
-			return undefined;
273  
-		}
274  
-
275  
-		if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) {
276  
-			ret = computedStyle.getPropertyValue( name );
  271
+		if ( computed ) {
  272
+			ret = computed.getPropertyValue( name );
277 273
 			if ( ret === "" && !jQuery.contains( elem.ownerDocument.documentElement, elem ) ) {
278 274
 				ret = jQuery.style( elem, name );
279 275
 			}
@@ -317,7 +313,7 @@ if ( document.documentElement.currentStyle ) {
317 313
 	};
318 314
 }
319 315
 
320  
-curCSS = getComputedStyle || currentStyle;
  316
+curCSS = computedStyle || currentStyle;
321 317
 
322 318
 function getWH( elem, name, extra ) {
323 319
 
5  src/offset.js
@@ -69,8 +69,7 @@ if ( "getBoundingClientRect" in document.documentElement ) {
69 69
 			doc = elem.ownerDocument,
70 70
 			docElem = doc.documentElement,
71 71
 			body = doc.body,
72  
-			defaultView = doc.defaultView,
73  
-			prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
  72
+			prevComputedStyle = window.getComputedStyle ? getComputedStyle( elem, null ) : elem.currentStyle,
74 73
 			top = elem.offsetTop,
75 74
 			left = elem.offsetLeft;
76 75
 
@@ -79,7 +78,7 @@ if ( "getBoundingClientRect" in document.documentElement ) {
79 78
 				break;
80 79
 			}
81 80
 
82  
-			computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
  81
+			computedStyle = window.getComputedStyle ? getComputedStyle(elem, null) : elem.currentStyle;
83 82
 			top  -= elem.scrollTop;
84 83
 			left -= elem.scrollLeft;
85 84
 
4  src/support.js
@@ -219,13 +219,13 @@ jQuery.support = (function() {
219 219
 	// info see bug #3333
220 220
 	// Fails in WebKit before Feb 2011 nightlies
221 221
 	// WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
222  
-	if ( document.defaultView && document.defaultView.getComputedStyle ) {
  222
+	if ( window.getComputedStyle ) {
223 223
 		marginDiv = document.createElement( "div" );
224 224
 		marginDiv.style.width = "0";
225 225
 		marginDiv.style.marginRight = "0";
226 226
 		div.appendChild( marginDiv );
227 227
 		support.reliableMarginRight =
228  
-			( parseInt( ( document.defaultView.getComputedStyle( marginDiv, null ) || { marginRight: 0 } ).marginRight, 10 ) || 0 ) === 0;
  228
+			( parseInt( ( getComputedStyle( marginDiv, null ) || { marginRight: 0 } ).marginRight, 10 ) || 0 ) === 0;
229 229
 	}
230 230
 
231 231
 	// Remove the body element we added
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.