summaryrefslogtreecommitdiff
path: root/test/docx/style-map.tests.js
blob: 4ddb12744a3783e29f40608740958ea51ba8cb81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var assert = require("assert");

var JSZip = require("jszip");

var zipfile = require("../../lib/zipfile");
var styleMap = require("../../lib/docx/style-map");
var test = require("../test")(module);

test('reading embedded style map on document without embedded style map returns null', function() {
    var zip = normalDocx();
    
    return styleMap.readStyleMap(zip).then(function(contents) {
        assert.equal(contents, null);
    });
});

test('embedded style map can be read after being written', function() {
    var zip = normalDocx();
    
    return styleMap.writeStyleMap(zip, "p => h1").then(function() {
        return styleMap.readStyleMap(zip).then(function(contents) {
            assert.equal(contents, "p => h1");
        });
    });
});

test('embedded style map is written to separate file', function() {
    var zip = normalDocx();
    
    return styleMap.writeStyleMap(zip, "p => h1").then(function() {
        return zip.read("mammoth/style-map", "utf8").then(function(contents) {
            assert.equal(contents, "p => h1");
        });
    });
});

test('embedded style map is referenced in relationships', function() {
    var zip = normalDocx();
    
    return styleMap.writeStyleMap(zip, "p => h1").then(function() {
        return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) {
            assert.equal(contents, expectedRelationshipsXml);
        });
    });
});

test('re-embedding style map replaces original', function() {
    var zip = normalDocx();
    
    return styleMap.writeStyleMap(zip, "p => h1").then(function() {
        return styleMap.writeStyleMap(zip, "p => h2");
    }).then(function() {
        return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) {
            assert.equal(contents, expectedRelationshipsXml);
        });
    }).then(function() {
        return styleMap.readStyleMap(zip).then(function(contents) {
            assert.equal(contents, "p => h2");
        });
    });
});

test('embedded style map has override content type in [Content_Types].xml', function() {
    var zip = normalDocx();
    
    return styleMap.writeStyleMap(zip, "p => h1").then(function() {
        return zip.read("[Content_Types].xml", "utf8").then(function(contents) {
            assert.equal(contents, expectedContentTypesXml);
        });
    });
});

test('replacing style map keeps content type', function() {
    var zip = normalDocx();
    
    return styleMap.writeStyleMap(zip, "p => h1").then(function() {
        return styleMap.writeStyleMap(zip, "p => h2");
    }).then(function() {
        return zip.read("[Content_Types].xml", "utf8").then(function(contents) {
            assert.equal(contents, expectedContentTypesXml);
        });
    });
});

var expectedRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
    '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
    '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
    '<Relationship Id="rMammothStyleMap" Type="http://schemas.zwobble.org/mammoth/style-map" Target="/mammoth/style-map"/>' +
    '</Relationships>';

var expectedContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
    '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
    '<Default Extension="png" ContentType="image/png"/>' +
    '<Override PartName="/mammoth/style-map" ContentType="text/prs.mammoth.style-map"/>' +
    '</Types>';

function normalDocx() {
    var zip = new JSZip();
    var originalRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
        '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
        '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
        '</Relationships>';
    var originalContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
        '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
        '<Default Extension="png" ContentType="image/png"/>' +
        '</Types>';
    zip.file("word/_rels/document.xml.rels", originalRelationshipsXml);
    zip.file("[Content_Types].xml", originalContentTypesXml);
    var buffer = zip.generate({type: "arraybuffer"});
    return zipfile.openArrayBuffer(buffer);
}