template-tokenize.js 839 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. const tokenize = require("postcss/lib/tokenize");
  3. function templateTokenize(...args) {
  4. const tokenizer = tokenize(...args);
  5. function nextToken(...args) {
  6. const returned = [];
  7. let token, lastPos;
  8. let depth = 0;
  9. while ((token = tokenizer.nextToken.apply(tokenizer, args))) {
  10. if (token[0] !== "word") {
  11. if (token[0] === "{") {
  12. ++depth;
  13. } else if (token[0] === "}") {
  14. --depth;
  15. }
  16. }
  17. if (depth || returned.length) {
  18. lastPos = token[3] || token[2] || lastPos;
  19. returned.push(token);
  20. }
  21. if (!depth) {
  22. break;
  23. }
  24. }
  25. if (returned.length) {
  26. token = [
  27. "word",
  28. returned.map((token) => token[1]).join(""),
  29. returned[0][2],
  30. lastPos,
  31. ];
  32. }
  33. return token;
  34. }
  35. return Object.assign({}, tokenizer, {
  36. nextToken,
  37. });
  38. }
  39. module.exports = templateTokenize;