browser/libs/dom.js
function _matchElement(parentNodes, name, notAllChildren) {
    var elems;
    var tag = 0;  // 0-tag, 1-id, 2-class.
    var nameattr, nameattrVal;

    // :checked, :disabled
    var selector = name.split(':');
    name = selector[0];
    name = stringUtils.trim(name);
    selector = selector[1];
    if (selector) {
      if (selector != 'checked' && selector != 'disabled') {
        throw new Error('only support `:checked or :disabled` selector');
      }
    }

    // attri.
    if (name.indexOf('[') > 0) {
      var iblace = name.indexOf('[');
      nameattr = name.substring(iblace+1, name.length-1);
      nameattr = nameattr.split('=');
      if (nameattr.length != 2) {
        throw new Error('Syntax error, unrecognized expression: ' + name);
      }
      nameattrVal = nameattr[1];
      if (nameattrVal.indexOf('\'') >= 0 || nameattrVal.indexOf('"') >= 0)
        nameattrVal = nameattrVal.substring(1, nameattrVal.length-1);
      nameattr = nameattr[0];
      name = name.substr(0, iblace);
    }
    
    if (name[0] == '.') {
      tag = 2;
      name = name.substr(1);
    }
    else if (name[0] == '#') {
      tag = 1;
      name = name.substr(1);
    }

    if (!parentNodes || parentNodes.length == 0) {
      if (2 == tag) {
        elems = window.document.getElementsByClassName(name);
      }
      else if (1 == tag) {
        elems = window.document.getElementById(name);
        if (elems) elems = [elems];
        else elems = [];
      }
      else if (0 == tag) {
        elems = window.document.getElementsByTagName(name);
      }

      // attrvalue.
      if (nameattr) {
        var tt_elems = elems;
        elems = [];
        for (var i = 0; i < tt_elems.length; i++) {
          if (tt_elems[i].getAttribute(nameattr) === nameattrVal) {
            elems.push(tt_elems[i]);
          }
        }
      } // if.

      if (selector) {
        var tt_elems = elems;
        elems = [];
        for (var i = 0; i < tt_elems.length; i++) {
          if (selector == 'disabled') {
            if (tt_elems[i].disabled) {
              elems.push(tt_elems[i]);  
            }
          }
          else 
          if (selector == 'checked') {
            if (tt_elems[i].checked) {
              elems.push(tt_elems[i]);  
            }
          }
        }
      } // if.
    }
    else {
      elems = [];
      for (var i = 0; i < parentNodes.length; i++) {
        var node1 = parentNodes[i].childNodes;
        if (!node1) continue;
        var node = [];
        for (var j = 0; j < node1.length; j++) {
          node.push(node1[j]);
        }

        for (var j = 0; j < node.length; j++) {
          var add = true;

          if (selector) {
            if (selector == 'disabled') {
              if (!node[j].disabled) {
                add = false;
              }
            }
            else 
            if (selector == 'checked') {
              if (!node[j].checked) {
                add = false;
              }
            }
          } // if.

          // attrvalue.
          if (add && nameattr) {
            if (node[j].getAttribute(nameattr) !== nameattrVal) {
              add = false;
            }
          }

          if (add) {
            if (2 == tag) {
              if (_hasClass(node[j], name)) {
                elems.push(node[j]);
                continue;
              }
            }
            else if (1 == tag) {
              if (node[j].id == name) {
                elems.push(node[j]);
                continue;
              }
            }
            else if (0 == tag) {
              if (node[j].nodeName.toUpperCase() == name.toUpperCase()) {
                elems.push(node[j]);
                continue;
              }
            }
          } // if.

          if (!notAllChildren) {
            var nn = node[j].childNodes;
            if (nn && nn.length > 0) {
              for (var k = 0; k < nn.length; k++) {
                node.push(nn[k]);
              }
              if (j > 20) {
                node = node.slice(j+1);
                j = -1;
              }
            }
          }
        } // for.
      } // for.
    } // if..else.

    return elems;
  }
