pseudos.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.verifyPseudoArgs = exports.pseudos = void 0;
  4. // While filters are precompiled, pseudos get called when they are needed
  5. exports.pseudos = {
  6. empty: function (elem, _a) {
  7. var adapter = _a.adapter;
  8. return !adapter.getChildren(elem).some(function (elem) {
  9. // FIXME: `getText` call is potentially expensive.
  10. return adapter.isTag(elem) || adapter.getText(elem) !== "";
  11. });
  12. },
  13. "first-child": function (elem, _a) {
  14. var adapter = _a.adapter, equals = _a.equals;
  15. var firstChild = adapter
  16. .getSiblings(elem)
  17. .find(function (elem) { return adapter.isTag(elem); });
  18. return firstChild != null && equals(elem, firstChild);
  19. },
  20. "last-child": function (elem, _a) {
  21. var adapter = _a.adapter, equals = _a.equals;
  22. var siblings = adapter.getSiblings(elem);
  23. for (var i = siblings.length - 1; i >= 0; i--) {
  24. if (equals(elem, siblings[i]))
  25. return true;
  26. if (adapter.isTag(siblings[i]))
  27. break;
  28. }
  29. return false;
  30. },
  31. "first-of-type": function (elem, _a) {
  32. var adapter = _a.adapter, equals = _a.equals;
  33. var siblings = adapter.getSiblings(elem);
  34. var elemName = adapter.getName(elem);
  35. for (var i = 0; i < siblings.length; i++) {
  36. var currentSibling = siblings[i];
  37. if (equals(elem, currentSibling))
  38. return true;
  39. if (adapter.isTag(currentSibling) &&
  40. adapter.getName(currentSibling) === elemName) {
  41. break;
  42. }
  43. }
  44. return false;
  45. },
  46. "last-of-type": function (elem, _a) {
  47. var adapter = _a.adapter, equals = _a.equals;
  48. var siblings = adapter.getSiblings(elem);
  49. var elemName = adapter.getName(elem);
  50. for (var i = siblings.length - 1; i >= 0; i--) {
  51. var currentSibling = siblings[i];
  52. if (equals(elem, currentSibling))
  53. return true;
  54. if (adapter.isTag(currentSibling) &&
  55. adapter.getName(currentSibling) === elemName) {
  56. break;
  57. }
  58. }
  59. return false;
  60. },
  61. "only-of-type": function (elem, _a) {
  62. var adapter = _a.adapter, equals = _a.equals;
  63. var elemName = adapter.getName(elem);
  64. return adapter
  65. .getSiblings(elem)
  66. .every(function (sibling) {
  67. return equals(elem, sibling) ||
  68. !adapter.isTag(sibling) ||
  69. adapter.getName(sibling) !== elemName;
  70. });
  71. },
  72. "only-child": function (elem, _a) {
  73. var adapter = _a.adapter, equals = _a.equals;
  74. return adapter
  75. .getSiblings(elem)
  76. .every(function (sibling) { return equals(elem, sibling) || !adapter.isTag(sibling); });
  77. },
  78. };
  79. function verifyPseudoArgs(func, name, subselect) {
  80. if (subselect === null) {
  81. if (func.length > 2) {
  82. throw new Error("pseudo-selector :".concat(name, " requires an argument"));
  83. }
  84. }
  85. else if (func.length === 2) {
  86. throw new Error("pseudo-selector :".concat(name, " doesn't have any arguments"));
  87. }
  88. }
  89. exports.verifyPseudoArgs = verifyPseudoArgs;