From 7037ee34ad3a18828d99a5543b7c7dc6f6c56ab5 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Tue, 10 Mar 2015 16:37:28 +0300 Subject: [PATCH] Add tests for normalization functions --- lib/index.js | 2 +- .../markdown-it/commonmark_extras.txt | 7 -- test/fixtures/markdown-it/normalize.txt | 66 +++++++++++++++++++ test/misc.js | 26 ++++++++ 4 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/markdown-it/normalize.txt diff --git a/lib/index.js b/lib/index.js index 13a284a..98c69e2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -41,7 +41,7 @@ function normalizeLink(url) { if (parsed.hostname) { try { - parsed.hostname = punycode.toAscii(parsed.hostname); + parsed.hostname = punycode.toASCII(parsed.hostname); } catch(er) {} } diff --git a/test/fixtures/markdown-it/commonmark_extras.txt b/test/fixtures/markdown-it/commonmark_extras.txt index dfb10dc..f00e96f 100644 --- a/test/fixtures/markdown-it/commonmark_extras.txt +++ b/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/αβγδ

-. - Escaping entities in links: . diff --git a/test/fixtures/markdown-it/normalize.txt b/test/fixtures/markdown-it/normalize.txt new file mode 100644 index 0000000..71e1ad8 --- /dev/null +++ b/test/fixtures/markdown-it/normalize.txt @@ -0,0 +1,66 @@ + +Encode link destination, decode text inside it: + +. + +. +

http://example.com/αβγδ

+. + +. +[foo](http://example.com/α%CE%B2γ%CE%B4) +. +

foo

+. + +Should decode punycode: + +. + +. +

http://☃.net/

+. + +. + +. +

http://☃.net/

+. + +Invalid punycode: + +. + +. +

http://xn–xn.com/

+. + +Invalid punycode (non-ascii): + +. + +. +

http://xn–γ.com/

+. + +Should auto-add protocol to autolinks: + +. +test google.com foo +. +

test google.com foo

+. + +Should support IDN in autolinks: + +. +test http://xn--n3h.net/ foo +. +

test http://☃.net/ foo

+. + +. +test http://☃.net/ foo +. +

test http://☃.net/ foo

+. diff --git a/test/misc.js b/test/misc.js index 7a4d29a..6785784 100644 --- a/test/misc.js +++ b/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'), '

TEXT

\n'); + assert.strictEqual(md.render('http://example.com'), '

TEXT

\n'); + assert.strictEqual(md.render(''), '

TEXT

\n'); + assert.strictEqual(md.render(''), '

TEXT

\n'); + assert.strictEqual(md.render('[test](http://example.com)'), '

test

\n'); + assert.strictEqual(md.render('![test](http://example.com)'), '

test

\n'); + }); + +}); + + describe('Links validation', function () { it('Override validator, disable everything', function () { @@ -228,6 +253,7 @@ describe('Links validation', function () { assert.strictEqual(md.render(''), '

<foo@example.com>

\n'); assert.strictEqual(md.render(''), '

<http://example.com>

\n'); assert.strictEqual(md.render('[test](http://example.com)'), '

[test](http://example.com)

\n'); + assert.strictEqual(md.render('![test](http://example.com)'), '

![test](http://example.com)

\n'); }); });