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

Side by Side Diff: Source/core/dom/NodeTraversal.cpp

Issue 307243004: Avoid calling slower Node::firstChild() / Node::lastChild() when possible (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase Created 6 years, 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/dom/Node.cpp ('k') | Source/core/dom/Range.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
6 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) 6 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 descendant = child; 103 descendant = child;
104 return descendant; 104 return descendant;
105 } 105 }
106 106
107 Node* NodeTraversal::previous(const Node& current, const Node* stayWithin) 107 Node* NodeTraversal::previous(const Node& current, const Node* stayWithin)
108 { 108 {
109 if (current == stayWithin) 109 if (current == stayWithin)
110 return 0; 110 return 0;
111 if (current.previousSibling()) { 111 if (current.previousSibling()) {
112 Node* previous = current.previousSibling(); 112 Node* previous = current.previousSibling();
113 while (previous->lastChild()) 113 while (Node* child = previous->lastChild())
114 previous = previous->lastChild(); 114 previous = child;
115 return previous; 115 return previous;
116 } 116 }
117 return current.parentNode(); 117 return current.parentNode();
118 } 118 }
119 119
120 Node* NodeTraversal::previousSkippingChildren(const Node& current, const Node* s tayWithin) 120 Node* NodeTraversal::previousSkippingChildren(const Node& current, const Node* s tayWithin)
121 { 121 {
122 if (current == stayWithin) 122 if (current == stayWithin)
123 return 0; 123 return 0;
124 if (current.previousSibling()) 124 if (current.previousSibling())
125 return current.previousSibling(); 125 return current.previousSibling();
126 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) { 126 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) {
127 if (parent == stayWithin) 127 if (parent == stayWithin)
128 return 0; 128 return 0;
129 if (parent->previousSibling()) 129 if (parent->previousSibling())
130 return parent->previousSibling(); 130 return parent->previousSibling();
131 } 131 }
132 return 0; 132 return 0;
133 } 133 }
134 134
135 Node* NodeTraversal::nextPostOrder(const Node& current, const Node* stayWithin) 135 Node* NodeTraversal::nextPostOrder(const Node& current, const Node* stayWithin)
136 { 136 {
137 if (current == stayWithin) 137 if (current == stayWithin)
138 return 0; 138 return 0;
139 if (!current.nextSibling()) 139 if (!current.nextSibling())
140 return current.parentNode(); 140 return current.parentNode();
141 Node* next = current.nextSibling(); 141 Node* next = current.nextSibling();
142 while (next->firstChild()) 142 while (Node* child = next->firstChild())
143 next = next->firstChild(); 143 next = child;
144 return next; 144 return next;
145 } 145 }
146 146
147 static Node* previousAncestorSiblingPostOrder(const Node& current, const Node* s tayWithin) 147 static Node* previousAncestorSiblingPostOrder(const Node& current, const Node* s tayWithin)
148 { 148 {
149 ASSERT(!current.previousSibling()); 149 ASSERT(!current.previousSibling());
150 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) { 150 for (Node* parent = current.parentNode(); parent; parent = parent->parentNod e()) {
151 if (parent == stayWithin) 151 if (parent == stayWithin)
152 return 0; 152 return 0;
153 if (parent->previousSibling()) 153 if (parent->previousSibling())
154 return parent->previousSibling(); 154 return parent->previousSibling();
155 } 155 }
156 return 0; 156 return 0;
157 } 157 }
158 158
159 Node* NodeTraversal::previousPostOrder(const Node& current, const Node* stayWith in) 159 Node* NodeTraversal::previousPostOrder(const Node& current, const Node* stayWith in)
160 { 160 {
161 if (current.lastChild()) 161 if (Node* lastChild = current.lastChild())
162 return current.lastChild(); 162 return lastChild;
163 if (current == stayWithin) 163 if (current == stayWithin)
164 return 0; 164 return 0;
165 if (current.previousSibling()) 165 if (current.previousSibling())
166 return current.previousSibling(); 166 return current.previousSibling();
167 return previousAncestorSiblingPostOrder(current, stayWithin); 167 return previousAncestorSiblingPostOrder(current, stayWithin);
168 } 168 }
169 169
170 } // namespace WebCore 170 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/dom/Node.cpp ('k') | Source/core/dom/Range.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698