Title: [115099] trunk/Source/WebCore
Revision
115099
Author
[email protected]
Date
2012-04-24 12:59:02 -0700 (Tue, 24 Apr 2012)

Log Message

Use Vector<Attribute> directly instead of encapsulating it in AttributeVector
https://bugs.webkit.org/show_bug.cgi?id=84413

Reviewed by Andreas Kling.

As commented in https://bugs.webkit.org/show_bug.cgi?id=79963#c16 we do not
usually subclass basic types like Vector. This patch changes code to use
Vector<Attribute> directly and move around the functionality of the former
methods to more specific helper functions or inline code at the callers.

* dom/Element.cpp:
(WebCore::Element::parserSetAttributes):
(WebCore::Element::normalizeAttributes):
* dom/Element.h:
(Element):
* dom/ElementAttributeData.cpp:
* dom/ElementAttributeData.h:
(WebCore::getAttributeFromVector):
(WebCore::ElementAttributeData::getAttributeItem):
(ElementAttributeData):
(WebCore::ElementAttributeData::attributeVector):
(WebCore::ElementAttributeData::clonedAttributeVector):
(WebCore::ElementAttributeData::getAttributeItemIndex):
(WebCore):
* html/parser/HTMLConstructionSite.cpp:
(WebCore::HTMLConstructionSite::createHTMLElementFromSavedElement):
* html/parser/HTMLToken.h:
(WebCore::AtomicHTMLToken::AtomicHTMLToken):
* html/parser/HTMLTreeBuilder.cpp:
(WebCore::HTMLTreeBuilder::processFakeStartTag):
(WebCore::HTMLTreeBuilder::attributesForIsindexInput):
* html/parser/HTMLTreeBuilder.h:
* html/parser/TextDocumentParser.cpp:
(WebCore::TextDocumentParser::insertFakePreElement):
* xml/XMLErrors.cpp:
(WebCore::createXHTMLParserErrorHeader):
(WebCore::XMLErrors::insertErrorMessageBlock):
* xml/parser/MarkupTokenBase.h:
(WebCore::AtomicMarkupTokenBase::AtomicMarkupTokenBase):
(WebCore::AtomicMarkupTokenBase::getAttributeItem):
(WebCore::AtomicMarkupTokenBase::attributes):
(AtomicMarkupTokenBase):
(WebCore::::initializeAttributes):
* xml/parser/XMLToken.h:
(WebCore::AtomicXMLToken::AtomicXMLToken):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (115098 => 115099)


--- trunk/Source/WebCore/ChangeLog	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/ChangeLog	2012-04-24 19:59:02 UTC (rev 115099)
@@ -1,3 +1,51 @@
+2012-04-24  Caio Marcelo de Oliveira Filho  <[email protected]>
+
+        Use Vector<Attribute> directly instead of encapsulating it in AttributeVector
+        https://bugs.webkit.org/show_bug.cgi?id=84413
+
+        Reviewed by Andreas Kling.
+
+        As commented in https://bugs.webkit.org/show_bug.cgi?id=79963#c16 we do not
+        usually subclass basic types like Vector. This patch changes code to use
+        Vector<Attribute> directly and move around the functionality of the former
+        methods to more specific helper functions or inline code at the callers.
+
+        * dom/Element.cpp:
+        (WebCore::Element::parserSetAttributes):
+        (WebCore::Element::normalizeAttributes):
+        * dom/Element.h:
+        (Element):
+        * dom/ElementAttributeData.cpp:
+        * dom/ElementAttributeData.h:
+        (WebCore::getAttributeFromVector):
+        (WebCore::ElementAttributeData::getAttributeItem):
+        (ElementAttributeData):
+        (WebCore::ElementAttributeData::attributeVector):
+        (WebCore::ElementAttributeData::clonedAttributeVector):
+        (WebCore::ElementAttributeData::getAttributeItemIndex):
+        (WebCore):
+        * html/parser/HTMLConstructionSite.cpp:
+        (WebCore::HTMLConstructionSite::createHTMLElementFromSavedElement):
+        * html/parser/HTMLToken.h:
+        (WebCore::AtomicHTMLToken::AtomicHTMLToken):
+        * html/parser/HTMLTreeBuilder.cpp:
+        (WebCore::HTMLTreeBuilder::processFakeStartTag):
+        (WebCore::HTMLTreeBuilder::attributesForIsindexInput):
+        * html/parser/HTMLTreeBuilder.h:
+        * html/parser/TextDocumentParser.cpp:
+        (WebCore::TextDocumentParser::insertFakePreElement):
+        * xml/XMLErrors.cpp:
+        (WebCore::createXHTMLParserErrorHeader):
+        (WebCore::XMLErrors::insertErrorMessageBlock):
+        * xml/parser/MarkupTokenBase.h:
+        (WebCore::AtomicMarkupTokenBase::AtomicMarkupTokenBase):
+        (WebCore::AtomicMarkupTokenBase::getAttributeItem):
+        (WebCore::AtomicMarkupTokenBase::attributes):
+        (AtomicMarkupTokenBase):
+        (WebCore::::initializeAttributes):
+        * xml/parser/XMLToken.h:
+        (WebCore::AtomicXMLToken::AtomicXMLToken):
+
 2012-04-24  Alexis Menard  <[email protected]>
 
         Rename CSSStyleSelector class to StyleResolver.

