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

Unified Diff: Source/core/dom/Element.h

Issue 436603003: Make Element::attributes() less error-prone and simplify call sites (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Keep synchronizeAllAttributes() in hasAttributes() Created 6 years, 4 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
Index: Source/core/dom/Element.h
diff --git a/Source/core/dom/Element.h b/Source/core/dom/Element.h
index 241698f723912cca3b6e56c9c4d27b3d77bb786b..1aff94f3a1222c4afbb4463d9be354d898a8c149 100644
--- a/Source/core/dom/Element.h
+++ b/Source/core/dom/Element.h
@@ -139,9 +139,6 @@ public:
bool hasNamedNodeMap() const;
#endif
bool hasAttributes() const;
- // This variant will not update the potentially invalid attributes. To be used when not interested
- // in style attribute or one of the SVG animation attributes.
- bool hasAttributesWithoutUpdate() const;
bool hasAttribute(const AtomicString& name) const;
bool hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const;
@@ -167,10 +164,14 @@ public:
// so this function is not suitable for non-style uses.
const AtomicString& idForStyleResolution() const;
- // Internal method that assumes the existence of attribute storage, one should use hasAttributes()
- // before calling it. This is not a trivial getter and its return value should be cached for
- // performance.
+ // This getter takes care of synchronizing all attributes before returning the
+ // AttributeCollection. If the Element has no attributes, an empty AttributeCollection
+ // will be returned. This is not a trivial getter and its return value should be cached
+ // for performance.
AttributeCollection attributes() const;
+ // This variant will not update the potentially invalid attributes. To be used when not interested
+ // in style attribute or one of the SVG animation attributes.
+ AttributeCollection attributesWithoutUpdate() const;
void scrollIntoView(bool alignToTop = true);
void scrollIntoViewIfNeeded(bool centerIfNeeded = true);
@@ -691,14 +692,14 @@ inline Element* Node::parentElement() const
inline bool Element::fastHasAttribute(const QualifiedName& name) const
{
ASSERT(fastAttributeLookupAllowed(name));
- return elementData() && attributes().findIndex(name) != kNotFound;
+ return elementData() && elementData()->attributes().findIndex(name) != kNotFound;
Inactive 2014/08/04 19:09:46 I could call "return attributesWithoutUpdate().fin
adamk 2014/08/04 19:29:51 For these cases I definitely see the advantage of
Inactive 2014/08/04 19:39:52 Ok, I kept them as is.
}
inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name) const
{
ASSERT(fastAttributeLookupAllowed(name));
if (elementData()) {
- if (const Attribute* attribute = attributes().find(name))
+ if (const Attribute* attribute = elementData()->attributes().find(name))
Inactive 2014/08/04 19:09:46 I could call "return attributesWithoutUpdate().fin
return attribute->value();
}
return nullAtom;
@@ -706,12 +707,22 @@ inline const AtomicString& Element::fastGetAttribute(const QualifiedName& name)
inline AttributeCollection Element::attributes() const
{
- ASSERT(elementData());
+ if (!elementData())
adamk 2014/08/04 18:54:34 Is it really safe to check this before calling syn
Inactive 2014/08/04 19:09:46 synchronizeAllAttributes() returns early if elemen
+ return AttributeCollection();
+ synchronizeAllAttributes();
+ return elementData()->attributes();
+}
+
+inline AttributeCollection Element::attributesWithoutUpdate() const
+{
+ if (!elementData())
+ return AttributeCollection();
return elementData()->attributes();
}
-inline bool Element::hasAttributesWithoutUpdate() const
+inline bool Element::hasAttributes() const
{
+ synchronizeAllAttributes();
return elementData() && !elementData()->attributes().isEmpty();
adamk 2014/08/04 18:54:34 Can't this just be: return !attributes().isEmpty(
Inactive 2014/08/04 19:09:46 Good point, yes. My original version of this patch
Inactive 2014/08/04 19:39:52 Done.
}

Powered by Google App Engine
This is Rietveld 408576698