You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 rivejä
1.8 KiB

4 vuotta sitten
  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. all available rules
  9. */
  10. module.exports = {
  11. __proto__: null,
  12. attribute: require("./attributes.js").compile,
  13. pseudo: require("./pseudos.js").compile,
  14. //tags
  15. tag: function(next, data){
  16. var name = data.name;
  17. return function tag(elem){
  18. return getName(elem) === name && next(elem);
  19. };
  20. },
  21. //traversal
  22. descendant: function(next, rule, options, context, acceptSelf){
  23. return function descendant(elem){
  24. if (acceptSelf && next(elem)) return true;
  25. var found = false;
  26. while(!found && (elem = getParent(elem))){
  27. found = next(elem);
  28. }
  29. return found;
  30. };
  31. },
  32. parent: function(next, data, options){
  33. if(options && options.strict) throw SyntaxError("Parent selector isn't part of CSS3");
  34. return function parent(elem){
  35. return getChildren(elem).some(test);
  36. };
  37. function test(elem){
  38. return isTag(elem) && next(elem);
  39. }
  40. },
  41. child: function(next){
  42. return function child(elem){
  43. var parent = getParent(elem);
  44. return !!parent && next(parent);
  45. };
  46. },
  47. sibling: function(next){
  48. return function sibling(elem){
  49. var siblings = getSiblings(elem);
  50. for(var i = 0; i < siblings.length; i++){
  51. if(isTag(siblings[i])){
  52. if(siblings[i] === elem) break;
  53. if(next(siblings[i])) return true;
  54. }
  55. }
  56. return false;
  57. };
  58. },
  59. adjacent: function(next){
  60. return function adjacent(elem){
  61. var siblings = getSiblings(elem),
  62. lastElement;
  63. for(var i = 0; i < siblings.length; i++){
  64. if(isTag(siblings[i])){
  65. if(siblings[i] === elem) break;
  66. lastElement = siblings[i];
  67. }
  68. }
  69. return !!lastElement && next(lastElement);
  70. };
  71. },
  72. universal: function(next){
  73. return next;
  74. }
  75. };