function _matchElement(parentNodes, name, notAllChildren) {
    var elems;
    var tag = 0;  // 0-tag, 1-id, 2-class.
    var nameattr, nameattrVal;

    // :checked, :disabled
    var selector = name.split(':');
    name = selector[0];
    name = stringUtils.trim(name);
    selector = selector[1];
    if (selector) {
      if (selector != 'checked' && selector != 'disabled') {
        throw new Error('only support `:checked or :disabled` selector');
      }
    }

    // attri.
    if (name.indexOf('[') > 0) {
      var iblace = name.indexOf('[');
      nameattr = name.substring(iblace+1, name.length-1);
      nameattr = nameattr.split('=');
      if (nameattr.length != 2) {
        throw new Error('Syntax error, unrecognized expression: ' + name);
      }
      nameattrVal = nameattr[1];
      if (nameattrVal.indexOf('\'') >= 0 || nameattrVal.indexOf('"') >= 0)
        nameattrVal = nameattrVal.substring(1, nameattrVal.length-1);
      nameattr = nameattr[0];
      name = name.substr(0, iblace);
    }
    
    if (name[0] == '.') {
      tag = 2;
      name = name.substr(1);
    }
    else if (name[0] == '#') {
      tag = 1;
      name = name.substr(1);
    }

    if (!parentNodes || parentNodes.length == 0) {
      if (2 == tag) {
        elems = window.document.getElementsByClassName(name);
      }
      else if (1 == tag) {
        elems = window.document.getElementById(name);
        if (elems) elems = [elems];
        else elems = [];
      }
      else if (0 == tag) {
        elems = window.document.getElementsByTagName(name);
      }

      // attrvalue.
      if (nameattr) {
        var tt_elems = elems;
        elems = [];
        for (var i = 0; i < tt_elems.length; i++) {
          if (tt_elems[i].getAttribute(nameattr) === nameattrVal) {
            elems.push(tt_elems[i]);
          }
        }
      } // if.

      if (selector) {
        var tt_elems = elems;
        elems = [];
        for (var i = 0; i < tt_elems.length; i++) {
          if (selector == 'disabled') {
            if (tt_elems[i].disabled) {
              elems.push(tt_elems[i]);  
            }
          }
          else 
          if (selector == 'checked') {
            if (tt_elems[i].checked) {
              elems.push(tt_elems[i]);  
            }
          }
        }
      } // if.
    }
    else {
      elems = [];
      for (var i = 0; i < parentNodes.length; i++) {
        var node1 = parentNodes[i].childNodes;
        if (!node1) continue;
        var node = [];
        for (var j = 0; j < node1.length; j++) {
          node.push(node1[j]);
        }

        for (var j = 0; j < node.length; j++) {
          var add = true;

          if (selector) {
            if (selector == 'disabled') {
              if (!node[j].disabled) {
                add = false;
              }
            }
            else 
            if (selector == 'checked') {
              if (!node[j].checked) {
                add = false;
              }
            }
          } // if.

          // attrvalue.
          if (add && nameattr) {
            if (node[j].getAttribute(nameattr) !== nameattrVal) {
              add = false;
            }
          }

          if (add) {
            if (2 == tag) {
              if (_hasClass(node[j], name)) {
                elems.push(node[j]);
                continue;
              }
            }
            else if (1 == tag) {
              if (node[j].id == name) {
                elems.push(node[j]);
                continue;
              }
            }
            else if (0 == tag) {
              if (node[j].nodeName.toUpperCase() == name.toUpperCase()) {
                elems.push(node[j]);
                continue;
              }
            }
          } // if.

          if (!notAllChildren) {
            var nn = node[j].childNodes;
            if (nn && nn.length > 0) {
              for (var k = 0; k < nn.length; k++) {
                node.push(nn[k]);
              }
              if (j > 20) {
                node = node.slice(j+1);
                j = -1;
              }
            }
          }
        } // for.
      } // for.
    } // if..else.

    return elems;
  }
