Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

SPEED - awesome speed improvement to attribute selectors, while still…

… keeping it safe
  • Loading branch information...
commit 17fbec2abb154903def4ffb62cb30418299e02ef 1 parent 8cb540c
Fabio M. Costa authored February 19, 2011
4  SlickSpec/specs/jquery.js
@@ -179,7 +179,9 @@ var specsJQuery = function(context){
179 179
 
180 180
 		same( jQuery("#form").find("input[name=action]").get(), q("text1"), "Name selector within the context of another element" );
181 181
 		same( jQuery("#form").find("input[name='foo[bar]']").get(), q("hidden2"), "Name selector for grouped form element within the context of another element" );
182  
-		
  182
+
  183
+		t( "Form with attribute action and an input with the same id inside it", "#form[action='formaction']", ["form"] );
  184
+
183 185
 		// fabiomcosta: maybe these specs where forgotten here by mistake, these ids/names are not in the jquery mock.
184 186
 		//var a = jQuery('<a id="tName1ID" name="tName1">tName1 A</a><a id="tName2ID" name="tName2">tName2 A</a><div id="tName1">tName1 Div</div>').appendTo('#main');
185 187
 		//t( "Find elements that have similar IDs", "[name=tName1]", ["tName1ID"] );
2  SlickSpec/specs/mock_template.js
@@ -29,7 +29,7 @@ var specsMockTemplate = function(context){
29 29
 	// removes 'tel:' 'a' tags that are just grabbed by iphone
30 30
 	//   and the META tag that puts IE8 in compatible mode <meta http-equiv="X-UA-Compatible" content="IE=7" />
31 31
 	//   and script tags
32  
-	it_should_find(1819, '*:not([href^=tel:]):not([http-equiv="X-UA-Compatible"]):not(script)');
  32
+	it_should_find(1818, '*:not([href^=tel:]):not(script):not(meta)');
33 33
 	it_should_find(1814, 'body *:not([href^=tel:])');
34 34
 	
35 35
 	it_should_find(1, 'html');
52  Source/Slick.Finder.js
@@ -51,7 +51,8 @@ local.setDocument = function(document){
51 51
 	= false;
52 52
 
53 53
 	var starSelectsClosed, starSelectsComments,
54  
-		brokenSecondClassNameGEBCN, cachedGetElementsByClassName;
  54
+		brokenSecondClassNameGEBCN, cachedGetElementsByClassName,
  55
+		brokenFormAttributeGetter;
55 56
 
56 57
 	var selected, id = 'slick_uniqueid';
57 58
 	var testNode = document.createElement('div');
@@ -132,9 +133,15 @@ local.setDocument = function(document){
132 133
 				testNode.innerHTML = '<a class=""></a>';
133 134
 				this.brokenEmptyAttributeQSA = (testNode.querySelectorAll('[class*=""]').length != 0);
134 135
 			} catch(e){};
135  
-			
  136
+
136 137
 		}
137  
-		
  138
+
  139
+		// IE6-7, if a form has an input of id x, form.getAttribute(x) returns a reference to the input
  140
+		try {
  141
+			testNode.innerHTML = '<form action="s"><input id="action"/></form>';
  142
+			brokenFormAttributeGetter = (testNode.firstChild.getAttribute('action') != 's');
  143
+		} catch(e){};
  144
+
138 145
 		// native matchesSelector function
139 146
 
140 147
 		this.nativeMatchesSelector = root.matchesSelector || root.msMatchesSelector || root.mozMatchesSelector || root.webkitMatchesSelector;
@@ -148,7 +155,7 @@ local.setDocument = function(document){
148 155
 
149 156
 	testRoot.removeChild(testNode);
150 157
 	testNode = selected = testRoot = null;
151  
-	
  158
+
152 159
 	// hasAttribute
153 160
 
154 161
 	this.hasAttribute = (root && this.isNativeCode(root.hasAttribute)) ? function(node, attribute) {
@@ -190,6 +197,20 @@ local.setDocument = function(document){
190 197
 		return aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
191 198
 	} : null ;
192 199
 
  200
+	// get attribute
  201
+
  202
+	this.getAttribute = (!this.isHTMLDocument) ? function(node, name){
  203
+		return node.getAttribute(name);
  204
+	} : (brokenFormAttributeGetter) ? function(node, name){
  205
+		var method = this.attributeGetters[name];
  206
+		if (method) return method.call(node);
  207
+		var attributeNode = node.getAttributeNode(name);
  208
+		return (attributeNode) ? attributeNode.nodeValue : null;
  209
+	} : function(node, name){
  210
+		var method = this.attributeGetters[name];
  211
+		return (method) ? method.call(node) : node.getAttribute(name);
  212
+	};
  213
+
193 214
 	this.getUID = (this.isHTMLDocument) ? this.getUIDHTML : this.getUIDXML;
194 215
 
195 216
 	root = null;
@@ -309,7 +330,7 @@ local.search = function(context, expression, append, first){
309 330
 			}
310 331
 
311 332
 			if (this.starSelectsClosedQSA) for (i = 0; node = nodes[i++];){
312  
-				if (node.nodeName > '@' && (!hasOthers || !uniques[this.getUIDHTML(node)])) found.push(node);
  333
+				if (node.nodeName > '@' && !(hasOthers && uniques[this.getUID(node)])) found.push(node);
313 334
 			} else for (i = 0; node = nodes[i++];){
314 335
 				if (!(hasOthers && uniques[this.getUID(node)])) found.push(node);
315 336
 			}
@@ -333,10 +354,10 @@ local.search = function(context, expression, append, first){
333 354
 		return found;
334 355
 	}
335 356
 
336  
-	// cache elements for the nth selectors
337  
-
338 357
 	/*<pseudo-selectors>*//*<nth-pseudo-selectors>*/
339 358
 
  359
+	// cache elements for the nth selectors
  360
+
340 361
 	this.posNTH = {};
341 362
 	this.posNTHLast = {};
342 363
 	this.posNTHType = {};
@@ -798,19 +819,19 @@ for (var p in pseudos) local['pseudo:' + p] = pseudos[p];
798 819
 local.attributeGetters = {
799 820
 
800 821
 	'class': function(){
801  
-		return this.getAttribute('class') || this.className;
  822
+		return this.className;
802 823
 	},
803 824
 
804 825
 	'for': function(){
805  
-		return ('htmlFor' in this) ? this.htmlFor : this.getAttribute('for');
  826
+		return this.htmlFor;
806 827
 	},
807 828
 
808 829
 	'href': function(){
809  
-		return ('href' in this) ? this.getAttribute('href', 2) : this.getAttribute('href');
  830
+		return this.getAttribute('href', 2);
810 831
 	},
811 832
 
812 833
 	'style': function(){
813  
-		return (this.style) ? this.style.cssText : this.getAttribute('style');
  834
+		return this.style.cssText;
814 835
 	},
815 836
 	
816 837
 	'tabindex': function(){
@@ -820,15 +841,6 @@ local.attributeGetters = {
820 841
 
821 842
 };
822 843
 
823  
-local.getAttribute = function(node, name){
824  
-	// FIXME: check if getAttribute() will get input elements on a form on this browser
825  
-	// getAttribute is faster than getAttributeNode().nodeValue
826  
-	var method = this.attributeGetters[name];
827  
-	if (method) return method.call(node);
828  
-	var attributeNode = node.getAttributeNode(name);
829  
-	return (attributeNode) ? attributeNode.nodeValue : null;
830  
-};
831  
-
832 844
 // Slick
833 845
 
834 846
 var Slick = local.Slick = (this.Slick || {});

0 notes on commit 17fbec2

Please sign in to comment.
Something went wrong with that request. Please try again.