Title: [92106] trunk/Source/WebCore
Revision
92106
Author
macpher...@chromium.org
Date
2011-08-01 01:31:43 -0700 (Mon, 01 Aug 2011)

Log Message

Add iterator to CSSValueList
https://bugs.webkit.org/show_bug.cgi?id=65297

Reviewed by Darin Adler.

No new tests / refactoring only.

* css/CSSPrimitiveValue.h:
(WebCore::CSSPrimitiveValue::isLength):
Add shorthand to determine if this primitive value is a length.
* css/CSSStyleSelector.cpp:
Use CSSValueListIterator throughout.
(WebCore::CSSStyleSelector::applyProperty):
(WebCore::CSSStyleSelector::applyPageSizeProperty):
(WebCore::CSSStyleSelector::createTransformOperations):
* css/CSSValueList.cpp:
(WebCore::CSSValueList::copy):
Use itemWithoutBoundsCheck() instead of item().
* css/CSSValueList.h:
Add CSSValueListIterator and CSSValueListInspector class definitions.
(WebCore::CSSValueList::item)
Provide inline definition of item.
(WebCore::CSSValueListIterator::CSSValueListIterator):
(WebCore::CSSValueListIterator::hasMore):
Return true if there are more values to consume, including the current value.
(WebCore::CSSValueListIterator::value):
Return the value at the current position.
(WebCore::CSSValueListIterator::next):
Move the iterator forward to the next item.
(WebCore::CSSValueListIterator::index):
Return the current position in the list.
(WebCore::CSSValueListInspector::item):
Return the item at a given index.
(WebCore::CSSValueListInspector::first):
Return the first item in the list.
(WebCore::CSSValueListInspector::second):
Return the second item in the list.
(WebCore::CSSValueListInspector::length):
Return the size of the underlying list.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (92105 => 92106)


--- trunk/Source/WebCore/ChangeLog	2011-08-01 08:17:05 UTC (rev 92105)
+++ trunk/Source/WebCore/ChangeLog	2011-08-01 08:31:43 UTC (rev 92106)
@@ -1,3 +1,45 @@
+2011-08-01  Luke Macpherson   <macpher...@chromium.org>
+
+        Add iterator to CSSValueList
+        https://bugs.webkit.org/show_bug.cgi?id=65297
+
+        Reviewed by Darin Adler.
+
+        No new tests / refactoring only.
+
+        * css/CSSPrimitiveValue.h:
+        (WebCore::CSSPrimitiveValue::isLength):
+        Add shorthand to determine if this primitive value is a length.
+        * css/CSSStyleSelector.cpp:
+        Use CSSValueListIterator throughout.
+        (WebCore::CSSStyleSelector::applyProperty):
+        (WebCore::CSSStyleSelector::applyPageSizeProperty):
+        (WebCore::CSSStyleSelector::createTransformOperations):
+        * css/CSSValueList.cpp:
+        (WebCore::CSSValueList::copy):
+        Use itemWithoutBoundsCheck() instead of item().
+        * css/CSSValueList.h:
+        Add CSSValueListIterator and CSSValueListInspector class definitions.
+        (WebCore::CSSValueList::item)
+        Provide inline definition of item.
+        (WebCore::CSSValueListIterator::CSSValueListIterator):
+        (WebCore::CSSValueListIterator::hasMore):
+        Return true if there are more values to consume, including the current value.
+        (WebCore::CSSValueListIterator::value):
+        Return the value at the current position.
+        (WebCore::CSSValueListIterator::next):
+        Move the iterator forward to the next item.
+        (WebCore::CSSValueListIterator::index):
+        Return the current position in the list.
+        (WebCore::CSSValueListInspector::item):
+        Return the item at a given index.
+        (WebCore::CSSValueListInspector::first):
+        Return the first item in the list.
+        (WebCore::CSSValueListInspector::second):
+        Return the second item in the list.
+        (WebCore::CSSValueListInspector::length):
+        Return the size of the underlying list.
+
 2011-08-01  Pavel Feldman  <pfeld...@google.com>
 
         Web Inspector: group scripts by folder in the scripts selector.

