Changeset 154769 in webkit
- Timestamp:
- Aug 28, 2013, 12:43:51 PM (12 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r154767 r154769 1 2013-08-28 Antti Koivisto <antti@apple.com> 2 3 Add child and descendant const iterators 4 https://bugs.webkit.org/show_bug.cgi?id=120430 5 6 Reviewed by Andreas Kling 7 8 This patch adds const-correct DOM tree traversal iterators. It also uses them in a few places. 9 10 Some const_casts have been applied where constness breaks. 11 12 * dom/ChildIterator.h: 13 (WebCore::::ChildConstIterator): 14 (WebCore::::operator): 15 (WebCore::=): 16 (WebCore::::ChildConstIteratorAdapter): 17 (WebCore::::begin): 18 (WebCore::::end): 19 (WebCore::elementChildren): 20 (WebCore::childrenOfType): 21 * dom/DescendantIterator.h: 22 (WebCore::::DescendantConstIterator): 23 (WebCore::::operator): 24 (WebCore::=): 25 (WebCore::::DescendantConstIteratorAdapter): 26 (WebCore::::begin): 27 (WebCore::::end): 28 (WebCore::elementDescendants): 29 (WebCore::descendantsOfType): 30 * dom/Node.cpp: 31 (WebCore::Node::numberOfScopedHTMLStyleChildren): 32 * html/HTMLFieldSetElement.cpp: 33 (WebCore::HTMLFieldSetElement::legend): 34 * html/HTMLFieldSetElement.h: 35 * html/HTMLMediaElement.cpp: 36 (WebCore::HTMLMediaElement::finishParsingChildren): 37 * html/HTMLObjectElement.cpp: 38 (WebCore::HTMLObjectElement::containsJavaApplet): 39 * svg/SVGElement.cpp: 40 (WebCore::SVGElement::title): 41 * svg/SVGSVGElement.cpp: 42 (WebCore::SVGSVGElement::collectIntersectionOrEnclosureList): 43 (WebCore::SVGSVGElement::checkIntersection): 44 (WebCore::SVGSVGElement::checkEnclosure): 45 (WebCore::SVGSVGElement::getElementById): 46 * svg/SVGSVGElement.h: 47 1 48 2013-08-28 Lukasz Gajowy <l.gajowy@samsung.com> 2 49 -
trunk/Source/WebCore/dom/ChildIterator.h
r154751 r154769 54 54 55 55 template <typename ElementType> 56 class ChildConstIterator { 57 public: 58 ChildConstIterator(); 59 ChildConstIterator(const ElementType* current); 60 ChildConstIterator& operator++(); 61 const ElementType& operator*() const; 62 const ElementType* operator->() const; 63 bool operator!=(const ChildConstIterator& other) const; 64 65 private: 66 const ElementType* m_current; 67 68 #if !ASSERT_DISABLED 69 DescendantIteratorAssertions m_assertions; 70 #endif 71 }; 72 73 template <typename ElementType> 56 74 class ChildIteratorAdapter { 57 75 public: … … 64 82 }; 65 83 84 template <typename ElementType> 85 class ChildConstIteratorAdapter { 86 public: 87 ChildConstIteratorAdapter(const ContainerNode* root); 88 ChildConstIterator<ElementType> begin() const; 89 ChildConstIterator<ElementType> end() const; 90 91 private: 92 const ContainerNode* m_root; 93 }; 94 66 95 ChildIteratorAdapter<Element> elementChildren(ContainerNode* root); 96 ChildConstIteratorAdapter<Element> elementChildren(const ContainerNode* root); 67 97 template <typename ElementType> ChildIteratorAdapter<ElementType> childrenOfType(ContainerNode* root); 98 template <typename ElementType> ChildConstIteratorAdapter<ElementType> childrenOfType(const ContainerNode* root); 99 100 // ChildIterator 68 101 69 102 template <typename ElementType> … … 119 152 } 120 153 154 // ChildConstIterator 155 156 template <typename ElementType> 157 inline ChildConstIterator<ElementType>::ChildConstIterator() 158 : m_current(nullptr) 159 { 160 } 161 162 template <typename ElementType> 163 inline ChildConstIterator<ElementType>::ChildConstIterator(const ElementType* current) 164 : m_current(current) 165 #if !ASSERT_DISABLED 166 , m_assertions(current) 167 #endif 168 { 169 } 170 171 template <typename ElementType> 172 inline ChildConstIterator<ElementType>& ChildConstIterator<ElementType>::operator++() 173 { 174 ASSERT(m_current); 175 ASSERT(!m_assertions.domTreeHasMutated()); 176 m_current = Traversal<ElementType>::nextSibling(m_current); 177 #if !ASSERT_DISABLED 178 // Drop the assertion when the iterator reaches the end. 179 if (!m_current) 180 m_assertions.dropEventDispatchAssertion(); 181 #endif 182 return *this; 183 } 184 185 template <typename ElementType> 186 inline const ElementType& ChildConstIterator<ElementType>::operator*() const 187 { 188 ASSERT(m_current); 189 ASSERT(!m_assertions.domTreeHasMutated()); 190 return *m_current; 191 } 192 193 template <typename ElementType> 194 inline const ElementType* ChildConstIterator<ElementType>::operator->() const 195 { 196 ASSERT(m_current); 197 ASSERT(!m_assertions.domTreeHasMutated()); 198 return m_current; 199 } 200 201 template <typename ElementType> 202 inline bool ChildConstIterator<ElementType>::operator!=(const ChildConstIterator& other) const 203 { 204 ASSERT(!m_assertions.domTreeHasMutated()); 205 return m_current != other.m_current; 206 } 207 208 // ChildIteratorAdapter 209 121 210 template <typename ElementType> 122 211 inline ChildIteratorAdapter<ElementType>::ChildIteratorAdapter(ContainerNode* root) … … 137 226 } 138 227 228 // ChildConstIteratorAdapter 229 230 template <typename ElementType> 231 inline ChildConstIteratorAdapter<ElementType>::ChildConstIteratorAdapter(const ContainerNode* root) 232 : m_root(root) 233 { 234 } 235 236 template <typename ElementType> 237 inline ChildConstIterator<ElementType> ChildConstIteratorAdapter<ElementType>::begin() const 238 { 239 return ChildConstIterator<ElementType>(Traversal<ElementType>::firstChild(m_root)); 240 } 241 242 template <typename ElementType> 243 inline ChildConstIterator<ElementType> ChildConstIteratorAdapter<ElementType>::end() const 244 { 245 return ChildConstIterator<ElementType>(); 246 } 247 248 // Standalone functions 249 139 250 inline ChildIteratorAdapter<Element> elementChildren(ContainerNode* root) 140 251 { … … 148 259 } 149 260 150 } 151 152 #endif 261 inline ChildConstIteratorAdapter<Element> elementChildren(const ContainerNode* root) 262 { 263 return ChildConstIteratorAdapter<Element>(root); 264 } 265 266 template <typename ElementType> 267 inline ChildConstIteratorAdapter<ElementType> childrenOfType(const ContainerNode* root) 268 { 269 return ChildConstIteratorAdapter<ElementType>(root); 270 } 271 272 } 273 274 #endif -
trunk/Source/WebCore/dom/DescendantIterator.h
r154751 r154769 55 55 56 56 template <typename ElementType> 57 class DescendantConstIterator { 58 public: 59 DescendantConstIterator(const ContainerNode* root); 60 DescendantConstIterator(const ContainerNode* root, const ElementType* current); 61 DescendantConstIterator& operator++(); 62 const ElementType& operator*() const; 63 const ElementType* operator->() const; 64 bool operator!=(const DescendantConstIterator& other) const; 65 66 private: 67 const ContainerNode* m_root; 68 const ElementType* m_current; 69 70 #if !ASSERT_DISABLED 71 DescendantIteratorAssertions m_assertions; 72 #endif 73 }; 74 75 template <typename ElementType> 57 76 class DescendantIteratorAdapter { 58 77 public: … … 65 84 }; 66 85 86 template <typename ElementType> 87 class DescendantConstIteratorAdapter { 88 public: 89 DescendantConstIteratorAdapter(const ContainerNode* root); 90 DescendantConstIterator<ElementType> begin() const; 91 DescendantConstIterator<ElementType> end() const; 92 93 private: 94 const ContainerNode* m_root; 95 }; 96 67 97 DescendantIteratorAdapter<Element> elementDescendants(ContainerNode* root); 98 DescendantConstIteratorAdapter<Element> elementDescendants(const ContainerNode* root); 68 99 template <typename ElementType> DescendantIteratorAdapter<ElementType> descendantsOfType(ContainerNode* root); 100 template <typename ElementType> DescendantConstIteratorAdapter<ElementType> descendantsOfType(const ContainerNode* root); 101 102 // DescendantIterator 69 103 70 104 template <typename ElementType> … … 123 157 } 124 158 159 // DescendantConstIterator 160 161 template <typename ElementType> 162 inline DescendantConstIterator<ElementType>::DescendantConstIterator(const ContainerNode* root) 163 : m_root(root) 164 , m_current(nullptr) 165 { 166 } 167 168 template <typename ElementType> 169 inline DescendantConstIterator<ElementType>::DescendantConstIterator(const ContainerNode* root, const ElementType* current) 170 : m_root(root) 171 , m_current(current) 172 #if !ASSERT_DISABLED 173 , m_assertions(current) 174 #endif 175 { 176 } 177 178 template <typename ElementType> 179 inline DescendantConstIterator<ElementType>& DescendantConstIterator<ElementType>::operator++() 180 { 181 ASSERT(m_current); 182 ASSERT(!m_assertions.domTreeHasMutated()); 183 m_current = Traversal<ElementType>::next(m_current, m_root); 184 #if !ASSERT_DISABLED 185 // Drop the assertion when the iterator reaches the end. 186 if (!m_current) 187 m_assertions.dropEventDispatchAssertion(); 188 #endif 189 return *this; 190 } 191 192 template <typename ElementType> 193 inline const ElementType& DescendantConstIterator<ElementType>::operator*() const 194 { 195 ASSERT(m_current); 196 ASSERT(!m_assertions.domTreeHasMutated()); 197 return *m_current; 198 } 199 200 template <typename ElementType> 201 inline const ElementType* DescendantConstIterator<ElementType>::operator->() const 202 { 203 ASSERT(m_current); 204 ASSERT(!m_assertions.domTreeHasMutated()); 205 return m_current; 206 } 207 208 template <typename ElementType> 209 inline bool DescendantConstIterator<ElementType>::operator!=(const DescendantConstIterator& other) const 210 { 211 ASSERT(m_root == other.m_root); 212 ASSERT(!m_assertions.domTreeHasMutated()); 213 return m_current != other.m_current; 214 } 215 216 // DescendantIteratorAdapter 217 125 218 template <typename ElementType> 126 219 inline DescendantIteratorAdapter<ElementType>::DescendantIteratorAdapter(ContainerNode* root) … … 141 234 } 142 235 236 // DescendantConstIteratorAdapter 237 238 template <typename ElementType> 239 inline DescendantConstIteratorAdapter<ElementType>::DescendantConstIteratorAdapter(const ContainerNode* root) 240 : m_root(root) 241 { 242 } 243 244 template <typename ElementType> 245 inline DescendantConstIterator<ElementType> DescendantConstIteratorAdapter<ElementType>::begin() const 246 { 247 return DescendantConstIterator<ElementType>(m_root, Traversal<ElementType>::firstWithin(m_root)); 248 } 249 250 template <typename ElementType> 251 inline DescendantConstIterator<ElementType> DescendantConstIteratorAdapter<ElementType>::end() const 252 { 253 return DescendantConstIterator<ElementType>(m_root); 254 } 255 256 // Standalone functions 257 143 258 inline DescendantIteratorAdapter<Element> elementDescendants(ContainerNode* root) 144 259 { … … 152 267 } 153 268 154 } 155 156 #endif 269 inline DescendantConstIteratorAdapter<Element> elementDescendants(const ContainerNode* root) 270 { 271 return DescendantConstIteratorAdapter<Element>(root); 272 } 273 274 template <typename ElementType> 275 inline DescendantConstIteratorAdapter<ElementType> descendantsOfType(const ContainerNode* root) 276 { 277 return DescendantConstIteratorAdapter<ElementType>(root); 278 } 279 280 } 281 282 #endif -
trunk/Source/WebCore/dom/Node.cpp
r154686 r154769 45 45 #include "DOMImplementation.h" 46 46 #include "DOMSettableTokenList.h" 47 #include "DescendantIterator.h" 47 48 #include "Document.h" 48 49 #include "DocumentFragment.h" … … 50 51 #include "Element.h" 51 52 #include "ElementRareData.h" 52 #include "ElementTraversal.h"53 53 #include "Event.h" 54 54 #include "EventContext.h" … … 2393 2393 size_t Node::numberOfScopedHTMLStyleChildren() const 2394 2394 { 2395 if (!isContainerNode()) 2396 return 0; 2395 2397 size_t count = 0; 2396 for (HTMLStyleElement* style = Traversal<HTMLStyleElement>::firstWithin(this); style; style = Traversal<HTMLStyleElement>::next(style, this)) { 2398 auto styleDescendants = descendantsOfType<HTMLStyleElement>(toContainerNode(this)); 2399 for (auto style = styleDescendants.begin(), end = styleDescendants.end(); style != end; ++style) { 2397 2400 if (style->isRegisteredAsScoped()) 2398 2401 count++; -
trunk/Source/WebCore/html/HTMLFieldSetElement.cpp
r154679 r154769 90 90 } 91 91 92 HTMLLegendElement* HTMLFieldSetElement::legend() const92 const HTMLLegendElement* HTMLFieldSetElement::legend() const 93 93 { 94 return Traversal<HTMLLegendElement>::firstWithin(this); 94 auto legendDescendants = descendantsOfType<HTMLLegendElement>(this); 95 auto firstLegend = legendDescendants.begin(); 96 if (firstLegend != legendDescendants.end()) 97 return &*firstLegend; 98 return nullptr; 95 99 } 96 100 -
trunk/Source/WebCore/html/HTMLFieldSetElement.h
r151947 r154769 35 35 public: 36 36 static PassRefPtr<HTMLFieldSetElement> create(const QualifiedName&, Document*, HTMLFormElement*); 37 HTMLLegendElement* legend() const;38 37 38 const HTMLLegendElement* legend() const; 39 39 PassRefPtr<HTMLCollection> elements(); 40 40 -
trunk/Source/WebCore/html/HTMLMediaElement.cpp
r154760 r154769 40 40 #include "CSSPropertyNames.h" 41 41 #include "CSSValueKeywords.h" 42 #include "DescendantIterator.h" 42 43 #include "DiagnosticLoggingKeys.h" 43 44 #include "DocumentLoader.h" … … 538 539 return; 539 540 540 if (Traversal<HTMLTrackElement>::firstWithin(this)) 541 auto trackDescendants = descendantsOfType<HTMLTrackElement>(this); 542 if (trackDescendants.begin() != trackDescendants.end()) 541 543 scheduleDelayedAction(ConfigureTextTracks); 542 544 #endif -
trunk/Source/WebCore/html/HTMLObjectElement.cpp
r154679 r154769 462 462 if (MIMETypeRegistry::isJavaAppletMIMEType(getAttribute(typeAttr))) 463 463 return true; 464 465 for (auto child = ElementTraversal::firstChild(this); child; child = ElementTraversal::nextSibling(child)) { 466 if (child->hasTagName(paramTag) 467 && equalIgnoringCase(child->getNameAttribute(), "type") 468 && MIMETypeRegistry::isJavaAppletMIMEType(child->getAttribute(valueAttr).string())) 464 465 for (auto child = elementChildren(this).begin(), end = elementChildren(this).end(); child != end; ++child) { 466 if (child->hasTagName(paramTag) && equalIgnoringCase(child->getNameAttribute(), "type") 467 && MIMETypeRegistry::isJavaAppletMIMEType(child->getAttribute(valueAttr).string())) 469 468 return true; 470 if (child->hasTagName(objectTag) 471 && static_cast<HTMLObjectElement*>(child)->containsJavaApplet()) 469 if (child->hasTagName(objectTag) && static_cast<const HTMLObjectElement&>(*child).containsJavaApplet()) 472 470 return true; 473 471 if (child->hasTagName(appletTag)) -
trunk/Source/WebCore/svg/SVGElement.cpp
r154713 r154769 32 32 #include "CSSParser.h" 33 33 #include "DOMImplementation.h" 34 #include "DescendantIterator.h" 34 35 #include "Document.h" 35 #include "ElementTraversal.h"36 36 #include "Event.h" 37 37 #include "EventNames.h" … … 880 880 // <title> child of this element. 881 881 // If a title child was found, return the text contents. 882 if (SVGTitleElement* titleElement = Traversal<SVGTitleElement>::firstWithin(this)) 883 return titleElement->innerText(); 882 auto titleDescendants = descendantsOfType<SVGTitleElement>(this); 883 auto firstTitle = titleDescendants.begin(); 884 if (firstTitle != titleDescendants.end()) 885 return const_cast<SVGTitleElement&>(*firstTitle).innerText(); 884 886 885 887 // Otherwise return a null/empty string. -
trunk/Source/WebCore/svg/SVGSVGElement.cpp
r154676 r154769 28 28 #include "Attribute.h" 29 29 #include "CSSHelper.h" 30 #include "DescendantIterator.h" 30 31 #include "Document.h" 31 #include "ElementTraversal.h"32 32 #include "EventListener.h" 33 33 #include "EventNames.h" … … 338 338 { 339 339 Vector<RefPtr<Node> > nodes; 340 SVGElement* svgElement = Traversal<SVGElement>::firstWithin(referenceElement ? referenceElement : this); 341 while (svgElement) { 340 341 auto svgDescendants = descendantsOfType<SVGElement>(referenceElement ? referenceElement : this); 342 for (auto it = svgDescendants.begin(), end = svgDescendants.end(); it != end; ++it) { 343 const SVGElement* svgElement = &*it; 342 344 if (collect == CollectIntersectionList) { 343 345 if (checkIntersection(svgElement, rect)) 344 nodes.append( svgElement);346 nodes.append(const_cast<SVGElement*>(svgElement)); 345 347 } else { 346 348 if (checkEnclosure(svgElement, rect)) 347 nodes.append( svgElement);349 nodes.append(const_cast<SVGElement*>(svgElement)); 348 350 } 349 svgElement = Traversal<SVGElement>::next(svgElement, referenceElement ? referenceElement : this);350 351 } 351 352 return StaticNodeList::adopt(nodes); … … 362 363 } 363 364 364 bool SVGSVGElement::checkIntersection( SVGElement* element, const FloatRect& rect) const365 bool SVGSVGElement::checkIntersection(const SVGElement* element, const FloatRect& rect) const 365 366 { 366 367 if (!element) … … 369 370 } 370 371 371 bool SVGSVGElement::checkEnclosure( SVGElement* element, const FloatRect& rect) const372 bool SVGSVGElement::checkEnclosure(const SVGElement* element, const FloatRect& rect) const 372 373 { 373 374 if (!element) … … 769 770 // getElementById on SVGSVGElement is restricted to only the child subtree defined by the <svg> element. 770 771 // See http://www.w3.org/TR/SVG11/struct.html#InterfaceSVGSVGElement 771 Element* SVGSVGElement::getElementById(const AtomicString& id) const772 Element* SVGSVGElement::getElementById(const AtomicString& id) 772 773 { 773 774 Element* element = treeScope()->getElementById(id); … … 777 778 // Fall back to traversing our subtree. Duplicate ids are allowed, the first found will 778 779 // be returned. 779 for ( Element* element = ElementTraversal::firstWithin(this); element; element = ElementTraversal::next(element, this)) {780 for (auto element = elementDescendants(this).begin(), end = elementDescendants(this).end(); element != end; ++element) { 780 781 if (element->getIdAttribute() == id) 781 return element;782 return &*element; 782 783 } 783 784 return 0; -
trunk/Source/WebCore/svg/SVGSVGElement.h
r154371 r154769 107 107 PassRefPtr<NodeList> getIntersectionList(const FloatRect&, SVGElement* referenceElement) const; 108 108 PassRefPtr<NodeList> getEnclosureList(const FloatRect&, SVGElement* referenceElement) const; 109 bool checkIntersection( SVGElement*, const FloatRect&) const;110 bool checkEnclosure( SVGElement*, const FloatRect&) const;109 bool checkIntersection(const SVGElement*, const FloatRect&) const; 110 bool checkEnclosure(const SVGElement*, const FloatRect&) const; 111 111 void deselectAll(); 112 112 … … 124 124 void setupInitialView(const String& fragmentIdentifier, Element* anchorNode); 125 125 126 Element* getElementById(const AtomicString&) const;126 Element* getElementById(const AtomicString&); 127 127 128 128 bool widthAttributeEstablishesViewport() const;
Note:
See TracChangeset
for help on using the changeset viewer.