You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var actionTypes = {
  4. equals: "",
  5. element: "~",
  6. start: "^",
  7. end: "$",
  8. any: "*",
  9. not: "!",
  10. hyphen: "|"
  11. };
  12. var simpleSelectors = {
  13. child: " > ",
  14. parent: " < ",
  15. sibling: " ~ ",
  16. adjacent: " + ",
  17. descendant: " ",
  18. universal: "*"
  19. };
  20. function stringify(token) {
  21. return token.map(stringifySubselector).join(", ");
  22. }
  23. exports.default = stringify;
  24. function stringifySubselector(token) {
  25. return token.map(stringifyToken).join("");
  26. }
  27. function stringifyToken(token) {
  28. if (token.type in simpleSelectors)
  29. return simpleSelectors[token.type];
  30. if (token.type === "tag")
  31. return escapeName(token.name);
  32. if (token.type === "pseudo-element")
  33. return "::" + escapeName(token.name);
  34. if (token.type === "attribute") {
  35. if (token.action === "exists") {
  36. return "[" + escapeName(token.name) + "]";
  37. }
  38. if (token.name === "id" &&
  39. token.action === "equals" &&
  40. !token.ignoreCase) {
  41. return "#" + escapeName(token.value);
  42. }
  43. if (token.name === "class" &&
  44. token.action === "element" &&
  45. !token.ignoreCase) {
  46. return "." + escapeName(token.value);
  47. }
  48. var atributeName = escapeName(token.name);
  49. var action = actionTypes[token.action];
  50. var value = escapeName(token.value);
  51. var ignoreCase = token.ignoreCase ? "i" : "";
  52. return "[" + atributeName + action + "='" + value + "'" + ignoreCase + "]";
  53. }
  54. if (token.type === "pseudo") {
  55. if (token.data === null)
  56. return ":" + escapeName(token.name);
  57. if (typeof token.data === "string") {
  58. return ":" + escapeName(token.name) + "(" + token.data + ")";
  59. }
  60. return ":" + escapeName(token.name) + "(" + stringify(token.data) + ")";
  61. }
  62. throw new Error("Unknown type");
  63. }
  64. function escapeName(str) {
  65. //TODO
  66. return str;
  67. }