diff --git a/packages/ckeditor5-link/src/autolink.js b/packages/ckeditor5-link/src/autolink.js index 9120246ade9..2ed42deed52 100644 --- a/packages/ckeditor5-link/src/autolink.js +++ b/packages/ckeditor5-link/src/autolink.js @@ -147,10 +147,11 @@ export default class AutoLink extends Plugin { enterCommand.on( 'execute', () => { const position = model.document.selection.getFirstPosition(); - const rangeToCheck = model.createRange( - model.createPositionAt( position.parent.previousSibling, 0 ), - model.createPositionAt( position.parent.previousSibling, 'end' ) - ); + if ( !position.parent.previousSibling ) { + return; + } + + const rangeToCheck = model.createRangeIn( position.parent.previousSibling ); this._checkAndApplyAutoLinkOnRange( rangeToCheck ); } ); diff --git a/packages/ckeditor5-link/tests/autolink.js b/packages/ckeditor5-link/tests/autolink.js index 59f04504352..b64f40db502 100644 --- a/packages/ckeditor5-link/tests/autolink.js +++ b/packages/ckeditor5-link/tests/autolink.js @@ -110,6 +110,56 @@ describe( 'AutoLink', () => { ); } ); + it( 'does not add linkHref attribute on enter when the link is selected', () => { + setData( model, '[https://www.cksource.com]' ); + + editor.execute( 'enter' ); + + expect( getData( model ) ).to.equal( + '[]' + ); + } ); + + it( 'does not add linkHref attribute on enter when the whole paragraph containing the link is selected', () => { + setData( model, '[This feature also works with e-mail addresses: https://www.cksource.com]' ); + + editor.execute( 'enter' ); + + expect( getData( model ) ).to.equal( + '[]' + ); + } ); + + it( 'adds linkHref attribute on enter when the link (that contains www) is partially selected (end)', () => { + setData( model, 'https://www.ckso[urce.com]' ); + + editor.execute( 'enter' ); + + expect( getData( model ) ).to.equal( + '<$text linkHref="https://www.ckso">https://www.ckso[]' + ); + } ); + + it( 'does not add linkHref attribute on enter when the link (that does not contain www) is partially selected (end)', () => { + setData( model, 'https://ckso[urce.com]' ); + + editor.execute( 'enter' ); + + expect( getData( model ) ).to.equal( + 'https://ckso[]' + ); + } ); + + it( 'does not add linkHref attribute on enter when the link is partially selected (beginning)', () => { + setData( model, '[https://www.ckso]urce.com' ); + + editor.execute( 'enter' ); + + expect( getData( model ) ).to.equal( + '[]urce.com' + ); + } ); + it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => { setData( model, 'Foo Bar [] Baz' );