| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #define PositionIterator_h | 27 #define PositionIterator_h |
| 28 | 28 |
| 29 #include "core/dom/Node.h" | 29 #include "core/dom/Node.h" |
| 30 #include "core/editing/EditingStrategy.h" | 30 #include "core/editing/EditingStrategy.h" |
| 31 #include "core/editing/EditingUtilities.h" | 31 #include "core/editing/EditingUtilities.h" |
| 32 #include "core/html/HTMLHtmlElement.h" | 32 #include "core/html/HTMLHtmlElement.h" |
| 33 #include "core/layout/LayoutBlock.h" | 33 #include "core/layout/LayoutBlock.h" |
| 34 | 34 |
| 35 namespace blink { | 35 namespace blink { |
| 36 | 36 |
| 37 // A Position iterator with constant-time | 37 // A Position iterator with nearly constant-time |
| 38 // increment, decrement, and several predicates on the Position it is at. | 38 // increment, decrement, and several predicates on the Position it is at. |
| 39 // Conversion to/from Position is O(n) in the offset. | 39 // Conversion from Position is O(n) in the depth. |
| 40 // Conversion to Position is O(1). |
| 41 // PositionIteratorAlgorithm must be used without DOM tree change. |
| 40 template <typename Strategy> | 42 template <typename Strategy> |
| 41 class PositionIteratorAlgorithm { | 43 class PositionIteratorAlgorithm { |
| 42 STACK_ALLOCATED(); | 44 STACK_ALLOCATED(); |
| 43 public: | 45 public: |
| 44 explicit PositionIteratorAlgorithm(const PositionTemplate<Strategy>&); | 46 explicit PositionIteratorAlgorithm(const PositionTemplate<Strategy>&); |
| 45 PositionIteratorAlgorithm(); | 47 PositionIteratorAlgorithm(); |
| 46 | 48 |
| 47 // Since |deprecatedComputePosition()| is slow, new code should use | 49 // Since |deprecatedComputePosition()| is slow, new code should use |
| 48 // |computePosition()| instead. | 50 // |computePosition()| instead. |
| 49 PositionTemplate<Strategy> deprecatedComputePosition() const; | 51 PositionTemplate<Strategy> deprecatedComputePosition() const; |
| 50 PositionTemplate<Strategy> computePosition() const; | 52 PositionTemplate<Strategy> computePosition() const; |
| 51 | 53 |
| 54 // increment() takes O(1) other than incrementing to a element that has |
| 55 // new parent. |
| 56 // In the later case, it takes time of O(<number of childlen>) but the case |
| 57 // happens at most depth-of-the-tree times over whole tree traversal. |
| 52 void increment(); | 58 void increment(); |
| 59 // decrement() takes O(1) other than decrement into new node that has |
| 60 // childlen. |
| 61 // In the later case, it takes time of O(<number of childlen>). |
| 53 void decrement(); | 62 void decrement(); |
| 54 | 63 |
| 55 Node* node() const { return m_anchorNode; } | 64 Node* node() const { return m_anchorNode; } |
| 56 int offsetInLeafNode() const { return m_offsetInAnchor; } | 65 int offsetInLeafNode() const { return m_offsetInAnchor; } |
| 57 | 66 |
| 58 bool atStart() const; | 67 bool atStart() const; |
| 59 bool atEnd() const; | 68 bool atEnd() const; |
| 60 bool atStartOfNode() const; | 69 bool atStartOfNode() const; |
| 61 bool atEndOfNode() const; | 70 bool atEndOfNode() const; |
| 62 | 71 |
| 63 private: | 72 private: |
| 64 PositionIteratorAlgorithm(Node* anchorNode, int offsetInAnchorNode); | 73 PositionIteratorAlgorithm(Node* anchorNode, int offsetInAnchorNode); |
| 65 | 74 |
| 75 bool isValid() const { return !m_anchorNode || m_domTreeVersion == m_anchorN
ode->document().domTreeVersion(); } |
| 76 |
| 66 RawPtrWillBeMember<Node> m_anchorNode; | 77 RawPtrWillBeMember<Node> m_anchorNode; |
| 67 RawPtrWillBeMember<Node> m_nodeAfterPositionInAnchor; // If this is non-null
, Strategy::parent(*m_nodeAfterPositionInAnchor) == m_anchorNode; | 78 RawPtrWillBeMember<Node> m_nodeAfterPositionInAnchor; // If this is non-null
, Strategy::parent(*m_nodeAfterPositionInAnchor) == m_anchorNode; |
| 68 int m_offsetInAnchor; | 79 int m_offsetInAnchor; |
| 80 size_t m_depthToAnchorNode; |
| 81 // If |m_nodeAfterPositionInAnchor| is not null, |
| 82 // m_offsetsInAnchorNode[m_depthToAnchorNode] == |
| 83 // Strategy::index(m_nodeAfterPositionInAnchor). |
| 84 Vector<int> m_offsetsInAnchorNode; |
| 85 uint64_t m_domTreeVersion; |
| 69 }; | 86 }; |
| 70 | 87 |
| 71 extern template class PositionIteratorAlgorithm<EditingStrategy>; | 88 extern template class PositionIteratorAlgorithm<EditingStrategy>; |
| 72 extern template class PositionIteratorAlgorithm<EditingInComposedTreeStrategy>; | 89 extern template class PositionIteratorAlgorithm<EditingInComposedTreeStrategy>; |
| 73 | 90 |
| 74 using PositionIterator = PositionIteratorAlgorithm<EditingStrategy>; | 91 using PositionIterator = PositionIteratorAlgorithm<EditingStrategy>; |
| 75 using PositionIteratorInComposedTree = PositionIteratorAlgorithm<EditingInCompos
edTreeStrategy>; | 92 using PositionIteratorInComposedTree = PositionIteratorAlgorithm<EditingInCompos
edTreeStrategy>; |
| 76 | 93 |
| 77 } // namespace blink | 94 } // namespace blink |
| 78 | 95 |
| 79 #endif // PositionIterator_h | 96 #endif // PositionIterator_h |
| OLD | NEW |