-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathmdh-html-to-text-test.js
184 lines (146 loc) · 6.92 KB
/
mdh-html-to-text-test.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
/*
* Copyright Adam Pritchard 2013
* MIT License : http://adampritchard.mit-license.org/
*/
"use strict";
/* jshint curly:true, noempty:true, newcap:true, eqeqeq:true, eqnull:true, undef:true, devel:true, browser:true, node:true, evil:false, latedef:false, nonew:true, trailing:false, immed:false, smarttabs:true, expr:true */
/* global describe, expect, it, before, beforeEach, after, afterEach */
/* global _, $, MarkdownRender, htmlToText, marked, hljs, Utils, MdhHtmlToText */
// TODO: Test ranges.
// TODO: Lots more tests.
describe('MdhHtmlToText', function() {
it('should exist', function() {
expect(MdhHtmlToText).to.exist;
expect(MdhHtmlToText.MdhHtmlToText).to.exist;
});
// Wraps the normal HTML-to-text calls
var get = function(mdHTML) {
var elem = $('<div>').html(mdHTML).appendTo('body');
var mdhHtmlToText = new MdhHtmlToText.MdhHtmlToText(elem.get(0));
var text = mdhHtmlToText.get();
$(elem).remove();
return text;
};
// Wraps the check-for-unrendered-MD HTML-to-text calls
var check = function(mdHTML) {
var elem = $('<div>').html(mdHTML).appendTo('body');
var mdhHtmlToText = new MdhHtmlToText.MdhHtmlToText(elem.get(0), null, true);
var text = mdhHtmlToText.get();
$(elem).remove();
return text;
};
describe('MdhHtmlToText (normal mode)', function() {
it('should be okay with an empty string', function() {
expect(get('')).to.equal('');
});
// Busted due to https://github.com/adam-p/markdown-here/issues/104
it('should NOT correctly handle pre-rendered links in inline code (busted due to issue #104)', function() {
var html = 'aaa `<a href="bbb">ccc</a>`';
// Real target
// var target = 'aaa `bbb`';
var target = 'aaa `[ccc](bbb)`';
expect(get(html)).to.equal(target);
});
// Busted due to https://github.com/adam-p/markdown-here/issues/104
it('should NOT correctly handle pre-rendered links in code blocks (busted due to issue #104)', function() {
var html = '```<br><a href="aaa">bbb</a><br>```';
// Real target
// var target = '```\nbbb\n```';
var target = '```\n[bbb](aaa)\n```';
expect(get(html)).to.equal(target);
});
// Test fix for bug https://github.com/adam-p/markdown-here/pull/233
// reflinks and nolinks weren't working with pre-rendered links.
it('should work correctly with pre-rendered links in reflinks and nolinks', function() {
//
// reflinks
//
var html = '[link text][1]<br>[1]: <a href="http://example.com">http://example.com</a><br>';
var target = '[link text][1]\n[1]: http://example.com';
expect(get(html)).to.equal(target);
// HTTPS and a target attribute
html = '[link text][1]<br>[1]: <a href="https://example.com" target="_blank">https://example.com</a><br>';
target = '[link text][1]\n[1]: https://example.com';
expect(get(html)).to.equal(target);
// Different URL for HREF and text of link (should use text of link)
html = '[link text][1]<br>[1]: <a href="http://hreflink.com">https://textlink.com</a><br>';
target = '[link text][1]\n[1]: https://textlink.com';
expect(get(html)).to.equal(target);
// Only the hostname part of the URL is a link
html = '[link text][1]<br>[1]: http://<a href="http://example.com">example.com</a><br>';
target = '[link text][1]\n[1]: http://example.com';
expect(get(html)).to.equal(target);
//
// nolinks
//
html = '[link text]<br>[link text]: <a href="http://example.com">http://example.com</a><br>';
target = '[link text]\n[link text]: http://example.com';
expect(get(html)).to.equal(target);
// HTTPS and a target attribute
html = '[link text]<br>[link text]: <a href="https://example.com" target="_blank">https://example.com</a><br>';
target = '[link text]\n[link text]: https://example.com';
expect(get(html)).to.equal(target);
// Different URL for HREF and text of link (should use text of link)
html = '[link text]<br>[link text]: <a href="http://hreflink.com">https://textlink.com</a><br>';
target = '[link text]\n[link text]: https://textlink.com';
expect(get(html)).to.equal(target);
// Only the hostname part of the URL is a link
html = '[link text]<br>[link text]: http://<a href="http://example.com">example.com</a><br>';
target = '[link text]\n[link text]: http://example.com';
expect(get(html)).to.equal(target);
});
// Test fix for bug https://github.com/adam-p/markdown-here/issues/251
// <br> at the end of <div> should not add a newline
it('should not add an extra newline for br at end of div', function() {
// HTML from issue
var html = '<div><div>mardown | test<br>-- |---<br></div>1 |\ntest<br></div>2 | test2<br clear="all">';
var target = 'mardown | test\n-- |---\n1 | test\n2 | test2';
expect(get(html)).to.equal(target);
});
// Test some cases with bare text nodes
it('should properly handle bare text nodes', function() {
var html = '';
var target = '';
expect(get(html)).to.equal(target);
html = 'asdf';
target = 'asdf';
expect(get(html)).to.equal(target);
html = 'asdf<div class="x">qwer</div>';
target = 'asdf\nqwer';
expect(get(html)).to.equal(target);
html = 'asdf<br class="x">qwer';
target = 'asdf\nqwer';
expect(get(html)).to.equal(target);
html = 'asdf<br class="x">qwer<div>zxcv</div>asdf';
target = 'asdf\nqwer\nzxcv\nasdf';
expect(get(html)).to.equal(target);
html = 'asdf<br class="x">qwer<div>zxcv</div>ghjk<div>yuio</div>asdf';
target = 'asdf\nqwer\nzxcv\nghjk\nyuio\nasdf';
expect(get(html)).to.equal(target);
html = 'asdf<br class="x">qwer<div><div>zxcv</div>ghjk<div>yuio</div></div>asdf';
target = 'asdf\nqwer\nzxcv\nghjk\nyuio\nasdf';
expect(get(html)).to.equal(target);
html = 'asdf\n<br class="x">qwer<div><div>zxcv</div>ghjk<div>yuio</div></div>asdf';
target = 'asdf \nqwer\nzxcv\nghjk\nyuio\nasdf';
expect(get(html)).to.equal(target);
});
});
describe('MdhHtmlToText (check-for-MD mode)', function() {
it('should be okay with an empty string', function() {
expect(check('')).to.equal('');
});
it('should not choke on links', function() {
// Links get de-rendered to MD normally, but not when checking
var html = '<a href="http://markdown-here.com">MDH</a>';
var target = 'MDH';
expect(check(html)).to.equal(target);
});
// Check for fix to https://github.com/adam-p/markdown-here/issues/128
it('should exclude the content of <code> elements', function() {
// Because otherwise unrendered MD in code would get falsely detected.
var html = 'Foo<code style="blah">inline code</code>Bar<pre style="blah"><code style="blah">code block</code></pre>Okay';
var target = 'FooBar\n\nOkay';
expect(check(html)).to.equal(target);
});
});
});