Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

fixes #10996 (simplify offset) #726

Closed
wants to merge 2 commits into from

4 participants

Mike Sherov Rick Waldron Sindre Sorhus Dave Methvin
Mike Sherov
Collaborator

In all of jQuery's supported browsers, either getBoundingClientRect() or window.webkitConvertPointFromNodeToPage() exists, thereby eliminating the need for the old lengthy getOffset function. Good riddance!

http://bugs.jquery.com/ticket/10996

In case anyone is curious, and I know you are, here is the size diff:

jQuery Size - compared to last make
  249938  (-2956) jquery.js
   93290  (-1499) jquery.min.js
   33167   (-470) jquery.min.js.gz
build/jshint-check.js
... ...
@@ -12,7 +12,8 @@
12 12
 			smarttabs: true,
13 13
 			predef: [
14 14
 				"define",
15  
-				"DOMParser"
  15
+				"DOMParser",
  16
+				"WebKitPoint",
1
Rick Waldron Collaborator
rwaldron added a note April 03, 2012

I know it's not an issue, really... but drop the trailing comma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Rick Waldron
Collaborator

If this is real, I'm impressed.

added some commits December 24, 2011
Mike Sherov remove old offset code 4b0e240
Mike Sherov function parity with getBoundingClientRect
return new object literal in all cases
style and JSHint fixes
e84fafe
Sindre Sorhus

Woah, if only there were more pull requests like this! :D

Dave Methvin
Owner

Landed in 1.8pre: 77536f5

"In recent news, github.com reports that their disk usage decreased significantly thanks to a recent @mikesherov pull request."

Dave Methvin dmethvin closed this April 05, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Apr 03, 2012
Mike Sherov remove old offset code 4b0e240
Mike Sherov function parity with getBoundingClientRect
return new object literal in all cases
style and JSHint fixes
e84fafe
This page is out of date. Refresh to see the latest.
3  build/jshint-check.js
@@ -12,7 +12,8 @@
12 12
 			smarttabs: true,
13 13
 			predef: [
14 14
 				"define",
15  
-				"DOMParser"
  15
+				"DOMParser",
  16
+				"WebKitPoint"
16 17
 			],
17 18
 			maxerr: 100
18 19
 		};
