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

Side by Side Diff: Source/core/html/HTMLCollection.cpp

Issue 406843002: Optimize hasTagName when called on an HTMLElement / SVGElement (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 5 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/editing/markup.cpp ('k') | Source/core/html/HTMLElement.h » ('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 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All r ights reserved. 4 * Copyright (C) 2003-2008, 2011, 2012, 2014 Apple Inc. All rights reserved.
5 * Copyright (C) 2014 Samsung Electronics. All rights reserved. 5 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public 8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 ownerNode().nodeLists()->removeCache(this, type()); 184 ownerNode().nodeLists()->removeCache(this, type());
185 #endif 185 #endif
186 } 186 }
187 187
188 void HTMLCollection::invalidateCache(Document* oldDocument) const 188 void HTMLCollection::invalidateCache(Document* oldDocument) const
189 { 189 {
190 m_collectionIndexCache.invalidate(); 190 m_collectionIndexCache.invalidate();
191 invalidateIdNameCacheMaps(oldDocument); 191 invalidateIdNameCacheMaps(oldDocument);
192 } 192 }
193 193
194 template <class NodeListType> 194 static inline bool isMatchingHTMLElement(const HTMLCollection& htmlCollection, c onst HTMLElement& element)
195 inline bool isMatchingElement(const NodeListType&, const Element&);
196
197 template <> inline bool isMatchingElement(const HTMLCollection& htmlCollection, const Element& element)
198 { 195 {
199 CollectionType type = htmlCollection.type(); 196 switch (htmlCollection.type()) {
200
201 // These collections apply to any kind of Elements, not just HTMLElements.
202 switch (type) {
203 case DocAll:
204 case NodeChildren:
205 return true;
206 case ClassCollectionType:
207 return toClassCollection(htmlCollection).elementMatches(element);
208 case TagCollectionType:
209 return toTagCollection(htmlCollection).elementMatches(element);
210 case HTMLTagCollectionType:
211 return toHTMLTagCollection(htmlCollection).elementMatches(element);
212 case DocumentNamedItems:
213 return toDocumentNameCollection(htmlCollection).elementMatches(element);
214 case WindowNamedItems:
215 return toWindowNameCollection(htmlCollection).elementMatches(element);
216 default:
217 break;
218 }
219
220 // The following only applies to HTMLElements.
221 if (!element.isHTMLElement())
222 return false;
223
224 switch (type) {
225 case DocImages: 197 case DocImages:
226 return element.hasLocalName(imgTag); 198 return element.hasTagName(imgTag);
227 case DocScripts: 199 case DocScripts:
228 return element.hasLocalName(scriptTag); 200 return element.hasTagName(scriptTag);
229 case DocForms: 201 case DocForms:
230 return element.hasLocalName(formTag); 202 return element.hasTagName(formTag);
231 case TableTBodies: 203 case TableTBodies:
232 return element.hasLocalName(tbodyTag); 204 return element.hasTagName(tbodyTag);
233 case TRCells: 205 case TRCells:
234 return element.hasLocalName(tdTag) || element.hasLocalName(thTag); 206 return element.hasTagName(tdTag) || element.hasTagName(thTag);
235 case TSectionRows: 207 case TSectionRows:
236 return element.hasLocalName(trTag); 208 return element.hasTagName(trTag);
237 case SelectOptions: 209 case SelectOptions:
238 return element.hasLocalName(optionTag); 210 return element.hasTagName(optionTag);
239 case SelectedOptions: 211 case SelectedOptions:
240 return element.hasLocalName(optionTag) && toHTMLOptionElement(element).s elected(); 212 return isHTMLOptionElement(element) && toHTMLOptionElement(element).sele cted();
241 case DataListOptions: 213 case DataListOptions:
242 if (element.hasLocalName(optionTag)) { 214 if (isHTMLOptionElement(element)) {
243 const HTMLOptionElement& option = toHTMLOptionElement(element); 215 const HTMLOptionElement& option = toHTMLOptionElement(element);
244 if (!option.isDisabledFormControl() && !option.value().isEmpty()) 216 if (!option.isDisabledFormControl() && !option.value().isEmpty())
245 return true; 217 return true;
246 } 218 }
247 return false; 219 return false;
248 case MapAreas: 220 case MapAreas:
249 return element.hasLocalName(areaTag); 221 return element.hasTagName(areaTag);
250 case DocApplets: 222 case DocApplets:
251 return element.hasLocalName(appletTag) || (element.hasLocalName(objectTa g) && toHTMLObjectElement(element).containsJavaApplet()); 223 return element.hasTagName(appletTag) || (isHTMLObjectElement(element) && toHTMLObjectElement(element).containsJavaApplet());
252 case DocEmbeds: 224 case DocEmbeds:
253 return element.hasLocalName(embedTag); 225 return element.hasTagName(embedTag);
254 case DocLinks: 226 case DocLinks:
255 return (element.hasLocalName(aTag) || element.hasLocalName(areaTag)) && element.fastHasAttribute(hrefAttr); 227 return (element.hasTagName(aTag) || element.hasTagName(areaTag)) && elem ent.fastHasAttribute(hrefAttr);
256 case DocAnchors: 228 case DocAnchors:
257 return element.hasLocalName(aTag) && element.fastHasAttribute(nameAttr); 229 return element.hasTagName(aTag) && element.fastHasAttribute(nameAttr);
258 case ClassCollectionType: 230 case ClassCollectionType:
259 case TagCollectionType: 231 case TagCollectionType:
260 case HTMLTagCollectionType: 232 case HTMLTagCollectionType:
261 case DocAll: 233 case DocAll:
262 case NodeChildren: 234 case NodeChildren:
263 case FormControls: 235 case FormControls:
264 case DocumentNamedItems: 236 case DocumentNamedItems:
265 case TableRows: 237 case TableRows:
266 case WindowNamedItems: 238 case WindowNamedItems:
267 case NameNodeListType: 239 case NameNodeListType:
268 case RadioNodeListType: 240 case RadioNodeListType:
269 case RadioImgNodeListType: 241 case RadioImgNodeListType:
270 case LabelsNodeListType: 242 case LabelsNodeListType:
271 ASSERT_NOT_REACHED(); 243 ASSERT_NOT_REACHED();
272 } 244 }
273 return false; 245 return false;
274 } 246 }
275 247
248 template <class NodeListType>
249 inline bool isMatchingElement(const NodeListType&, const Element&);
250
251 template <> inline bool isMatchingElement(const HTMLCollection& htmlCollection, const Element& element)
252 {
253 // These collections apply to any kind of Elements, not just HTMLElements.
254 switch (htmlCollection.type()) {
255 case DocAll:
256 case NodeChildren:
257 return true;
258 case ClassCollectionType:
259 return toClassCollection(htmlCollection).elementMatches(element);
260 case TagCollectionType:
261 return toTagCollection(htmlCollection).elementMatches(element);
262 case HTMLTagCollectionType:
263 return toHTMLTagCollection(htmlCollection).elementMatches(element);
264 case DocumentNamedItems:
265 return toDocumentNameCollection(htmlCollection).elementMatches(element);
266 case WindowNamedItems:
267 return toWindowNameCollection(htmlCollection).elementMatches(element);
268 default:
269 break;
270 }
271
272 // The following only applies to HTMLElements.
273 return element.isHTMLElement() && isMatchingHTMLElement(htmlCollection, toHT MLElement(element));
274 }
275
276 template <> inline bool isMatchingElement(const ClassCollection& collection, con st Element& element) 276 template <> inline bool isMatchingElement(const ClassCollection& collection, con st Element& element)
277 { 277 {
278 return collection.elementMatches(element); 278 return collection.elementMatches(element);
279 } 279 }
280 280
281 template <> inline bool isMatchingElement(const HTMLTagCollection& collection, c onst Element& element) 281 template <> inline bool isMatchingElement(const HTMLTagCollection& collection, c onst Element& element)
282 { 282 {
283 return collection.elementMatches(element); 283 return collection.elementMatches(element);
284 } 284 }
285 285
286 Element* HTMLCollection::virtualItemAfter(Element*) const 286 Element* HTMLCollection::virtualItemAfter(Element*) const
287 { 287 {
288 ASSERT_NOT_REACHED(); 288 ASSERT_NOT_REACHED();
289 return 0; 289 return 0;
290 } 290 }
291 291
292 static inline bool nameShouldBeVisibleInDocumentAll(const HTMLElement& element) 292 static inline bool nameShouldBeVisibleInDocumentAll(const HTMLElement& element)
293 { 293 {
294 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-in terfaces.html#dom-htmlallcollection-nameditem: 294 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-in terfaces.html#dom-htmlallcollection-nameditem:
295 // The document.all collection returns only certain types of elements by nam e, 295 // The document.all collection returns only certain types of elements by nam e,
296 // although it returns any type of element by id. 296 // although it returns any type of element by id.
297 return element.hasLocalName(aTag) 297 return element.hasTagName(aTag)
298 || element.hasLocalName(appletTag) 298 || element.hasTagName(appletTag)
299 || element.hasLocalName(areaTag) 299 || element.hasTagName(areaTag)
300 || element.hasLocalName(embedTag) 300 || element.hasTagName(embedTag)
301 || element.hasLocalName(formTag) 301 || element.hasTagName(formTag)
302 || element.hasLocalName(frameTag) 302 || element.hasTagName(frameTag)
303 || element.hasLocalName(framesetTag) 303 || element.hasTagName(framesetTag)
304 || element.hasLocalName(iframeTag) 304 || element.hasTagName(iframeTag)
305 || element.hasLocalName(imgTag) 305 || element.hasTagName(imgTag)
306 || element.hasLocalName(inputTag) 306 || element.hasTagName(inputTag)
307 || element.hasLocalName(objectTag) 307 || element.hasTagName(objectTag)
308 || element.hasLocalName(selectTag); 308 || element.hasTagName(selectTag);
309 } 309 }
310 310
311 inline Element* firstMatchingChildElement(const HTMLCollection& nodeList) 311 inline Element* firstMatchingChildElement(const HTMLCollection& nodeList)
312 { 312 {
313 Element* element = ElementTraversal::firstChild(nodeList.rootNode()); 313 Element* element = ElementTraversal::firstChild(nodeList.rootNode());
314 while (element && !isMatchingElement(nodeList, *element)) 314 while (element && !isMatchingElement(nodeList, *element))
315 element = ElementTraversal::nextSibling(*element); 315 element = ElementTraversal::nextSibling(*element);
316 return element; 316 return element;
317 } 317 }
318 318
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 } 515 }
516 516
517 void HTMLCollection::trace(Visitor* visitor) 517 void HTMLCollection::trace(Visitor* visitor)
518 { 518 {
519 visitor->trace(m_namedItemCache); 519 visitor->trace(m_namedItemCache);
520 visitor->trace(m_collectionIndexCache); 520 visitor->trace(m_collectionIndexCache);
521 LiveNodeListBase::trace(visitor); 521 LiveNodeListBase::trace(visitor);
522 } 522 }
523 523
524 } // namespace blink 524 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/editing/markup.cpp ('k') | Source/core/html/HTMLElement.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698