walk.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. function walkRules(node, item, list) {
  2. switch (node.type) {
  3. case 'StyleSheet':
  4. var oldStylesheet = this.stylesheet;
  5. this.stylesheet = node;
  6. node.rules.each(walkRules, this);
  7. this.stylesheet = oldStylesheet;
  8. break;
  9. case 'Atrule':
  10. if (node.block !== null) {
  11. walkRules.call(this, node.block);
  12. }
  13. this.fn(node, item, list);
  14. break;
  15. case 'Ruleset':
  16. this.fn(node, item, list);
  17. break;
  18. }
  19. }
  20. function walkRulesRight(node, item, list) {
  21. switch (node.type) {
  22. case 'StyleSheet':
  23. var oldStylesheet = this.stylesheet;
  24. this.stylesheet = node;
  25. node.rules.eachRight(walkRulesRight, this);
  26. this.stylesheet = oldStylesheet;
  27. break;
  28. case 'Atrule':
  29. if (node.block !== null) {
  30. walkRulesRight.call(this, node.block);
  31. }
  32. this.fn(node, item, list);
  33. break;
  34. case 'Ruleset':
  35. this.fn(node, item, list);
  36. break;
  37. }
  38. }
  39. function walkAll(node, item, list) {
  40. switch (node.type) {
  41. case 'StyleSheet':
  42. var oldStylesheet = this.stylesheet;
  43. this.stylesheet = node;
  44. node.rules.each(walkAll, this);
  45. this.stylesheet = oldStylesheet;
  46. break;
  47. case 'Atrule':
  48. if (node.expression !== null) {
  49. walkAll.call(this, node.expression);
  50. }
  51. if (node.block !== null) {
  52. walkAll.call(this, node.block);
  53. }
  54. break;
  55. case 'Ruleset':
  56. this.ruleset = node;
  57. if (node.selector !== null) {
  58. walkAll.call(this, node.selector);
  59. }
  60. walkAll.call(this, node.block);
  61. this.ruleset = null;
  62. break;
  63. case 'Selector':
  64. var oldSelector = this.selector;
  65. this.selector = node;
  66. node.selectors.each(walkAll, this);
  67. this.selector = oldSelector;
  68. break;
  69. case 'Block':
  70. node.declarations.each(walkAll, this);
  71. break;
  72. case 'Declaration':
  73. this.declaration = node;
  74. walkAll.call(this, node.property);
  75. walkAll.call(this, node.value);
  76. this.declaration = null;
  77. break;
  78. case 'Attribute':
  79. walkAll.call(this, node.name);
  80. if (node.value !== null) {
  81. walkAll.call(this, node.value);
  82. }
  83. break;
  84. case 'FunctionalPseudo':
  85. case 'Function':
  86. this['function'] = node;
  87. node.arguments.each(walkAll, this);
  88. this['function'] = null;
  89. break;
  90. case 'AtruleExpression':
  91. this.atruleExpression = node;
  92. node.sequence.each(walkAll, this);
  93. this.atruleExpression = null;
  94. break;
  95. case 'Value':
  96. case 'Argument':
  97. case 'SimpleSelector':
  98. case 'Braces':
  99. case 'Negation':
  100. node.sequence.each(walkAll, this);
  101. break;
  102. case 'Url':
  103. case 'Progid':
  104. walkAll.call(this, node.value);
  105. break;
  106. // nothig to do with
  107. // case 'Property':
  108. // case 'Combinator':
  109. // case 'Dimension':
  110. // case 'Hash':
  111. // case 'Identifier':
  112. // case 'Nth':
  113. // case 'Class':
  114. // case 'Id':
  115. // case 'Percentage':
  116. // case 'PseudoClass':
  117. // case 'PseudoElement':
  118. // case 'Space':
  119. // case 'Number':
  120. // case 'String':
  121. // case 'Operator':
  122. // case 'Raw':
  123. }
  124. this.fn(node, item, list);
  125. }
  126. function createContext(root, fn) {
  127. var context = {
  128. fn: fn,
  129. root: root,
  130. stylesheet: null,
  131. atruleExpression: null,
  132. ruleset: null,
  133. selector: null,
  134. declaration: null,
  135. function: null
  136. };
  137. return context;
  138. }
  139. module.exports = {
  140. all: function(root, fn) {
  141. walkAll.call(createContext(root, fn), root);
  142. },
  143. rules: function(root, fn) {
  144. walkRules.call(createContext(root, fn), root);
  145. },
  146. rulesRight: function(root, fn) {
  147. walkRulesRight.call(createContext(root, fn), root);
  148. }
  149. };