Opened 14 years ago
Closed 14 years ago
#6837 closed Bug (fixed)
Improve getBoundaryInformation
Reported by: | Alfonso Martínez de Lizarrondo | Owned by: | |
---|---|---|---|
Priority: | Normal | Milestone: | |
Component: | Core : Selection | Version: | 3.0 |
Keywords: | HasPatch | Cc: |
Description
Reported here: http://cksource.com/forums/viewtopic.php?f=11&t=20956
We should test if it keeps working correctly and it improves performance.
var getBoundaryInformation = function( range, start ) { var getIndex = function( n ) { var parent = n.parentNode, currentIndex = -1, node; if ( parent ) { node = parent.firstChild; while ( node ) { currentIndex++; if ( node === n )break; node = node.nextSibling; } } return currentIndex; }; range = range.duplicate(); range.collapse( start ); var parent = range.parentElement(); //如果节点里没有子节点,直接退出 if(!parent.hasChildNodes()){ return {container:parent,offset:0}; } var siblings = parent.children, child, testRange = range.duplicate(), startIndex = 0,endIndex = siblings.length - 1,index=-1, distance; while (startIndex <= endIndex) { index = Math.floor((startIndex + endIndex) / 2); child = siblings[index]; testRange.moveToElementText(child); var position = testRange.compareEndPoints( 'StartToStart', range ); if (position > 0) { endIndex = index - 1; } else if (position < 0) { startIndex = index + 1; } else { return {container:parent,offset:getIndex(child)} } } if(index==-1){ testRange.moveToElementText(parent); testRange.setEndPoint('StartToStart',range); distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length; siblings = parent.childNodes; if(!distance){ child = siblings[siblings.length-1]; return {container:child,offset:child.nodeValue.length}; } var i = siblings.length ; while ( distance > 0 ) distance -= siblings[ --i ].nodeValue.length; return {container:siblings[i],offset:-distance} } if(position>0){ testRange.collapse(); testRange.setEndPoint('StartToStart',range); distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length; if(!distance){ return {container:parent,offset:getIndex(child)-1} } while(distance>0){ child = child.previousSibling; try{ distance -= child.nodeValue.length; }catch(e){ return {container:parent,offset:getIndex(child)} } } return {container:child,offset:-distance} }else{ testRange.collapse(false); testRange.setEndPoint('EndToStart' , range); distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length; if(!distance){ return {container:parent,offset:getIndex(child)+1} } while(distance>0){ child = child.nextSibling; try{ distance -= child.nodeValue.length; }catch(e){ return {container:parent,offset:getIndex(child)} } } return {container:child,offset:child.nodeValue.length-distance} } };
Change History (3)
comment:1 Changed 14 years ago by
Milestone: | → CKEditor 3.6 |
---|
comment:2 Changed 14 years ago by
Version: | → 3.0 |
---|
comment:3 Changed 14 years ago by
Milestone: | CKEditor 3.6 |
---|---|
Resolution: | → fixed |
Status: | new → closed |
Similar code was introduced in #6438. Thanks!
As noted by Alfonso, if the code is correct and much faster, then we should use it.