Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i/7983: Prevent showing an error on pressing "enter" when autolink is selected. #8044

Merged
merged 5 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>'
);
} );
Comment on lines +133 to +141
Copy link
Member

@oleq oleq Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is OK? I mean:

  • Why should the www prefix change the auto-link behavior? It does now. For instance, starting with <paragraph>https://www.ckso[urce.com]</paragraph> vs. <paragraph>https://ckso[urce.com]</paragraph> ends with a link (former) and no link (latter).
  • <paragraph>https://www.ckso[urce.com]</paragraph>  also ends up as a broken link: linkHref="https://www.ckso". This is not a valid link (it's just a fragment of the original link) and from the user point of view, this is a bug.
  • It looks like something is wrong with URL_REG_EXP. I don't think we should ever auto-link if a link is partially selected in the first place.

Just to be clear, it's beyond the scope of this PR. But if you agree with me, can you please create a follow-up issue @panr? The test can stay, it's harmless. Maybe a comment that this is something weird would do the trick for now.

Copy link
Member

@oleq oleq Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably because the URL_REG_EXP considers ckso a domain:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@panr panr Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleq I agree that this incorrect behavior. I was surprised as well at first that this didn't pass the first version of the test, but I thought that this is how should it be... 😬

I'll create a followup to this issue 👍


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