@@ -1905,7 +1905,7 @@ var Selection;
19051905 }
19061906 }
19071907
1908- if ( selectionState . emptyBlocksIndex && selectionState . end === nextCharIndex ) {
1908+ if ( selectionState . emptyBlocksIndex ) {
19091909 var targetNode = Util . getTopBlockContainer ( range . startContainer ) ,
19101910 index = 0 ;
19111911 // Skip over empty blocks until we hit the block we want the selection to be in
@@ -1960,6 +1960,7 @@ var Selection;
19601960 }
19611961 }
19621962 range . setStart ( currentNode . parentNode , currentNodeIndex + 1 ) ;
1963+ range . collapse ( true ) ;
19631964 }
19641965 }
19651966 return range ;
@@ -6128,8 +6129,7 @@ function MediumEditor(elements, options) {
61286129 } ,
61296130
61306131 createLink : function ( opts ) {
6131- var customEvent ,
6132- i ;
6132+ var customEvent , i ;
61336133
61346134 if ( opts . url && opts . url . trim ( ) . length > 0 ) {
61356135 var currentSelection = this . options . contentWindow . getSelection ( ) ;
@@ -6139,17 +6139,22 @@ function MediumEditor(elements, options) {
61396139 endContainerParentElement ,
61406140 textNodes ;
61416141
6142- startContainerParentElement = Util . getClosestBlockContainer (
6143- currentSelection . getRangeAt ( 0 ) . startContainer ) ;
6144- endContainerParentElement = Util . getClosestBlockContainer (
6145- currentSelection . getRangeAt ( 0 ) . endContainer ) ;
6142+ startContainerParentElement = Util . getClosestBlockContainer ( currentSelection . getRangeAt ( 0 ) . startContainer ) ;
6143+ endContainerParentElement = Util . getClosestBlockContainer ( currentSelection . getRangeAt ( 0 ) . endContainer ) ;
61466144
61476145 if ( startContainerParentElement === endContainerParentElement ) {
61486146 var currentEditor = Selection . getSelectionElement ( this . options . contentWindow ) ,
61496147 parentElement = ( startContainerParentElement || currentEditor ) ,
61506148 fragment = this . options . ownerDocument . createDocumentFragment ( ) ;
6149+
6150+ // since we are going to create a link from an extracted text,
6151+ // be sure that if we are updating a link, we won't let an empty link behind (see #754)
6152+ // (Workaroung for Chrome)
6153+ this . execAction ( 'unlink' ) ;
6154+
61516155 exportedSelection = this . exportSelection ( ) ;
61526156 fragment . appendChild ( parentElement . cloneNode ( true ) ) ;
6157+
61536158 if ( currentEditor === parentElement ) {
61546159 // We have to avoid the editor itself being wiped out when it's the only block element,
61556160 // as our reference inside this.elements gets detached from the page when insertHTML runs.
@@ -6162,37 +6167,50 @@ function MediumEditor(elements, options) {
61626167 // In WebKit:
61636168 // an invented <br /> tag at the end in the same situation
61646169
6165- Selection . select ( this . options . ownerDocument ,
6166- parentElement . firstChild , 0 ,
6167- parentElement . lastChild , parentElement . lastChild . nodeType === 3 ?
6168- parentElement . lastChild . nodeValue . length : parentElement . lastChild . childNodes . length ) ;
6170+ Selection . select (
6171+ this . options . ownerDocument ,
6172+ parentElement . firstChild ,
6173+ 0 ,
6174+ parentElement . lastChild ,
6175+ parentElement . lastChild . nodeType === 3 ?
6176+ parentElement . lastChild . nodeValue . length : parentElement . lastChild . childNodes . length
6177+ ) ;
61696178 } else {
6170- Selection . select ( this . options . ownerDocument ,
6171- parentElement , 0 ,
6172- parentElement , parentElement . childNodes . length ) ;
6179+ Selection . select (
6180+ this . options . ownerDocument ,
6181+ parentElement ,
6182+ 0 ,
6183+ parentElement ,
6184+ parentElement . childNodes . length
6185+ ) ;
61736186 }
6187+
61746188 var modifiedExportedSelection = this . exportSelection ( ) ;
61756189
6176- textNodes = Util . findOrCreateMatchingTextNodes ( this . options . ownerDocument ,
6177- fragment ,
6178- {
6179- start : exportedSelection . start - modifiedExportedSelection . start ,
6180- end : exportedSelection . end - modifiedExportedSelection . start ,
6181- editableElementIndex : exportedSelection . editableElementIndex
6182- } ) ;
6190+ textNodes = Util . findOrCreateMatchingTextNodes (
6191+ this . options . ownerDocument ,
6192+ fragment ,
6193+ {
6194+ start : exportedSelection . start - modifiedExportedSelection . start ,
6195+ end : exportedSelection . end - modifiedExportedSelection . start ,
6196+ editableElementIndex : exportedSelection . editableElementIndex
6197+ }
6198+ ) ;
6199+
61836200 // Creates the link in the document fragment
61846201 Util . createLink ( this . options . ownerDocument , textNodes , opts . url . trim ( ) ) ;
61856202 // Chrome trims the leading whitespaces when inserting HTML, which messes up restoring the selection.
61866203 var leadingWhitespacesCount = ( fragment . firstChild . innerHTML . match ( / ^ \s + / ) || [ '' ] ) [ 0 ] . length ;
61876204 // Now move the created link back into the original document in a way to preserve undo/redo history
6188- Util . insertHTMLCommand ( this . options . ownerDocument ,
6189- fragment . firstChild . innerHTML . replace ( / ^ \s + / , '' ) ) ;
6205+ Util . insertHTMLCommand ( this . options . ownerDocument , fragment . firstChild . innerHTML . replace ( / ^ \s + / , '' ) ) ;
61906206 exportedSelection . start -= leadingWhitespacesCount ;
61916207 exportedSelection . end -= leadingWhitespacesCount ;
6208+
61926209 this . importSelection ( exportedSelection ) ;
61936210 } else {
61946211 this . options . ownerDocument . execCommand ( 'createLink' , false , opts . url ) ;
61956212 }
6213+
61966214 if ( this . options . targetBlank || opts . target === '_blank' ) {
61976215 Util . setTargetBlank ( Selection . getSelectionStart ( this . options . ownerDocument ) , opts . url ) ;
61986216 }
@@ -6249,7 +6267,7 @@ MediumEditor.parseVersionString = function (release) {
62496267
62506268MediumEditor . version = MediumEditor . parseVersionString . call ( this , ( {
62516269 // grunt-bump looks for this:
6252- 'version' : '5.5.2 '
6270+ 'version' : '5.5.3 '
62536271} ) . version ) ;
62546272
62556273 return MediumEditor ;
0 commit comments