Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: third_party/WebKit/Source/core/editing/EphemeralRange.cpp

Issue 1409073004: Return EphemeralRange from Editor::findRangeOfString. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use assertion for nodes common ancestor Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/editing/EphemeralRange.h" 6 #include "core/editing/EphemeralRange.h"
7 7
8 #include "core/dom/Document.h" 8 #include "core/dom/Document.h"
9 #include "core/dom/Element.h" 9 #include "core/dom/Element.h"
10 #include "core/dom/Range.h" 10 #include "core/dom/Range.h"
11 #include "core/dom/Text.h" 11 #include "core/dom/Text.h"
12 #include "core/layout/LayoutText.h"
12 13
13 namespace blink { 14 namespace blink {
14 15
15 template <typename Strategy> 16 template <typename Strategy>
16 EphemeralRangeTemplate<Strategy>::EphemeralRangeTemplate(const PositionTemplate< Strategy>& start, const PositionTemplate<Strategy>& end) 17 EphemeralRangeTemplate<Strategy>::EphemeralRangeTemplate(const PositionTemplate< Strategy>& start, const PositionTemplate<Strategy>& end)
17 : m_startPosition(start) 18 : m_startPosition(start)
18 , m_endPosition(end) 19 , m_endPosition(end)
19 #if ENABLE(ASSERT) 20 #if ENABLE(ASSERT)
20 , m_domTreeVersion(start.isNull() ? 0 : start.document()->domTreeVersion()) 21 , m_domTreeVersion(start.isNull() ? 0 : start.document()->domTreeVersion())
21 #endif 22 #endif
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 ASSERT(isValid()); 117 ASSERT(isValid());
117 return m_startPosition == m_endPosition; 118 return m_startPosition == m_endPosition;
118 } 119 }
119 120
120 template <typename Strategy> 121 template <typename Strategy>
121 EphemeralRangeTemplate<Strategy> EphemeralRangeTemplate<Strategy>::rangeOfConten ts(const Node& node) 122 EphemeralRangeTemplate<Strategy> EphemeralRangeTemplate<Strategy>::rangeOfConten ts(const Node& node)
122 { 123 {
123 return EphemeralRangeTemplate<Strategy>(PositionTemplate<Strategy>::firstPos itionInNode(&const_cast<Node&>(node)), PositionTemplate<Strategy>::lastPositionI nNode(&const_cast<Node&>(node))); 124 return EphemeralRangeTemplate<Strategy>(PositionTemplate<Strategy>::firstPos itionInNode(&const_cast<Node&>(node)), PositionTemplate<Strategy>::lastPositionI nNode(&const_cast<Node&>(node)));
124 } 125 }
125 126
127 static IntRect uniteRects(const Vector<IntRect>& rects)
128 {
129 IntRect result;
130 for (const auto& rect : rects)
131 result.unite(rect);
132 return result;
133 }
134
135 template <typename Strategy>
136 IntRect EphemeralRangeTemplate<Strategy>::textBoundingBox() const
yosin_UTC9 2015/11/02 03:44:17 Rather than copying code from Range::textRects(),
Andrey Kraynov 2015/11/02 14:06:57 I tried to do it. Pros: it can be used to shorten
137 {
138 if (isCollapsed())
139 return IntRect();
140
141 Node* const startContainer = m_startPosition.computeContainerNode();
142 ASSERT(startContainer);
143 Node* const endContainer = m_endPosition.computeContainerNode();
144 ASSERT(endContainer);
145
146 IntRect result;
147 Vector<IntRect> rects;
148 Node* stopNode = m_endPosition.nodeAsRangePastLastNode();
149 for (Node* node = m_startPosition.nodeAsRangeFirstNode(); node != stopNode; node = Strategy::next(*node)) {
150 LayoutObject* const layoutObject = node->layoutObject();
151 if (!layoutObject || !layoutObject->isText())
152 continue;
153
154 const int startOffset = node == startContainer ? m_startPosition.compute OffsetInContainerNode() : 0;
155 const int endOffset = node == endContainer ? m_endPosition.computeOffset InContainerNode() : std::numeric_limits<int>::max();
156 toLayoutText(layoutObject)->absoluteRectsForRange(rects, startOffset, en dOffset);
157
158 result.unite(uniteRects(rects));
159 rects.clear();
160 }
161
162 return result;
163 }
164
126 #if ENABLE(ASSERT) 165 #if ENABLE(ASSERT)
127 template <typename Strategy> 166 template <typename Strategy>
128 bool EphemeralRangeTemplate<Strategy>::isValid() const 167 bool EphemeralRangeTemplate<Strategy>::isValid() const
129 { 168 {
130 return m_startPosition.isNull() || m_domTreeVersion == m_startPosition.docum ent()->domTreeVersion(); 169 return m_startPosition.isNull() || m_domTreeVersion == m_startPosition.docum ent()->domTreeVersion();
131 } 170 }
132 #else 171 #else
133 template <typename Strategy> 172 template <typename Strategy>
134 bool EphemeralRangeTemplate<Strategy>::isValid() const 173 bool EphemeralRangeTemplate<Strategy>::isValid() const
135 { 174 {
136 return true; 175 return true;
137 } 176 }
138 #endif 177 #endif
139 178
140 PassRefPtrWillBeRawPtr<Range> createRange(const EphemeralRange& range) 179 PassRefPtrWillBeRawPtr<Range> createRange(const EphemeralRange& range)
141 { 180 {
142 if (range.isNull()) 181 if (range.isNull())
143 return nullptr; 182 return nullptr;
144 return Range::create(range.document(), range.startPosition(), range.endPosit ion()); 183 return Range::create(range.document(), range.startPosition(), range.endPosit ion());
145 } 184 }
146 185
147 template class CORE_TEMPLATE_EXPORT EphemeralRangeTemplate<EditingStrategy>; 186 template class CORE_TEMPLATE_EXPORT EphemeralRangeTemplate<EditingStrategy>;
148 template class CORE_TEMPLATE_EXPORT EphemeralRangeTemplate<EditingInComposedTree Strategy>; 187 template class CORE_TEMPLATE_EXPORT EphemeralRangeTemplate<EditingInComposedTree Strategy>;
149 188
150 } // namespace blink 189 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698