Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit c2d4cec

Browse files
authored
Merge pull request #1273 from ckeditor/t/1271
Other: Manual test for #475 now works correctly. Closes #1271.
2 parents b017890 + 133c508 commit c2d4cec

File tree

1 file changed

+24
-33
lines changed
  • tests/manual/tickets/475

1 file changed

+24
-33
lines changed

tests/manual/tickets/475/1.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
99

1010
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
11-
12-
import TreeWalker from '../../../../src/model/treewalker';
13-
import Position from '../../../../src/model/position';
1411
import Range from '../../../../src/model/range';
1512
import LivePosition from '../../../../src/model/liveposition';
1613

@@ -45,51 +42,45 @@ class Link extends Plugin {
4542
}
4643
}
4744

48-
const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/;
49-
5045
class AutoLinker extends Plugin {
5146
init() {
52-
this.editor.model.document.on( 'change', ( event, type, changes, batch ) => {
53-
if ( type != 'insert' ) {
54-
return;
55-
}
47+
this.editor.model.document.on( 'change', () => {
48+
const changes = this.editor.model.document.differ.getChanges();
5649

57-
for ( const value of changes.range.getItems( { singleCharacters: true } ) ) {
58-
const walker = new TreeWalker( {
59-
direction: 'backward',
60-
startPosition: Position.createAfter( value )
61-
} );
50+
for ( const entry of changes ) {
51+
if ( entry.type != 'insert' || entry.name != '$text' || !entry.position.textNode ) {
52+
continue;
53+
}
6254

63-
const currentValue = walker.next().value;
64-
const text = currentValue.item.data;
55+
const textNode = entry.position.textNode;
56+
const text = textNode.data;
6557

6658
if ( !text ) {
6759
return;
6860
}
6961

70-
const matchedUrl = urlRegex.exec( text );
71-
72-
if ( !matchedUrl ) {
73-
return;
62+
const regexp = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
63+
let match;
64+
65+
while ( ( match = regexp.exec( text ) ) !== null ) {
66+
const index = match.index;
67+
const url = match[ 0 ];
68+
const length = url.length;
69+
70+
if ( entry.position.offset + entry.length == index + length ) {
71+
const livePos = LivePosition.createFromParentAndOffset( textNode.parent, index );
72+
this.editor.model.enqueueChange( writer => {
73+
const urlRange = Range.createFromPositionAndShift( livePos, length );
74+
writer.setAttribute( 'link', url, urlRange );
75+
} );
76+
return;
77+
}
7478
}
75-
76-
const url = matchedUrl[ 0 ];
77-
const offset = _getLastPathPart( currentValue.nextPosition.path ) + matchedUrl.index;
78-
const livePos = LivePosition.createFromParentAndOffset( currentValue.item.parent, offset );
79-
80-
this.editor.model.enqueueChange( batch, writer => {
81-
const urlRange = Range.createFromPositionAndShift( livePos, url.length );
82-
writer.setAttribute( 'link', url, urlRange );
83-
} );
8479
}
8580
} );
8681
}
8782
}
8883

89-
function _getLastPathPart( path ) {
90-
return path[ path.length - 1 ];
91-
}
92-
9384
ClassicEditor.create( document.querySelector( '#editor' ), {
9485
plugins: [ Enter, Typing, Paragraph, Undo, Link, AutoLinker ],
9586
toolbar: [ 'undo', 'redo' ]

0 commit comments

Comments
 (0)