compile.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. compiles a selector to an executable function
  3. */
  4. module.exports = compile;
  5. module.exports.compileUnsafe = compileUnsafe;
  6. module.exports.compileToken = compileToken;
  7. var parse = require("css-what"),
  8. DomUtils = require("domutils"),
  9. isTag = DomUtils.isTag,
  10. Rules = require("./general.js"),
  11. sortRules = require("./sort.js"),
  12. BaseFuncs = require("boolbase"),
  13. trueFunc = BaseFuncs.trueFunc,
  14. falseFunc = BaseFuncs.falseFunc,
  15. procedure = require("./procedure.json");
  16. function compile(selector, options, context){
  17. var next = compileUnsafe(selector, options, context);
  18. return wrap(next);
  19. }
  20. function wrap(next){
  21. return function base(elem){
  22. return isTag(elem) && next(elem);
  23. };
  24. }
  25. function compileUnsafe(selector, options, context){
  26. var token = parse(selector, options);
  27. return compileToken(token, options, context);
  28. }
  29. function includesScopePseudo(t){
  30. return t.type === "pseudo" && (
  31. t.name === "scope" || (
  32. Array.isArray(t.data) &&
  33. t.data.some(function(data){
  34. return data.some(includesScopePseudo);
  35. })
  36. )
  37. );
  38. }
  39. var DESCENDANT_TOKEN = {type: "descendant"},
  40. SCOPE_TOKEN = {type: "pseudo", name: "scope"},
  41. PLACEHOLDER_ELEMENT = {},
  42. getParent = DomUtils.getParent;
  43. //CSS 4 Spec (Draft): 3.3.1. Absolutizing a Scope-relative Selector
  44. //http://www.w3.org/TR/selectors4/#absolutizing
  45. function absolutize(token, context){
  46. //TODO better check if context is document
  47. var hasContext = !!context && !!context.length && context.every(function(e){
  48. return e === PLACEHOLDER_ELEMENT || !!getParent(e);
  49. });
  50. token.forEach(function(t){
  51. if(t.length > 0 && isTraversal(t[0]) && t[0].type !== "descendant"){
  52. //don't return in else branch
  53. } else if(hasContext && !includesScopePseudo(t)){
  54. t.unshift(DESCENDANT_TOKEN);
  55. } else {
  56. return;
  57. }
  58. t.unshift(SCOPE_TOKEN);
  59. });
  60. }
  61. function compileToken(token, options, context){
  62. token = token.filter(function(t){ return t.length > 0; });
  63. token.forEach(sortRules);
  64. var isArrayContext = Array.isArray(context);
  65. context = (options && options.context) || context;
  66. if(context && !isArrayContext) context = [context];
  67. absolutize(token, context);
  68. return token
  69. .map(function(rules){ return compileRules(rules, options, context, isArrayContext); })
  70. .reduce(reduceRules, falseFunc);
  71. }
  72. function isTraversal(t){
  73. return procedure[t.type] < 0;
  74. }
  75. function compileRules(rules, options, context, isArrayContext){
  76. var acceptSelf = (isArrayContext && rules[0].name === "scope" && rules[1].type === "descendant");
  77. return rules.reduce(function(func, rule, index){
  78. if(func === falseFunc) return func;
  79. return Rules[rule.type](func, rule, options, context, acceptSelf && index === 1);
  80. }, options && options.rootFunc || trueFunc);
  81. }
  82. function reduceRules(a, b){
  83. if(b === falseFunc || a === trueFunc){
  84. return a;
  85. }
  86. if(a === falseFunc || b === trueFunc){
  87. return b;
  88. }
  89. return function combine(elem){
  90. return a(elem) || b(elem);
  91. };
  92. }
  93. //:not, :has and :matches have to compile selectors
  94. //doing this in lib/pseudos.js would lead to circular dependencies,
  95. //so we add them here
  96. var Pseudos = require("./pseudos.js"),
  97. filters = Pseudos.filters,
  98. existsOne = DomUtils.existsOne,
  99. isTag = DomUtils.isTag,
  100. getChildren = DomUtils.getChildren;
  101. function containsTraversal(t){
  102. return t.some(isTraversal);
  103. }
  104. filters.not = function(next, token, options, context){
  105. var opts = {
  106. xmlMode: !!(options && options.xmlMode),
  107. strict: !!(options && options.strict)
  108. };
  109. if(opts.strict){
  110. if(token.length > 1 || token.some(containsTraversal)){
  111. throw new SyntaxError("complex selectors in :not aren't allowed in strict mode");
  112. }
  113. }
  114. var func = compileToken(token, opts, context);
  115. if(func === falseFunc) return next;
  116. if(func === trueFunc) return falseFunc;
  117. return function(elem){
  118. return !func(elem) && next(elem);
  119. };
  120. };
  121. filters.has = function(next, token, options){
  122. var opts = {
  123. xmlMode: !!(options && options.xmlMode),
  124. strict: !!(options && options.strict)
  125. };
  126. //FIXME: Uses an array as a pointer to the current element (side effects)
  127. var context = token.some(containsTraversal) ? [PLACEHOLDER_ELEMENT] : null;
  128. var func = compileToken(token, opts, context);
  129. if(func === falseFunc) return falseFunc;
  130. if(func === trueFunc) return function(elem){
  131. return getChildren(elem).some(isTag) && next(elem);
  132. };
  133. func = wrap(func);
  134. if(context){
  135. return function has(elem){
  136. return next(elem) && (
  137. (context[0] = elem), existsOne(func, getChildren(elem))
  138. );
  139. };
  140. }
  141. return function has(elem){
  142. return next(elem) && existsOne(func, getChildren(elem));
  143. };
  144. };
  145. filters.matches = function(next, token, options, context){
  146. var opts = {
  147. xmlMode: !!(options && options.xmlMode),
  148. strict: !!(options && options.strict),
  149. rootFunc: next
  150. };
  151. return compileToken(token, opts, context);
  152. };