javascript css selector engine-mini
2009-09-21 23:38 蜡笔小旧 阅读(657) 评论(0) 收藏 举报Javascript类库圈最近似乎活跃。类库、插件层出不穷。今天看到一则selector engine的新闻。介绍一款新的seletor engine - mini(好像是车吧,名如其J,很小很强大)。其他3款车(1)Sizzle(这个知道,jQuery中的selector),(2) Peppy(从没听过)      (3) Sly(还是没听过)。4者数据分析上来看,mini快很多,也小得多。一起来看看他支持一些什么选择器;
(1)div
(2).example
(3)body div
(4)div,p
(5)div,p,example
(6)div p
(7)div > p 例如:mini('div > p').length
(8)div . example
(9)ul .exaple
(10)#title
(11)h1#title
(12)div #title
(13)ul.foo > * span
是不是很简单列。源码也很简单。嘿嘿。不过211行(Version 0.01Beta),使用该源码请遵守:MIT&GPL license。
(1)div
(2).example
(3)body div
(4)div,p
(5)div,p,example
(6)div p
(7)div > p 例如:mini('div > p').length
(8)div . example
(9)ul .exaple
(10)#title
(11)h1#title
(12)div #title
(13)ul.foo > * span
是不是很简单列。源码也很简单。嘿嘿。不过211行(Version 0.01Beta),使用该源码请遵守:MIT&GPL license。
 var mini = (function(){})();
接下来定义了一些正则:    var snack = / /,
exprClassName = / /,
exprId = / /,
exprNodeName = / /,
na = [null,null];
查找器:exprClassName = / /,
exprId = / /,
exprNodeName = / /,
na = [null,null];
 1
    function _find(selctor, context)
2
    {
3
        //以为看到了jQuery的源码,嘿嘿
4
        context = context || document;
5
6
    var simple = / /.test(selector);
7
8
    if(!simple && context,querySelectorAll)
9
    {
10
        //返回数组
11
        return realArray(context,querySelectorAll( selector ));
12
    }
13
    if( selector.indexOf(',') > -1 )
14
    {
15
        var split = selector.split(/,/g,ret =[],
16
                sIndex = 0,len = split.length;
17
        for(;sIndex < len ; ++sIndex)
18
        {
19
            ret = ret.concat(_find(split[sIndex],context));
20
        }
21
        //返回唯一
22
        return unique(ret);
23
    }
24
    var parts = selector.match(snack),
25
        part = parts.pop();
26
        id = ( part.match( exprId ) || na )[1],
27
        className = !id && ( part.match( exprClassName ) || na )[1],
28
        nodeName = !id && ( part.match( exprNodeName || na )[1],
29
        collection;
30
    if( className && !nodeName && context.getElementByClassName )
31
    {
32
        collection = realArray(context.getElementsByClassName( className ));
33
    }
34
    else
35
    {
36
        collection = realArray( context.getElementByTagName( nodeName || '*'));
37
        if(className)
38
        {
39
            collection = filterByAttr(collection,
40
                'className',regExp('(^|\\s)'+className+'(\\s|$)'));
41
        }
42
        if(id)
43
        {
44
            var byId = context.getElementById(id);
45
            return byId?[byId]:[];
46
        }
47
    }
48
    return parts[0] && collection[0] ? filterParents(parts,collection):collection;
49
    }
    function _find(selctor, context)2
    {3
        //以为看到了jQuery的源码,嘿嘿4
        context = context || document;5

6
    var simple = / /.test(selector);7

8
    if(!simple && context,querySelectorAll)9
    {10
        //返回数组11
        return realArray(context,querySelectorAll( selector ));12
    }13
    if( selector.indexOf(',') > -1 )14
    {15
        var split = selector.split(/,/g,ret =[],16
                sIndex = 0,len = split.length;17
        for(;sIndex < len ; ++sIndex)18
        {19
            ret = ret.concat(_find(split[sIndex],context));20
        }21
        //返回唯一22
        return unique(ret);23
    }24
    var parts = selector.match(snack),25
        part = parts.pop();26
        id = ( part.match( exprId ) || na )[1],27
        className = !id && ( part.match( exprClassName ) || na )[1],28
        nodeName = !id && ( part.match( exprNodeName || na )[1],29
        collection;30
    if( className && !nodeName && context.getElementByClassName )31
    {32
        collection = realArray(context.getElementsByClassName( className ));33
    }34
    else35
    {36
        collection = realArray( context.getElementByTagName( nodeName || '*'));37
        if(className)38
        {39
            collection = filterByAttr(collection,40
                'className',regExp('(^|\\s)'+className+'(\\s|$)'));41
        }42
        if(id)43
        {44
            var byId = context.getElementById(id);45
            return byId?[byId]:[];46
        }47
    }48
    return parts[0] && collection[0] ? filterParents(parts,collection):collection;49
    }
                    
                
    
    
                
            
        
浙公网安备 33010602011771号