simpsora
6 Apr 2007, 10:05 AM
Hi,
I've been using DomQuery with great success lately -- it's an excellent library.
Recently, I modified my copy of DomQuery to work around an IE annoyance (some would call it a bug), and I wanted to submit it back to you, in case you wanted to include it in the source.
The issue is with getElementById(). In IE, if an element with the given id value is not found, it will fall back to searching for an element whose name attribute matches the given id value, and return the first match.
The fix merely compares the passed-in id against the id of the returned element, and either returns the element if they match, or null if not.
Since DomQuery calls getElementById() three times, I created a convenience method to do the check. The diff (against http://www.yui-ext.com/deploy/ext-1.0-alpha3/source/core/DomQuery.js) follows:
$ diff -u DomQuery.js DomQuery_rs.js
--- DomQuery.js 2007-04-06 11:39:24.000000000 -0600
+++ DomQuery_rs.js 2007-04-06 11:48:11.000000000 -0600
@@ -284,11 +284,16 @@
}
return r;
}
+
+ function gebi(parent, id) {
+ var r = parent.getElementById(id);
+ return r && r.id === id ? r : null;
+ }
function quickId(ns, mode, root, id){
if(ns == root){
var d = root.ownerDocument || root;
- return d.getElementById(id);
+ return gebi(d, id);
}
ns = getNodes(ns, mode, "*");
return byId(ns, null, id);
@@ -381,7 +386,7 @@
root = document;
}
if(typeof root == "string"){
- root = document.getElementById(root);
+ root = gebi(document, root);
}
var paths = path.split(",");
var results = [];
@@ -448,7 +453,7 @@
*/
is : function(el, ss){
if(typeof el == "string"){
- el = document.getElementById(el);
+ el = gebi(document, el);
}
var isArray = (el instanceof Array);
var result = Ext.DomQuery.filter(isArray ? el : [el], ss);
Thanks
I've been using DomQuery with great success lately -- it's an excellent library.
Recently, I modified my copy of DomQuery to work around an IE annoyance (some would call it a bug), and I wanted to submit it back to you, in case you wanted to include it in the source.
The issue is with getElementById(). In IE, if an element with the given id value is not found, it will fall back to searching for an element whose name attribute matches the given id value, and return the first match.
The fix merely compares the passed-in id against the id of the returned element, and either returns the element if they match, or null if not.
Since DomQuery calls getElementById() three times, I created a convenience method to do the check. The diff (against http://www.yui-ext.com/deploy/ext-1.0-alpha3/source/core/DomQuery.js) follows:
$ diff -u DomQuery.js DomQuery_rs.js
--- DomQuery.js 2007-04-06 11:39:24.000000000 -0600
+++ DomQuery_rs.js 2007-04-06 11:48:11.000000000 -0600
@@ -284,11 +284,16 @@
}
return r;
}
+
+ function gebi(parent, id) {
+ var r = parent.getElementById(id);
+ return r && r.id === id ? r : null;
+ }
function quickId(ns, mode, root, id){
if(ns == root){
var d = root.ownerDocument || root;
- return d.getElementById(id);
+ return gebi(d, id);
}
ns = getNodes(ns, mode, "*");
return byId(ns, null, id);
@@ -381,7 +386,7 @@
root = document;
}
if(typeof root == "string"){
- root = document.getElementById(root);
+ root = gebi(document, root);
}
var paths = path.split(",");
var results = [];
@@ -448,7 +453,7 @@
*/
is : function(el, ss){
if(typeof el == "string"){
- el = document.getElementById(el);
+ el = gebi(document, el);
}
var isArray = (el instanceof Array);
var result = Ext.DomQuery.filter(isArray ? el : [el], ss);
Thanks