javascript css selector engine-mini
2009-09-21 23:38 蜡笔小旧 阅读(653) 评论(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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
var simple = / /.test(selector);
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)