@@ -223,26 +223,39 @@ export default class AutoLink extends Plugin {
223223 }
224224
225225 /**
226- * Applies a link on a given range.
226+ * Applies a link on a given range if the link should be applied .
227227 *
228228 * @param {String } url The URL to link.
229229 * @param {module:engine/model/range~Range } range The text range to apply the link attribute to.
230230 * @private
231231 */
232- _applyAutoLink ( link , range ) {
232+ _applyAutoLink ( url , range ) {
233233 const model = this . editor . model ;
234- const deletePlugin = this . editor . plugins . get ( 'Delete' ) ;
235234
236235 const defaultProtocol = this . editor . config . get ( 'link.defaultProtocol' ) ;
237- const parsedUrl = addLinkProtocolIfApplicable ( link , defaultProtocol ) ;
236+ const fullUrl = addLinkProtocolIfApplicable ( url , defaultProtocol ) ;
238237
239- if ( ! this . isEnabled || ! isLinkAllowedOnRange ( range , model ) || ! linkHasProtocol ( parsedUrl ) ) {
238+ if ( ! this . isEnabled || ! isLinkAllowedOnRange ( range , model ) || ! linkHasProtocol ( fullUrl ) || linkIsAlreadySet ( range ) ) {
240239 return ;
241240 }
242241
242+ this . _persistAutoLink ( fullUrl , range ) ;
243+ }
244+
245+ /**
246+ * Enqueues autolink changes in the model.
247+ *
248+ * @param {String } url The URL to link.
249+ * @param {module:engine/model/range~Range } range The text range to apply the link attribute to.
250+ * @protected
251+ */
252+ _persistAutoLink ( url , range ) {
253+ const model = this . editor . model ;
254+ const deletePlugin = this . editor . plugins . get ( 'Delete' ) ;
255+
243256 // Enqueue change to make undo step.
244257 model . enqueueChange ( writer => {
245- writer . setAttribute ( 'linkHref' , parsedUrl , range ) ;
258+ writer . setAttribute ( 'linkHref' , url , range ) ;
246259
247260 model . enqueueChange ( ( ) => {
248261 deletePlugin . requestUndoOnBackspace ( ) ;
@@ -265,3 +278,8 @@ function getUrlAtTextEnd( text ) {
265278function isLinkAllowedOnRange ( range , model ) {
266279 return model . schema . checkAttributeInSelection ( model . createSelection ( range ) , 'linkHref' ) ;
267280}
281+
282+ function linkIsAlreadySet ( range ) {
283+ const item = range . start . nodeAfter ;
284+ return item && item . hasAttribute ( 'linkHref' ) ;
285+ }
0 commit comments