Posted on 2008-09-24 19:57
G_G 阅读(504)
评论(0) 编辑 收藏
function trim(str){ return str.replace(/(^\s*)|(\s*$)/g,""); }
function recursiveNodes(array,dom,layer){
if( layer==0 ) return ;
if(layer>0){
--layer;
if( dom.hasChildNodes && dom.hasChildNodes() ){
for(var i=0;i<dom.childNodes.length;i++ ){
array.push(dom.childNodes.item(i));
recursiveNodes(array, dom.childNodes.item(i),layer ) ;
}
}else return ;
}else if(layer<0){
++layer;
if( dom.parentNode ){
array.push( dom.parentNode );
recursiveNodes( array,dom.parentNode,layer)
}else return ;
}
}
/*
[@name || && > < >= <= != @location='UK']
*/
function singleStep(dom,stepAction,stepStr){
var allArrData = [] ;
var arrData = [] ;
var num = stepAction.split(/\//).length ;
recursiveNodes(allArrData,dom,num==2?1:999);
stepStr = trim(stepStr) ;
var nodeName ;
var nodeNum ;
var conds = true ;
//判断单匹配 是否有 属性对比 [@..=..]
if( /\[(.*?)\]/.test(stepStr) ){
nodeName = trim( /.*?(?=\[{1})/g.exec(stepStr)[0] ).replace(/\*/g,".*") ;
conds = /\[(.*?)\]/.exec(stepStr)[1].replace(/\*/g,".*") ;
if(! isNaN(conds) ){
nodeNum = parseInt(conds)-1;
}else{
//属性 替换为
conds = conds.replace(/(@)(.*?)(?=\W)/g," allArrData[i].getAttribute('$2') ") ;
conds = conds.replace( /([\W]+)(.*?)::\'(.*?)\'/g," $1 /$3/g.test($2) " ) ;
conds = conds.replace( /(text\(\))/g ," allArrData[i].innerHTML ") ;
conds = conds.replace( /([\w\s])(=)/g ,"$2=") ;
}
}else{
nodeName = stepStr ;
}
for(var j=i=0;i<allArrData.length;i++){
if( eval('/'+nodeName+'/i.test( allArrData[i].nodeName ) ') ){
if(typeof nodeNum=="number"){
if(j==nodeNum){ arrData.push( allArrData[i] ); return arrData; }
else ++j;
}else if( eval(' allArrData[i].getAttribute && '+conds) ){
arrData.push( allArrData[i] );
}
}
}
return arrData ;
}
function getXpath(dom,xpathStr){
var xpaths = xpathStr.split(/(\/{1,2})/g);
var arrDom = [dom] ;
for(var i=1;i<xpaths.length;i=i+2){
var arrData = [];
for(var j=0;j<arrDom.length;j++)
arrData = arrData.concat( singleStep(arrDom[j],xpaths[i],xpaths[i+1]) );
arrDom = arrData ;
}
return arrDom ;
}
******************************************************************************************************
function trim(str){ return str.replace(/(^\s*)|(\s*$)/g,""); }
function recursiveNodes(array,dom,layer){
if( layer==0 ) return ;
if(layer>0){
--layer;
if( dom.hasChildNodes && dom.hasChildNodes() ){
for(var i=0;i<dom.childNodes.length;i++ ){
array.push(dom.childNodes.item(i));
recursiveNodes(array, dom.childNodes.item(i),layer ) ;
}
}else return ;
}else if(layer<0){
++layer;
if( dom.parentNode ){
array.push( dom.parentNode );
recursiveNodes( array,dom.parentNode,layer)
}else return ;
}
}
/*
[@name || && > < >= <= != @location='UK']
*/
function singleStep(dom,stepAction,stepStr){
var allArrData = [] ;
var arrData = [] ;
var num = stepAction.split(/\//).length ;
recursiveNodes(allArrData,dom,num==2?1:999);
stepStr = trim(stepStr) ;
var nodeName ;
var conds = true ;
//判断单匹配 是否有 属性对比 [@..=..]
if( /\[(.*?)\]/.test(stepStr) ){
nodeName = trim( /^(.*?)\[{1}/.exec(stepStr)[1] ) ;
//属性 替换为
conds = /\[(.*?)\]/.exec(stepStr)[1].replace(/(@)(.*?)(?=\W)/g," allArrData[i].getAttribute('$2').toString() ") ;
conds = conds.replace( /([\W]+)(.*?)::\'(.*?)\'/g," $1 /$3/g.test($2) " ) ;
conds = conds.replace( /(text\(\))/g ," allArrData[i].innerHTML ") ;
conds = conds.replace( /([\w\s])(=)/g ,"$2=") ;
}else{
nodeName = stepStr ;
}
for(var i=0;i<allArrData.length;i++){
if( new RegExp(nodeName, 'i').test( allArrData[i].nodeName ) && eval(conds) ){
arrData.push( allArrData[i] );
}
}
return arrData ;
}
function getXpath(dom,xpathStr){
var xpaths = xpathStr.split(/(\/{1,2})/g);
var arrDom = [dom] ;
for(var i=1;i<xpaths.length;i=i+2){
var arrData = [];
for(var j=0;j<arrDom.length;j++)
arrData = arrData.concat( singleStep(arrDom[j],xpaths[i],xpaths[i+1]) );
arrDom = arrData ;
}
return arrDom ;
}