+ // Adapt one side of test range to the actual range\r
+ // for measuring the offset between them.\r
+ testRange.collapse( position > 0 ? true : false );\r
+ testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );\r
+\r
+ // IE report line break as CRLF with range.text but\r
+ // only LF with textnode.nodeValue, normalize them to avoid\r
+ // breaking character counting logic below. (#3949)\r
+ distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
+\r
+ // Actual range anchor right beside test range at the inner boundary of text node.\r
+ if ( !distance )\r
+ return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };\r
+\r
+ // Start the measuring until distance overflows, meanwhile count the text nodes.\r
+ while ( distance > 0 )\r
+ {\r
+ child = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
+ try\r
+ {\r
+ distance -= child.nodeValue.length;\r
+ }\r
+ // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
+ catch( e )\r
+ {\r
+ return { container : parent, offset : getNodeIndex( child ) };\r
+ }\r
+ }\r
+\r
+ return { container : child, offset : position > 0 ? -distance : child.nodeValue.length + distance };\r