Skip to content

Commit

Permalink
Merge pull request #8044 from ckeditor/i/7983
Browse files Browse the repository at this point in the history
Fix (link): Pressing the <kbd>Enter</kbd> key should not throw an error when a non-collapsed selection ends with a valid URL. Closes #7983.
  • Loading branch information
oleq authored Sep 9, 2020
2 parents 030d0c3 + fd638fb commit bcf3af6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/ckeditor5-link/src/autolink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );
Expand Down
50 changes: 50 additions & 0 deletions packages/ckeditor5-link/tests/autolink.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,56 @@ describe( 'AutoLink', () => {
);
} );

it( 'does not add linkHref attribute on enter when the link is selected', () => {
setData( model, '<paragraph>[https://www.cksource.com]</paragraph>' );

editor.execute( 'enter' );

expect( getData( model ) ).to.equal(
'<paragraph>[]</paragraph>'
);
} );

it( 'does not add linkHref attribute on enter when the whole paragraph containing the link is selected', () => {
setData( model, '<paragraph>[This feature also works with e-mail addresses: https://www.cksource.com]</paragraph>' );

editor.execute( 'enter' );

expect( getData( model ) ).to.equal(
'<paragraph>[]</paragraph>'
);
} );

it( 'adds linkHref attribute on enter when the link (that contains www) is partially selected (end)', () => {
setData( model, '<paragraph>https://www.ckso[urce.com]</paragraph>' );

editor.execute( 'enter' );

expect( getData( model ) ).to.equal(
'<paragraph><$text linkHref="https://www.ckso">https://www.ckso</$text></paragraph><paragraph>[]</paragraph>'
);
} );

it( 'does not add linkHref attribute on enter when the link (that does not contain www) is partially selected (end)', () => {
setData( model, '<paragraph>https://ckso[urce.com]</paragraph>' );

editor.execute( 'enter' );

expect( getData( model ) ).to.equal(
'<paragraph>https://ckso</paragraph><paragraph>[]</paragraph>'
);
} );

it( 'does not add linkHref attribute on enter when the link is partially selected (beginning)', () => {
setData( model, '<paragraph>[https://www.ckso]urce.com</paragraph>' );

editor.execute( 'enter' );

expect( getData( model ) ).to.equal(
'<paragraph></paragraph><paragraph>[]urce.com</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );

Expand Down

0 comments on commit bcf3af6

Please sign in to comment.