index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { parse } from "css-what";
  2. import { filters } from "./filters.js";
  3. import { pseudos, verifyPseudoArgs } from "./pseudos.js";
  4. import { aliases } from "./aliases.js";
  5. import { subselects } from "./subselects.js";
  6. export { filters, pseudos, aliases };
  7. export function compilePseudoSelector(next, selector, options, context, compileToken) {
  8. var _a;
  9. const { name, data } = selector;
  10. if (Array.isArray(data)) {
  11. if (!(name in subselects)) {
  12. throw new Error(`Unknown pseudo-class :${name}(${data})`);
  13. }
  14. return subselects[name](next, data, options, context, compileToken);
  15. }
  16. const userPseudo = (_a = options.pseudos) === null || _a === void 0 ? void 0 : _a[name];
  17. const stringPseudo = typeof userPseudo === "string" ? userPseudo : aliases[name];
  18. if (typeof stringPseudo === "string") {
  19. if (data != null) {
  20. throw new Error(`Pseudo ${name} doesn't have any arguments`);
  21. }
  22. // The alias has to be parsed here, to make sure options are respected.
  23. const alias = parse(stringPseudo);
  24. return subselects["is"](next, alias, options, context, compileToken);
  25. }
  26. if (typeof userPseudo === "function") {
  27. verifyPseudoArgs(userPseudo, name, data, 1);
  28. return (elem) => userPseudo(elem, data) && next(elem);
  29. }
  30. if (name in filters) {
  31. return filters[name](next, data, options, context);
  32. }
  33. if (name in pseudos) {
  34. const pseudo = pseudos[name];
  35. verifyPseudoArgs(pseudo, name, data, 2);
  36. return (elem) => pseudo(elem, options, data) && next(elem);
  37. }
  38. throw new Error(`Unknown pseudo-class :${name}`);
  39. }
  40. //# sourceMappingURL=index.js.map