Sfoglia il codice sorgente

Merge pull request #1273 from ckeditor/t/1271

Other: Manual test for #475 now works correctly. Closes #1271.
Oskar Wróbel 7 anni fa
parent
commit
7e8c1b2096
1 ha cambiato i file con 24 aggiunte e 33 eliminazioni
  1. 24 33
      packages/ckeditor5-engine/tests/manual/tickets/475/1.js

+ 24 - 33
packages/ckeditor5-engine/tests/manual/tickets/475/1.js

@@ -8,9 +8,6 @@
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
-
-import TreeWalker from '../../../../src/model/treewalker';
-import Position from '../../../../src/model/position';
 import Range from '../../../../src/model/range';
 import LivePosition from '../../../../src/model/liveposition';
 
@@ -45,51 +42,45 @@ class Link extends Plugin {
 	}
 }
 
-const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/;
-
 class AutoLinker extends Plugin {
 	init() {
-		this.editor.model.document.on( 'change', ( event, type, changes, batch ) => {
-			if ( type != 'insert' ) {
-				return;
-			}
+		this.editor.model.document.on( 'change', () => {
+			const changes = this.editor.model.document.differ.getChanges();
 
-			for ( const value of changes.range.getItems( { singleCharacters: true } ) ) {
-				const walker = new TreeWalker( {
-					direction: 'backward',
-					startPosition: Position.createAfter( value )
-				} );
+			for ( const entry of changes ) {
+				if ( entry.type != 'insert' || entry.name != '$text' || !entry.position.textNode ) {
+					continue;
+				}
 
-				const currentValue = walker.next().value;
-				const text = currentValue.item.data;
+				const textNode = entry.position.textNode;
+				const text = textNode.data;
 
 				if ( !text ) {
 					return;
 				}
 
-				const matchedUrl = urlRegex.exec( text );
-
-				if ( !matchedUrl ) {
-					return;
+				const regexp = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
+				let match;
+
+				while ( ( match = regexp.exec( text ) ) !== null ) {
+					const index = match.index;
+					const url = match[ 0 ];
+					const length = url.length;
+
+					if ( entry.position.offset + entry.length == index + length ) {
+						const livePos = LivePosition.createFromParentAndOffset( textNode.parent, index );
+						this.editor.model.enqueueChange( writer => {
+							const urlRange = Range.createFromPositionAndShift( livePos, length );
+							writer.setAttribute( 'link', url, urlRange );
+						} );
+						return;
+					}
 				}
-
-				const url = matchedUrl[ 0 ];
-				const offset = _getLastPathPart( currentValue.nextPosition.path ) + matchedUrl.index;
-				const livePos = LivePosition.createFromParentAndOffset( currentValue.item.parent, offset );
-
-				this.editor.model.enqueueChange( batch, writer => {
-					const urlRange = Range.createFromPositionAndShift( livePos, url.length );
-					writer.setAttribute( 'link', url, urlRange );
-				} );
 			}
 		} );
 	}
 }
 
-function _getLastPathPart( path ) {
-	return path[ path.length - 1 ];
-}
-
 ClassicEditor.create( document.querySelector( '#editor' ), {
 	plugins: [ Enter, Typing, Paragraph, Undo, Link, AutoLinker ],
 	toolbar: [ 'undo', 'redo' ]