diff options
Diffstat (limited to 'Source/WebCore/dom/Position.h')
-rw-r--r-- | Source/WebCore/dom/Position.h | 118 |
1 files changed, 53 insertions, 65 deletions
diff --git a/Source/WebCore/dom/Position.h b/Source/WebCore/dom/Position.h index e1002d871..f7c42bb8e 100644 --- a/Source/WebCore/dom/Position.h +++ b/Source/WebCore/dom/Position.h @@ -10,10 +10,10 @@ * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR @@ -23,15 +23,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef Position_h -#define Position_h +#pragma once #include "ContainerNode.h" #include "EditingBoundary.h" #include "TextAffinity.h" -#include "TextDirection.h" +#include "TextFlags.h" #include <wtf/Assertions.h> -#include <wtf/PassRefPtr.h> #include <wtf/RefPtr.h> namespace WebCore { @@ -44,6 +42,7 @@ class Range; class RenderElement; class RenderObject; class Text; +class TextStream; enum PositionMoveType { CodePoint, // Move by a single code point. @@ -62,45 +61,30 @@ public: }; Position() - : m_offset(0) - , m_anchorType(PositionIsOffsetInAnchor) + : m_anchorType(PositionIsOffsetInAnchor) , m_isLegacyEditingPosition(false) { } - // For creating legacy editing positions: (Anchor type will be determined from editingIgnoresContent(node)) - class LegacyEditingOffset { - public: - int value() const { return m_offset; } - - private: - explicit LegacyEditingOffset(int offset) : m_offset(offset) { } - - friend Position createLegacyEditingPosition(PassRefPtr<Node>, int offset); - - int m_offset; - }; - Position(PassRefPtr<Node> anchorNode, LegacyEditingOffset); - // For creating before/after positions: - Position(PassRefPtr<Node> anchorNode, AnchorType); - Position(PassRefPtr<Text> textNode, unsigned offset); + WEBCORE_EXPORT Position(Node* anchorNode, AnchorType); + Position(Text* textNode, unsigned offset); // For creating offset positions: // FIXME: This constructor should eventually go away. See bug 63040. - Position(PassRefPtr<Node> anchorNode, int offset, AnchorType); + WEBCORE_EXPORT Position(Node* anchorNode, int offset, AnchorType); AnchorType anchorType() const { return static_cast<AnchorType>(m_anchorType); } - void clear() { m_anchorNode.clear(); m_offset = 0; m_anchorType = PositionIsOffsetInAnchor; m_isLegacyEditingPosition = false; } + void clear() { m_anchorNode = nullptr; m_offset = 0; m_anchorType = PositionIsOffsetInAnchor; m_isLegacyEditingPosition = false; } // 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; // NULL for a before/after position anchored to a node with no parent + // will return img->parentNode() and img->computeNodeIndex() from these functions. + WEBCORE_EXPORT Node* containerNode() const; // null for a before/after position anchored to a node with no parent Text* containerText() const; int computeOffsetInContainerNode() const; // O(n) for before/after-anchored positions, O(1) for parent-anchored positions - Position parentAnchoredEquivalent() const; // Convenience method for DOM positions that also fixes up some positions for editing + WEBCORE_EXPORT Position parentAnchoredEquivalent() const; // Convenience method for DOM positions that also fixes up some positions for editing // Inline O(1) access for Positions which callers know to be parent-anchored int offsetInContainerNode() const @@ -128,29 +112,29 @@ public: // will be treated as before ignoredNode (thus node() is really after the position, not containing it). Node* deprecatedNode() const { return m_anchorNode.get(); } - Document* document() const { return m_anchorNode ? &m_anchorNode->document() : 0; } + Document* document() const { return m_anchorNode ? &m_anchorNode->document() : nullptr; } Element* rootEditableElement() const { Node* container = containerNode(); - return container ? container->rootEditableElement() : 0; + return container ? container->rootEditableElement() : nullptr; } // These should only be used for PositionIsOffsetInAnchor positions, unless // the position is a legacy editing position. - void moveToPosition(PassRefPtr<Node> anchorNode, int offset); + void moveToPosition(Node* anchorNode, int offset); void moveToOffset(int offset); bool isNull() const { return !m_anchorNode; } bool isNotNull() const { return m_anchorNode; } - bool isOrphan() const { return m_anchorNode && !m_anchorNode->inDocument(); } + bool isOrphan() const { return m_anchorNode && !m_anchorNode->isConnected(); } Element* element() const; // Move up or down the DOM by one position. // Offsets are computed using render text for nodes that have renderers - but note that even when // using composed characters, the result may be inside a single user-visible character if a ligature is formed. - Position previous(PositionMoveType = CodePoint) const; - Position next(PositionMoveType = CodePoint) const; + WEBCORE_EXPORT Position previous(PositionMoveType = CodePoint) const; + WEBCORE_EXPORT Position next(PositionMoveType = CodePoint) const; static int uncheckedPreviousOffset(const Node*, int current); static int uncheckedPreviousOffsetForBackwardDeletion(const Node*, int current); static int uncheckedNextOffset(const Node*, int current); @@ -170,12 +154,12 @@ public: // FIXME: Make these non-member functions and put them somewhere in the editing directory. // These aren't really basic "position" operations. More high level editing helper functions. - Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const; - Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const; + WEBCORE_EXPORT Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const; + WEBCORE_EXPORT Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const; // These return useful visually equivalent positions. - Position upstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const; - Position downstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const; + WEBCORE_EXPORT Position upstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const; + WEBCORE_EXPORT Position downstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const; bool isCandidate() const; bool isRenderedCharacter() const; @@ -186,6 +170,9 @@ public: TextDirection primaryDirection() const; + // Returns the number of positions that exist between two positions. + static unsigned positionCountBetweenPositions(const Position&, const Position&); + static bool hasRenderedNonAnonymousDescendantsWithHeight(const RenderElement&); static bool nodeIsUserSelectNone(Node*); #if ENABLE(USERSELECT_ALL) @@ -195,18 +182,26 @@ public: static bool nodeIsUserSelectAll(const Node*) { return false; } static Node* rootUserSelectAllForNode(Node*) { return 0; } #endif - static ContainerNode* findParent(const Node*); - + void debugPosition(const char* msg = "") const; -#ifndef NDEBUG +#if ENABLE(TREE_DEBUGGING) void formatForDebugger(char* buffer, unsigned length) const; void showAnchorTypeAndOffset() const; void showTreeForThis() const; #endif - + + // This is a tentative enhancement of operator== to account for different position types. + // FIXME: Combine this function with operator== + bool equals(const Position&) const; + private: - int offsetForPositionAfterAnchor() const; + // For creating legacy editing positions: (Anchor type will be determined from editingIgnoresContent(node)) + enum class LegacyEditingPositionFlag { On }; + WEBCORE_EXPORT Position(Node* anchorNode, unsigned offset, LegacyEditingPositionFlag); + friend Position createLegacyEditingPosition(Node*, unsigned offset); + + WEBCORE_EXPORT int offsetForPositionAfterAnchor() const; Position previousCharacterPosition(EAffinity) const; Position nextCharacterPosition(EAffinity) const; @@ -217,14 +212,14 @@ private: // m_offset can be the offset inside m_anchorNode, or if editingIgnoresContent(m_anchorNode) // returns true, then other places in editing will treat m_offset == 0 as "before the anchor" // and m_offset > 0 as "after the anchor node". See parentAnchoredEquivalent for more info. - int m_offset; + int m_offset { 0 }; unsigned m_anchorType : 3; bool m_isLegacyEditingPosition : 1; }; -inline Position createLegacyEditingPosition(PassRefPtr<Node> node, int offset) +inline Position createLegacyEditingPosition(Node* node, unsigned offset) { - return Position(node, Position::LegacyEditingOffset(offset)); + return { node, offset, Position::LegacyEditingPositionFlag::On }; } inline bool operator==(const Position& a, const Position& b) @@ -245,7 +240,7 @@ inline bool operator<(const Position& a, const Position& b) return false; if (a.anchorNode() == b.anchorNode()) return a.deprecatedEditingOffset() < b.deprecatedEditingOffset(); - return b.anchorNode()->compareDocumentPosition(a.anchorNode()) == Node::DOCUMENT_POSITION_PRECEDING; + return b.anchorNode()->compareDocumentPosition(*a.anchorNode()) == Node::DOCUMENT_POSITION_PRECEDING; } inline bool operator>(const Position& a, const Position& b) @@ -263,23 +258,16 @@ inline bool operator<=(const Position& a, const Position& b) return !a.isNull() && !b.isNull() && (a == b || a < b); } -// We define position creation functions to make callsites more readable. -// These are inline to prevent ref-churn when returning a Position object. -// If we ever add a PassPosition we can make these non-inline. - inline Position positionInParentBeforeNode(const Node* node) { - // FIXME: This should ASSERT(node->parentNode()) - // At least one caller currently hits this ASSERT though, which indicates - // that the caller is trying to make a position relative to a disconnected node (which is likely an error) - // Specifically, editing/deleting/delete-ligature-001.html crashes with ASSERT(node->parentNode()) - return Position(Position::findParent(node), node->nodeIndex(), Position::PositionIsOffsetInAnchor); + ASSERT(node->parentNode()); + return Position(node->parentNode(), node->computeNodeIndex(), Position::PositionIsOffsetInAnchor); } inline Position positionInParentAfterNode(const Node* node) { - ASSERT(Position::findParent(node)); - return Position(Position::findParent(node), node->nodeIndex() + 1, Position::PositionIsOffsetInAnchor); + ASSERT(node->parentNode()); + return Position(node->parentNode(), node->computeNodeIndex() + 1, Position::PositionIsOffsetInAnchor); } // positionBeforeNode and positionAfterNode return neighbor-anchored positions, construction is O(1) @@ -297,10 +285,10 @@ inline Position positionAfterNode(Node* anchorNode) inline int lastOffsetInNode(Node* node) { - return node->offsetInCharacters() ? node->maxCharacterOffset() : static_cast<int>(node->childNodeCount()); + return node->offsetInCharacters() ? node->maxCharacterOffset() : static_cast<int>(node->countChildNodes()); } -// firstPositionInNode and lastPositionInNode return parent-anchored positions, lastPositionInNode construction is O(n) due to childNodeCount() +// firstPositionInNode and lastPositionInNode return parent-anchored positions, lastPositionInNode construction is O(n) due to countChildNodes() inline Position firstPositionInNode(Node* anchorNode) { if (anchorNode->isTextNode()) @@ -340,12 +328,12 @@ inline bool offsetIsBeforeLastNodeOffset(int offset, Node* anchorNode) return offset < currentOffset; } +TextStream& operator<<(TextStream&, const Position&); + } // namespace WebCore -#ifndef NDEBUG -// Outside the WebCore namespace for ease of invocation from gdb. +#if ENABLE(TREE_DEBUGGING) +// Outside the WebCore namespace for ease of invocation from the debugger. void showTree(const WebCore::Position&); void showTree(const WebCore::Position*); #endif - -#endif // Position_h |