Blame view

node_modules/css-select/lib/general.js 1.84 KB
aaac7fed   liuqimichale   add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  var DomUtils    = require("domutils"),
      isTag       = DomUtils.isTag,
      getParent   = DomUtils.getParent,
      getChildren = DomUtils.getChildren,
      getSiblings = DomUtils.getSiblings,
      getName     = DomUtils.getName;
  
  /*
  	all available rules
  */
  module.exports = {
  	__proto__: null,
  
  	attribute: require("./attributes.js").compile,
  	pseudo: require("./pseudos.js").compile,
  
  	//tags
  	tag: function(next, data){
  		var name = data.name;
  		return function tag(elem){
  			return getName(elem) === name && next(elem);
  		};
  	},
  
  	//traversal
  	descendant: function(next, rule, options, context, acceptSelf){
  		return function descendant(elem){
  
  			if (acceptSelf && next(elem)) return true;
  
  			var found = false;
  
  			while(!found && (elem = getParent(elem))){
  				found = next(elem);
  			}
  
  			return found;
  		};
  	},
  	parent: function(next, data, options){
  		if(options && options.strict) throw SyntaxError("Parent selector isn't part of CSS3");
  
  		return function parent(elem){
  			return getChildren(elem).some(test);
  		};
  
  		function test(elem){
  			return isTag(elem) && next(elem);
  		}
  	},
  	child: function(next){
  		return function child(elem){
  			var parent = getParent(elem);
  			return !!parent && next(parent);
  		};
  	},
  	sibling: function(next){
  		return function sibling(elem){
  			var siblings = getSiblings(elem);
  
  			for(var i = 0; i < siblings.length; i++){
  				if(isTag(siblings[i])){
  					if(siblings[i] === elem) break;
  					if(next(siblings[i])) return true;
  				}
  			}
  
  			return false;
  		};
  	},
  	adjacent: function(next){
  		return function adjacent(elem){
  			var siblings = getSiblings(elem),
  			    lastElement;
  
  			for(var i = 0; i < siblings.length; i++){
  				if(isTag(siblings[i])){
  					if(siblings[i] === elem) break;
  					lastElement = siblings[i];
  				}
  			}
  
  			return !!lastElement && next(lastElement);
  		};
  	},
  	universal: function(next){
  		return next;
  	}
  };