@@ -187,9 +187,6 @@ const Selection = Module({
187187 const { props } = this ;
188188 const currentSelection = this . getCurrentSelection ( ) ;
189189 let currentRange ;
190- if ( ! currentSelection ) {
191- console . log ( 'getCurrentRange:currentSelection' , currentSelection ) ;
192- }
193190
194191 if ( this . validateSelection ( currentSelection ) ) {
195192 currentRange = currentSelection . getRangeAt ( 0 ) ;
@@ -204,17 +201,11 @@ const Selection = Module({
204201
205202 getAnchorNode ( ) {
206203 const currentSelection = this . getCurrentSelection ( ) ;
207- if ( ! currentSelection ) {
208- console . log ( 'getAnchorNode:currentSelection' , currentSelection ) ;
209- }
210204 return currentSelection && currentSelection . anchorNode ;
211205 } ,
212206
213207 getCommonAncestor ( ) {
214208 const currentSelection = this . getCurrentSelection ( ) ;
215- if ( ! currentSelection ) {
216- console . log ( 'getCommonAncestor:currentSelection' , currentSelection ) ;
217- }
218209 if ( currentSelection . rangeCount > 0 ) {
219210 const selectionRange = currentSelection . getRangeAt ( 0 ) ;
220211 return selectionRange . commonAncestorContainer ;
@@ -310,10 +301,14 @@ const Selection = Module({
310301 const endTrimmableSuffix = endContainer . textContent . match ( / ( \r ? \n | \r ) ? ( \s + ) ? $ / ) ;
311302 const startElement = DOM . closestElement ( startContainer ) ;
312303 const endElement = DOM . closestElement ( endContainer ) ;
304+ let startPrefixTrimLength = 0 ;
305+ let endPrefixTrimLength = 0 ;
306+ let endSuffixTrimLength = 0 ;
313307
314308 console . log ( {
315309 startTrimmablePrefix,
316310 endTrimmablePrefix,
311+ endTrimmableSuffix,
317312 startPrefixLength : startTrimmablePrefix && startTrimmablePrefix [ 0 ] . length ,
318313 endPrefixLength : endTrimmablePrefix && endTrimmablePrefix [ 0 ] . length ,
319314 startContent : startContainer . textContent ,
@@ -324,24 +319,32 @@ const Selection = Module({
324319 } ) ;
325320
326321 if ( startTrimmablePrefix && startTrimmablePrefix [ 0 ] . length ) {
327- startOffset - = startTrimmablePrefix [ 0 ] . length ;
322+ startPrefixTrimLength + = startTrimmablePrefix [ 0 ] . length ;
328323 if ( DOM . nodeIsInline ( startElement ) ) {
329- startOffset -= startTrimmablePrefix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
324+ startPrefixTrimLength -= startTrimmablePrefix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
325+ }
326+ startOffset -= startPrefixTrimLength ;
327+ if ( endContainer === startContainer ) {
328+ endOffset -= startPrefixTrimLength ;
330329 }
331330 }
332331
333- if ( endTrimmablePrefix && endTrimmablePrefix [ 0 ] . length ) {
334- endOffset - = endTrimmablePrefix [ 0 ] . length ;
332+ if ( endTrimmablePrefix && endTrimmablePrefix [ 0 ] . length && endContainer !== startContainer ) {
333+ endPrefixTrimLength + = endTrimmablePrefix [ 0 ] . length ;
335334 if ( DOM . nodeIsInline ( endElement ) ) {
336- endOffset -= endTrimmablePrefix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
335+ endPrefixTrimLength -= endTrimmablePrefix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
337336 }
337+ endOffset -= endPrefixTrimLength ;
338338 }
339339
340340 if ( endTrimmableSuffix && endTrimmableSuffix [ 0 ] . length ) {
341- // endOffset - = endTrimmableSuffix[0].length;
341+ endSuffixTrimLength + = endTrimmableSuffix [ 0 ] . length ;
342342 if ( DOM . nodeIsInline ( endElement ) ) {
343- endOffset -= endTrimmableSuffix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
343+ endSuffixTrimLength -= endTrimmableSuffix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
344344 }
345+ let trimmedTextLength = endContainer . textContent . length - endPrefixTrimLength - endSuffixTrimLength ;
346+ console . log ( '>>' , { endOffset, trimmedTextLength, textLength : endContainer . textContent . length , endPrefixTrimLength, endSuffixTrimLength } ) ;
347+ endOffset = Math . min ( trimmedTextLength , endOffset ) ;
345348 }
346349
347350 console . log ( {
@@ -402,9 +405,7 @@ const Selection = Module({
402405 const currentSelection = this . getCurrentSelection ( ) ;
403406 let { anchorNode, focusNode } = currentSelection ;
404407 const selectionContainsNode = currentSelection . containsNode ( node , true ) ;
405- if ( ! currentSelection ) {
406- console . log ( 'containsNode:currentSelection' , currentSelection ) ;
407- }
408+
408409 if ( ! currentSelection . rangeCount ) {
409410 return false ;
410411 }
@@ -416,6 +417,7 @@ const Selection = Module({
416417 if ( anchorNode . nodeType !== Node . ELEMENT_NODE ) {
417418 anchorNode = anchorNode . parentNode ;
418419 }
420+
419421 if ( focusNode . nodeType !== Node . ELEMENT_NODE ) {
420422 focusNode = focusNode . parentNode ;
421423 }
@@ -538,9 +540,7 @@ const Selection = Module({
538540 updateRange ( range , opts = { } ) {
539541 const { mediator, props } = this ;
540542 const currentSelection = this . getCurrentSelection ( ) ;
541- if ( ! currentSelection ) {
542- console . log ( 'updateRange:currentSelection' , currentSelection ) ;
543- }
543+
544544 if ( opts . silent ) {
545545 props . silenceChanges . push ( true ) ; // silence removeAllRanges
546546 props . silenceChanges . push ( true ) ; // silence addRange
0 commit comments