stringify.js 837 B

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. const postcssStringify = require("postcss/lib/stringify");
  3. const Document = require("./html/document");
  4. module.exports = stringify;
  5. function stringify(node, builder) {
  6. if (!(node instanceof Document)) {
  7. const syntax = node.source.syntax || node.root().source.syntax;
  8. if (syntax && syntax.stringify) {
  9. syntax.stringify(node, builder);
  10. } else {
  11. postcssStringify(node, builder);
  12. }
  13. return;
  14. }
  15. if (node.nodes.length) {
  16. node.nodes.forEach((root) => {
  17. builder(root.raws.codeBefore, root, "codeBefore");
  18. if (root.source.syntax) {
  19. root.source.syntax.stringify(root, builder);
  20. } else {
  21. postcssStringify(root, builder);
  22. }
  23. builder(root.raws.codeAfter || "", root, "codeAfter");
  24. });
  25. } else {
  26. // If it do not have root, it will output the input.
  27. builder(node.source.input.css);
  28. }
  29. }