Modified: trunk/Source/WebCore/dom/Element.cpp (115098 => 115099)


--- trunk/Source/WebCore/dom/Element.cpp	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/dom/Element.cpp	2012-04-24 19:59:02 UTC (rev 115099)
@@ -760,7 +760,7 @@
     return (name.localName().endsWith(hrefAttr.localName()) || name == srcAttr || name == actionAttr) && protocolIsJavaScript(stripLeadingAndTrailingHTMLSpaces(value));       
 }
 
-void Element::parserSetAttributes(const AttributeVector& attributeVector, FragmentScriptingPermission scriptingPermission)
+void Element::parserSetAttributes(const Vector<Attribute>& attributeVector, FragmentScriptingPermission scriptingPermission)
 {
     ASSERT(!inDocument());
     ASSERT(!parentNode());
@@ -793,7 +793,7 @@
 
     // Store the set of attributes that changed on the stack in case
     // attributeChanged mutates m_attributeData.
-    AttributeVector clonedAttributes = m_attributeData->clonedAttributeVector();
+    Vector<Attribute> clonedAttributes = m_attributeData->clonedAttributeVector();
     for (unsigned i = 0; i < clonedAttributes.size(); ++i)
         attributeChanged(&clonedAttributes[i]);
 }
@@ -1737,7 +1737,7 @@
     if (!attributeData || attributeData->isEmpty())
         return;
 
-    const AttributeVector& attributes = attributeData->attributeVector();
+    const Vector<Attribute>& attributes = attributeData->attributeVector();
     for (size_t i = 0; i < attributes.size(); ++i) {
         if (RefPtr<Attr> attr = attrIfExists(attributes[i].name()))
             attr->normalize();

Modified: trunk/Source/WebCore/dom/Element.h (115098 => 115099)


--- trunk/Source/WebCore/dom/Element.h	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/dom/Element.h	2012-04-24 19:59:02 UTC (rev 115099)
@@ -244,7 +244,7 @@
     virtual void attributeChanged(Attribute*);
 
     // Only called by the parser immediately after element construction.
-    void parserSetAttributes(const AttributeVector&, FragmentScriptingPermission);
+    void parserSetAttributes(const Vector<Attribute>&, FragmentScriptingPermission);
 
     ElementAttributeData* attributeData() const { return m_attributeData.get(); }
     ElementAttributeData* ensureAttributeData() const;

Modified: trunk/Source/WebCore/dom/ElementAttributeData.cpp (115098 => 115099)


--- trunk/Source/WebCore/dom/ElementAttributeData.cpp	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/dom/ElementAttributeData.cpp	2012-04-24 19:59:02 UTC (rev 115099)
@@ -71,15 +71,6 @@
     --m_attrCount;
 }
 
-void AttributeVector::removeAttribute(const QualifiedName& name)
-{
-    size_t index = getAttributeItemIndex(name);
-    if (index == notFound)
-        return;
-
-    remove(index);
-}
-
 ElementAttributeData::~ElementAttributeData()
 {
 }

Modified: trunk/Source/WebCore/dom/ElementAttributeData.h (115098 => 115099)


--- trunk/Source/WebCore/dom/ElementAttributeData.h	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/dom/ElementAttributeData.h	2012-04-24 19:59:02 UTC (rev 115099)
@@ -36,41 +36,16 @@
 class Attr;
 class Element;
 
