domquery.js
来自「php绿色服务器,让大家试用greenamp」· JavaScript 代码 · 共 716 行 · 第 1/2 页
JS
716 行
break;
}
}
// prevent infinite loop on bad selector
if(!matched){
throw 'Error parsing selector, parsing failed at "' + q + '"';
}
}
if(mm[1]){
fn[fn.length] = 'mode="'+mm[1]+'";';
q = q.replace(mm[1], "");
}
}
fn[fn.length] = "return nodup(n);\n}";
eval(fn.join(""));
return f;
},
/**
* Selects a group of elements.
* @param {String} selector The selector/xpath query
* @param {Node} root (optional) The start of the query (defaults to document).
* @return {Array}
*/
select : function(path, root, type){
if(!root || root == document){
root = document;
}
if(typeof root == "string"){
root = document.getElementById(root);
}
var paths = path.split(",");
var results = [];
for(var i = 0, len = paths.length; i < len; i++){
var p = paths[i].replace(trimRe, "");
if(!cache[p]){
cache[p] = Ext.DomQuery.compile(p);
if(!cache[p]){
throw p + " is not a valid selector";
}
}
var result = cache[p](root);
if(result && result != document){
results = results.concat(result);
}
}
return results;
},
/**
* Selects a single element.
* @param {String} selector The selector/xpath query
* @param {Node} root (optional) The start of the query (defaults to document).
* @return {Element}
*/
selectNode : function(path, root){
return Ext.DomQuery.select(path, root)[0];
},
/**
* Selects the value of a node, optionally replacing null with the defaultValue.
* @param {String} selector The selector/xpath query
* @param {Node} root (optional) The start of the query (defaults to document).
* @param {String} defaultValue
*/
selectValue : function(path, root, defaultValue){
path = path.replace(trimRe, "");
if(!valueCache[path]){
valueCache[path] = Ext.DomQuery.compile(path, "select");
}
var n = valueCache[path](root);
n = n[0] ? n[0] : n;
var v = (n && n.firstChild ? n.firstChild.nodeValue : null);
return (v === null ? defaultValue : v);
},
/**
* Selects the value of a node, parsing integers and floats.
* @param {String} selector The selector/xpath query
* @param {Node} root (optional) The start of the query (defaults to document).
* @param {Number} defaultValue
* @return {Number}
*/
selectNumber : function(path, root, defaultValue){
var v = Ext.DomQuery.selectValue(path, root, defaultValue || 0);
return parseFloat(v);
},
/**
* Returns true if the passed element(s) match the passed simple selector (e.g. div.some-class or span:first-child)
* @param {String/HTMLElement/Array} el An element id, element or array of elements
* @param {String} selector The simple selector to test
* @return {Boolean}
*/
is : function(el, ss){
if(typeof el == "string"){
el = document.getElementById(el);
}
var isArray = (el instanceof Array);
var result = Ext.DomQuery.filter(isArray ? el : [el], ss);
return isArray ? (result.length == el.length) : (result.length > 0);
},
/**
* Filters an array of elements to only include matches of a simple selector (e.g. div.some-class or span:first-child)
* @param {Array} el An array of elements to filter
* @param {String} selector The simple selector to test
* @param {Boolean} nonMatches If true, it returns the elements that DON'T match
* the selector instead of the ones that match
* @return {Array}
*/
filter : function(els, ss, nonMatches){
ss = ss.replace(trimRe, "");
if(!simpleCache[ss]){
simpleCache[ss] = Ext.DomQuery.compile(ss, "simple");
}
var result = simpleCache[ss](els);
return nonMatches ? quickDiff(result, els) : result;
},
/**
* Collection of matching regular expressions and code snippets.
*/
matchers : [{
re: /^\.([\w-]+)/,
select: 'n = byClassName(n, null, " {1} ");'
}, {
re: /^\:([\w-]+)(?:\(((?:[^\s>\/]*|.*?))\))?/,
select: 'n = byPseudo(n, "{1}", "{2}");'
},{
re: /^(?:([\[\{])(?:@)?([\w-]+)\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]\}])/,
select: 'n = byAttribute(n, "{2}", "{4}", "{3}", "{1}");'
}, {
re: /^#([\w-]+)/,
select: 'n = byId(n, null, "{1}");'
},{
re: /^@([\w-]+)/,
select: 'return {firstChild:{nodeValue:attrValue(n, "{1}")}};'
}
],
/**
* Collection of operator comparison functions. The default operators are =, !=, ^=, $=, *= and %=.
* New operators can be added as long as the match the format <i>c</i>= where <i>c<i> is any character other than space, > <.
*/
operators : {
"=" : function(a, v){
return a == v;
},
"!=" : function(a, v){
return a != v;
},
"^=" : function(a, v){
return a && a.substr(0, v.length) == v;
},
"$=" : function(a, v){
return a && a.substr(a.length-v.length) == v;
},
"*=" : function(a, v){
return a && a.indexOf(v) !== -1;
},
"%=" : function(a, v){
return (a % v) == 0;
}
},
/**
* Collection of "pseudo class" processors. Each processor is passed the current nodeset (array)
* and the argument (if any) supplied in the selector.
*/
pseudos : {
"first-child" : function(c){
var r = [], n;
for(var i = 0, ci; ci = n = c[i]; i++){
while((n = n.previousSibling) && n.nodeType != 1);
if(!n){
r[r.length] = ci;
}
}
return r;
},
"last-child" : function(c){
var r = [];
for(var i = 0, ci; ci = n = c[i]; i++){
while((n = n.nextSibling) && n.nodeType != 1);
if(!n){
r[r.length] = ci;
}
}
return r;
},
"nth-child" : function(c, a){
var r = [];
if(a != "odd" && a != "even"){
for(var i = 0, ci; ci = c[i]; i++){
var m = child(ci.parentNode, a);
if(m == ci){
r[r.length] = m;
}
}
return r;
}
var p;
// first let's clean up the parent nodes
for(var i = 0, l = c.length; i < l; i++){
var cp = c[i].parentNode;
if(cp != p){
clean(cp);
p = cp;
}
}
// then lets see if we match
for(var i = 0, ci; ci = c[i]; i++){
var m = false;
if(a == "odd"){
m = ((ci.nodeIndex+1) % 2 == 1);
}else if(a == "even"){
m = ((ci.nodeIndex+1) % 2 == 0);
}
if(m){
r[r.length] = ci;
}
}
return r;
},
"only-child" : function(c){
var r = [];
for(var i = 0, ci; ci = c[i]; i++){
if(!prev(ci) && !next(ci)){
r[r.length] = ci;
}
}
return r;
},
"empty" : function(c){
var r = [];
for(var i = 0, ci; ci = c[i]; i++){
var cns = ci.childNodes, j = 0, cn, empty = true;
while(cn = cns[j]){
++j;
if(cn.nodeType == 1 || cn.nodeType == 3){
empty = false;
break;
}
}
if(empty){
r[r.length] = ci;
}
}
return r;
},
"contains" : function(c, v){
var r = [];
for(var i = 0, ci; ci = c[i]; i++){
if(ci.innerHTML.indexOf(v) !== -1){
r[r.length] = ci;
}
}
return r;
},
"nodeValue" : function(c, v){
var r = [];
for(var i = 0, ci; ci = c[i]; i++){
if(ci.firstChild && ci.firstChild.nodeValue == v){
r[r.length] = ci;
}
}
return r;
},
"checked" : function(c){
var r = [];
for(var i = 0, ci; ci = c[i]; i++){
if(ci.checked == true){
r[r.length] = ci;
}
}
return r;
},
"not" : function(c, ss){
return Ext.DomQuery.filter(c, ss, true);
},
"odd" : function(c){
return this["nth-child"](c, "odd");
},
"even" : function(c){
return this["nth-child"](c, "even");
},
"nth" : function(c, a){
return c[a-1] || [];
},
"first" : function(c){
return c[0] || [];
},
"last" : function(c){
return c[c.length-1] || [];
},
"has" : function(c, ss){
var s = Ext.DomQuery.select;
var r = [];
for(var i = 0, ci; ci = c[i]; i++){
if(s(ss, ci).length > 0){
r[r.length] = ci;
}
}
return r;
},
"next" : function(c, ss){
var is = Ext.DomQuery.is;
var r = [];
for(var i = 0, ci; ci = c[i]; i++){
var n = next(ci);
if(n && is(n, ss)){
r[r.length] = ci;
}
}
return r;
},
"prev" : function(c, ss){
var is = Ext.DomQuery.is;
var r = [];
for(var i = 0, ci; ci = c[i]; i++){
var n = prev(ci);
if(n && is(n, ss)){
r[r.length] = ci;
}
}
return r;
}
}
};
}();
/**
* Selects an array of DOM nodes by CSS/XPath selector. Shorthand of {@link Ext.DomQuery#select}
* @param {String} path The selector/xpath query
* @param {Node} root (optional) The start of the query (defaults to document).
* @return {Array}
* @member Ext
* @method query
*/
Ext.query = Ext.DomQuery.select;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?