From 133c508d0f36ee9eb985772da643b258639e1952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kup=C5=9B?= Date: Fri, 2 Feb 2018 12:52:04 +0100 Subject: [PATCH] Fixed manual test for #475. --- tests/manual/tickets/475/1.js | 57 +++++++++++++++-------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/tests/manual/tickets/475/1.js b/tests/manual/tickets/475/1.js index 79bcff479..a65394e0f 100644 --- a/tests/manual/tickets/475/1.js +++ b/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' ]