57  src/offset.js
... ...
@@ -1,11 +1,12 @@
1 1
 (function( jQuery ) {
2 2
 
3 3
 var getOffset,
4  
-	rtable = /^t(?:able|d|h)$/i,
5 4
 	rroot = /^(?:body|html)$/i;
6 5
 
7 6
 if ( "getBoundingClientRect" in document.documentElement ) {
8  
-	getOffset = function( elem, doc, docElem, box ) {
  7
+	getOffset = function( elem, doc, docElem ) {
  8
+		var box;
  9
+
9 10
 		try {
10 11
 			box = elem.getBoundingClientRect();
11 12
 		} catch(e) {}
@@ -29,56 +30,12 @@ if ( "getBoundingClientRect" in document.documentElement ) {
29 30
 
30 31
 } else {
31 32
 	getOffset = function( elem, doc, docElem ) {
32  
-		var computedStyle,
33  
-			offsetParent = elem.offsetParent,
34  
-			prevOffsetParent = elem,
35  
-			body = doc.body,
36  
-			defaultView = doc.defaultView,
37  
-			prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
38  
-			top = elem.offsetTop,
39  
-			left = elem.offsetLeft;
40  
-
41  
-		while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
42  
-			if ( jQuery.support.fixedPosition && prevComputedStyle.position === "fixed" ) {
43  
-				break;
44  
-			}
45  
-
46  
-			computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
47  
-			top  -= elem.scrollTop;
48  
-			left -= elem.scrollLeft;
49  
-
50  
-			if ( elem === offsetParent ) {
51  
-				top  += elem.offsetTop;
52  
-				left += elem.offsetLeft;
53  
-
54  
-				if ( jQuery.support.doesNotAddBorder && !(jQuery.support.doesAddBorderForTableAndCells && rtable.test(elem.nodeName)) ) {
55  
-					top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
56  
-					left += parseFloat( computedStyle.borderLeftWidth ) || 0;
57  
-				}
58  
-
59  
-				prevOffsetParent = offsetParent;
60  
-				offsetParent = elem.offsetParent;
61  
-			}
62  
-
63  
-			if ( jQuery.support.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
64  
-				top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
65  
-				left += parseFloat( computedStyle.borderLeftWidth ) || 0;
66  
-			}
67  
-
68  
-			prevComputedStyle = computedStyle;
69  
-		}
70  
-
71  
-		if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
72  
-			top  += body.offsetTop;
73  
-			left += body.offsetLeft;
74  
-		}
75  
-
76  
-		if ( jQuery.support.fixedPosition && prevComputedStyle.position === "fixed" ) {
77  
-			top  += Math.max( docElem.scrollTop, body.scrollTop );
78  
-			left += Math.max( docElem.scrollLeft, body.scrollLeft );
  33
+		if ( !jQuery.contains( docElem, elem ) ) {
  34
+			return { top: 0, left: 0 };
79 35
 		}
  36
+		var point = getWindow( doc ).webkitConvertPointFromNodeToPage( elem, new WebKitPoint( 0, 0 ) );
  37
+		return { top: point.y, left: point.x };
80 38
 
81  
-		return { top: top, left: left };
82 39
 	};
83 40
 }
84 41
 
41  src/support.js
@@ -178,9 +178,8 @@ jQuery.support = (function() {
178 178
 
179 179
 	// Run tests that need a body at doc ready
180 180
 	jQuery(function() {
181  
-		var container, outer, inner, table, td, offsetSupport,
182  
-			marginDiv, conMarginTop, style, html, positionTopLeftWidthHeight,
183  
-			paddingMarginBorderVisibility, paddingMarginBorder,
  181
+		var container, offsetSupport, marginDiv,
  182
+			conMarginTop = 1,
184 183
 			body = document.getElementsByTagName("body")[0];
185 184
 
186 185
 		if ( !body ) {
@@ -188,17 +187,8 @@ jQuery.support = (function() {
188 187
 			return;
189 188
 		}
190 189
 
191  
-		conMarginTop = 1;
192  
-		paddingMarginBorder = "padding:0;margin:0;border:";
193  
-		positionTopLeftWidthHeight = "position:absolute;top:0;left:0;width:1px;height:1px;";
194  
-		paddingMarginBorderVisibility = paddingMarginBorder + "0;visibility:hidden;";
195  
-		style = "style='" + positionTopLeftWidthHeight + paddingMarginBorder + "5px solid #000;";
196  
-		html = "<div " + style + "display:block;'><div style='" + paddingMarginBorder + "0;display:block;overflow:hidden;'></div></div>" +
197  
-			"<table " + style + "' cellpadding='0' cellspacing='0'>" +
198  
-			"<tr><td></td></tr></table>";
199  
-
200 190
 		container = document.createElement("div");
201  
-		container.style.cssText = paddingMarginBorderVisibility + "width:0;height:0;position:static;top:0;margin-top:" + conMarginTop + "px";
  191
+		container.style.cssText = "visibility:hidden;border:0;width:0;height:0;position:static;top:0;margin-top:" + conMarginTop + "px";
202 192
 		body.insertBefore( container, body.firstChild );
203 193
 
204 194
 		// Construct the test element
@@ -212,7 +202,7 @@ jQuery.support = (function() {
212 202
 		// display:none (it is still safe to use offsets if a parent element is
213 203
 		// hidden; don safety goggles and see bug #4512 for more information).
214 204
 		// (only IE 8 fails this test)
215  
-		div.innerHTML = "<table><tr><td style='" + paddingMarginBorder + "0;display:none'></td><td>t</td></tr></table>";
  205
+		div.innerHTML = "<table><tr><td style='padding:0;margin:0;border:0;display:none'></td><td>t</td></tr></table>";
216 206
 		tds = div.getElementsByTagName( "td" );
217 207
 		isSupported = ( tds[ 0 ].offsetHeight === 0 );
218 208
 
@@ -260,31 +250,10 @@ jQuery.support = (function() {
260 250
 			support.shrinkWrapBlocks = ( div.offsetWidth !== 3 );
261 251
 		}
262 252
 
263  
-		div.style.cssText = positionTopLeftWidthHeight + paddingMarginBorderVisibility;
264  
-		div.innerHTML = html;
265  
-
266  
-		outer = div.firstChild;
267  
-		inner = outer.firstChild;
268  
-		td = outer.nextSibling.firstChild.firstChild;
269  
-
270 253
 		offsetSupport = {
271  
-			doesNotAddBorder: ( inner.offsetTop !== 5 ),
272  
-			doesAddBorderForTableAndCells: ( td.offsetTop === 5 )
  254
+			doesNotIncludeMarginInBodyOffset: ( body.offsetTop !== conMarginTop )
273 255
 		};
274 256
 
275  
-		inner.style.position = "fixed";
276  
-		inner.style.top = "20px";
277  
-
278  
-		// safari subtracts parent border width here which is 5px
279  
-		offsetSupport.fixedPosition = ( inner.offsetTop === 20 || inner.offsetTop === 15 );
280  
-		inner.style.position = inner.style.top = "";
281  
-
282  
-		outer.style.overflow = "hidden";
283  
-		outer.style.position = "relative";
284  
-
285  
-		offsetSupport.subtractsBorderForOverflowNotVisible = ( inner.offsetTop === -5 );
286  
-		offsetSupport.doesNotIncludeMarginInBodyOffset = ( body.offsetTop !== conMarginTop );
287  
-
288 257
 		if ( window.getComputedStyle ) {
289 258
 			div.style.marginTop = "1%";
290 259
 			support.pixelMargin = ( window.getComputedStyle( div, null ) || { marginTop: 0 } ).marginTop !== "1%";
24  test/unit/support.js
@@ -98,10 +98,6 @@ if ( /chrome\/16\.0/i.test(userAgent) ) {
98 98
 			"reliableHiddenOffsets":true,
99 99
 			"ajax":true,
100 100
 			"cors":true,
101  
-			"doesNotAddBorder":true,
102  
-			"doesAddBorderForTableAndCells":false,
103  
-			"fixedPosition":true,
104  
-			"subtractsBorderForOverflowNotVisible":false,
105 101
 			"doesNotIncludeMarginInBodyOffset":true
106 102
 		};
107 103
 		for ( i in expected ) {
@@ -141,10 +137,6 @@ if ( /chrome\/16\.0/i.test(userAgent) ) {
141 137
 			"reliableHiddenOffsets":false,
142 138
 			"ajax":true,
143 139
 			"cors":false,
144  
-			"doesNotAddBorder":false,
145  
-			"doesAddBorderForTableAndCells":true,
146  
-			"fixedPosition":true,
147  
-			"subtractsBorderForOverflowNotVisible":false,
148 140
 			"doesNotIncludeMarginInBodyOffset":true
149 141
 		};
150 142
 		for ( i in expected ) {
@@ -164,11 +156,8 @@ if ( /chrome\/16\.0/i.test(userAgent) ) {
164 156
 			"cors": false,
165 157
 			"cssFloat": false,
166 158
 			"deleteExpando": false,
167  
-			"doesAddBorderForTableAndCells": true,
168  
-			"doesNotAddBorder": true,
169 159
 			"doesNotIncludeMarginInBodyOffset": true,
170 160
 			"enctype": true,
171  
-			"fixedPosition": true,
172 161
 			"focusinBubbles": true,
173 162
 			"getSetAttribute": false,
174 163
 			"hrefNormalized": false,
@@ -186,7 +175,6 @@ if ( /chrome\/16\.0/i.test(userAgent) ) {
186 175
 			"reliableMarginRight": true,
187 176
 			"shrinkWrapBlocks": false,
188 177
 			"submitBubbles": false,
189  
-			"subtractsBorderForOverflowNotVisible": false,
190 178
 			"tbody": false,
191 179
 			"style": false
192 180
 		};
@@ -227,10 +215,6 @@ if ( /chrome\/16\.0/i.test(userAgent) ) {
227 215
 			"reliableHiddenOffsets":false,
228 216
 			"ajax":true,
229 217
 			"cors":false,
230  
-			"doesNotAddBorder":true,
231  
-			"doesAddBorderForTableAndCells":true,
232  
-			"fixedPosition":false,
233  
-			"subtractsBorderForOverflowNotVisible":false,
234 218
 			"doesNotIncludeMarginInBodyOffset":true
235 219
 		};
236 220
 		for ( i in expected ) {
@@ -270,10 +254,6 @@ if ( /chrome\/16\.0/i.test(userAgent) ) {
270 254
 			"reliableHiddenOffsets":true,
271 255
 			"ajax":true,
272 256
 			"cors":true,
273  
-			"doesNotAddBorder":true,
274  
-			"doesAddBorderForTableAndCells":false,
275  
-			"fixedPosition":true,
276  
-			"subtractsBorderForOverflowNotVisible":false,
277 257
 			"doesNotIncludeMarginInBodyOffset":true
278 258
 		};
279 259
 		for ( i in expected ) {
@@ -313,10 +293,6 @@ if ( /chrome\/16\.0/i.test(userAgent) ) {
313 293
 			"reliableHiddenOffsets":true,
314 294
 			"ajax":true,
315 295
 			"cors":true,
316  
-			"doesNotAddBorder":true,
317  
-			"doesAddBorderForTableAndCells":true,
318  
-			"fixedPosition":true,
319  
-			"subtractsBorderForOverflowNotVisible":false,
320 296
 			"doesNotIncludeMarginInBodyOffset":true
321 297
 		};
322 298
 		for ( i in expected ) {
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.