simplify.tests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. var assert = require("assert");
  2. var _ = require("underscore");
  3. var test = require("../test")(module);
  4. var html = require("../../lib/html");
  5. var htmlPaths = require("../../lib/styles/html-paths");
  6. var nonFreshElement = html.nonFreshElement;
  7. var text = html.text;
  8. test("empty text nodes are removed", function() {
  9. assert.deepEqual(
  10. simplifyNode(text("")),
  11. []
  12. );
  13. });
  14. test("elements with no children are removed", function() {
  15. assert.deepEqual(
  16. simplifyNode(nonFreshElement("p", {}, [])),
  17. []
  18. );
  19. });
  20. test("elements only containing empty nodes are removed", function() {
  21. assert.deepEqual(
  22. simplifyNode(nonFreshElement("p", {}, [text("")])),
  23. []
  24. );
  25. });
  26. test("empty children of element are removed", function() {
  27. assert.deepEqual(
  28. simplifyNode(nonFreshElement("p", {}, [text("Hello"), text("")])),
  29. [nonFreshElement("p", {}, [text("Hello")])]
  30. );
  31. });
  32. test("successive fresh elements are not collapsed", function() {
  33. var path = htmlPaths.elements([
  34. htmlPaths.element("p", {}, {fresh: true})
  35. ]);
  36. var original = concat(
  37. pathToNodes(path, [text("Hello")]),
  38. pathToNodes(path, [text(" there")])
  39. );
  40. assert.deepEqual(
  41. html.simplify(original),
  42. original);
  43. });
  44. test("successive plain non-fresh elements are collapsed if they have the same tag name", function() {
  45. var path = htmlPaths.elements([
  46. htmlPaths.element("p", {}, {fresh: false})
  47. ]);
  48. assert.deepEqual(
  49. html.simplify(concat(
  50. pathToNodes(path, [text("Hello")]),
  51. pathToNodes(path, [text(" there")])
  52. )),
  53. pathToNodes(path, [text("Hello"), text(" there")])
  54. );
  55. });
  56. test("non-fresh can collapse into preceding fresh element", function() {
  57. var freshPath = htmlPaths.elements([
  58. htmlPaths.element("p", {}, {fresh: true})]);
  59. var nonFreshPath = htmlPaths.elements([
  60. htmlPaths.element("p", {}, {fresh: false})]);
  61. assert.deepEqual(
  62. html.simplify(concat(
  63. pathToNodes(freshPath, [text("Hello")]),
  64. pathToNodes(nonFreshPath, [text(" there")])
  65. )),
  66. pathToNodes(freshPath, [text("Hello"), text(" there")])
  67. );
  68. });
  69. test("children of collapsed element can collapse with children of another collapsed element", function() {
  70. assert.deepEqual(
  71. html.simplify([
  72. nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello")])]),
  73. nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("there")])])
  74. ]),
  75. [nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello"), text("there")])])]
  76. );
  77. });
  78. test("empty elements are removed before collapsing", function() {
  79. var freshPath = htmlPaths.elements([
  80. htmlPaths.element("p", {}, {fresh: true})]);
  81. var nonFreshPath = htmlPaths.elements([
  82. htmlPaths.element("p", {}, {fresh: false})]);
  83. assert.deepEqual(
  84. html.simplify(concat(
  85. pathToNodes(nonFreshPath, [text("Hello")]),
  86. pathToNodes(freshPath, []),
  87. pathToNodes(nonFreshPath, [text(" there")])
  88. )),
  89. pathToNodes(nonFreshPath, [text("Hello"), text(" there")])
  90. );
  91. });
  92. test("when separator is present then separator is prepended to collapsed element", function() {
  93. var unseparatedPath = htmlPaths.elements([
  94. htmlPaths.element("pre", {}, {fresh: false})
  95. ]);
  96. var separatedPath = htmlPaths.elements([
  97. htmlPaths.element("pre", {}, {fresh: false, separator: "\n"})
  98. ]);
  99. assert.deepEqual(
  100. html.simplify(concat(
  101. pathToNodes(unseparatedPath, [text("Hello")]),
  102. pathToNodes(separatedPath, [text(" the"), text("re")])
  103. )),
  104. pathToNodes(unseparatedPath, [text("Hello"), text("\n"), text(" the"), text("re")])
  105. );
  106. });
  107. function simplifyNode(node) {
  108. return html.simplify([node]);
  109. }
  110. function concat() {
  111. return _.flatten(arguments, true);
  112. }
  113. function pathToNodes(path, nodes) {
  114. return path.wrap(function() {
  115. return nodes;
  116. });
  117. }