OLD | NEW |
(Empty) | |
| 1 var DomUtils = require("domutils"), |
| 2 isTag = DomUtils.isTag, |
| 3 getParent = DomUtils.getParent, |
| 4 getChildren = DomUtils.getChildren, |
| 5 getSiblings = DomUtils.getSiblings, |
| 6 getName = DomUtils.getName; |
| 7 |
| 8 /* |
| 9 all available rules |
| 10 */ |
| 11 module.exports = { |
| 12 __proto__: null, |
| 13 |
| 14 attribute: require("./attributes.js").compile, |
| 15 pseudo: require("./pseudos.js").compile, |
| 16 |
| 17 //tags |
| 18 tag: function(next, data){ |
| 19 var name = data.name; |
| 20 return function tag(elem){ |
| 21 return getName(elem) === name && next(elem); |
| 22 }; |
| 23 }, |
| 24 |
| 25 //traversal |
| 26 descendant: function(next){ |
| 27 return function descendant(elem){ |
| 28 var found = false; |
| 29 |
| 30 while(!found && (elem = getParent(elem))){ |
| 31 found = next(elem); |
| 32 } |
| 33 |
| 34 return found; |
| 35 }; |
| 36 }, |
| 37 parent: function(next){ |
| 38 return function parent(elem){ |
| 39 return getChildren(elem).some(next); |
| 40 }; |
| 41 }, |
| 42 child: function(next){ |
| 43 return function child(elem){ |
| 44 var parent = getParent(elem); |
| 45 return !!parent && next(parent); |
| 46 }; |
| 47 }, |
| 48 sibling: function(next){ |
| 49 return function sibling(elem){ |
| 50 var siblings = getSiblings(elem); |
| 51 |
| 52 for(var i = 0; i < siblings.length; i++){ |
| 53 if(isTag(siblings[i])){ |
| 54 if(siblings[i] === elem) break; |
| 55 if(next(siblings[i])) return true; |
| 56 } |
| 57 } |
| 58 |
| 59 return false; |
| 60 }; |
| 61 }, |
| 62 adjacent: function(next){ |
| 63 return function adjacent(elem){ |
| 64 var siblings = getSiblings(elem), |
| 65 lastElement; |
| 66 |
| 67 for(var i = 0; i < siblings.length; i++){ |
| 68 if(isTag(siblings[i])){ |
| 69 if(siblings[i] === elem) break; |
| 70 lastElement = siblings[i]; |
| 71 } |
| 72 } |
| 73 |
| 74 return !!lastElement && next(lastElement); |
| 75 }; |
| 76 }, |
| 77 universal: function(next){ |
| 78 return next; |
| 79 } |
| 80 }; |
OLD | NEW |