selectors.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isTraversal = isTraversal;
  4. exports.sortRules = sortRules;
  5. exports.getQuality = getQuality;
  6. exports.includesScopePseudo = includesScopePseudo;
  7. var css_what_1 = require("css-what");
  8. function isTraversal(token) {
  9. return token.type === "_flexibleDescendant" || (0, css_what_1.isTraversal)(token);
  10. }
  11. /**
  12. * Sort the parts of the passed selector, as there is potential for
  13. * optimization (some types of selectors are faster than others).
  14. *
  15. * @param arr Selector to sort
  16. */
  17. function sortRules(arr) {
  18. var ratings = arr.map(getQuality);
  19. for (var i = 1; i < arr.length; i++) {
  20. var procNew = ratings[i];
  21. if (procNew < 0)
  22. continue;
  23. // Use insertion sort to move the token to the correct position.
  24. for (var j = i; j > 0 && procNew < ratings[j - 1]; j--) {
  25. var token = arr[j];
  26. arr[j] = arr[j - 1];
  27. arr[j - 1] = token;
  28. ratings[j] = ratings[j - 1];
  29. ratings[j - 1] = procNew;
  30. }
  31. }
  32. }
  33. function getAttributeQuality(token) {
  34. switch (token.action) {
  35. case css_what_1.AttributeAction.Exists: {
  36. return 10;
  37. }
  38. case css_what_1.AttributeAction.Equals: {
  39. // Prefer ID selectors (eg. #ID)
  40. return token.name === "id" ? 9 : 8;
  41. }
  42. case css_what_1.AttributeAction.Not: {
  43. return 7;
  44. }
  45. case css_what_1.AttributeAction.Start: {
  46. return 6;
  47. }
  48. case css_what_1.AttributeAction.End: {
  49. return 6;
  50. }
  51. case css_what_1.AttributeAction.Any: {
  52. return 5;
  53. }
  54. case css_what_1.AttributeAction.Hyphen: {
  55. return 4;
  56. }
  57. case css_what_1.AttributeAction.Element: {
  58. return 3;
  59. }
  60. }
  61. }
  62. /**
  63. * Determine the quality of the passed token. The higher the number, the
  64. * faster the token is to execute.
  65. *
  66. * @param token Token to get the quality of.
  67. * @returns The token's quality.
  68. */
  69. function getQuality(token) {
  70. // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
  71. switch (token.type) {
  72. case css_what_1.SelectorType.Universal: {
  73. return 50;
  74. }
  75. case css_what_1.SelectorType.Tag: {
  76. return 30;
  77. }
  78. case css_what_1.SelectorType.Attribute: {
  79. return Math.floor(getAttributeQuality(token) /
  80. // `ignoreCase` adds some overhead, half the result if applicable.
  81. (token.ignoreCase ? 2 : 1));
  82. }
  83. case css_what_1.SelectorType.Pseudo: {
  84. return !token.data
  85. ? 3
  86. : token.name === "has" ||
  87. token.name === "contains" ||
  88. token.name === "icontains"
  89. ? // Expensive in any case — run as late as possible.
  90. 0
  91. : Array.isArray(token.data)
  92. ? // Eg. `:is`, `:not`
  93. Math.max(
  94. // If we have traversals, try to avoid executing this selector
  95. 0, Math.min.apply(Math, token.data.map(function (d) {
  96. return Math.min.apply(Math, d.map(getQuality));
  97. })))
  98. : 2;
  99. }
  100. default: {
  101. return -1;
  102. }
  103. }
  104. }
  105. function includesScopePseudo(t) {
  106. return (t.type === css_what_1.SelectorType.Pseudo &&
  107. (t.name === "scope" ||
  108. (Array.isArray(t.data) &&
  109. t.data.some(function (data) { return data.some(includesScopePseudo); }))));
  110. }
  111. //# sourceMappingURL=selectors.js.map