Commit b4c2e6c9 authored by eric@webkit.org's avatar eric@webkit.org
Browse files

Reviewed by Darin Adler.

        Add containerNode(), computeOffsetInContainerNode(),
        computeNodeBeforePosition() and computeNodeAfterPosition().
        https://bugs.webkit.org/show_bug.cgi?id=24763

        These functions will replace uses of rangeCompliantPosition()
        in future patches.

        No changes in behavior, thus no tests.

        * dom/Position.cpp:
        (WebCore::Position::containerNode):
        (WebCore::Position::computeOffsetInContainerNode):
        (WebCore::Position::computeNodeBeforePosition):
        (WebCore::Position::computeNodeAfterPosition):
        (WebCore::Position::anchorType):
        * dom/Position.h:
        (WebCore::Position::):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@42263 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent f4037a2a
2009-04-07 Eric Seidel <eric@webkit.org>
Reviewed by Darin Adler.
Add containerNode(), computeOffsetInContainerNode(),
computeNodeBeforePosition() and computeNodeAfterPosition().
https://bugs.webkit.org/show_bug.cgi?id=24763
These functions will replace uses of rangeCompliantPosition()
in future patches.
No changes in behavior, thus no tests.
* dom/Position.cpp:
(WebCore::Position::containerNode):
(WebCore::Position::computeOffsetInContainerNode):
(WebCore::Position::computeNodeBeforePosition):
(WebCore::Position::computeNodeAfterPosition):
(WebCore::Position::anchorType):
* dom/Position.h:
(WebCore::Position::):
2009-04-06 Sam Weinig <sam@webkit.org>
 
Reviewed by Mark Rowe.
......@@ -73,6 +73,91 @@ static Node* previousRenderedEditable(Node* node)
return 0;
}
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;
}
int Position::computeOffsetInContainerNode() const
{
if (!m_anchorNode)
return 0;
switch (anchorType()) {
case PositionIsOffsetInAnchor:
{
int maximumValidOffset = m_anchorNode->offsetInCharacters() ? m_anchorNode->maxCharacterOffset() : m_anchorNode->childNodeCount();
return std::min(maximumValidOffset, m_offset);
}
case PositionIsBeforeAnchor:
return m_anchorNode->nodeIndex();
case PositionIsAfterAnchor:
return m_anchorNode->nodeIndex() + 1;
}
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;
}
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;
}
// FIXME: Position should store an AnchorType up-front, instead of lazily
// determining it via editingIgnoresContent(anchorNode())
// That would require fixing moveToOffset to know how to recompute the AnchorType
// for positions which need historical editing-compatible behavior
// (and for explicitly anchored positions to ASSERT_NOT_REACHED())
Position::AnchorType Position::anchorType() const
{
if (m_anchorNode && editingIgnoresContent(m_anchorNode.get())) {
if (m_offset == 0)
return PositionIsBeforeAnchor;
return PositionIsAfterAnchor;
}
return PositionIsOffsetInAnchor;
}
Element* Position::documentElement() const
{
if (Node* n = node())
......
......@@ -62,6 +62,15 @@ public:
void clear() { m_anchorNode.clear(); m_offset = 0; }
// These are always DOM compliant values. Editing positions like [img, 0] (aka [img, before])
// will return img->parentNode() and img->nodeIndex() from these functions.
Node* containerNode() const;
int computeOffsetInContainerNode() const;
// These are convenience methods which are smart about whether the position is neighbor anchored or parent anchored
Node* computeNodeBeforePosition() const;
Node* computeNodeAfterPosition() const;
Node* anchorNode() const { return m_anchorNode.get(); }
// FIXME: Callers should be moved off of node(), node() is not always the container for this position.
......@@ -133,6 +142,14 @@ public:
private:
int renderedOffset() const;
enum AnchorType {
PositionIsOffsetInAnchor,
PositionIsAfterAnchor,
PositionIsBeforeAnchor
};
AnchorType anchorType() const;
Position previousCharacterPosition(EAffinity) const;
Position nextCharacterPosition(EAffinity) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment