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

Type link: Fixed ending punctuation to recognise links. Closes #14497. #16264

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/ckeditor5-link/src/autolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class AutoLink extends Plugin {

const watcher = new TextWatcher( editor.model, text => {
// 1. Detect <kbd>Space</kbd> after a text with a potential link.
if ( !isSingleSpaceAtTheEnd( text ) ) {
if ( !isSingleSpaceOrPunctuationAtTheEnd( text ) ) {
return;
}

Expand Down Expand Up @@ -335,8 +335,15 @@ export default class AutoLink extends Plugin {
}

// Check if text should be evaluated by the plugin in order to reduce number of RegExp checks on whole text.
function isSingleSpaceAtTheEnd( text: string ): boolean {
return text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' && text[ text.length - 2 ] !== ' ';
function isSingleSpaceOrPunctuationAtTheEnd( text: string ): boolean {
return ( text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ' ' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '. ' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '! ' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ': ' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === ', ' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '; ' ||
text.length > MIN_LINK_LENGTH_WITH_SPACE_AT_END && text[ text.length - 1 ] === '? '
) && text[ text.length - 2 ] !== ' ';
}

function getUrlAtTextEnd( text: string ): string | null {
Expand Down
50 changes: 49 additions & 1 deletion packages/ckeditor5-link/tests/autolink.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,62 @@ describe( 'AutoLink', () => {
);
} );

it( 'adds linkHref attribute to a text link after space', () => {
it( 'adds linkHref attribute to a text link after space( )', () => {
simulateTyping( 'https://www.cksource.com ' );

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

it( 'adds linkHref attribute to a text link after period(.)', () => {
simulateTyping( 'https://www.cksource.com.' );

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

it( 'adds linkHref attribute to a text link after exclamation mark(!)', () => {
simulateTyping( 'https://www.cksource.com!' );

expect( getData( model ) ).to.equal(
'<paragraph>https://www.cksource.com![]</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after colon(:)', () => {
simulateTyping( 'https://www.cksource.com:' );

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

it( 'adds linkHref attribute to a text link after comma(,)', () => {
simulateTyping( 'https://www.cksource.com,' );

expect( getData( model ) ).to.equal(
'<paragraph>https://www.cksource.com,[]</paragraph>'
);
} );

it( 'adds linkHref attribute to a text link after semi-colon(;)', () => {
simulateTyping( 'https://www.cksource.com;' );

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

it( 'adds linkHref attribute to a text link after question mark(?)', () => {
simulateTyping( 'https://www.cksource.com?' );

expect( getData( model ) ).to.equal(
'<paragraph>https://www.cksource.com?[]</paragraph>'
);
} );

it( 'does not add linkHref attribute if linkHref is not allowed', () => {
model.schema.addAttributeCheck( () => false ); // Disable all attributes.

Expand Down
17 changes: 17 additions & 0 deletions packages/ckeditor5-link/tests/manual/autolink.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
2. Type space after a URL.
3. Check if text typed before space get converted to link.

#### _Special characters_
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a blank line above the heading for better markdown formatting.

+ 
#### _Special characters_

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
#### _Special characters_
#### _Special characters_

The process above can be completed with a set of certain special characters in place of a space:
1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a space after the comma for better readability.

- 4. A comma (`,`)
+ 4. A comma (`, `)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
4. A comma (`,`)
4. A comma (`, `)

5. A Semicolon (`;`)
6. A Question mark (`?`)

Comment on lines +15 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

Surround the list with blank lines for proper markdown formatting.

+ 
1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
5. A Semicolon (`;`)
6. A Question mark (`?`)
+ 

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
5. A Semicolon (`;`)
6. A Question mark (`?`)
1. A Period (`.`)
2. An exclamation mark (`!`)
3. A Colon (`:`)
4. A comma (`,`)
5. A Semicolon (`;`)
6. A Question mark (`?`)

### After a soft break/new paragraph

1. Type a URL as in base scenario.
Expand All @@ -30,3 +39,11 @@
2. Select some content
3. Paste
4. Check the selected content is now a link using the copied URL.

### Ending Punctuation

1. Copy a URL to the clipboard
2. Select some content
3. Paste
4. Check the selected content is now a link using the copied URL.