From b0caf5427178dfe7f005f121792f39f36e4bdbcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kwa=C5=9Bnik?= Date: Mon, 22 Feb 2021 20:34:58 +0100 Subject: [PATCH 1/3] Improved validation of autolink. --- packages/ckeditor5-link/src/autolink.js | 8 +++++--- packages/ckeditor5-link/tests/autolink.js | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/ckeditor5-link/src/autolink.js b/packages/ckeditor5-link/src/autolink.js index 6e7848fd73d..6c8d5a25871 100644 --- a/packages/ckeditor5-link/src/autolink.js +++ b/packages/ckeditor5-link/src/autolink.js @@ -37,6 +37,8 @@ const URL_REG_EXP = new RegExp( '(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))' + '|' + '(' + + // Do not allow `www.foo` - see https://github.com/ckeditor/ckeditor5/issues/8050. + '((?!www\\.)|(www\\.))' + // Host & domain names. '(?![-_])(?:[-\\w\\u00a1-\\uffff]{0,63}[^-_]\\.)+' + // TLD identifier name. @@ -54,9 +56,9 @@ const URL_REG_EXP = new RegExp( '(www.|(\\S+@))' + // Host & domain names. '((?![-_])(?:[-\\w\\u00a1-\\uffff]{0,63}[^-_]\\.))+' + - // TLD identifier name. - '(?:[a-z\\u00a1-\\uffff]{2,})' + - ')' + + // TLD identifier name. + '(?:[a-z\\u00a1-\\uffff]{2,})' + + ')' + ')$', 'i' ); const URL_GROUP_IN_MATCH = 2; diff --git a/packages/ckeditor5-link/tests/autolink.js b/packages/ckeditor5-link/tests/autolink.js index 418741545d0..99dd2e9ea7f 100644 --- a/packages/ckeditor5-link/tests/autolink.js +++ b/packages/ckeditor5-link/tests/autolink.js @@ -130,13 +130,25 @@ describe( 'AutoLink', () => { ); } ); - it( 'adds linkHref attribute on enter when the link (that contains www) is partially selected (end)', () => { + it( 'adds linkHref attribute on enter when the link (that contains www) is partially selected (end)' + + 'and the remaining fragment is a proper URL', () => { + setData( model, 'https://www.foo.ba[r.com]' ); + + editor.execute( 'enter' ); + + expect( getData( model ) ).to.equal( + '<$text linkHref="https://www.foo.ba">https://www.foo.ba[]' + ); + } ); + + it( 'does not add linkHref attribute on enter when the link (that contains www) is partially selected (end)' + + 'and the remaining fragment is not a proper URL', () => { setData( model, 'https://www.ckso[urce.com]' ); editor.execute( 'enter' ); expect( getData( model ) ).to.equal( - '<$text linkHref="https://www.ckso">https://www.ckso[]' + 'https://www.ckso[]' ); } ); From 2f0aef3b275acc01ba6fb19347451ccc25b95e4e Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Tue, 23 Feb 2021 10:59:27 +0100 Subject: [PATCH 2/3] Tests (link): Added more test cases for auto link matching. --- packages/ckeditor5-link/tests/autolink.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/ckeditor5-link/tests/autolink.js b/packages/ckeditor5-link/tests/autolink.js index 99dd2e9ea7f..fd84cacc616 100644 --- a/packages/ckeditor5-link/tests/autolink.js +++ b/packages/ckeditor5-link/tests/autolink.js @@ -277,6 +277,8 @@ describe( 'AutoLink', () => { 'http://🥳.cksource.com/', 'http://code.cksource.com/woot/#&product=browser', 'http://j.mp', + 'http://ww.mp', + 'http://wwww.mp', 'ftp://cksource.com/baz', 'http://cksource.com/?q=Test%20URL-encoded%20stuff', 'http://مثال.إختبار', @@ -326,6 +328,7 @@ describe( 'AutoLink', () => { 'http://-error-.invalid/', 'http://localhost', 'http:/cksource.com', + 'http://www.cksource', // https://github.com/ckeditor/ckeditor5/issues/8050. 'cksource.com', 'ww.cksource.com', 'www.cksource' From 44cbbb2bab11dc509fbeac7778e64e88ca16cb9a Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Tue, 23 Feb 2021 11:10:08 +0100 Subject: [PATCH 3/3] Tests (link): Adjusted unit test name and added a reference. --- packages/ckeditor5-link/tests/autolink.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/ckeditor5-link/tests/autolink.js b/packages/ckeditor5-link/tests/autolink.js index fd84cacc616..6ebe9a1726c 100644 --- a/packages/ckeditor5-link/tests/autolink.js +++ b/packages/ckeditor5-link/tests/autolink.js @@ -130,7 +130,7 @@ describe( 'AutoLink', () => { ); } ); - it( 'adds linkHref attribute on enter when the link (that contains www) is partially selected (end)' + + it( 'adds linkHref attribute on enter when the link (containing www) is partially selected (end)' + 'and the remaining fragment is a proper URL', () => { setData( model, 'https://www.foo.ba[r.com]' ); @@ -141,8 +141,8 @@ describe( 'AutoLink', () => { ); } ); - it( 'does not add linkHref attribute on enter when the link (that contains www) is partially selected (end)' + - 'and the remaining fragment is not a proper URL', () => { + it( 'does not add a linkHref attribute for links with www subdomain only, pressing enter with part of its end selected', () => { + // https://github.com/ckeditor/ckeditor5/issues/8050. setData( model, 'https://www.ckso[urce.com]' ); editor.execute( 'enter' );