inside )].
// Picking out which child to recurse into here is a special case since we
// don't want to check past -- once we find that the final range starts
// in , we can look at all of its children (and all of their children)
// to find the break point.
// At all times, (bestContainer, bestOffset) is the latest single-line start
// point that we know of.
let currentContainer = bestContainer;
let maxIndexToConsider = bestOffset - 1;
do {
const nodeValue = currentContainer.nodeValue;
let ii = maxIndexToConsider;
for (; ii >= 0; ii--) {
if (nodeValue != null && ii > 0 && UnicodeUtils.isSurrogatePair(nodeValue, ii - 1)) {
// We're in the middle of a surrogate pair -- skip over so we never
// return a range with an endpoint in the middle of a code point.
continue;
}
range.setStart(currentContainer, ii);
if (areRectsOnOneLine(getRangeClientRects(range), lineHeight)) {
bestContainer = currentContainer;
bestOffset = ii;
} else {
break;
}
}
if (ii === -1 || currentContainer.childNodes.length === 0) {
// If ii === -1, then (bestContainer, bestOffset), which is equal to
// (currentContainer, 0), was a single-line start point but a start
// point before currentContainer wasn't, so the line break seems to
// have occurred immediately after currentContainer's start tag
//
// If currentContainer.childNodes.length === 0, we're already at a
// terminal node (e.g., text node) and should return our current best.
break;
}
currentContainer = currentContainer.childNodes[ii];
maxIndexToConsider = getNodeLength(currentContainer);
} while (true);
range.setStart(bestContainer, bestOffset);
return range;
}
module.exports = expandRangeToStartOfLine;