Changeset 1155

Show
Ignore:
Timestamp:
10/08/06 09:18:22 (2 years ago)
Author:
therve@gmail.com
Message:

Syntax cleaning

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • mochikit/branches/selector/MochiKit/Selector.js

    r1152 r1155  
    2424    this.expression = expression.toString().replace(/(^\s+|\s+$)/g, ''); 
    2525    this.parseExpression(); 
    26     this.compileMatcher();     
     26    this.compileMatcher(); 
    2727}; 
    2828 
    2929MochiKit.Selector.Selector.prototype = { 
    30      
     30 
    3131    __class__: MochiKit.Selector.Selector, 
    32      
    33     parseExpression: function() { 
    34         function abort(message) { throw 'Parse error in selector: ' + message; } 
    35  
    36         if (this.expression == '')  abort('empty expression'); 
     32 
     33    parseExpression: function () { 
     34        function abort(message) { 
     35            throw 'Parse error in selector: ' + message; 
     36        } 
     37 
     38        if (this.expression == '')  { 
     39            abort('empty expression'); 
     40        } 
    3741 
    3842        var params = this.params, expr = this.expression, match, modifier, clause, rest; 
     
    4347        } 
    4448 
    45         if (expr == '*') return this.params.wildcard = true; 
     49        if (expr == '*') { 
     50            return this.params.wildcard = true; 
     51        } 
    4652 
    4753        while (match = expr.match(/^([^a-z0-9_-])?([a-z0-9_-]+(?:\([^)]*\))?)(.*)/i)) { 
    4854            modifier = match[1], clause = match[2], rest = match[3]; 
    4955            switch (modifier) { 
    50                 case '#':       params.id = clause; break; 
    51                 case '.':       params.classNames.push(clause); break; 
    52                 case ':':       params.pseudoClassNames.push(clause); break; 
     56                case '#':  
     57                    params.id = clause; 
     58                    break; 
     59                case '.':  
     60                    params.classNames.push(clause); 
     61                    break; 
     62                case ':': 
     63                    params.pseudoClassNames.push(clause); 
     64                    break; 
    5365                case '': 
    54                 case undefined: params.tagName = clause.toUpperCase(); break; 
    55                 default:        abort(repr(expr)); 
     66                case undefined: 
     67                    params.tagName = clause.toUpperCase(); 
     68                    break; 
     69                default: 
     70                    abort(repr(expr)); 
    5671            } 
    5772            expr = rest; 
    5873        } 
    5974 
    60         if (expr.length > 0) abort(repr(expr));         
    61     }, 
    62  
    63     buildMatchExpression: function() { 
    64         var params = this.params, conditions = [], clause; 
    65          
     75        if (expr.length > 0) { 
     76            abort(repr(expr)); 
     77        } 
     78    }, 
     79 
     80    buildMatchExpression: function () { 
     81        var params = this.params; 
     82        var conditions = []; 
     83        var clause; 
     84 
    6685        function childElements(element) { 
    67             return "filter(function(node){ return node.nodeType == Node.ELEMENT_NODE; }, " + element + ".childNodes)"; 
    68         } 
    69  
    70         if (params.wildcard) 
     86            return "filter(function (node){ return node.nodeType == Node.ELEMENT_NODE; }, " + element + ".childNodes)"; 
     87        } 
     88 
     89        if (params.wildcard) { 
    7190            conditions.push('true'); 
    72         if (clause = params.id) 
     91        } 
     92        if (clause = params.id) { 
    7393            conditions.push('element.id == ' + repr(clause)); 
    74         if (clause = params.tagName) 
     94        } 
     95        if (clause = params.tagName) { 
    7596            conditions.push('element.tagName.toUpperCase() == ' + repr(clause)); 
    76         if ((clause = params.classNames).length > 0) 
    77             for (var i = 0; i < clause.length; i++) 
     97        } 
     98        if ((clause = params.classNames).length > 0) { 
     99            for (var i = 0; i < clause.length; i++) { 
    78100                conditions.push('hasElementClass(element, ' + repr(clause[i]) + ')'); 
    79         if ((clause = params.pseudoClassNames).length > 0) 
     101            } 
     102        } 
     103        if ((clause = params.pseudoClassNames).length > 0) { 
    80104            for (var i = 0; i < clause.length; i++) { 
    81105                var match = clause[i].match(/^([^(]+)(?:\((.*)\))?$/); 
    82                 var pseudoClass = match[1], pseudoClassArgument = match[2] 
     106                var pseudoClass = match[1]; 
     107                var pseudoClassArgument = match[2]; 
    83108                switch (pseudoClass) { 
    84109                    case 'root': 
     
    89114                    case 'nth-last-of-type': 
    90115                        match = pseudoClassArgument.match(/^((?:(\d+)n\+)?(\d+)|odd|even)$/); 
    91                         if (!match) throw "Invalid argument to pseudo element nth-child: " + pseudoClassArgument; 
    92                         var a,b; 
    93                         if (match[0] == 'odd') a = 2, b = 1; 
    94                         else if (match[0] == 'even') a = 2, b = 0; 
    95                         else a = match[2] && parseInt(match), b = parseInt(match[3]); 
    96                         conditions.push('this.nthChild(element,' + a + ',' + b  
    97                                                 + ',' + !!pseudoClass.match('^nth-last')    // Reverse 
    98                                                 + ',' + !!pseudoClass.match('of-type$')     // Restrict to same tagName 
    99                                                 + ')'); 
     116                        if (!match) { 
     117                            throw "Invalid argument to pseudo element nth-child: " + pseudoClassArgument; 
     118                        } 
     119                        var a, b; 
     120                        if (match[0] == 'odd') { 
     121                            a = 2; 
     122                            b = 1; 
     123                        } else if (match[0] == 'even') { 
     124                            a = 2; 
     125                            b = 0; 
     126                        } else { 
     127                            a = match[2] && parseInt(match); 
     128                            b = parseInt(match[3]); 
     129                        } 
     130                        conditions.push('this.nthChild(element,' + a + ',' + b 
     131                                        + ',' + !!pseudoClass.match('^nth-last')    // Reverse 
     132                                        + ',' + !!pseudoClass.match('of-type$')     // Restrict to same tagName 
     133                                        + ')'); 
    100134                        break; 
    101135                    case 'first-child': 
     
    135169                } 
    136170            } 
     171        } 
    137172        if (clause = params.attributes) { 
    138             map(function(attribute) { 
     173            map(function (attribute) { 
    139174                var value = 'element.getAttribute(' + repr(attribute.name) + ')'; 
    140                 var splitValueBy = function(delimiter) { 
     175                var splitValueBy = function (delimiter) { 
    141176                    return value + ' && ' + value + '.split(' + repr(delimiter) + ')'; 
    142177                } 
    143178 
    144179                switch (attribute.operator) { 
    145                     case '=':       conditions.push(value + ' == ' + repr(attribute.value)); break; 
    146                     case '~=':      conditions.push('findValue(' + splitValueBy(' ') + ', ' + repr(attribute.value) + ') > -1'); break; 
    147                     case '^=':      conditions.push(value + '.substring(0, ' + attribute.value.length + ') == ' + repr(attribute.value)); break; 
    148                     case '$=':      conditions.push(value + '.substring(' + value + '.length - ' + attribute.value.length + ') == ' + repr(attribute.value)); break; 
    149                     case '*=':      conditions.push(value + '.match(' + repr(attribute.value) + ')'); break; 
    150                     case '|=':      conditions.push( 
    151                                         splitValueBy('-') + '[0].toUpperCase() == ' + repr(attribute.value.toUpperCase()) 
    152                                     ); break; 
    153                     case '!=':      conditions.push(value + ' != ' + repr(attribute.value)); break; 
     180                    case '=': 
     181                        conditions.push(value + ' == ' + repr(attribute.value)); 
     182                        break; 
     183                    case '~=': 
     184                        conditions.push('findValue(' + splitValueBy(' ') + ', ' + repr(attribute.value) + ') > -1'); 
     185                        break; 
     186                    case '^=': 
     187                        conditions.push(value + '.substring(0, ' + attribute.value.length + ') == ' + repr(attribute.value)); 
     188                        break; 
     189                    case '$=': 
     190                        conditions.push(value + '.substring(' + value + '.length - ' + attribute.value.length + ') == ' + repr(attribute.value)); 
     191                        break; 
     192                    case '*=': 
     193                        conditions.push(value + '.match(' + repr(attribute.value) + ')'); 
     194                        break; 
     195                    case '|=': 
     196                        conditions.push( 
     197                            splitValueBy('-') + '[0].toUpperCase() == ' + repr(attribute.value.toUpperCase()) 
     198                        ); 
     199                        break; 
     200                    case '!=': 
     201                        conditions.push(value + ' != ' + repr(attribute.value)); 
     202                        break; 
    154203                    case '': 
    155                     case undefined: conditions.push(value + ' != null'); break; 
    156                     default:        throw 'Unknown operator ' + attribute.operator + ' in selector'; 
     204                    case undefined: 
     205                        conditions.push(value + ' != null'); 
     206                        break; 
     207                    default: 
     208                        throw 'Unknown operator ' + attribute.operator + ' in selector'; 
    157209                } 
    158210            }, clause); 
     
    161213        return conditions.join(' && '); 
    162214    }, 
    163      
    164     compileMatcher: function() { 
     215 
     216    compileMatcher: function () { 
    165217        this.match = new Function('element', 'if (!element.tagName) return false; \ 
    166218                return ' + this.buildMatchExpression()); 
    167219    }, 
    168      
    169     nthChild: function(element, a, b, reverse, sametag){ 
    170         var siblings = filter(function(node){ return node.nodeType == Node.ELEMENT_NODE; }, element.parentNode.childNodes); 
    171         if (sametag) siblings = filter(function(node){ return node.tagName == element.tagName}, siblings);  
    172         if (reverse) siblings = MochiKit.Iter.reversed(siblings); 
     220 
     221    nthChild: function (element, a, b, reverse, sametag){ 
     222        var siblings = filter(function (node) { 
     223            return node.nodeType == Node.ELEMENT_NODE; 
     224            }, element.parentNode.childNodes); 
     225        if (sametag) { 
     226            siblings = filter(function (node) { 
     227                return node.tagName == element.tagName; 
     228            }, siblings); 
     229        } 
     230        if (reverse) { 
     231            siblings = MochiKit.Iter.reversed(siblings); 
     232        } 
    173233        if (a) { 
    174             var actualIndex = findIdentical(siblings, element); 
     234            var actualIndex = MochiKit.Base.findIdentical(siblings, element); 
    175235            return ((actualIndex + 1 - b) / a) % 1 == 0; 
    176236        } else { 
    177             return b == findIdentical(siblings, element) + 1; 
    178         } 
    179     }, 
    180  
    181     findElements: function(scope, axis) { 
     237            return b == MochiKit.Base.findIdentical(siblings, element) + 1; 
     238        } 
     239    }, 
     240 
     241    findElements: function (scope, axis) { 
    182242        var element; 
    183        
    184         if (axis == undefined) axis = ""; 
    185        
     243 
     244        if (axis == undefined) { 
     245            axis = ""; 
     246        } 
     247 
    186248        function inScope(element, scope) { 
    187             if (axis == "") 
     249            if (axis == "") { 
    188250                return MochiKit.DOM.isChildNode(element, scope); 
    189             else if (axis == ">") 
     251            } else if (axis == ">") { 
    190252                return element.parentNode == scope; 
    191             else if (axis == "+") 
     253            } else if (axis == "+") { 
    192254                return element.previousSibling == scope; 
    193             else if (axis == "~") { 
     255            } else if (axis == "~") { 
    194256                while (element.previousSibling) { 
    195257                    if (element.previousSibling == scope) return true; 
     
    202264        } 
    203265 
    204         if (element = $(this.params.id)) 
    205             if (this.match(element)) 
    206                 if (!scope || inScope(element, scope)) 
     266        if (element = $(this.params.id)) { 
     267            if (this.match(element)) { 
     268                if (!scope || inScope(element, scope)) { 
    207269                    return [element]; 
     270                } 
     271            } 
     272        } 
    208273 
    209274        function nextSiblingElement(node) { 
    210275            node = node.nextSibling; 
    211             while (node && node.nodeType != Node.ELEMENT_NODE) 
     276            while (node && node.nodeType != Node.ELEMENT_NODE) { 
    212277                node = node.nextSibling; 
     278            } 
    213279            return node; 
    214280        } 
     
    217283            scope = (scope || document).getElementsByTagName(this.params.tagName || '*'); 
    218284        } else if (axis == ">") { 
    219             if (!scope) throw "> combinator not allowed without preceeding expression"; 
    220             scope = filter(function(node){ return node.nodeType == Node.ELEMENT_NODE; }, scope.childNodes); 
     285            if (!scope) { 
     286                throw "> combinator not allowed without preceeding expression"; 
     287            } 
     288            scope = filter(function (node) { 
     289                return node.nodeType == Node.ELEMENT_NODE; 
     290            }, scope.childNodes); 
    221291        } else if (axis == "+") { 
    222             if (!scope) throw "+ combinator not allowed without preceeding expression"; 
     292            if (!scope) { 
     293                throw "+ combinator not allowed without preceeding expression"; 
     294            } 
    223295            scope = nextSiblingElement(scope) && [nextSiblingElement(scope)]; 
    224296        } else if (axis == "~") { 
    225             if (!scope) throw "~ combinator not allowed without preceeding expression"; 
     297            if (!scope) { 
     298                throw "~ combinator not allowed without preceeding expression"; 
     299            } 
    226300            var newscope = new Array(); 
    227301            while (nextSiblingElement(scope)) { 
     
    231305            scope = newscope; 
    232306        } 
    233          
    234         if (!scope) return []; 
     307 
     308        if (!scope) { 
     309            return []; 
     310        } 
    235311 
    236312        var results = []; 
    237         for (var i = 0; i < scope.length; i++) 
    238             if (this.match(element = scope[i])) 
     313        for (var i = 0; i < scope.length; i++) { 
     314            if (this.match(element = scope[i])) { 
    239315                results.push(element); 
     316            } 
     317        } 
    240318 
    241319        return results; 
    242320    }, 
    243321 
    244     toString: function() { 
     322    toString: function () { 
    245323        return this.expression; 
    246324    } 
    247          
     325 
    248326}; 
    249327 
    250328MochiKit.Base.update(MochiKit.Selector, { 
    251     matchElements: function(elements, expression) { 
     329    matchElements: function (elements, expression) { 
    252330        var selector = new Selector(expression); 
    253331        return elements.select(selector.match.bind(selector)).collect(Element.extend); 
    254332    }, 
    255    
    256     findElement: function(elements, expression, index) { 
    257         if (typeof expression == 'number') index = expression, expression = false; 
     333 
     334    findElement: function (elements, expression, index) { 
     335        if (typeof expression == 'number') { 
     336            index = expression; 
     337            expression = false; 
     338        } 
    258339        return Selector.matchElements(elements, expression || '*')[index || 0]; 
    259340    }, 
    260    
    261     findChildElements: function(element, expressions) { 
    262         return flattenArray(map(function(expression)
     341 
     342    findChildElements: function (element, expressions) { 
     343        return flattenArray(map(function (expression)
    263344            var nextScope = ""; 
    264             return reduce(function(results, expr)
     345            return reduce(function (results, expr)
    265346                if (match = expr.match(/^[>+~]$/)) { 
    266347                    nextScope = match[0]; 
     
    268349                } else { 
    269350                    var selector = new MochiKit.Selector.Selector(expr); 
    270                     var elements = reduce(function(elements, result)
     351                    var elements = reduce(function (elements, result)
    271352                        return extend(elements, selector.findElements(result || element, nextScope)); 
    272353                    }, results, []); 
     
    277358        }, expressions)); 
    278359    } 
    279    
     360 
    280361}); 
    281362 
     
    283364    return MochiKit.Selector.findChildElements(document, arguments); 
    284365} 
     366