Browse Source

Add tests for normalization functions

pull/82/head
Alex Kocharin 10 years ago
parent
commit
7037ee34ad
  1. 2
      lib/index.js
  2. 7
      test/fixtures/markdown-it/commonmark_extras.txt
  3. 66
      test/fixtures/markdown-it/normalize.txt
  4. 26
      test/misc.js

2
lib/index.js

@ -41,7 +41,7 @@ function normalizeLink(url) {
if (parsed.hostname) { if (parsed.hostname) {
try { try {
parsed.hostname = punycode.toAscii(parsed.hostname); parsed.hostname = punycode.toASCII(parsed.hostname);
} catch(er) {} } catch(er) {}
} }

7
test/fixtures/markdown-it/commonmark_extras.txt

@ -36,13 +36,6 @@ Not a closing tag
. .
Normalize link destination, but not text inside it:
.
<http://example.com/α%CE%B2γ%CE%B4>
.
<p><a href="http://example.com/%CE%B1%CE%B2%CE%B3%CE%B4">http://example.com/αβγδ</a></p>
.
Escaping entities in links: Escaping entities in links:
. .

66
test/fixtures/markdown-it/normalize.txt

@ -0,0 +1,66 @@
Encode link destination, decode text inside it:
.
<http://example.com/α%CE%B2γ%CE%B4>
.
<p><a href="http://example.com/%CE%B1%CE%B2%CE%B3%CE%B4">http://example.com/αβγδ</a></p>
.
.
[foo](http://example.com/α%CE%B2γ%CE%B4)
.
<p><a href="http://example.com/%CE%B1%CE%B2%CE%B3%CE%B4">foo</a></p>
.
Should decode punycode:
.
<http://xn--n3h.net/>
.
<p><a href="http://xn--n3h.net/">http://☃.net/</a></p>
.
.
<http://☃.net/>
.
<p><a href="http://xn--n3h.net/">http://☃.net/</a></p>
.
Invalid punycode:
.
<http://xn--xn.com/>
.
<p><a href="http://xn--xn.com/">http://xn–xn.com/</a></p>
.
Invalid punycode (non-ascii):
.
<http://xn--γ.com/>
.
<p><a href="http://xn--xn---emd.com/">http://xn–γ.com/</a></p>
.
Should auto-add protocol to autolinks:
.
test google.com foo
.
<p>test <a href="http://google.com">google.com</a> foo</p>
.
Should support IDN in autolinks:
.
test http://xn--n3h.net/ foo
.
<p>test <a href="http://xn--n3h.net/">http://☃.net/</a> foo</p>
.
.
test http://☃.net/ foo
.
<p>test <a href="http://xn--n3h.net/">http://☃.net/</a> foo</p>
.

26
test/misc.js

@ -216,6 +216,31 @@ describe('Misc', function () {
}); });
describe('Url normalization', function () {
it('Should be overridable', function () {
var md = markdownit({ linkify: true });
md.normalizeLink = function (url) {
assert(url.match(/example\.com/), 'wrong url passed');
return 'LINK';
};
md.normalizeLinkText = function (url) {
assert(url.match(/example\.com/), 'wrong url passed');
return 'TEXT';
};
assert.strictEqual(md.render('foo@example.com'), '<p><a href="LINK">TEXT</a></p>\n');
assert.strictEqual(md.render('http://example.com'), '<p><a href="LINK">TEXT</a></p>\n');
assert.strictEqual(md.render('<foo@example.com>'), '<p><a href="LINK">TEXT</a></p>\n');
assert.strictEqual(md.render('<http://example.com>'), '<p><a href="LINK">TEXT</a></p>\n');
assert.strictEqual(md.render('[test](http://example.com)'), '<p><a href="LINK">test</a></p>\n');
assert.strictEqual(md.render('![test](http://example.com)'), '<p><img src="LINK" alt="test"></p>\n');
});
});
describe('Links validation', function () { describe('Links validation', function () {
it('Override validator, disable everything', function () { it('Override validator, disable everything', function () {
@ -228,6 +253,7 @@ describe('Links validation', function () {
assert.strictEqual(md.render('<foo@example.com>'), '<p>&lt;foo@example.com&gt;</p>\n'); assert.strictEqual(md.render('<foo@example.com>'), '<p>&lt;foo@example.com&gt;</p>\n');
assert.strictEqual(md.render('<http://example.com>'), '<p>&lt;http://example.com&gt;</p>\n'); assert.strictEqual(md.render('<http://example.com>'), '<p>&lt;http://example.com&gt;</p>\n');
assert.strictEqual(md.render('[test](http://example.com)'), '<p>[test](http://example.com)</p>\n'); assert.strictEqual(md.render('[test](http://example.com)'), '<p>[test](http://example.com)</p>\n');
assert.strictEqual(md.render('![test](http://example.com)'), '<p>![test](http://example.com)</p>\n');
}); });
}); });

Loading…
Cancel
Save