root / trunk / web / dojo / dojox / json / query.js @ 9
History | View | Annotate | Download (3.69 KB)
| 1 | 9 | andrej.cim | /*
|
|---|---|---|---|
| 2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
| 3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
| 4 | see: http://dojotoolkit.org/license for details
|
||
| 5 | */
|
||
| 6 | |||
| 7 | |||
| 8 | if(!dojo._hasResource["dojox.json.query"]){ |
||
| 9 | dojo._hasResource["dojox.json.query"]=true; |
||
| 10 | dojo.provide("dojox.json.query");
|
||
| 11 | (function(){
|
||
| 12 | dojox.json._slice=function(_1,_2,_3,_4){ |
||
| 13 | var _5=_1.length,_6=[];
|
||
| 14 | _3=_3||_5; |
||
| 15 | _2=(_2<0)?Math.max(0,_2+_5):Math.min(_5,_2); |
||
| 16 | _3=(_3<0)?Math.max(0,_3+_5):Math.min(_5,_3); |
||
| 17 | for(var i=_2;i<_3;i+=_4){ |
||
| 18 | _6.push(_1[i]); |
||
| 19 | } |
||
| 20 | return _6;
|
||
| 21 | }; |
||
| 22 | dojox.json._find=function e(_7,_8){ |
||
| 23 | var _9=[];
|
||
| 24 | function _a(_b){ |
||
| 25 | if(_8){
|
||
| 26 | if(_8===true&&!(_b instanceof Array)){ |
||
| 27 | _9.push(_b); |
||
| 28 | }else{
|
||
| 29 | if(_b[_8]){
|
||
| 30 | _9.push(_b[_8]); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | } |
||
| 34 | for(var i in _b){ |
||
| 35 | var _c=_b[i];
|
||
| 36 | if(!_8){
|
||
| 37 | _9.push(_c); |
||
| 38 | }else{
|
||
| 39 | if(_c&&typeof _c=="object"){ |
||
| 40 | _a(_c); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 | }; |
||
| 45 | if(_8 instanceof Array){ |
||
| 46 | if(_8.length==1){ |
||
| 47 | return _7[_8[0]]; |
||
| 48 | } |
||
| 49 | for(var i=0;i<_8.length;i++){ |
||
| 50 | _9.push(_7[_8[i]]); |
||
| 51 | } |
||
| 52 | }else{
|
||
| 53 | _a(_7); |
||
| 54 | } |
||
| 55 | return _9;
|
||
| 56 | }; |
||
| 57 | dojox.json._distinctFilter=function(_d,_e){ |
||
| 58 | var _f=[];
|
||
| 59 | var _10={};
|
||
| 60 | for(var i=0,l=_d.length;i<l;++i){ |
||
| 61 | var _11=_d[i];
|
||
| 62 | if(_e(_11,i,_d)){
|
||
| 63 | if((typeof _11=="object")&&_11){ |
||
| 64 | if(!_11.__included){
|
||
| 65 | _11.__included=true;
|
||
| 66 | _f.push(_11); |
||
| 67 | } |
||
| 68 | }else{
|
||
| 69 | if(!_10[_11+typeof _11]){ |
||
| 70 | _10[_11+typeof _11]=true; |
||
| 71 | _f.push(_11); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | for(i=0,l=_f.length;i<l;++i){ |
||
| 77 | if(_f[i]){
|
||
| 78 | delete _f[i].__included;
|
||
| 79 | } |
||
| 80 | } |
||
| 81 | return _f;
|
||
| 82 | }; |
||
| 83 | dojox.json.query=function(_12,obj){ |
||
| 84 | var _13=0; |
||
| 85 | var str=[];
|
||
| 86 | _12=_12.replace(/"(\\.|[^"\\])*"|'(\\.|[^'\\])*'|[\[\]]/g,function(t){ |
||
| 87 | _13+=t=="["?1:t=="]"?-1:0; |
||
| 88 | return (t=="]"&&_13>0)?"`]":(t.charAt(0)=="\""||t.charAt(0)=="'")?"`"+(str.push(t)-1):t; |
||
| 89 | }); |
||
| 90 | var _14=""; |
||
| 91 | function _15(_16){ |
||
| 92 | _14=_16+"("+_14;
|
||
| 93 | }; |
||
| 94 | function _17(t,a,b,c,d,e,f,g){ |
||
| 95 | return str[g].match(/[\*\?]/)||f=="~"?"/^"+str[g].substring(1,str[g].length-1).replace(/\\([btnfr\\"'])|([^\w\*\?])/g,"\\$1$2").replace(/([\*\?])/g,"[\\w\\W]$1")+(f=="~"?"$/i":"$/")+".test("+a+")":t; |
||
| 96 | }; |
||
| 97 | _12.replace(/(\]|\)|push|pop|shift|splice|sort|reverse)\s*\(/,function(){ |
||
| 98 | throw new Error("Unsafe function call"); |
||
| 99 | }); |
||
| 100 | _12=_12.replace(/([^=]=)([^=])/g,"$1=$2").replace(/@|(\.\s*)?[a-zA-Z\$_]+(\s*:)?/g,function(t){ |
||
| 101 | return t.charAt(0)=="."?t:t=="@"?"$obj":(t.match(/:|^(\$|Math|true|false|null)$/)?"":"$obj.")+t; |
||
| 102 | }).replace(/\.?\.?\[(`\]|[^\]])*\]|\?.*|\.\.([\w\$_]+)|\.\*/g,function(t,a,b){ |
||
| 103 | var _18=t.match(/^\.?\.?(\[\s*\^?\?|\^?\?|\[\s*==)(.*?)\]?$/); |
||
| 104 | if(_18){
|
||
| 105 | var _19=""; |
||
| 106 | if(t.match(/^\./)){ |
||
| 107 | _15("dojox.json._find");
|
||
| 108 | _19=",true)";
|
||
| 109 | } |
||
| 110 | _15(_18[1].match(/\=/)?"dojo.map":_18[1].match(/\^/)?"dojox.json._distinctFilter":"dojo.filter"); |
||
| 111 | return _19+",function($obj){return "+_18[2]+"})"; |
||
| 112 | } |
||
| 113 | _18=t.match(/^\[\s*([\/\\].*)\]/);
|
||
| 114 | if(_18){
|
||
| 115 | return ".concat().sort(function(a,b){"+_18[1].replace(/\s*,?\s*([\/\\])\s*([^,\\\/]+)/g,function(t,a,b){ |
||
| 116 | return "var av= "+b.replace(/\$obj/,"a")+",bv= "+b.replace(/\$obj/,"b")+";if(av>bv||bv==null){return "+(a=="/"?1:-1)+";}\n"+"if(bv>av||av==null){return "+(a=="/"?-1:1)+";}\n"; |
||
| 117 | })+"return 0;})";
|
||
| 118 | } |
||
| 119 | _18=t.match(/^\[(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)\]/);
|
||
| 120 | if(_18){
|
||
| 121 | _15("dojox.json._slice");
|
||
| 122 | return ","+(_18[1]||0)+","+(_18[2]||0)+","+(_18[3]||1)+")"; |
||
| 123 | } |
||
| 124 | if(t.match(/^\.\.|\.\*|\[\s*\*\s*\]|,/)){ |
||
| 125 | _15("dojox.json._find");
|
||
| 126 | return (t.charAt(1)=="."?",'"+b+"'":t.match(/,/)?","+t:"")+")"; |
||
| 127 | } |
||
| 128 | return t;
|
||
| 129 | }).replace(/(\$obj\s*((\.\s*[\w_$]+\s*)|(\[\s*`([0-9]+)\s*`\]))*)(==|~)\s*`([0-9]+)/g,_17).replace(/`([0-9]+)\s*(==|~)\s*(\$obj\s*((\.\s*[\w_$]+)|(\[\s*`([0-9]+)\s*`\]))*)/g,function(t,a,b,c,d,e,f,g){ |
||
| 130 | return _17(t,c,d,e,f,g,b,a);
|
||
| 131 | }); |
||
| 132 | _12=_14+(_12.charAt(0)=="$"?"":"$")+_12.replace(/`([0-9]+|\])/g,function(t,a){ |
||
| 133 | return a=="]"?"]":str[a]; |
||
| 134 | }); |
||
| 135 | var _1a=eval("1&&function($,$1,$2,$3,$4,$5,$6,$7,$8,$9){var $obj=$;return "+_12+"}"); |
||
| 136 | for(var i=0;i<arguments.length-1;i++){ |
||
| 137 | arguments[i]=arguments[i+1]; |
||
| 138 | } |
||
| 139 | return obj?_1a.apply(this,arguments):_1a; |
||
| 140 | }; |
||
| 141 | })(); |
||
| 142 | } |