Skip to content

Commit

Permalink
Merge pull request #17680 from ckeditor/ck/14497
Browse files Browse the repository at this point in the history
Fix (link): Trailing punctuation is no longer included in auto-linked URL. Closes #14497
  • Loading branch information
Mati365 authored Dec 20, 2024
2 parents 2a114f4 + d23adc5 commit 3ba31a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/ckeditor5-link/src/autolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,40 @@ export default class AutoLink extends Plugin {
const editor = this.editor;

const watcher = new TextWatcher( editor.model, text => {
let mappedText = text;

// 1. Detect <kbd>Space</kbd> after a text with a potential link.
if ( !isSingleSpaceAtTheEnd( text ) ) {
if ( !isSingleSpaceAtTheEnd( mappedText ) ) {
return;
}

// 2. Check text before last typed <kbd>Space</kbd>.
const url = getUrlAtTextEnd( text.substr( 0, text.length - 1 ) );
// 2. Remove the last space character.
mappedText = mappedText.slice( 0, -1 );

// 3. Remove punctuation at the end of the URL if it exists.
if ( '!.:,;?'.includes( mappedText[ mappedText.length - 1 ] ) ) {
mappedText = mappedText.slice( 0, -1 );
}

// 4. Check text before last typed <kbd>Space</kbd> or punctuation.
const url = getUrlAtTextEnd( mappedText );

if ( url ) {
return { url };
return {
url,
removedTrailingCharacters: text.length - mappedText.length
};
}
} );

watcher.on<TextWatcherMatchedDataEvent<{ url: string }>>( 'matched:data', ( evt, data ) => {
const { batch, range, url } = data;
watcher.on<TextWatcherMatchedDataEvent<{ url: string; removedTrailingCharacters: number }>>( 'matched:data', ( evt, data ) => {
const { batch, range, url, removedTrailingCharacters } = data;

if ( !batch.isTyping ) {
return;
}

const linkEnd = range.end.getShiftedBy( -1 ); // Executed after a space character.
const linkEnd = range.end.getShiftedBy( -removedTrailingCharacters ); // Executed after a space character or punctuation.
const linkStart = linkEnd.getShiftedBy( -url.length );

const linkRange = editor.model.createRange( linkStart, linkEnd );
Expand Down
10 changes: 10 additions & 0 deletions packages/ckeditor5-link/tests/autolink.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,16 @@ describe( 'AutoLink', () => {
sinon.assert.notCalled( spy );
} );

for ( const punctuation of '!.:,;?' ) {
it( `does not include "${ punctuation }" at the end of the link after space`, () => {
simulateTyping( `https://www.cksource.com${ punctuation } ` );

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

// Some examples came from https://mathiasbynens.be/demo/url-regex.
describe( 'supported URL', () => {
const supportedURLs = [
Expand Down

0 comments on commit 3ba31a7

Please sign in to comment.