function _matchElement ( parentNodes , name , notAllChildren ) { var elems ; var tag = 0 ; // 0-tag, 1-id, 2-class. var nameattr , nameattrVal ; // :checked, :disabled var selector = name . split ( ':' ) ; name = selector [ 0 ] ; name = stringUtils . trim ( name ) ; selector = selector [ 1 ] ; if ( selector ) { if ( selector != 'checked' && selector != 'disabled' ) { throw new Error ( 'only support `:checked or :disabled` selector' ) ; } } // attri. if ( name . indexOf ( '[' ) > 0 ) { var iblace = name . indexOf ( '[' ) ; nameattr = name . substring ( iblace + 1 , name . length - 1 ) ; nameattr = nameattr . split ( '=' ) ; if ( nameattr . length != 2 ) { throw new Error ( 'Syntax error, unrecognized expression: ' + name ) ; } nameattrVal = nameattr [ 1 ] ; if ( nameattrVal . indexOf ( '\'' ) >= 0 || nameattrVal . indexOf ( '"' ) >= 0 ) nameattrVal = nameattrVal . substring ( 1 , nameattrVal . length - 1 ) ; nameattr = nameattr [ 0 ] ; name = name . substr ( 0 , iblace ) ; } if ( name [ 0 ] == '.' ) { tag = 2 ; name = name . substr ( 1 ) ; } else if ( name [ 0 ] == '#' ) { tag = 1 ; name = name . substr ( 1 ) ; } if ( ! parentNodes || parentNodes . length == 0 ) { if ( 2 == tag ) { elems = window . document . getElementsByClassName ( name ) ; } else if ( 1 == tag ) { elems = window . document . getElementById ( name ) ; if ( elems ) elems = [ elems ] ; else elems = [ ] ; } else if ( 0 == tag ) { elems = window . document . getElementsByTagName ( name ) ; } // attrvalue. if ( nameattr ) { var tt_elems = elems ; elems = [ ] ; for ( var i = 0 ; i < tt_elems . length ; i ++ ) { if ( tt_elems [ i ] . getAttribute ( nameattr ) === nameattrVal ) { elems . push ( tt_elems [ i ] ) ; } } } // if. if ( selector ) { var tt_elems = elems ; elems = [ ] ; for ( var i = 0 ; i < tt_elems . length ; i ++ ) { if ( selector == 'disabled' ) { if ( tt_elems [ i ] . disabled ) { elems . push ( tt_elems [ i ] ) ; } } else if ( selector == 'checked' ) { if ( tt_elems [ i ] . checked ) { elems . push ( tt_elems [ i ] ) ; } } } } // if. } else { elems = [ ] ; for ( var i = 0 ; i < parentNodes . length ; i ++ ) { var node1 = parentNodes [ i ] . childNodes ; if ( ! node1 ) continue ; var node = [ ] ; for ( var j = 0 ; j < node1 . length ; j ++ ) { node . push ( node1 [ j ] ) ; } for ( var j = 0 ; j < node . length ; j ++ ) { var add = true ; if ( selector ) { if ( selector == 'disabled' ) { if ( ! node [ j ] . disabled ) { add = false ; } } else if ( selector == 'checked' ) { if ( ! node [ j ] . checked ) { add = false ; } } } // if. // attrvalue. if ( add && nameattr ) { if ( node [ j ] . getAttribute ( nameattr ) !== nameattrVal ) { add = false ; } } if ( add ) { if ( 2 == tag ) { if ( _hasClass ( node [ j ] , name ) ) { elems . push ( node [ j ] ) ; continue ; } } else if ( 1 == tag ) { if ( node [ j ] . id == name ) { elems . push ( node [ j ] ) ; continue ; } } else if ( 0 == tag ) { if ( node [ j ] . nodeName . toUpperCase ( ) == name . toUpperCase ( ) ) { elems . push ( node [ j ] ) ; continue ; } } } // if. if ( ! notAllChildren ) { var nn = node [ j ] . childNodes ; if ( nn && nn . length > 0 ) { for ( var k = 0 ; k < nn . length ; k ++ ) { node . push ( nn [ k ] ) ; } if ( j > 20 ) { node = node . slice ( j + 1 ) ; j = - 1 ; } } } } // for. } // for. } // if..else. return elems ; }
- parentNodes 父节点 (HTMLNode) - name 子节点selector. - notAllChildren 仅查询一层子节点. 返回匹配到的元素集合.
- parentNodes 父节点 ( HTMLNode ) - name 子节点selector . - notAllChildren 仅查询一层子节点 . 返回匹配到的元素集合 .