-
-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathhtml-to-text.js
More file actions
633 lines (531 loc) · 30.9 KB
/
html-to-text.js
File metadata and controls
633 lines (531 loc) · 30.9 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/* eslint max-len: "off" */
var expect = require('chai').expect;
var htmlToText = require('..');
var path = require('path');
var fs = require('fs');
describe('html-to-text', function() {
describe('.fromString()', function() {
describe('wordwrap option', function() {
var longStr;
beforeEach(function() {
longStr = '111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888 999999999';
});
it('should wordwrap at 80 characters by default', function() {
expect(htmlToText.fromString(longStr)).to.equal('111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888\n999999999');
});
it('should wordwrap at given amount of characters when give a number', function() {
expect(htmlToText.fromString(longStr, { wordwrap: 20 })).to.equal('111111111 222222222\n333333333 444444444\n555555555 666666666\n777777777 888888888\n999999999');
expect(htmlToText.fromString(longStr, { wordwrap: 50 })).to.equal('111111111 222222222 333333333 444444444 555555555\n666666666 777777777 888888888 999999999');
expect(htmlToText.fromString(longStr, { wordwrap: 70 })).to.equal('111111111 222222222 333333333 444444444 555555555 666666666 777777777\n888888888 999999999');
});
it('should not wordwrap when given null', function() {
expect(htmlToText.fromString(longStr, { wordwrap: null })).to.equal(longStr);
});
it('should not wordwrap when given false', function() {
expect(htmlToText.fromString(longStr, { wordwrap: false })).to.equal(longStr);
});
it('should not exceed the line width when processing embedded format tags', function() {
var testString = '<p><strong>This text isn\'t counted</strong> when calculating where to break a string for 80 character line lengths.</p>';
expect(htmlToText.fromString(testString, {} )).to.equal('This text isn\'t counted when calculating where to break a string for 80\ncharacter line lengths.');
});
it('should work with a long string containing line feeds', function() {
var testString = '<p>If a word with a line feed exists over the line feed boundary then\nyou\nmust\nrespect it.</p>';
expect(htmlToText.fromString(testString, {} )).to.equal('If a word with a line feed exists over the line feed boundary then you must\nrespect it.');
});
it('should not wrongly truncate lines when processing embedded format tags', function() {
var testString = '<p><strong>This text isn\'t counted</strong> when calculating where to break a string for 80 character line lengths. However it can affect where the next line breaks and this could lead to having an early line break</p>';
expect(htmlToText.fromString(testString, {} )).to.equal('This text isn\'t counted when calculating where to break a string for 80\ncharacter line lengths. However it can affect where the next line breaks and\nthis could lead to having an early line break');
});
it('should not exceed the line width when processing anchor tags', function() {
var testString = "<p>We appreciate your business. And we hope you'll check out our <a href=\"http://example.com/\">new products</a>!</p>";
expect(htmlToText.fromString(testString, {} )).to.equal('We appreciate your business. And we hope you\'ll check out our new products\n[http://example.com/]!');
});
it('should honour line feeds from a long word across the wrap, where the line feed is before the wrap', function() {
var testString = '<p>This string is meant to test if a string is split properly across a\nnewlineandlongword with following text.</p>';
expect(htmlToText.fromString(testString, {} ))
.to.equal('This string is meant to test if a string is split properly across a\nnewlineandlongword with following text.');
});
it('should remove line feeds from a long word across the wrap, where the line feed is after the wrap', function() {
var testString = '<p>This string is meant to test if a string is split properly across anewlineandlong\nword with following text.</p>';
expect(htmlToText.fromString(testString, {} ))
.to.equal('This string is meant to test if a string is split properly across\nanewlineandlong word with following text.');
});
});
describe('preserveNewlines option', function() {
var newlineStr;
beforeEach(function() {
newlineStr = '<p\n>One\nTwo\nThree</p>';
});
it('should not preserve newlines by default', function() {
expect(htmlToText.fromString(newlineStr)).to.not.contain('\n');
});
it('should preserve newlines when provided with a truthy value', function() {
expect(htmlToText.fromString(newlineStr, { preserveNewlines: true })).to.contain('\n');
});
it('should not preserve newlines in the tags themselves', function() {
var output_text = htmlToText.fromString(newlineStr, { preserveNewlines: true });
expect(output_text.slice(0,1)).to.equal("O");
});
it('should preserve line feeds in a long wrapping string containing line feeds', function() {
var testString = '<p>If a word with a line feed exists over the line feed boundary then\nyou\nmust\nrespect it.</p>';
expect(htmlToText.fromString(testString, { preserveNewlines: true } ))
.to.equal('If a word with a line feed exists over the line feed boundary then\nyou\nmust\nrespect it.');
});
it('should preserve line feeds in a long string containing line feeds across the wrap', function() {
var testString = '<p>If a word with a line feed exists over the line feed boundary then\nyou must respect it.</p>';
expect(htmlToText.fromString(testString, { preserveNewlines: true } ))
.to.equal('If a word with a line feed exists over the line feed boundary then\nyou must respect it.');
});
it('should preserve line feeds in a long string containing line feeds across the wrap with a line feed before 80 chars', function() {
var testString = '<p>This string is meant to test if a string is split properly across a\nnewlineandlongword with following text.</p>';
expect(htmlToText.fromString(testString, { preserveNewlines: true } ))
.to.equal('This string is meant to test if a string is split properly across a\nnewlineandlongword with following text.');
});
it('should preserve line feeds in a long string containing line feeds across the wrap with a line feed after 80 chars', function() {
var testString = '<p>This string is meant to test if a string is split properly across anewlineandlong\nword with following text.</p>';
expect(htmlToText.fromString(testString, { preserveNewlines: true } ))
.to.equal('This string is meant to test if a string is split properly across\nanewlineandlong\nword with following text.');
});
it('should split long lines', function() {
var testString = '<p>If a word with a line feed exists over the line feed boundary then you must respect it.</p>';
expect(htmlToText.fromString(testString, { preserveNewlines: true } ))
.to.equal('If a word with a line feed exists over the line feed boundary then you must\nrespect it.');
});
});
describe('single line paragraph option', function() {
var paragraphsString;
beforeEach(function() {
paragraphsString = '<p>First</p><p>Second</p>';
});
it('should not use single new line when given null', function() {
expect(htmlToText.fromString(paragraphsString, { singleNewLineParagraphs: null } )).to.equal('First\n\nSecond');
});
it('should not use single new line when given false', function() {
expect(htmlToText.fromString(paragraphsString, { singleNewLineParagraphs: false } )).to.equal('First\n\nSecond');
});
it('should use single new line when given true', function() {
expect(htmlToText.fromString(paragraphsString, { singleNewLineParagraphs: true } )).to.equal('First\nSecond');
});
});
});
describe('.fromFile()', function() {
it('should convert file at given path', function(done) {
var htmlFile = path.join(__dirname, 'test.html'),
txtFile = path.join(__dirname, 'test.txt');
var expectedTxt = fs.readFileSync(txtFile, 'utf8');
htmlToText.fromFile(htmlFile, { tables: ['#invoice', '.address'] }, function(err, text) {
expect(err).to.be.a('null');
expect(text).to.equal(expectedTxt);
done();
});
});
});
describe('tables', function () {
it('does not process tables with uppercase tags / does not process tables with center tag', function () {
var html = 'Good morning Jacob, \
<TABLE> \
<CENTER> \
<TBODY> \
<TR> \
<TD>Lorem ipsum dolor sit amet.</TD> \
</TR> \
</CENTER> \
</TBODY> \
</TABLE> \
';
var resultExpected = 'Good morning Jacob, Lorem ipsum dolor sit amet.';
var result = htmlToText.fromString(html, { tables: true });
expect(result).to.equal(resultExpected);
});
it('does handle non-integer colspan on td element gracefully', function () {
var html = 'Good morning Jacob, \
<TABLE> \
<CENTER> \
<TBODY> \
<TR> \
<TD colspan="abc">Lorem ipsum dolor sit amet.</TD> \
</TR> \
</CENTER> \
</TBODY> \
</TABLE> \
';
var resultExpected = 'Good morning Jacob, Lorem ipsum dolor sit amet.';
var result = htmlToText.fromString(html, { tables: true });
expect(result).to.equal(resultExpected);
});
});
describe('a', function () {
it('should decode html attribute entities from href', function () {
var result = htmlToText.fromString('<a href="/foo?a=b">test</a>');
expect(result).to.equal('test [/foo?a=b]');
});
it('should strip mailto: from email links', function () {
var result = htmlToText.fromString('<a href="mailto:foo@example.com">email me</a>');
expect(result).to.equal('email me [foo@example.com]');
});
it('should return link with brackets', function () {
var result = htmlToText.fromString('<a href="http://my.link">test</a>');
expect(result).to.equal('test [http://my.link]');
});
it('should return link without brackets', function () {
var result = htmlToText.fromString('<a href="http://my.link">test</a>', {
noLinkBrackets: true
});
expect(result).to.equal('test http://my.link');
});
it('should not return link for anchor if noAnchorUrl is set to true', function () {
var result = htmlToText.fromString('<a href="#link">test</a>', {
noAnchorUrl: true
});
expect(result).to.equal('test');
});
it('should return link for anchor if noAnchorUrl is set to false', function () {
var result = htmlToText.fromString('<a href="#link">test</a>', {
noAnchorUrl: false
});
expect(result).to.equal('test [#link]');
});
});
describe('lists', function() {
describe('ul', function() {
it('should handle empty unordered lists', function() {
var testString = '<ul></ul>';
expect(htmlToText.fromString(testString)).to.equal('');
});
it('should handle an unordered list with multiple elements', function() {
var testString = '<ul><li>foo</li><li>bar</li></ul>';
expect(htmlToText.fromString(testString)).to.equal(' * foo\n * bar');
});
it('should handle an unordered list with line break', function() {
var testString = '<ul><br><li>foo</li><li>bar</li></ul>';
expect(htmlToText.fromString(testString)).to.equal(' * foo\n * bar');
});
it('should handle an unordered list prefix option', function() {
var testString = '<ul><li>foo</li><li>bar</li></ul>';
var options = {unorderedListItemPrefix: ' test '};
expect(htmlToText.fromString(testString, options)).to.equal(' test foo\n test bar');
});
});
describe('ol', function() {
it('should handle empty ordered lists', function() {
var testString = '<ol></ol>';
expect(htmlToText.fromString(testString)).to.equal('');
});
it('should handle an ordered list with multiple elements', function() {
var testString = '<ol><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
});
it('should handle an ordered list with line break', function() {
var testString = '<ol><br><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
});
it('should support the ordered list type="1" attribute', function() {
var testString = '<ol type="1"><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
});
it('should fallback to type="!" behavior if type attribute is invalid', function() {
var testString = '<ol type="1"><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
});
it('should support the ordered list type="a" attribute', function() {
var testString = '<ol type="a"><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' a. foo\n b. bar');
});
it('should support the ordered list type="A" attribute', function() {
var testString = '<ol type="A"><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' A. foo\n B. bar');
});
it('should support the ordered list type="i" attribute by falling back to type="1"', function() {
var testString = '<ol type="i"><li>foo</li><li>bar</li></ol>';
// TODO Implement lowercase roman numerals
// expect(htmlToText.fromString(testString)).to.equal('i. foo\nii. bar');
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
});
it('should support the ordered list type="I" attribute by falling back to type="1"', function() {
var testString = '<ol type="I"><li>foo</li><li>bar</li></ol>';
// TODO Implement uppercase roman numerals
// expect(htmlToText.fromString(testString)).to.equal('I. foo\nII. bar');
expect(htmlToText.fromString(testString)).to.equal(' 1. foo\n 2. bar');
});
it('should support the ordered list start attribute', function() {
var testString = '<ol start="2"><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal(' 2. foo\n 3. bar');
});
/*
* Currently failing tests for continuing to fill out the specification
* Spec: https://html.spec.whatwg.org/multipage/semantics.html#the-ol-element
*
it('should support the ordered list type="a" attribute past 26 characters', function() {
var testString = '<ol start="26" type="a"><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal('z. foo\naa. bar');
});
it('should support the ordered list type="A" attribute past 26 characters', function() {
var testString = '<ol start="26" type="A"><li>foo</li><li>bar</li></ol>';
expect(htmlToText.fromString(testString)).to.equal('Z. foo\nAA. bar');
});
*/
});
it('doesnt wrap li if wordwrap isnt', function () {
var html = 'Good morning Jacob, \
<p>Lorem ipsum dolor sit amet</p> \
<p><strong>Lorem ipsum dolor sit amet.</strong></p> \
<ul> \
<li>run in the park <span style="color:#888888;">(in progress)</span></li> \
</ul> \
';
var resultExpected = 'Good morning Jacob, Lorem ipsum dolor sit amet\n\nLorem ipsum dolor sit amet.\n\n * run in the park (in progress)';
var result = htmlToText.fromString(html, { wordwrap: false });
expect(result).to.equal(resultExpected);
});
});
describe('entities', function () {
it('does not insert null bytes', function () {
var html = '<a href="some-url?a=b&b=c">Testing & Done</a>';
var result = htmlToText.fromString(html);
expect(result).to.equal('Testing & Done [some-url?a=b&b=c]');
});
it('should replace entities inside `alt` attributes of images', function () {
var html = '<img src="test.png" alt=""Awesome"">';
var result = htmlToText.fromString(html);
expect(result).to.equal('"Awesome" [test.png]');
});
});
describe('unicode support', function () {
it('should decode 😂 to 😂', function () {
var result = htmlToText.fromString('😂');
expect(result).to.equal('😂');
});
});
describe('disable uppercaseHeadings', function () {
[1, 2, 3, 4, 5, 6].forEach(function (i) {
it('should return h' + i + ' in lowercase', function () {
var result = htmlToText.fromString('<h' + i + '>test</h' + i + '>', {
uppercaseHeadings: false
});
expect(result).to.equal('test');
});
});
});
describe('custom formatting', function () {
it('should allow to pass custom formatting functions', function () {
var result = htmlToText.fromString('<h1>TeSt</h1>', {
format: {
heading: function (elem, fn, options) {
var h = fn(elem.children, options);
return '====\n' + h.toLowerCase() + '\n====';
}
}
});
expect(result).to.equal('====\ntest\n====');
});
});
describe('Base element', function () {
it('should retrieve and convert the entire document under `body` by default', function(done) {
var htmlFile = path.join(__dirname, 'test.html');
var txtFile = path.join(__dirname, 'test.txt');
var expectedTxt = fs.readFileSync(txtFile, 'utf8');
var options = {
tables: ['#invoice', '.address']
};
htmlToText.fromFile(htmlFile, options, function(err, text) {
expect(err).to.be.a('null');
expect(text).to.equal(expectedTxt);
done();
});
});
it('should only retrieve and convert content under the specified base element if found', function(done) {
var htmlFile = path.join(__dirname, 'test.html');
var txtFile = path.join(__dirname, 'test-address.txt');
var expectedTxt = fs.readFileSync(txtFile, 'utf8');
var options = {
tables: ['.address'],
baseElement: 'table.address'
};
htmlToText.fromFile(htmlFile, options, function(err, text) {
expect(err).to.be.a('null');
expect(text).to.equal(expectedTxt);
done();
});
});
it('should retrieve and convert content under multiple base elements', function(done) {
var htmlFile = path.join(__dirname, 'test.html');
var txtFile = path.join(__dirname, 'test-address-dup.txt');
var expectedTxt = fs.readFileSync(txtFile, 'utf8');
var options = {
tables: ['.address'],
baseElement: ['table.address', 'table.address']
};
htmlToText.fromFile(htmlFile, options, function(err, text) {
expect(err).to.be.a('null');
expect(text).to.equal(expectedTxt);
done();
});
});
it('should retrieve and convert content under multiple base elements in any order', function(done) {
var htmlFile = path.join(__dirname, 'test.html');
var txtFile = path.join(__dirname, 'test-any-order.txt');
var expectedTxt = fs.readFileSync(txtFile, 'utf8');
var options = {
tables: ['.address'],
baseElement: ['table.address', 'p.normal-space', 'table.address']
};
htmlToText.fromFile(htmlFile, options, function(err, text) {
expect(err).to.be.a('null');
expect(text).to.equal(expectedTxt);
done();
});
});
it('should process the first base element found when multiple exist', function(done) {
var htmlFile = path.join(__dirname, 'test.html');
var txtFile = path.join(__dirname, 'test-first-element.txt');
var expectedTxt = fs.readFileSync(txtFile, 'utf8');
var options = {
tables: ['.address'],
baseElement: 'p.normal-space'
};
htmlToText.fromFile(htmlFile, options, function(err, text) {
expect(err).to.be.a('null');
expect(text).to.equal(expectedTxt);
done();
});
});
it('should retrieve and convert the entire document by default if no base element is found', function(done) {
var htmlFile = path.join(__dirname, 'test.html');
var txtFile = path.join(__dirname, 'test.txt');
var expectedTxt = fs.readFileSync(txtFile, 'utf8');
var options = {
tables: ['#invoice', '.address'],
baseElement: 'table.notthere'
};
htmlToText.fromFile(htmlFile, options, function(err, text) {
expect(err).to.be.a('null');
expect(text).to.equal(expectedTxt);
done();
});
});
it('should return null if the base element isn\'t found and we\'re not returning the DOM by default', function(done) {
var htmlFile = path.join(__dirname, 'test.html');
var expectedTxt = '';
var options = {
tables: ['#invoice', '.address'],
baseElement: 'table.notthere',
returnDomByDefault: false
};
htmlToText.fromFile(htmlFile, options, function(err, text) {
expect(err).to.be.a('null');
expect(text).to.equal(expectedTxt);
done();
});
});
});
describe('Long words', function() {
it('should split long words if forceWrapOnLimit is set, existing linefeeds converted to space', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['/'], forceWrapOnLimit: true }} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlo\nng word_with_following_text.');
});
it('should not wrap a string if longWordSplit is not set', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlongword_with_following_text.</p>';
expect(htmlToText.fromString(testString, {} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlongword_with_following_text.');
});
it('should not wrap a string if not wrapCharacters are found and forceWrapOnLimit is not set', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['/'], forceWrapOnLimit: false }} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.');
});
it('should not wrap a string if no wrapCharacters are set and forceWrapOnLimit is not set', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: [], forceWrapOnLimit: false }} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.');
});
it('should wrap on the last instance of a wrap character before the wordwrap limit.', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['/', '_'], forceWrapOnLimit: false }} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_\nanewlineandlong word_with_following_text.');
});
it('should wrap on the last instance of a wrap character before the wordwrap limit. Content of wrapCharacters shouldn\'t matter.', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['/','-', '_'], forceWrapOnLimit: false }} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_\nanewlineandlong word_with_following_text.');
});
it('should wrap on the last instance of a wrap character before the wordwrap limit. Order of wrapCharacters shouldn\'t matter.', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['_', '/'], forceWrapOnLimit: false }} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_\nanewlineandlong word_with_following_text.');
});
it('should wrap on the last instance of a wrap character before the wordwrap limit. Should preference wrapCharacters in order', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split-properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['-', '_', '/'], forceWrapOnLimit: false }} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split-\nproperly_across_anewlineandlong word_with_following_text.');
});
it('should not wrap a string that is too short', function() {
var testString = '<p>https://github.com/werk85/node-html-to-text/blob/master/lib/html-to-text.js</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['/', '-'], forceWrapOnLimit: false }} ))
.to.equal('https://github.com/werk85/node-html-to-text/blob/master/lib/html-to-text.js');
});
it('should wrap a url string using \'/\'', function() {
var testString = '<p>https://github.com/AndrewFinlay/node-html-to-text/commit/64836a5bd97294a672b24c26cb8a3ccdace41001</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['/', '-'], forceWrapOnLimit: false }} ))
.to.equal('https://github.com/AndrewFinlay/node-html-to-text/commit/\n64836a5bd97294a672b24c26cb8a3ccdace41001');
});
it('should wrap very long url strings using \'/\'', function() {
var testString = '<p>https://github.com/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/html-to-text.js</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: ['/', '-'], forceWrapOnLimit: false }} ))
.to.equal('https://github.com/werk85/node-html-to-text/blob/master/lib/werk85/\nnode-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/\nwerk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/\nlib/html-to-text.js');
});
it('should wrap very long url strings using limit', function() {
var testString = '<p>https://github.com/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/html-to-text.js</p>';
expect(htmlToText.fromString(testString, { longWordSplit: { wrapCharacters: [], forceWrapOnLimit: true }} ))
.to.equal('https://github.com/werk85/node-html-to-text/blob/master/lib/werk85/node-html-to-\ntext/blob/master/lib/werk85/node-html-to-text/blob/master/lib/werk85/node-html-t\no-text/blob/master/lib/werk85/node-html-to-text/blob/master/lib/html-to-text.js');
});
it('should honour preserveNewlines and split long words', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { preserveNewlines: true, longWordSplit: { wrapCharacters: ['/', '_'], forceWrapOnLimit: false }} ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_\nanewlineandlong\nword_with_following_text.');
});
it('should not put in extra linefeeds if the end of the untouched long string coincides with a preserved line feed', function() {
var testString = '<p>_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.</p>';
expect(htmlToText.fromString(testString, { preserveNewlines: true } ))
.to.equal('_This_string_is_meant_to_test_if_a_string_is_split_properly_across_anewlineandlong\nword_with_following_text.');
});
it('should split long strings buried in links and hide the href', function() {
var testString = '<a href="http://images.fb.com/2015/12/21/ivete-sangalo-launches-360-music-video-on-facebook/">http://images.fb.com/2015/12/21/ivete-sangalo-launches-360-music-video-on-facebook/</a>';
expect(htmlToText.fromString(testString, { hideLinkHrefIfSameAsText: true, longWordSplit: { wrapCharacters: ['/', '_'], forceWrapOnLimit: false }} ))
.to.equal('http://images.fb.com/2015/12/21/\nivete-sangalo-launches-360-music-video-on-facebook/');
});
it('should split long strings buried in links and show the href', function() {
var testString = '<a href="http://images.fb.com/2015/12/21/ivete-sangalo-launches-360-music-video-on-facebook/">http://images.fb.com/2015/12/21/ivete-sangalo-launches-360-music-video-on-facebook/</a>';
expect(htmlToText.fromString(testString, { hideLinkHrefIfSameAsText: false, longWordSplit: { wrapCharacters: ['/', '_'], forceWrapOnLimit: false }} ))
.to.equal('http://images.fb.com/2015/12/21/\nivete-sangalo-launches-360-music-video-on-facebook/\n[http://images.fb.com/2015/12/21/\nivete-sangalo-launches-360-music-video-on-facebook/]');
});
});
describe('whitespace', function() {
it('should not be ignored inside a whitespace-only node', function() {
var testString = 'foo<span> </span>bar';
expect(htmlToText.fromString(testString)).to.equal('foo bar');
});
});
describe('wbr', function() {
it('should handle a large number of wbr tags w/o stack overflow', function() {
var testString = "<!DOCTYPE html><html><head></head><body>\n";
var expectedResult = "";
for (var i = 0; i < 1000; i++){
if (i !== 0 && i % 80 === 0) {
expectedResult += "\n";
}
expectedResult += "n";
testString += "<wbr>n";
}
testString += "</body></html>";
expect(htmlToText.fromString(testString)).to.equal(expectedResult);
});
});
describe('blockquote', function() {
it('should handle format blockquote', function() {
var testString = 'foo<blockquote>test</blockquote>bar';
var expectedResult = 'foo> test\nbar';
expect(htmlToText.fromString(testString)).to.equal(expectedResult);
});
});
});