@@ -187,6 +187,9 @@ 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+ }
190193
191194 if ( this . validateSelection ( currentSelection ) ) {
192195 currentRange = currentSelection . getRangeAt ( 0 ) ;
@@ -201,11 +204,17 @@ const Selection = Module({
201204
202205 getAnchorNode ( ) {
203206 const currentSelection = this . getCurrentSelection ( ) ;
204- return currentSelection . anchorNode ;
207+ if ( ! currentSelection ) {
208+ console . log ( 'getAnchorNode:currentSelection' , currentSelection ) ;
209+ }
210+ return currentSelection && currentSelection . anchorNode ;
205211 } ,
206212
207213 getCommonAncestor ( ) {
208214 const currentSelection = this . getCurrentSelection ( ) ;
215+ if ( ! currentSelection ) {
216+ console . log ( 'getCommonAncestor:currentSelection' , currentSelection ) ;
217+ }
209218 if ( currentSelection . rangeCount > 0 ) {
210219 const selectionRange = currentSelection . getRangeAt ( 0 ) ;
211220 return selectionRange . commonAncestorContainer ;
@@ -298,9 +307,50 @@ const Selection = Module({
298307
299308 const startTrimmablePrefix = startContainer . textContent . match ( / ^ ( \r ? \n | \r ) ? ( \s + ) ? / ) ;
300309 const endTrimmablePrefix = endContainer . textContent . match ( / ^ ( \r ? \n | \r ) ? ( \s + ) ? / ) ;
310+ const endTrimmableSuffix = endContainer . textContent . match ( / ( \r ? \n | \r ) ? ( \s + ) ? $ / ) ;
311+ const startElement = DOM . closestElement ( startContainer ) ;
312+ const endElement = DOM . closestElement ( endContainer ) ;
313+
314+ console . log ( {
315+ startTrimmablePrefix,
316+ endTrimmablePrefix,
317+ startPrefixLength : startTrimmablePrefix && startTrimmablePrefix [ 0 ] . length ,
318+ endPrefixLength : endTrimmablePrefix && endTrimmablePrefix [ 0 ] . length ,
319+ startContent : startContainer . textContent ,
320+ endContent : endContainer . textContent ,
321+ endSuffix : endContainer . textContent . match ( / ( \r ? \n | \r ) ? ( \s + ) ? $ / ) ,
322+ startOffset,
323+ endOffset
324+ } ) ;
325+
326+ if ( startTrimmablePrefix && startTrimmablePrefix [ 0 ] . length ) {
327+ startOffset -= startTrimmablePrefix [ 0 ] . length ;
328+ if ( DOM . nodeIsInline ( startElement ) ) {
329+ startOffset -= startTrimmablePrefix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
330+ }
331+ }
332+
333+ if ( endTrimmablePrefix && endTrimmablePrefix [ 0 ] . length ) {
334+ endOffset -= endTrimmablePrefix [ 0 ] . length ;
335+ if ( DOM . nodeIsInline ( endElement ) ) {
336+ endOffset -= endTrimmablePrefix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
337+ }
338+ }
339+
340+ if ( endTrimmableSuffix && endTrimmableSuffix [ 0 ] . length ) {
341+ // endOffset -= endTrimmableSuffix[0].length;
342+ if ( DOM . nodeIsInline ( endElement ) ) {
343+ endOffset -= endTrimmableSuffix [ 0 ] . match ( / \s / ) ? 1 : 0 ;
344+ }
345+ }
346+
347+ console . log ( {
348+ startOffset,
349+ endOffset
350+ } ) ;
301351
302- startOffset -= startTrimmablePrefix ? startTrimmablePrefix [ 0 ] . length : 0 ;
303- endOffset -= endTrimmablePrefix ? endTrimmablePrefix [ 0 ] . length : 0 ;
352+ startOffset = Math . max ( 0 , startOffset ) ;
353+ endOffset = Math . min ( endContainer . textContent . length , endOffset ) ;
304354
305355 startCoordinates . unshift ( startOffset ) ;
306356 endCoordinates . unshift ( endOffset ) ;
@@ -331,7 +381,6 @@ const Selection = Module({
331381 }
332382
333383 const isIn = DOM . isIn ( anchorNode , selectors , rootEl ) ;
334-
335384 if ( isIn ) {
336385 return isIn ;
337386 }
@@ -341,8 +390,8 @@ const Selection = Module({
341390 let contains = false ;
342391
343392 if ( rangeFrag . childNodes . length ) {
344- selectors . forEach ( selector => {
345- contains = contains || rangeFrag . childNodes [ 0 ] . nodeName === selector ;
393+ rangeFrag . childNodes . forEach ( childNode => {
394+ contains = contains || selectors . indexOf ( childNode . nodeName ) > - 1 ;
346395 } ) ;
347396 }
348397
@@ -353,7 +402,9 @@ const Selection = Module({
353402 const currentSelection = this . getCurrentSelection ( ) ;
354403 let { anchorNode, focusNode } = currentSelection ;
355404 const selectionContainsNode = currentSelection . containsNode ( node , true ) ;
356-
405+ if ( ! currentSelection ) {
406+ console . log ( 'containsNode:currentSelection' , currentSelection ) ;
407+ }
357408 if ( ! currentSelection . rangeCount ) {
358409 return false ;
359410 }
@@ -487,7 +538,9 @@ const Selection = Module({
487538 updateRange ( range , opts = { } ) {
488539 const { mediator, props } = this ;
489540 const currentSelection = this . getCurrentSelection ( ) ;
490-
541+ if ( ! currentSelection ) {
542+ console . log ( 'updateRange:currentSelection' , currentSelection ) ;
543+ }
491544 if ( opts . silent ) {
492545 props . silenceChanges . push ( true ) ; // silence removeAllRanges
493546 props . silenceChanges . push ( true ) ; // silence addRange
@@ -634,6 +687,8 @@ const Selection = Module({
634687 const startOffset = startCoordinates . pop ( ) ;
635688 const endOffset = endCoordinates . pop ( ) ;
636689
690+ console . log ( 'selectByCoordinates' , { rangeCoordinates } ) ;
691+
637692 let startContainer = dom . el [ 0 ] ;
638693 let endContainer = dom . el [ 0 ] ;
639694
@@ -647,6 +702,8 @@ const Selection = Module({
647702 endContainer = endContainer . childNodes [ endIndex ] ;
648703 }
649704
705+ console . log ( { startContainer, endContainer, startLength : startContainer . length , endLength : endContainer . length , startOffset, endOffset } ) ;
706+
650707 newRange . setStart ( startContainer , startOffset ) ;
651708 newRange . setEnd ( endContainer , endOffset ) ;
652709
0 commit comments