static Position positionForIndex(HTMLElement* innerEditor, int index) { ASSERT(index >= 0); if (index == 0) { Node* node = NodeTraversal::next(*innerEditor, innerEditor); if (node && node->isTextNode()) return Position(node, 0); return Position(innerEditor, 0); } int remainingCharactersToMoveForward = index; Node* lastBrOrText = innerEditor; for (Node& node : NodeTraversal::descendantsOf(*innerEditor)) { ASSERT(remainingCharactersToMoveForward >= 0); if (node.hasTagName(brTag)) { if (remainingCharactersToMoveForward == 0) return positionBeforeNode(&node); --remainingCharactersToMoveForward; lastBrOrText = &node; continue; } if (node.isTextNode()) { Text& text = toText(node); if (remainingCharactersToMoveForward < static_cast<int>(text.length())) return Position(&text, remainingCharactersToMoveForward); remainingCharactersToMoveForward -= text.length(); lastBrOrText = &node; continue; } ASSERT_NOT_REACHED(); } return lastPositionInOrAfterNode(lastBrOrText); }
static Position positionForIndex(TextControlInnerTextElement* innerText, unsigned index) { unsigned remainingCharactersToMoveForward = index; Node* lastBrOrText = innerText; for (Node* node = innerText; node; node = NodeTraversal::next(*node, innerText)) { if (node->hasTagName(brTag)) { if (!remainingCharactersToMoveForward) return positionBeforeNode(node); remainingCharactersToMoveForward--; lastBrOrText = node; } else if (is<Text>(*node)) { Text& text = downcast<Text>(*node); if (remainingCharactersToMoveForward < text.length()) return Position(&text, remainingCharactersToMoveForward); remainingCharactersToMoveForward -= text.length(); lastBrOrText = node; } } return lastPositionInOrAfterNode(lastBrOrText); }
void VisibleSelection::adjustSelectionToAvoidCrossingShadowBoundaries() { if (m_base.isNull() || m_start.isNull() || m_end.isNull()) return; Node* startRootNode = m_start.anchorNode()->nonBoundaryShadowTreeRootNode(); Node* endRootNode = m_end.anchorNode()->nonBoundaryShadowTreeRootNode(); if (!startRootNode && !endRootNode) return; if (startRootNode == endRootNode) return; if (m_baseIsFirst) { m_extent = startRootNode ? lastPositionInOrAfterNode(startRootNode) : positionBeforeNode(endRootNode->shadowAncestorNode()); m_end = m_extent; } else { m_extent = endRootNode ? firstPositionInOrBeforeNode(endRootNode) : positionAfterNode(startRootNode->shadowAncestorNode()); m_start = m_extent; } ASSERT(m_start.anchorNode()->treeScope() == m_end.anchorNode()->treeScope()); }