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

Unified Diff: Source/core/editing/TextIterator.cpp

Issue 171953002: Get rid of inefficient uses of childNodeCount() (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Use lastChild() Created 6 years, 10 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/editing/ReplaceSelectionCommand.cpp ('k') | Source/core/editing/htmlediting.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/editing/TextIterator.cpp
diff --git a/Source/core/editing/TextIterator.cpp b/Source/core/editing/TextIterator.cpp
index b85b148eaf9608052ac88662b451df18dd732357..a406ad8cc5ad2bfd340e3531adcc16bcab8583fa 100644
--- a/Source/core/editing/TextIterator.cpp
+++ b/Source/core/editing/TextIterator.cpp
@@ -1183,15 +1183,19 @@ SimplifiedBackwardsTextIterator::SimplifiedBackwardsTextIterator(const Range* r,
int startOffset = r->startOffset();
int endOffset = r->endOffset();
- if (!startNode->offsetInCharacters()) {
- if (startOffset >= 0 && startOffset < static_cast<int>(startNode->childNodeCount())) {
- startNode = startNode->childNode(startOffset);
+ if (!startNode->offsetInCharacters() && startOffset >= 0) {
+ // childNode() will return 0 if the offset is out of range. We rely on this behavior
+ // instead of calling childNodeCount() to avoid traversing the children twice.
+ if (Node* childAtOffset = startNode->childNode(startOffset)) {
+ startNode = childAtOffset;
startOffset = 0;
}
}
- if (!endNode->offsetInCharacters()) {
- if (endOffset > 0 && endOffset <= static_cast<int>(endNode->childNodeCount())) {
- endNode = endNode->childNode(endOffset - 1);
+ if (!endNode->offsetInCharacters() && endOffset > 0) {
+ // childNode() will return 0 if the offset is out of range. We rely on this behavior
+ // instead of calling childNodeCount() to avoid traversing the children twice.
+ if (Node* childAtOffset = endNode->childNode(endOffset - 1)) {
+ endNode = childAtOffset;
endOffset = lastOffsetInNode(endNode);
}
}
« no previous file with comments | « Source/core/editing/ReplaceSelectionCommand.cpp ('k') | Source/core/editing/htmlediting.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698