[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Web/Javascripts/prototype/ -> selector.js (source)

   1  var Selector = Class.create();
   2  Selector.prototype = {
   3    initialize: function(expression) {
   4      this.params = {classNames: []};
   5      this.expression = expression.toString().strip();
   6      this.parseExpression();
   7      this.compileMatcher();
   8    },
   9  
  10    parseExpression: function() {
  11      function abort(message) { throw 'Parse error in selector: ' + message; }
  12  
  13      if (this.expression == '')  abort('empty expression');
  14  
  15      var params = this.params, expr = this.expression, match, modifier, clause, rest;
  16      while (match = expr.match(/^(.*)\[([a-z0-9_:-]+?)(?:([~\|!]?=)(?:"([^"]*)"|([^\]\s]*)))?\]$/i)) {
  17        params.attributes = params.attributes || [];
  18        params.attributes.push({name: match[2], operator: match[3], value: match[4] || match[5] || ''});
  19        expr = match[1];
  20      }
  21  
  22      if (expr == '*') return this.params.wildcard = true;
  23      
  24      while (match = expr.match(/^([^a-z0-9_-])?([a-z0-9_-]+)(.*)/i)) {
  25        modifier = match[1], clause = match[2], rest = match[3];
  26        switch (modifier) {
  27          case '#':       params.id = clause; break;
  28          case '.':       params.classNames.push(clause); break;
  29          case '':
  30          case undefined: params.tagName = clause.toUpperCase(); break;
  31          default:        abort(expr.inspect());
  32        }
  33        expr = rest;
  34      }
  35      
  36      if (expr.length > 0) abort(expr.inspect());
  37    },
  38  
  39    buildMatchExpression: function() {
  40      var params = this.params, conditions = [], clause;
  41  
  42      if (params.wildcard)
  43        conditions.push('true');
  44      if (clause = params.id)
  45        conditions.push('element.id == ' + clause.inspect());
  46      if (clause = params.tagName)
  47        conditions.push('element.tagName.toUpperCase() == ' + clause.inspect());
  48      if ((clause = params.classNames).length > 0)
  49        for (var i = 0; i < clause.length; i++)
  50          conditions.push('Element.hasClassName(element, ' + clause[i].inspect() + ')');
  51      if (clause = params.attributes) {
  52        clause.each(function(attribute) {
  53          var value = 'element.getAttribute(' + attribute.name.inspect() + ')';
  54          var splitValueBy = function(delimiter) {
  55            return value + ' && ' + value + '.split(' + delimiter.inspect() + ')';
  56          }
  57          
  58          switch (attribute.operator) {
  59            case '=':       conditions.push(value + ' == ' + attribute.value.inspect()); break;
  60            case '~=':      conditions.push(splitValueBy(' ') + '.include(' + attribute.value.inspect() + ')'); break;
  61            case '|=':      conditions.push(
  62                              splitValueBy('-') + '.first().toUpperCase() == ' + attribute.value.toUpperCase().inspect()
  63                            ); break;
  64            case '!=':      conditions.push(value + ' != ' + attribute.value.inspect()); break;
  65            case '':
  66            case undefined: conditions.push(value + ' != null'); break;
  67            default:        throw 'Unknown operator ' + attribute.operator + ' in selector';
  68          }
  69        });
  70      }
  71  
  72      return conditions.join(' && ');
  73    },
  74  
  75    compileMatcher: function() {
  76      this.match = new Function('element', 'if (!element.tagName) return false; \
  77        return ' + this.buildMatchExpression());
  78    },
  79  
  80    findElements: function(scope) {
  81      var element;
  82  
  83      if (element = $(this.params.id))
  84        if (this.match(element))
  85          if (!scope || Element.childOf(element, scope))
  86            return [element];
  87  
  88      scope = (scope || document).getElementsByTagName(this.params.tagName || '*');
  89  
  90      var results = [];
  91      for (var i = 0; i < scope.length; i++)
  92        if (this.match(element = scope[i]))
  93          results.push(Element.extend(element));
  94  
  95      return results;
  96    },
  97  
  98    toString: function() {
  99      return this.expression;
 100    }
 101  }
 102  
 103  function $$() {
 104    return $A(arguments).map(function(expression) {
 105      return expression.strip().split(/\s+/).inject([null], function(results, expr) {
 106        var selector = new Selector(expr);
 107        return results.map(selector.findElements.bind(selector)).flatten();
 108      });
 109    }).flatten();
 110  }


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7