-class AttributeVector : public Vector<Attribute> {
-public:
-    AttributeVector() { }
-
-    Attribute* getAttributeItem(const QualifiedName&) const;
-    size_t getAttributeItemIndex(const QualifiedName&) const;
-
-    // Used during parsing: only inserts if not already there.
-    void insertAttribute(const Attribute&);
-    void removeAttribute(const QualifiedName&);
-};
-
-inline Attribute* AttributeVector::getAttributeItem(const QualifiedName& name) const
+inline Attribute* findAttributeInVector(const Vector<Attribute>& attributes, const QualifiedName& name)
 {
-    size_t index = getAttributeItemIndex(name);
-    if (index != notFound)
-        return &const_cast<AttributeVector*>(this)->at(index);
+    ASSERT(attributes);
+    for (unsigned i = 0; i < attributes.size(); ++i) {
+        if (attributes.at(i).name().matches(name))
+            return &const_cast<Vector<Attribute>& >(attributes).at(i);
+    }
     return 0;
 }
 
-inline size_t AttributeVector::getAttributeItemIndex(const QualifiedName& name) const
-{
-    for (unsigned i = 0; i < size(); ++i) {
-        if (at(i).name().matches(name))
-            return i;
-    }
-    return notFound;
-}
-
-inline void AttributeVector::insertAttribute(const Attribute& newAttribute)
-{
-    if (!getAttributeItem(newAttribute.name()))
-        append(newAttribute);
-}
-
 enum EInUpdateStyleAttribute { NotInUpdateStyleAttribute, InUpdateStyleAttribute };
 
 class ElementAttributeData {
@@ -107,8 +82,8 @@
 
     // Internal interface.
     Attribute* attributeItem(unsigned index) const { return &const_cast<ElementAttributeData*>(this)->m_attributes[index]; }
-    Attribute* getAttributeItem(const QualifiedName& name) const { return m_attributes.getAttributeItem(name); }
-    size_t getAttributeItemIndex(const QualifiedName& name) const { return m_attributes.getAttributeItemIndex(name); }
+    Attribute* getAttributeItem(const QualifiedName& name) const { return findAttributeInVector(m_attributes, name); }
+    size_t getAttributeItemIndex(const QualifiedName&) const;
     size_t getAttributeItemIndex(const String& name, bool shouldIgnoreAttributeCase) const;
 
     // These functions do no error checking.
@@ -136,8 +111,8 @@
     {
     }
 
-    const AttributeVector& attributeVector() const { return m_attributes; }
-    AttributeVector clonedAttributeVector() const { return m_attributes; }
+    const Vector<Attribute>& attributeVector() const { return m_attributes; }
+    Vector<Attribute> clonedAttributeVector() const { return m_attributes; }
 
     void detachAttributesFromElement(Element*);
     Attribute* getAttributeItem(const String& name, bool shouldIgnoreAttributeCase) const;
@@ -150,7 +125,7 @@
     RefPtr<StylePropertySet> m_attributeStyle;
     SpaceSplitString m_classNames;
     AtomicString m_idForStyleResolution;
-    AttributeVector m_attributes;
+    Vector<Attribute> m_attributes;
 
     unsigned m_attrCount;
 };
@@ -173,6 +148,15 @@
     return 0;
 }
 
+inline size_t ElementAttributeData::getAttributeItemIndex(const QualifiedName& name) const
+{
+    for (unsigned i = 0; i < m_attributes.size(); ++i) {
+        if (m_attributes.at(i).name().matches(name))
+            return i;
+    }
+    return notFound;
+}
+
 // We use a boolean parameter instead of calling shouldIgnoreAttributeCase so that the caller
 // can tune the behavior (hasAttribute is case sensitive whereas getAttribute is not).
 inline size_t ElementAttributeData::getAttributeItemIndex(const String& name, bool shouldIgnoreAttributeCase) const

Modified: trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp (115098 => 115099)


--- trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/html/parser/HTMLConstructionSite.cpp	2012-04-24 19:59:02 UTC (rev 115099)
@@ -432,7 +432,7 @@
 
     ASSERT(element->isHTMLElement()); // otherwise localName() might be wrong.
 
-    AttributeVector clonedAttributes;
+    Vector<Attribute> clonedAttributes;
     if (ElementAttributeData* attributeData = element->updatedAttributeData())
         clonedAttributes = attributeData->clonedAttributeVector();
 

Modified: trunk/Source/WebCore/html/parser/HTMLToken.h (115098 => 115099)


--- trunk/Source/WebCore/html/parser/HTMLToken.h	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/html/parser/HTMLToken.h	2012-04-24 19:59:02 UTC (rev 115099)
@@ -86,7 +86,7 @@
 public:
     AtomicHTMLToken(HTMLToken& token) : AtomicMarkupTokenBase<HTMLToken>(&token) { }
 
