@@ -87,16 +87,17 @@ export default class Selection extends Cursor {
8787 link . setAttribute ( key , value )
8888 }
8989 }
90- if ( config . linkMarkup . trim ) this . trimRangeWhitespaces ( )
90+ if ( config . linkMarkup . trim ) this . trimRange ( )
9191
9292 this . forceWrap ( link )
9393 }
9494
9595 // trims whitespaces on the left and right of a selection, i.e. what you want in case of links
96- trimRangeWhitespaces ( ) {
96+ trimRange ( ) {
9797 const textToLink = this . range . toString ( )
9898 const whitespacesOnTheLeft = textToLink . search ( / \S | $ / )
99- const whitespacesOnTheRight = textToLink . length - whitespacesOnTheLeft - textToLink . trim ( ) . length
99+ const lastNonWhitespace = textToLink . search ( / \S [ \s ] + $ / )
100+ const whitespacesOnTheRight = lastNonWhitespace === - 1 ? 0 : textToLink . length - ( lastNonWhitespace + 1 )
100101 this . range . setStart ( this . range . startContainer , this . range . startOffset + whitespacesOnTheLeft )
101102 this . range . setEnd ( this . range . endContainer , this . range . endOffset - whitespacesOnTheRight )
102103 }
@@ -160,37 +161,37 @@ export default class Selection extends Cursor {
160161
161162 makeBold ( ) {
162163 const bold = this . createElement ( config . boldMarkup . name , config . boldMarkup . attribs )
163- if ( config . boldMarkup . trim ) this . trimRangeWhitespaces ( )
164+ if ( config . boldMarkup . trim ) this . trimRange ( )
164165 this . forceWrap ( bold )
165166 }
166167
167168 toggleBold ( ) {
168169 const bold = this . createElement ( config . boldMarkup . name , config . boldMarkup . attribs )
169- if ( config . boldMarkup . trim ) this . trimRangeWhitespaces ( )
170+ if ( config . boldMarkup . trim ) this . trimRange ( )
170171 this . toggle ( bold )
171172 }
172173
173174 giveEmphasis ( ) {
174175 const em = this . createElement ( config . italicMarkup . name , config . italicMarkup . attribs )
175- if ( config . italicMarkup . trim ) this . trimRangeWhitespaces ( )
176+ if ( config . italicMarkup . trim ) this . trimRange ( )
176177 this . forceWrap ( em )
177178 }
178179
179180 toggleEmphasis ( ) {
180181 const em = this . createElement ( config . italicMarkup . name , config . italicMarkup . attribs )
181- if ( config . italicMarkup . trim ) this . trimRangeWhitespaces ( )
182+ if ( config . italicMarkup . trim ) this . trimRange ( )
182183 this . toggle ( em )
183184 }
184185
185186 makeUnderline ( ) {
186187 const u = this . createElement ( config . underlineMarkup . name , config . underlineMarkup . attribs )
187- if ( config . underlineMarkup . trim ) this . trimRangeWhitespaces ( )
188+ if ( config . underlineMarkup . trim ) this . trimRange ( )
188189 this . forceWrap ( u )
189190 }
190191
191192 toggleUnderline ( ) {
192193 const u = this . createElement ( config . underlineMarkup . name , config . underlineMarkup . attribs )
193- if ( config . underlineMarkup . trim ) this . trimRangeWhitespaces ( )
194+ if ( config . underlineMarkup . trim ) this . trimRange ( )
194195 this . toggle ( u )
195196 }
196197
0 commit comments