Modified: trunk/Source/WebCore/css/CSSPrimitiveValue.h (92105 => 92106)


--- trunk/Source/WebCore/css/CSSPrimitiveValue.h	2011-08-01 08:17:05 UTC (rev 92105)
+++ trunk/Source/WebCore/css/CSSPrimitiveValue.h	2011-08-01 08:31:43 UTC (rev 92106)
@@ -119,6 +119,8 @@
     static bool isUnitTypeLength(int type) { return (type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG) ||
                                                     type == CSSPrimitiveValue::CSS_REMS; }
 
+    bool isLength() const { return isUnitTypeLength(m_type); }
+
     static PassRefPtr<CSSPrimitiveValue> createIdentifier(int identifier) { return adoptRef(new CSSPrimitiveValue(identifier)); }
     static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue) { return adoptRef(new CSSPrimitiveValue(rgbValue)); }
     static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type) { return adoptRef(new CSSPrimitiveValue(value, type)); }

Modified: trunk/Source/WebCore/css/CSSStyleSelector.cpp (92105 => 92106)


--- trunk/Source/WebCore/css/CSSStyleSelector.cpp	2011-08-01 08:17:05 UTC (rev 92105)
+++ trunk/Source/WebCore/css/CSSStyleSelector.cpp	2011-08-01 08:31:43 UTC (rev 92106)
@@ -191,11 +191,10 @@
 size_t childIndex = 0; \
 if (value->isValueList()) { \
     /* Walk each value and put it into an animation, creating new animations as needed. */ \
-    CSSValueList* valueList = static_cast<CSSValueList*>(value); \
-    for (unsigned int i = 0; i < valueList->length(); i++) { \
+    for (CSSValueListIterator i = value; i.hasMore(); i.advance()) { \
         if (childIndex <= list->size()) \
             list->append(Animation::create()); \
-        mapAnimation##Prop(list->animation(childIndex), valueList->itemWithoutBoundsCheck(i)); \
+        mapAnimation##Prop(list->animation(childIndex), i.value()); \
         ++childIndex; \
     } \
 } else { \
@@ -241,11 +240,10 @@
 size_t childIndex = 0; \
 if (value->isValueList()) { \
     /* Walk each value and put it into a transition, creating new animations as needed. */ \
-    CSSValueList* valueList = static_cast<CSSValueList*>(value); \
-    for (unsigned int i = 0; i < valueList->length(); i++) { \
+    for (CSSValueListIterator i = value; i.hasMore(); i.advance()) { \
         if (childIndex <= list->size()) \
             list->append(Animation::create()); \
-        mapAnimation##Prop(list->animation(childIndex), valueList->itemWithoutBoundsCheck(i)); \
+        mapAnimation##Prop(list->animation(childIndex), i.value()); \
         ++childIndex; \
     } \
 } else { \
@@ -4018,12 +4016,10 @@
         if (!value->isValueList())
             return;
 
-        CSSValueList* list = static_cast<CSSValueList*>(value);
-        int len = list->length();
-
 #if ENABLE(CSS_REGIONS)
-        if (len == 1 && list->itemWithoutBoundsCheck(0)->isPrimitiveValue()) {
-            CSSPrimitiveValue* contentValue = static_cast<CSSPrimitiveValue*>(list->itemWithoutBoundsCheck(0));
+        CSSValueListInspector inspector = value;
+        if (inspector.length() == 1 && inspector.first()->isPrimitiveValue()) {
+            CSSPrimitiveValue* contentValue = static_cast<CSSPrimitiveValue*>(inspector->first());
             if (contentValue->primitiveType() == CSSPrimitiveValue::CSS_FROM_FLOW) {
                 m_style->setRegionThread(contentValue->getStringValue().impl());
                 return;
@@ -4032,8 +4028,8 @@
 #endif
 
         bool didSet = false;
-        for (int i = 0; i < len; i++) {
-            CSSValue* item = list->itemWithoutBoundsCheck(i);
+        for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
+            CSSValue* item = i.value();
             if (item->isImageGeneratorValue()) {
                 m_style->setContent(static_cast<CSSImageGeneratorValue*>(item)->generatedImage(), didSet);
                 didSet = true;
@@ -4116,17 +4112,16 @@
         }
         if (value->isValueList()) {
             CSSValueList* list = static_cast<CSSValueList*>(value);
-            size_t length = list->length();
-            QuotesData* data = ""
+            QuotesData* data = ""
             if (!data)
                 return; // Out of memory
             String* quotes = data->data();
-            for (size_t i = 0; i < length; i++) {
-                CSSValue* item = list->itemWithoutBoundsCheck(i);
+            for (CSSValueListIterator i = list; i.hasMore(); i.advance()) {
+                CSSValue* item = i.value();
                 ASSERT(item->isPrimitiveValue());
                 primitiveValue = static_cast<CSSPrimitiveValue*>(item);
                 ASSERT(primitiveValue->primitiveType() == CSSPrimitiveValue::CSS_STRING);
-                quotes[i] = primitiveValue->getStringValue();
+                quotes[i.index()] = primitiveValue->getStringValue();
             }
             m_style->setQuotes(adoptRef(data));
         } else if (primitiveValue) {
@@ -4162,17 +4157,15 @@
         if (!value->isValueList())
             return;
         FontDescription fontDescription = m_style->fontDescription();
-        CSSValueList* list = static_cast<CSSValueList*>(value);
-        int len = list->length();
         FontFamily& firstFamily = fontDescription.firstFamily();
         FontFamily* currFamily = 0;
-        
+
         // Before mapping in a new font-family property, we should reset the generic family.
         bool oldFamilyUsedFixedDefaultSize = fontDescription.useFixedDefaultSize();
         fontDescription.setGenericFamily(FontDescription::NoFamily);
 
-        for (int i = 0; i < len; i++) {
-            CSSValue* item = list->itemWithoutBoundsCheck(i);
+        for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
+            CSSValue* item = i.value();
             if (!item->isPrimitiveValue())
                 continue;
             CSSPrimitiveValue* contentValue = static_cast<CSSPrimitiveValue*>(item);
@@ -4248,11 +4241,9 @@
         } else {
             if (!value->isValueList())
                 return;
-            CSSValueList *list = static_cast<CSSValueList*>(value);
-            int len = list->length();
-            for (int i = 0; i < len; i++)
+            for (CSSValueListIterator i = value; i.hasMore(); i.advance())
             {
-                CSSValue *item = list->itemWithoutBoundsCheck(i);
+                CSSValue* item = i.value();
                 if (!item->isPrimitiveValue())
                     continue;
                 primitiveValue = static_cast<CSSPrimitiveValue*>(item);
@@ -4453,13 +4444,11 @@
         if (!value->isValueList())
             return;
 
-        CSSValueList *list = static_cast<CSSValueList*>(value);
-        int len = list->length();
-        for (int i = 0; i < len; i++) {
-            CSSValue* currValue = list->itemWithoutBoundsCheck(i);
+        for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
+            CSSValue* currValue = i.value();
             if (!currValue->isShadowValue())
                 continue;
-            ShadowValue* item = static_cast<ShadowValue*>(list->itemWithoutBoundsCheck(i));
+            ShadowValue* item = static_cast<ShadowValue*>(currValue);
             int x = item->x->computeLength<int>(style(), m_rootElementStyle, zoomFactor);
             int y = item->y->computeLength<int>(style(), m_rootElementStyle, zoomFactor);
             int blur = item->blur ? item->blur->computeLength<int>(style(), m_rootElementStyle, zoomFactor) : 0;
@@ -4470,9 +4459,9 @@
                 color = getColorFromPrimitiveValue(item->color.get());
             OwnPtr<ShadowData> shadowData = adoptPtr(new ShadowData(x, y, blur, spread, shadowStyle, id == CSSPropertyWebkitBoxShadow, color.isValid() ? color : Color::transparent));
             if (id == CSSPropertyTextShadow)
-                m_style->setTextShadow(shadowData.release(), i /* add to the list if this is not the firsty entry */);
+                m_style->setTextShadow(shadowData.release(), i.index()); // add to the list if this is not the first entry
             else
-                m_style->setBoxShadow(shadowData.release(), i /* add to the list if this is not the firsty entry */);
+                m_style->setBoxShadow(shadowData.release(), i.index()); // add to the list if this is not the first entry
         }
         return;
     }
@@ -5285,48 +5274,43 @@
 void CSSStyleSelector::applyPageSizeProperty(CSSValue* value)
 {
     m_style->resetPageSizeType();
-    if (!value->isValueList())
-        return;
-    CSSValueList* valueList = static_cast<CSSValueList*>(value);
     Length width;
     Length height;
     PageSizeType pageSizeType = PAGE_SIZE_AUTO;
-    switch (valueList->length()) {
+    CSSValueListInspector inspector = value;
+    switch (inspector.length()) {
     case 2: {
         // <length>{2} | <page-size> <orientation>
         pageSizeType = PAGE_SIZE_RESOLVED;
-        if (!valueList->item(0)->isPrimitiveValue() || !valueList->item(1)->isPrimitiveValue())
+        if (!inspector.first()->isPrimitiveValue() || !inspector.second()->isPrimitiveValue())
             return;
-        CSSPrimitiveValue* primitiveValue0 = static_cast<CSSPrimitiveValue*>(valueList->item(0));
-        CSSPrimitiveValue* primitiveValue1 = static_cast<CSSPrimitiveValue*>(valueList->item(1));
-        int type0 = primitiveValue0->primitiveType();
-        int type1 = primitiveValue1->primitiveType();
-        if (CSSPrimitiveValue::isUnitTypeLength(type0)) {
+        CSSPrimitiveValue* first = static_cast<CSSPrimitiveValue*>(inspector.first());
+        CSSPrimitiveValue* second = static_cast<CSSPrimitiveValue*>(inspector.second());
+        if (first->isLength()) {
             // <length>{2}
-            if (!CSSPrimitiveValue::isUnitTypeLength(type1))
+            if (!second->isLength())
                 return;
-            width = primitiveValue0->computeLength<Length>(style(), m_rootElementStyle);
-            height = primitiveValue1->computeLength<Length>(style(), m_rootElementStyle);
+            width = first->computeLength<Length>(style(), m_rootElementStyle);
+            height = second->computeLength<Length>(style(), m_rootElementStyle);
         } else {
             // <page-size> <orientation>
             // The value order is guaranteed. See CSSParser::parseSizeParameter.
-            if (!pageSizeFromName(primitiveValue0, primitiveValue1, width, height))
+            if (!pageSizeFromName(first, second, width, height))
                 return;
         }
         break;
     }
     case 1: {
         // <length> | auto | <page-size> | [ portrait | landscape]
-        if (!valueList->item(0)->isPrimitiveValue())
+        if (!inspector.first()->isPrimitiveValue())
             return;
-        CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(valueList->item(0));
-        int type = primitiveValue->primitiveType();
-        if (CSSPrimitiveValue::isUnitTypeLength(type)) {
+        CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(inspector.first());
+        if (primitiveValue->isLength()) {
             // <length>
             pageSizeType = PAGE_SIZE_RESOLVED;
             width = height = primitiveValue->computeLength<Length>(style(), m_rootElementStyle);
         } else {
-            if (type != CSSPrimitiveValue::CSS_IDENT)
+            if (primitiveValue->primitiveType() != CSSPrimitiveValue::CSS_IDENT)
                 return;
             switch (primitiveValue->getIdent()) {
             case CSSValueAuto:
@@ -6280,14 +6264,12 @@
 
     float zoomFactor = style ? style->effectiveZoom() : 1;
     TransformOperations operations;
-    CSSValueList* list = static_cast<CSSValueList*>(inValue);
-    unsigned size = list->length();
-    for (unsigned i = 0; i < size; i++) {
-        CSSValue* currValue = list->itemWithoutBoundsCheck(i);
+    for (CSSValueListIterator i = inValue; i.hasMore(); i.advance()) {
+        CSSValue* currValue = i.value();
         if (!currValue->isWebKitCSSTransformValue())
             continue;
 
-        WebKitCSSTransformValue* transformValue = static_cast<WebKitCSSTransformValue*>(list->itemWithoutBoundsCheck(i));
+        WebKitCSSTransformValue* transformValue = static_cast<WebKitCSSTransformValue*>(i.value());
         if (!transformValue->length())
             continue;
 

Modified: trunk/Source/WebCore/css/CSSValueList.cpp (92105 => 92106)


--- trunk/Source/WebCore/css/CSSValueList.cpp	2011-08-01 08:17:05 UTC (rev 92105)
+++ trunk/Source/WebCore/css/CSSValueList.cpp	2011-08-01 08:31:43 UTC (rev 92106)
@@ -46,13 +46,6 @@
 {
 }
 
-CSSValue* CSSValueList::item(unsigned index)
-{
-    if (index >= m_values.size())
-        return 0;
-    return m_values[index].get();
-}
-
 unsigned short CSSValueList::cssValueType() const
 {
     return CSS_VALUE_LIST;
@@ -98,7 +91,7 @@
 {
     PassRefPtr<CSSValueList> newList = m_isSpaceSeparated ? createSpaceSeparated() : createCommaSeparated();
     for (size_t index = 0; index < m_values.size(); index++)
-        newList->append(item(index));
+        newList->append(m_values[index]);
     return newList;
 }
 

Modified: trunk/Source/WebCore/css/CSSValueList.h (92105 => 92106)


--- trunk/Source/WebCore/css/CSSValueList.h	2011-08-01 08:17:05 UTC (rev 92105)
+++ trunk/Source/WebCore/css/CSSValueList.h	2011-08-01 08:31:43 UTC (rev 92106)
@@ -47,8 +47,8 @@
     virtual ~CSSValueList();
 
     size_t length() const { return m_values.size(); }
-    CSSValue* item(unsigned);
-    CSSValue* itemWithoutBoundsCheck(unsigned index) { return m_values[index].get(); }
+    CSSValue* item(size_t index) { return index < m_values.size() ? m_values[index].get() : 0; }
+    CSSValue* itemWithoutBoundsCheck(size_t index) { return m_values[index].get(); }
 
     void append(PassRefPtr<CSSValue>);
     void prepend(PassRefPtr<CSSValue>);
@@ -73,6 +73,33 @@
     bool m_isSpaceSeparated;
 };
 
+// Objects of this class are intended to be stack-allocated and scoped to a single function.
+// Please take care not to pass these around as they do hold onto a raw pointer.
+class CSSValueListInspector {
+public:
+    CSSValueListInspector(CSSValue* value) : m_list((value && value->isValueList()) ? static_cast<CSSValueList*>(value) : 0) { }
+    CSSValue* item(size_t index) const { ASSERT(index < length()); return m_list->itemWithoutBoundsCheck(index); }
+    CSSValue* first() const { return item(0); }
+    CSSValue* second() const { return item(1); }
+    size_t length() const { return m_list ? m_list->length() : 0; }
+private:
+    CSSValueList* m_list;
+};
+
+// Wrapper that can be used to iterate over any CSSValue. Non-list values and 0 behave as zero-length lists.
+// Objects of this class are intended to be stack-allocated and scoped to a single function.
+// Please take care not to pass these around as they do hold onto a raw pointer.
+class CSSValueListIterator {
+public:
+    CSSValueListIterator(CSSValue* value) : m_inspector(value), m_position(0) { }
+    bool hasMore() const { return m_position < m_inspector.length(); }
+    CSSValue* value() const { return m_inspector.item(m_position); }
+    void advance() { m_position++; ASSERT(m_position <= m_inspector.length());}
+    size_t index() const { return m_position; }
+private:
+    CSSValueListInspector m_inspector;
+    size_t m_position;
+};
 } // namespace WebCore
 
 #endif // CSSValueList_h
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to