-    AtomicHTMLToken(HTMLTokenTypes::Type type, AtomicString name, const AttributeVector& attributes = AttributeVector())
+    AtomicHTMLToken(HTMLTokenTypes::Type type, AtomicString name, const Vector<Attribute>& attributes = Vector<Attribute>())
         : AtomicMarkupTokenBase<HTMLToken>(type, name, attributes)
     {
     }

Modified: trunk/Source/WebCore/html/parser/HTMLTreeBuilder.cpp (115098 => 115099)


--- trunk/Source/WebCore/html/parser/HTMLTreeBuilder.cpp	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/html/parser/HTMLTreeBuilder.cpp	2012-04-24 19:59:02 UTC (rev 115099)
@@ -531,7 +531,7 @@
     parseError(token);
 }
 
-void HTMLTreeBuilder::processFakeStartTag(const QualifiedName& tagName, const AttributeVector& attributes)
+void HTMLTreeBuilder::processFakeStartTag(const QualifiedName& tagName, const Vector<Attribute>& attributes)
 {
     // FIXME: We'll need a fancier conversion than just "localName" for SVG/MathML tags.
     AtomicHTMLToken fakeToken(HTMLTokenTypes::StartTag, tagName.localName(), attributes);
@@ -560,16 +560,21 @@
     processEndTag(endP);
 }
 
-AttributeVector HTMLTreeBuilder::attributesForIsindexInput(AtomicHTMLToken& token)
+Vector<Attribute> HTMLTreeBuilder::attributesForIsindexInput(AtomicHTMLToken& token)
 {
-    AttributeVector attributes = token.attributes();
-    if (!attributes.isEmpty()) {
-        attributes.removeAttribute(nameAttr);
-        attributes.removeAttribute(actionAttr);
-        attributes.removeAttribute(promptAttr);
+    Vector<Attribute> attributes = token.attributes();
+    Vector<unsigned, 3> indicesToRemove;
+
+    for (unsigned i = 0; i < attributes.size(); ++i) {
+        const QualifiedName& name = attributes.at(i).name();
+        if (name.matches(nameAttr) || name.matches(actionAttr) || name.matches(promptAttr))
+            indicesToRemove.append(i);
     }
 
-    attributes.insertAttribute(Attribute(nameAttr, isindexTag.localName()));
+    for (unsigned i = 0; i < indicesToRemove.size(); ++i)
+        attributes.remove(indicesToRemove.at(i));
+
+    attributes.append(Attribute(nameAttr, isindexTag.localName()));
     return attributes;
 }
 

Modified: trunk/Source/WebCore/html/parser/HTMLTreeBuilder.h (115098 => 115099)


--- trunk/Source/WebCore/html/parser/HTMLTreeBuilder.h	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/html/parser/HTMLTreeBuilder.h	2012-04-24 19:59:02 UTC (rev 115099)
@@ -151,7 +151,7 @@
     void processCharacterBuffer(ExternalCharacterTokenBuffer&);
     inline void processCharacterBufferForInBody(ExternalCharacterTokenBuffer&);
 
-    void processFakeStartTag(const QualifiedName&, const AttributeVector& attributes = AttributeVector());
+    void processFakeStartTag(const QualifiedName&, const Vector<Attribute>& attributes = Vector<Attribute>());
     void processFakeEndTag(const QualifiedName&);
     void processFakeCharacters(const String&);
     void processFakePEndTagIfPInButtonScope();
@@ -172,7 +172,7 @@
     inline bool shouldProcessTokenInForeignContent(AtomicHTMLToken&);
     void processTokenInForeignContent(AtomicHTMLToken&);
 
-    AttributeVector attributesForIsindexInput(AtomicHTMLToken&);
+    Vector<Attribute> attributesForIsindexInput(AtomicHTMLToken&);
 
     HTMLElementStack::ElementRecord* furthestBlockForFormattingElement(Element*);
     void callTheAdoptionAgency(AtomicHTMLToken&);

Modified: trunk/Source/WebCore/html/parser/TextDocumentParser.cpp (115098 => 115099)


--- trunk/Source/WebCore/html/parser/TextDocumentParser.cpp	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/html/parser/TextDocumentParser.cpp	2012-04-24 19:59:02 UTC (rev 115099)
@@ -60,8 +60,8 @@
     // sending fake bytes through the front-end of the parser to avoid
     // distrubing the line/column number calculations.
 
-    AttributeVector attributes;
-    attributes.insertAttribute(Attribute("style", "word-wrap: break-word; white-space: pre-wrap;"));
+    Vector<Attribute> attributes;
+    attributes.append(Attribute(styleAttr, "word-wrap: break-word; white-space: pre-wrap;"));
     AtomicHTMLToken fakePre(HTMLTokenTypes::StartTag, preTag.localName(), attributes);
 
     treeBuilder()->constructTreeFromAtomicToken(fakePre);

Modified: trunk/Source/WebCore/xml/XMLErrors.cpp (115098 => 115099)


--- trunk/Source/WebCore/xml/XMLErrors.cpp	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/xml/XMLErrors.cpp	2012-04-24 19:59:02 UTC (rev 115099)
@@ -91,7 +91,7 @@
 {
     RefPtr<Element> reportElement = doc->createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), true);
 
