void Position::moveToOffset(int offset) { ASSERT(anchorType() == PositionIsOffsetInAnchor || m_isLegacyEditingPosition); m_offset = offset; if (m_isLegacyEditingPosition) m_anchorType = anchorTypeForLegacyEditingPosition(m_anchorNode.get(), m_offset); }
Node* Position::containerNode() const { if (!m_anchorNode) return 0; switch (anchorType()) { case PositionIsOffsetInAnchor: return m_anchorNode.get(); case PositionIsBeforeAnchor: case PositionIsAfterAnchor: return m_anchorNode->parentNode(); } ASSERT_NOT_REACHED(); return 0; }
Node* Position::computeNodeAfterPosition() const { if (!m_anchorNode) return 0; switch (anchorType()) { case PositionIsOffsetInAnchor: return m_anchorNode->childNode(m_offset); case PositionIsBeforeAnchor: return m_anchorNode.get(); case PositionIsAfterAnchor: return m_anchorNode->nextSibling(); } ASSERT_NOT_REACHED(); return 0; }
Node* Position::computeNodeBeforePosition() const { if (!m_anchorNode) return 0; switch (anchorType()) { case PositionIsOffsetInAnchor: return m_anchorNode->childNode(m_offset - 1); // -1 converts to childNode((unsigned)-1) and returns null. case PositionIsBeforeAnchor: return m_anchorNode->previousSibling(); case PositionIsAfterAnchor: return m_anchorNode.get(); } ASSERT_NOT_REACHED(); return 0; }
int Position::computeOffsetInContainerNode() const { if (!m_anchorNode) return 0; switch (anchorType()) { case PositionIsOffsetInAnchor: return std::min(lastOffsetInNode(m_anchorNode.get()), m_offset); case PositionIsBeforeAnchor: return m_anchorNode->nodeIndex(); case PositionIsAfterAnchor: return m_anchorNode->nodeIndex() + 1; } ASSERT_NOT_REACHED(); return 0; }
Node* PositionTemplate<Strategy>::computeContainerNode() const { if (!m_anchorNode) return 0; switch (anchorType()) { case PositionAnchorType::BeforeChildren: case PositionAnchorType::AfterChildren: case PositionAnchorType::OffsetInAnchor: return m_anchorNode.get(); case PositionAnchorType::BeforeAnchor: case PositionAnchorType::AfterAnchor: return Strategy::parent(*m_anchorNode); } ASSERT_NOT_REACHED(); return 0; }
Node* PositionTemplate<Strategy>::computeNodeBeforePosition() const { if (!m_anchorNode) return 0; switch (anchorType()) { case PositionAnchorType::BeforeChildren: return 0; case PositionAnchorType::AfterChildren: return Strategy::lastChild(*m_anchorNode); case PositionAnchorType::OffsetInAnchor: return m_offset ? Strategy::childAt(*m_anchorNode, m_offset - 1) : 0; case PositionAnchorType::BeforeAnchor: return Strategy::previousSibling(*m_anchorNode); case PositionAnchorType::AfterAnchor: return m_anchorNode.get(); } ASSERT_NOT_REACHED(); return 0; }
int PositionTemplate<Strategy>::computeOffsetInContainerNode() const { if (!m_anchorNode) return 0; switch (anchorType()) { case PositionAnchorType::BeforeChildren: return 0; case PositionAnchorType::AfterChildren: return lastOffsetInNode(m_anchorNode.get()); case PositionAnchorType::OffsetInAnchor: return minOffsetForNode(m_anchorNode.get(), m_offset); case PositionAnchorType::BeforeAnchor: return Strategy::index(*m_anchorNode); case PositionAnchorType::AfterAnchor: return Strategy::index(*m_anchorNode) + 1; } ASSERT_NOT_REACHED(); return 0; }
void PositionTemplate<Strategy>::showAnchorTypeAndOffset() const { switch (anchorType()) { case PositionAnchorType::OffsetInAnchor: fputs("offset", stderr); break; case PositionAnchorType::BeforeChildren: fputs("beforeChildren", stderr); break; case PositionAnchorType::AfterChildren: fputs("afterChildren", stderr); break; case PositionAnchorType::BeforeAnchor: fputs("before", stderr); break; case PositionAnchorType::AfterAnchor: fputs("after", stderr); break; } fprintf(stderr, ", offset:%d\n", m_offset); }