1- import { FF_DEV_2480 , isFF } from './feature-flags' ;
21import { clamp , isDefined } from './utilities' ;
32
43export const isTextNode = node => node && node . nodeType === Node . TEXT_NODE ;
@@ -287,6 +286,8 @@ const textNodeLookup = (commonContainer, node, offset, direction = 'forward') =>
287286
288287 const walker = commonContainer . ownerDocument . createTreeWalker ( commonContainer , NodeFilter . SHOW_ALL ) ;
289288 let currentNode = walker . nextNode ( ) ;
289+ // tree walker can't go backward, so we go forward to startNode and record every text node
290+ // to find the last one before startNode
290291 let lastTextNode ;
291292
292293 while ( currentNode && currentNode !== startNode ) {
@@ -325,10 +326,10 @@ const fixRange = range => {
325326 // if user started selection from the end of the tag, start could be this tag,
326327 // so we should move it to more relevant one
327328 const selectionFromTheEnd = startContainer . wholeText . length === startOffset ;
328- // we skip ephemeral whitespace only text nodes, like \n between tags in original html
329+ // we skip ephemeral whitespace- only text nodes, like \n between tags in original html
329330 const isBasicallyEmpty = textNode => / ^ \s * $ / . test ( textNode . wholeText ) ;
330331
331- if ( isFF ( FF_DEV_2480 ) && ( selectionFromTheEnd || isBasicallyEmpty ( startContainer ) ) ) {
332+ if ( selectionFromTheEnd || isBasicallyEmpty ( startContainer ) ) {
332333 do {
333334 startContainer = textNodeLookup ( commonContainer , startContainer , startOffset , 'forward-next' ) ;
334335 if ( ! startContainer ) return null ;
@@ -339,23 +340,15 @@ const fixRange = range => {
339340 }
340341
341342 if ( ! isTextNode ( endContainer ) ) {
342- let isIncluded = false ;
343-
344343 endContainer = textNodeLookup ( commonContainer , endContainer , endOffset , 'backward' ) ;
345344 if ( ! endContainer ) return null ;
346345
347- if ( isFF ( FF_DEV_2480 ) ) {
348- while ( / ^ \s * $ / . test ( endContainer . wholeText ) ) {
349- endContainer = textNodeLookup ( commonContainer , endContainer , endOffset , 'backward-next' ) ;
350- if ( ! endContainer ) return null ;
351- }
352- // we skip empty whitespace only text nodes, so we need the found one to be included
353- isIncluded = true ;
354- } else {
355- isIncluded = range . toString ( ) . includes ( endContainer . wholeText ) ;
346+ while ( / ^ \s * $ / . test ( endContainer . wholeText ) ) {
347+ endContainer = textNodeLookup ( commonContainer , endContainer , endOffset , 'backward-next' ) ;
348+ if ( ! endContainer ) return null ;
356349 }
357-
358- range . setEnd ( endContainer , isIncluded ? endContainer . length : 0 ) ;
350+ // we skip empty whitespace-only text nodes, so we need the found one to be included
351+ range . setEnd ( endContainer , endContainer . length ) ;
359352 }
360353
361354 return range ;
0 commit comments