style-map.tests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. var assert = require("assert");
  2. var JSZip = require("jszip");
  3. var zipfile = require("../../lib/zipfile");
  4. var styleMap = require("../../lib/docx/style-map");
  5. var test = require("../test")(module);
  6. test('reading embedded style map on document without embedded style map returns null', function() {
  7. return normalDocx().then(function(zip) {
  8. return styleMap.readStyleMap(zip).then(function(contents) {
  9. assert.equal(contents, null);
  10. });
  11. });
  12. });
  13. test('embedded style map can be read after being written', function() {
  14. return normalDocx().then(function(zip) {
  15. return styleMap.writeStyleMap(zip, "p => h1").then(function() {
  16. return styleMap.readStyleMap(zip).then(function(contents) {
  17. assert.equal(contents, "p => h1");
  18. });
  19. });
  20. });
  21. });
  22. test('embedded style map is written to separate file', function() {
  23. return normalDocx().then(function(zip) {
  24. return styleMap.writeStyleMap(zip, "p => h1").then(function() {
  25. return zip.read("mammoth/style-map", "utf8").then(function(contents) {
  26. assert.equal(contents, "p => h1");
  27. });
  28. });
  29. });
  30. });
  31. test('embedded style map is referenced in relationships', function() {
  32. return normalDocx().then(function(zip) {
  33. return styleMap.writeStyleMap(zip, "p => h1").then(function() {
  34. return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) {
  35. assert.equal(contents, expectedRelationshipsXml);
  36. });
  37. });
  38. });
  39. });
  40. test('re-embedding style map replaces original', function() {
  41. return normalDocx().then(function(zip) {
  42. return styleMap.writeStyleMap(zip, "p => h1").then(function() {
  43. return styleMap.writeStyleMap(zip, "p => h2");
  44. }).then(function() {
  45. return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) {
  46. assert.equal(contents, expectedRelationshipsXml);
  47. });
  48. }).then(function() {
  49. return styleMap.readStyleMap(zip).then(function(contents) {
  50. assert.equal(contents, "p => h2");
  51. });
  52. });
  53. });
  54. });
  55. test('embedded style map has override content type in [Content_Types].xml', function() {
  56. return normalDocx().then(function(zip) {
  57. return styleMap.writeStyleMap(zip, "p => h1").then(function() {
  58. return zip.read("[Content_Types].xml", "utf8").then(function(contents) {
  59. assert.equal(contents, expectedContentTypesXml);
  60. });
  61. });
  62. });
  63. });
  64. test('replacing style map keeps content type', function() {
  65. return normalDocx().then(function(zip) {
  66. return styleMap.writeStyleMap(zip, "p => h1").then(function() {
  67. return styleMap.writeStyleMap(zip, "p => h2");
  68. }).then(function() {
  69. return zip.read("[Content_Types].xml", "utf8").then(function(contents) {
  70. assert.equal(contents, expectedContentTypesXml);
  71. });
  72. });
  73. });
  74. });
  75. var expectedRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
  76. '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
  77. '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
  78. '<Relationship Id="rMammothStyleMap" Type="http://schemas.zwobble.org/mammoth/style-map" Target="/mammoth/style-map"/>' +
  79. '</Relationships>';
  80. var expectedContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
  81. '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
  82. '<Default Extension="png" ContentType="image/png"/>' +
  83. '<Override PartName="/mammoth/style-map" ContentType="text/prs.mammoth.style-map"/>' +
  84. '</Types>';
  85. function normalDocx() {
  86. var zip = new JSZip();
  87. var originalRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
  88. '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
  89. '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
  90. '</Relationships>';
  91. var originalContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
  92. '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
  93. '<Default Extension="png" ContentType="image/png"/>' +
  94. '</Types>';
  95. zip.file("word/_rels/document.xml.rels", originalRelationshipsXml);
  96. zip.file("[Content_Types].xml", originalContentTypesXml);
  97. return zip.generateAsync({type: "arraybuffer"}).then(function(buffer) {
  98. return zipfile.openArrayBuffer(buffer);
  99. });
  100. }