forked from bpampuch/pdfmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextBreaker.spec.js
229 lines (185 loc) · 7.45 KB
/
TextBreaker.spec.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const assert = require('assert');
const TextBreaker = require('../../js/TextBreaker').default;
const StyleContextStack = require('../../js/StyleContextStack').default;
describe('TextBreaker', function () {
var textBreaker = new TextBreaker();
var sampleText = 'Przyklad, bez nowych linii, ale !!!! rozne!!!konstrukcje i ..blablablabla.';
var sampleText2 = 'Przyklad, z nowy\nmi liniami\n, \n \n ale\n\n !!!! rozne!!!konstrukcje i ..blablablabla.';
var plainText = 'Imię: Jan Nazwisko: Nowak\nDodatkowe informacje:';
var plainTextArray = [
'Imię: ',
'Jan ',
' Nazwisko:',
' Nowak\nDodatkowe informacje:'
];
var mixedTextArray = [
{ text: 'Imię: ', bold: true },
'Jan ',
{ text: ' Nazwisko:', bold: true },
{ text: ' Nowak\nDodatkowe informacje:', bold: true }
];
var mixedTextArrayWithUnknownStyleDefinitions = [
{ text: 'Imię: ', bold: true },
'Jan ',
{ text: ' Nazwisko:', bold: true },
{ text: ' Nowak\nDodatkowe informacje:', bold: true, unknownStyle: 123 }
];
var styleStackNoWrap = new StyleContextStack({}, { noWrap: true });
var styleStackWrap = new StyleContextStack({}, { noWrap: false });
describe('getBreaks', function () {
it('should do basic splitting', function () {
var result = textBreaker.getBreaks(sampleText);
assert.equal(result.length, 8);
});
it('should not set lineEnd on inlines if there are no new-lines', function () {
var result = textBreaker.getBreaks(sampleText);
result.forEach(function (item) {
assert.notEqual(item.lineEnd, true);
});
});
it('should split into lines if there are new-line chars', function () {
var result = textBreaker.getBreaks(sampleText2);
assert.equal(result.length, 14);
});
it('should split properly when adjacent newlines appear', function () {
var result = textBreaker.getBreaks(sampleText2);
assert.equal(result[9].text.length, 0);
assert.equal(result[9].lineEnd, true);
});
it('should support whitespace-only lines', function () {
var result = textBreaker.getBreaks(sampleText2);
assert.equal(result[6].text, ' ');
assert.equal(result[6].lineEnd, true);
});
it('should split ZERO WIDTH SPACE character', function () {
var result = textBreaker.getBreaks('first line\u200Bsecond line\u200Bthird line');
assert.equal(result.length, 6);
});
it('should split basic Chinese text', function () {
var result = textBreaker.getBreaks('起来!不愿做奴隶的人们!');
assert.equal(result.length, 10);
});
it('should split Chinese text into lines if there are new-line chars', function () {
var result = textBreaker.getBreaks('中华民族到了最危险的时候,\n每个人被迫着发出最后的吼声。\n起来!起来!起来!');
assert.equal(result.length, 31);
});
it('should split an array of inline texts', function () {
var arrayText = [
{ text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' },
{ text: 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.' }
];
var result = textBreaker.getBreaks(arrayText);
assert.equal(result.length, 52);
});
it('should support keep noWrap from style', function () {
var result = textBreaker.getBreaks([{ text: 'very long text' }], styleStackNoWrap);
assert.equal(result.length, 1);
});
it('should support disable noWrap in text', function () {
var result = textBreaker.getBreaks([{ text: 'very long text', noWrap: false }], styleStackNoWrap);
assert.equal(result.length, 3);
});
it('should support enable noWrap in text', function () {
var result = textBreaker.getBreaks([{ text: 'very long text', noWrap: true }], styleStackWrap);
assert.equal(result.length, 1);
});
it('should support not line break if is text inlines', function () {
var arrayText = [
{ text: 'Celestial Circle—' },
{ text: 'The Faithful Ally' },
{ text: ', ' },
{ text: 'Gift of Knowledge' },
];
var result = textBreaker.getBreaks(arrayText);
assert.equal(result.length, 10);
assert.equal(result[5].text, 'Ally');
assert.equal(result[5].noNewLine, true);
});
it('should support not line break if is text inlines as one word', function () {
var arrayText = [
{ text: 're' },
{ text: 'mark' },
{ text: 'able' }
];
var result = textBreaker.getBreaks(arrayText);
assert.equal(result.length, 3);
assert.equal(result[0].noNewLine, true);
assert.equal(result[1].noNewLine, true);
});
it('should support line break if is text inlines and is new line on end', function () {
var arrayText = [
{ text: 'First line.\n' },
{ text: 'Second line.' }
];
var result = textBreaker.getBreaks(arrayText);
assert.equal(result.length, 4);
assert.equal(result[1].lineEnd, true);
assert.equal(result[1].noNewLine, undefined);
});
it('should support line break if is text inlines and is new line on begin', function () {
var arrayText = [
{ text: 'First line.' },
{ text: '\nSecond line.' }
];
var result = textBreaker.getBreaks(arrayText);
assert.equal(result.length, 5);
assert.equal(result[1].noNewLine, true);
assert.equal(result[2].lineEnd, true);
assert.equal(result[2].noNewLine, undefined);
});
it('should support line break with noWrap', function () {
var arrayText = [
{ text: 'First line', noWrap: true },
{ text: 'second line', noWrap: true }
];
var result = textBreaker.getBreaks(arrayText);
assert.equal(result.length, 2);
assert.equal(result[0].noNewLine, true);
assert.equal(result[1].noNewLine, undefined);
});
it.skip('should support no line break if is text inlines and is space on begin', function () {
var arrayText = [
{ text: 'First line', noWrap: true },
{ text: ' Second line', noWrap: true }
];
// TODO: Fix a test.
var result = textBreaker.getBreaks(arrayText);
assert.equal(result.length, 2);
assert.equal(result[0].noNewLine, undefined);
assert.equal(result[1].noNewLine, undefined);
});
it('should support plain strings', function () {
var result = textBreaker.getBreaks(plainText);
assert.equal(result.length, 6);
});
it('should support plain strings with new-lines', function () {
var result = textBreaker.getBreaks(plainText);
assert(result[3].lineEnd);
});
it('should support an array of plain strings', function () {
var result = textBreaker.getBreaks(plainTextArray);
assert.equal(result.length, 8);
});
it('should support an array of plain strings with new-lines', function () {
var result = textBreaker.getBreaks(plainTextArray);
assert.equal(result[5].lineEnd, true);
});
it('should support arrays with style definition', function () {
var result = textBreaker.getBreaks(mixedTextArray);
assert.equal(result.length, 8);
});
it('should keep style definitions after splitting new-lines', function () {
var result = textBreaker.getBreaks(mixedTextArray);
[0, 2, 3, 4, 5, 6].forEach(function (i) {
assert.equal(result[i].bold, true);
});
assert(!result[1].bold);
});
it('should keep unknown style fields after splitting new-lines', function () {
var result = textBreaker.getBreaks(mixedTextArrayWithUnknownStyleDefinitions);
assert.equal(result.length, 8);
assert.equal(result[5].unknownStyle, 123);
assert.equal(result[6].unknownStyle, 123);
});
});
});