summaryrefslogtreecommitdiff
path: root/test/writers/html-writer.tests.js
blob: b5cdf4e7d0fd4999c84bc16be43d40d22f86f68f (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
113
114
115
var assert = require("assert");
var test = require("../test")(module);

var htmlWriter = require("../../lib/writers/html-writer");


test('can generate simple paragraph', function() {
    var writer = htmlWriter.writer();
    writer.open("p");
    writer.text("Hello");
    writer.close("p");
    return assert.equal(writer.asString(), "<p>Hello</p>");
});

test('can nest elements', function() {
    var writer = htmlWriter.writer();
    writer.open("ul");
    writer.open("li");
    writer.text("One");
    writer.close("li");
    writer.open("li");
    writer.text("Two");
    writer.close("li");
    writer.close("ul");
    return assert.equal(writer.asString(), "<ul><li>One</li><li>Two</li></ul>");
});

test('indents text if prettyPrint is true', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("p");
    writer.text("One");
    writer.close("p");
    writer.open("p");
    writer.text("Two");
    writer.close("p");
    return assert.equal(writer.asString(), "<p>\n  One\n</p>\n<p>\n  Two\n</p>");
});

test('indents element if prettyPrint is true', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("ul");
    writer.open("li");
    writer.text("One");
    writer.close("li");
    writer.open("li");
    writer.text("Two");
    writer.close("li");
    writer.close("ul");
    return assert.equal(writer.asString(), "<ul>\n  <li>\n    One\n  </li>\n  <li>\n    Two\n  </li>\n</ul>");
});

test('when prettyPrint is true inner elements do not have new lines', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("p");
    writer.text("One");
    writer.open("em");
    writer.text("Two");
    writer.close("em");
    writer.close("p");
    return assert.equal(writer.asString(), "<p>\n  One<em>Two</em>\n</p>");
});

test('indents closing element correctly when nested inside another indented element', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("div");
    writer.open("div");
    writer.open("div");
    writer.text("Hello");
    writer.close("div");
    writer.close("div");
    writer.close("div");
    return assert.equal(writer.asString(), "<div>\n  <div>\n    <div>\n      Hello\n    </div>\n  </div>\n</div>");
});

test('newlines in text are indented', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("p");
    writer.text("One\nTwo");
    writer.close("p");
    return assert.equal(writer.asString(), "<p>\n  One\n  Two\n</p>");
});

test('run of text has only one new line', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("p");
    writer.text("One");
    writer.text("Two");
    writer.close("p");
    return assert.equal(writer.asString(), "<p>\n  OneTwo\n</p>");
});

test('run of html has only one new line', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("p");
    writer.append("One");
    writer.append("Two");
    writer.close("p");
    return assert.equal(writer.asString(), "<p>\n  OneTwo\n</p>");
});

test('self closing elements are indented', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("p");
    writer.selfClosing("br");
    writer.close("p");
    return assert.equal(writer.asString(), "<p>\n  <br />\n</p>");
});

test('newlines in appended HTML are indented', function() {
    var writer = htmlWriter.writer({prettyPrint: true});
    writer.open("p");
    writer.append("One\nTwo");
    writer.close("p");
    return assert.equal(writer.asString(), "<p>\n  One\n  Two\n</p>");
});