@@ -1416,6 +1416,45 @@ class CodeitElement extends HTMLElement {
14161416
14171417 function getCaretTextNode ( caretPosInText ) {
14181418
1419+ if ( caretPosInText === cd . textContent . length ) {
1420+
1421+ function getLastNode ( parentNode ) {
1422+
1423+ // if parent has child nodes
1424+ if ( parentNode . childNodes . length > 0 ) {
1425+
1426+ const lastNode = parentNode . childNodes [ parentNode . childNodes . length - 1 ] ;
1427+
1428+ // if found a text node
1429+ if ( lastNode . nodeType === 3 ) {
1430+
1431+ // return
1432+ return lastNode ;
1433+
1434+ } else { // continue recursive call
1435+
1436+ return getLastNode ( lastNode ) ;
1437+
1438+ }
1439+
1440+ } else {
1441+
1442+ // return
1443+ return parentNode ;
1444+
1445+ }
1446+
1447+ }
1448+
1449+ // get end node
1450+ const endNode = getLastNode ( cd ) ;
1451+ const endOffset = ( endNode . nodeValue || endNode . textContent ) . length ;
1452+
1453+ return [ endNode , endOffset ] ;
1454+
1455+ }
1456+
1457+
14191458 let overallLength = 0 ,
14201459 lastNode ;
14211460
@@ -1505,62 +1544,17 @@ class CodeitElement extends HTMLElement {
15051544 } ;
15061545
15071546 } else {
1508-
1509- if ( startPos === cd . textContent . length ) {
1510-
1511- function getLastNode ( parentNode ) {
1512-
1513- // if parent has child nodes
1514- if ( parentNode . childNodes . length > 0 ) {
1515-
1516- const lastNode = parentNode . childNodes [ parentNode . childNodes . length - 1 ] ;
1517-
1518- // if found a text node
1519- if ( lastNode . nodeType === 3 ) {
1520-
1521- // return
1522- return lastNode ;
15231547
1524- } else { // continue recursive call
1548+ // get start nodes
1549+ const [ startNode , startOffset ] = getCaretTextNode ( startPos ) ;
15251550
1526- return getLastNode ( lastNode ) ;
1551+ return {
1552+ startNode : startNode ,
1553+ startOffset : startOffset ,
1554+ endNode : startNode ,
1555+ endOffset : startOffset
1556+ } ;
15271557
1528- }
1529-
1530- } else {
1531-
1532- // return
1533- return parentNode ;
1534-
1535- }
1536-
1537- }
1538-
1539- // get end node
1540- const endNode = getLastNode ( cd ) ;
1541- const endOffset = ( endNode . nodeValue || endNode . textContent ) . length ;
1542-
1543- return {
1544- startNode : endNode ,
1545- startOffset : endOffset ,
1546- endNode : endNode ,
1547- endOffset : endOffset
1548- } ;
1549-
1550- } else {
1551-
1552- // get start nodes
1553- const [ startNode , startOffset ] = getCaretTextNode ( startPos ) ;
1554-
1555- return {
1556- startNode : startNode ,
1557- startOffset : startOffset ,
1558- endNode : startNode ,
1559- endOffset : startOffset
1560- } ;
1561-
1562- }
1563-
15641558 }
15651559
15661560 }
0 commit comments