-    AttributeVector reportAttributes;
+    Vector<Attribute> reportAttributes;
     reportAttributes.append(Attribute(styleAttr, "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"));
     reportElement->parserSetAttributes(reportAttributes, FragmentScriptingNotAllowed);
 
@@ -100,7 +100,7 @@
     h3->parserAddChild(doc->createTextNode("This page contains the following errors:"));
 
     RefPtr<Element> fixed = doc->createElement(divTag, true);
-    AttributeVector fixedAttributes;
+    Vector<Attribute> fixedAttributes;
     fixedAttributes.append(Attribute(styleAttr, "font-family:monospace;font-size:12px"));
     fixed->parserSetAttributes(fixedAttributes, FragmentScriptingNotAllowed);
     reportElement->parserAddChild(fixed.get());
@@ -158,7 +158,7 @@
 
 #if ENABLE(XSLT)
     if (m_document->transformSourceDocument()) {
-        AttributeVector attributes;
+        Vector<Attribute> attributes;
         attributes.append(Attribute(styleAttr, "white-space: normal"));
         RefPtr<Element> paragraph = m_document->createElement(pTag, true);
         paragraph->parserSetAttributes(attributes, FragmentScriptingNotAllowed);

Modified: trunk/Source/WebCore/xml/parser/MarkupTokenBase.h (115098 => 115099)


--- trunk/Source/WebCore/xml/parser/MarkupTokenBase.h	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/xml/parser/MarkupTokenBase.h	2012-04-24 19:59:02 UTC (rev 115099)
@@ -409,7 +409,7 @@
         }
     }
 
-    AtomicMarkupTokenBase(typename Token::Type::Type type, AtomicString name, const AttributeVector& attributes = AttributeVector())
+    AtomicMarkupTokenBase(typename Token::Type::Type type, AtomicString name, const Vector<Attribute>& attributes = Vector<Attribute>())
         : m_type(type)
         , m_name(name)
         , m_externalCharacters(0)
@@ -441,16 +441,16 @@
     Attribute* getAttributeItem(const QualifiedName& attributeName)
     {
         ASSERT(usesAttributes());
-        return m_attributes.getAttributeItem(attributeName);
+        return findAttributeInVector(m_attributes, attributeName);
     }
 
-    AttributeVector& attributes()
+    Vector<Attribute>& attributes()
     {
         ASSERT(usesAttributes());
         return m_attributes;
     }
 
-    const AttributeVector& attributes() const
+    const Vector<Attribute>& attributes() const
     {
         ASSERT(usesAttributes());
         return m_attributes;
@@ -514,7 +514,7 @@
     // For StartTag and EndTag
     bool m_selfClosing;
 
-    AttributeVector m_attributes;
+    Vector<Attribute> m_attributes;
 };
 
 template<typename Token>
@@ -539,7 +539,9 @@
         ASSERT(attribute.m_valueRange.m_end);
 
         AtomicString value(attribute.m_value.data(), attribute.m_value.size());
-        m_attributes.insertAttribute(Attribute(nameForAttribute(attribute), value));
+        const QualifiedName name = nameForAttribute(attribute);
+        if (!findAttributeInVector(m_attributes, name))
+            m_attributes.append(Attribute(name, value));
     }
 }
 

Modified: trunk/Source/WebCore/xml/parser/XMLToken.h (115098 => 115099)


--- trunk/Source/WebCore/xml/parser/XMLToken.h	2012-04-24 19:47:10 UTC (rev 115098)
+++ trunk/Source/WebCore/xml/parser/XMLToken.h	2012-04-24 19:59:02 UTC (rev 115099)
@@ -431,7 +431,7 @@
         }
     }
 
-    AtomicXMLToken(XMLTokenTypes::Type type, AtomicString name, const AttributeVector& attributes = AttributeVector())
+    AtomicXMLToken(XMLTokenTypes::Type type, AtomicString name, const Vector<Attribute>& attributes = Vector<Attribute>())
         : AtomicMarkupTokenBase<XMLToken>(type, name, attributes)
     {
     }
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to