From c249e3bc1978cb89996b495fed71b4227fe54fa7 Mon Sep 17 00:00:00 2001 From: Umer Farooq Date: Tue, 14 Apr 2015 21:16:13 -0400 Subject: [PATCH] Document that if the current selection is not collapsed, that it will be deleted and replaced by the html. Make style and consistency changes. --- README.md | 2 +- source/Editor.js | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 33ddfcf..f60baa6 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ Returns a reference to the newly inserted image element. ### insertHTML -Inserts an HTML fragment at the current cursor location. The value supplied should not contain `` tags or anything outside of that. +Inserts an HTML fragment at the current cursor location, or replaces the selection if selected. The value supplied should not contain `` tags or anything outside of that. The method takes one argument: diff --git a/source/Editor.js b/source/Editor.js index c2c82af..e3ba550 100644 --- a/source/Editor.js +++ b/source/Editor.js @@ -2263,9 +2263,11 @@ proto.insertImage = function ( src ) { return img; }; +// Insert HTML at the cursor location. If the selection is not collapsed +// insertTreeFragmentIntoRange will delete the selection so that it is replaced +// by the html being inserted. proto.insertHTML = function ( html ) { - var self = this, - range = this.getSelection(), + var range = this.getSelection(), frag = this._doc.createDocumentFragment(), div = this.createElement( 'DIV' ); @@ -2274,9 +2276,8 @@ proto.insertHTML = function ( html ) { frag.appendChild( empty( div ) ); // Record undo checkpoint - self._recordUndoState( range ); - self._getRangeAndRemoveBookmark( range ); - + this._recordUndoState( range ); + this._getRangeAndRemoveBookmark( range ); try { frag.normalize(); @@ -2293,17 +2294,17 @@ proto.insertHTML = function ( html ) { insertTreeFragmentIntoRange( range, frag ); if ( !canObserveMutations ) { - self._docWasChanged(); + this._docWasChanged(); } range.collapse( false ); - self._ensureBottomLine(); - - self.setSelection( range ); - self._updatePath( range, true ); + this._ensureBottomLine(); + this.setSelection( range ); + this._updatePath( range, true ); } catch ( error ) { - self.didError( error ); + this.didError( error ); } + return this; }; // --- Formatting ---