summaryrefslogtreecommitdiff
path: root/test/docx/styles-reader.tests.js
blob: 43cba9854b57f3fa7fe6ebf4c3edf39e1e9c33e1 (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
var assert = require("assert");

var readStylesXml = require("../../lib/docx/styles-reader").readStylesXml;
var XmlElement = require("../../lib/xml").Element;
var test = require("../test")(module);


test('paragraph style is null if no style with that ID exists', function() {
    var styles = readStylesXml(
        new XmlElement("w:styles", {}, [])
    );
    assert.equal(styles.findParagraphStyleById("Heading1"), null);
});

test('paragraph style can be found by ID', function() {
    var styles = readStylesXml(
        new XmlElement("w:styles", {}, [
            paragraphStyleElement("Heading1", "Heading 1")
        ])
    );
    assert.equal(styles.findParagraphStyleById("Heading1").styleId, "Heading1");
});

test('table style can be found by ID', function() {
    var styles = readStylesXml(
        new XmlElement("w:styles", {}, [
            tableStyleElement("TableNormal", "Normal Table")
        ])
    );
    assert.equal(styles.findTableStyleById("TableNormal").styleId, "TableNormal");
});

test('character style can be found by ID', function() {
    var styles = readStylesXml(
        new XmlElement("w:styles", {}, [
            characterStyleElement("Heading1Char", "Heading 1 Char")
        ])
    );
    assert.equal(styles.findCharacterStyleById("Heading1Char").styleId, "Heading1Char");
});

test('paragraph and character styles are distinct', function() {
    var styles = readStylesXml(
        new XmlElement("w:styles", {}, [
            paragraphStyleElement("Heading1", "Heading 1"),
            characterStyleElement("Heading1Char", "Heading 1 Char")
        ])
    );
    assert.equal(styles.findCharacterStyleById("Heading1"), null);
    assert.equal(styles.findParagraphStyleById("Heading1Char"), null);
});

test('character and table styles are distinct', function() {
    var styles = readStylesXml(
        new XmlElement("w:styles", {}, [
            tableStyleElement("Heading1", "Heading 1")
        ])
    );
    assert.equal(styles.findCharacterStyleById("Heading1"), null);
});

test('styles include names', function() {
    var styles = readStylesXml(
        new XmlElement("w:styles", {}, [
            paragraphStyleElement("Heading1", "Heading 1")
        ])
    );
    assert.equal(styles.findParagraphStyleById("Heading1").name, "Heading 1");
});

test('style name is null if w:name element does not exist', function() {
    var styles = readStylesXml(
        new XmlElement("w:styles", {}, [
            styleWithoutWNameElement("paragraph", "Heading1"),
            styleWithoutWNameElement("character", "Heading1Char")
        ])
    );
    assert.equal(styles.findParagraphStyleById("Heading1").name, null);
    assert.equal(styles.findCharacterStyleById("Heading1Char").name, null);
});

function paragraphStyleElement(id, name) {
    return styleElement("paragraph", id, name);
}

function characterStyleElement(id, name) {
    return styleElement("character", id, name);
}

function tableStyleElement(id, name) {
    return styleElement("table", id, name);
}

function styleElement(type, id, name) {
    return new XmlElement("w:style", {"w:type": type, "w:styleId": id}, [
        new XmlElement("w:name", {"w:val": name}, [])
    ]);
}

function styleWithoutWNameElement(type, id) {
    return new XmlElement("w:style", {"w:type": type, "w:styleId": id}, []);
}