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

Unified Diff: third_party/WebKit/Source/core/css/StylePropertySet.cpp

Issue 1455053003: Custom property CSSOM patch with templates [not for landing] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@_levisPatch
Patch Set: Created 5 years, 1 month 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
« no previous file with comments | « third_party/WebKit/Source/core/css/StylePropertySet.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/css/StylePropertySet.cpp
diff --git a/third_party/WebKit/Source/core/css/StylePropertySet.cpp b/third_party/WebKit/Source/core/css/StylePropertySet.cpp
index e0eaf6b7ba7c5f72b5a65eae77c77bed7f827454..3efac37118cb8ef463f753c7544b222a878dd61a 100644
--- a/third_party/WebKit/Source/core/css/StylePropertySet.cpp
+++ b/third_party/WebKit/Source/core/css/StylePropertySet.cpp
@@ -24,6 +24,7 @@
#include "core/css/StylePropertySet.h"
#include "core/StylePropertyShorthand.h"
+#include "core/css/CSSCustomPropertyDeclaration.h"
#include "core/css/CSSPropertyMetadata.h"
#include "core/css/CSSValuePool.h"
#include "core/css/StylePropertySerializer.h"
@@ -103,31 +104,40 @@ ImmutableStylePropertySet::~ImmutableStylePropertySet()
#endif
}
-int ImmutableStylePropertySet::findPropertyIndex(CSSPropertyID propertyID) const
+// Convert property into an uint16_t for comparison with metadata's m_propertyID to avoid
+// the compiler converting it to an int multiple times in a loop.
+static uint16_t getConvertedCSSPropertyID(CSSPropertyID propertyID)
{
- // Convert here propertyID into an uint16_t to compare it with the metadata's m_propertyID to avoid
- // the compiler converting it to an int multiple times in the loop.
- uint16_t id = static_cast<uint16_t>(propertyID);
- for (int n = m_arraySize - 1 ; n >= 0; --n) {
- if (metadataArray()[n].m_propertyID == id) {
- // Only enabled properties should be part of the style.
- ASSERT(CSSPropertyMetadata::isEnabledProperty(propertyID));
- return n;
- }
- }
+ return static_cast<uint16_t>(propertyID);
+}
- return -1;
+static uint16_t getConvertedCSSPropertyID(const AtomicString&)
+{
+ return static_cast<uint16_t>(CSSPropertyVariable);
+}
+
+static bool isPropertyMatch(const StylePropertyMetadata& metadata, const CSSValue&, uint16_t id, CSSPropertyID propertyID)
+{
+ ASSERT(id == propertyID);
+ bool result = metadata.m_propertyID == id;
+ // Only enabled properties should be part of the style.
+ ASSERT(!result || CSSPropertyMetadata::isEnabledProperty(propertyID));
+ return result;
}
-int ImmutableStylePropertySet::findCustomPropertyIndex(const AtomicString& propertyName) const
+static bool isPropertyMatch(const StylePropertyMetadata& metadata, const CSSValue& value, uint16_t id, const AtomicString& customPropertyName)
{
- // Convert here propertyID into an uint16_t to compare it with the metadata's m_propertyID to avoid
- // the compiler converting it to an int multiple times in the loop.
- const uint16_t variableId = static_cast<uint16_t>(CSSPropertyVariable);
+ ASSERT(id == CSSPropertyVariable);
+ return metadata.m_propertyID == id
+ && toCSSCustomPropertyDeclaration(value).name() == customPropertyName;
+}
+template<typename T>
+int ImmutableStylePropertySet::findPropertyIndex(T property) const
+{
+ uint16_t id = getConvertedCSSPropertyID(property);
for (int n = m_arraySize - 1 ; n >= 0; --n) {
- if (metadataArray()[n].m_propertyID == variableId
- && toCSSCustomPropertyDeclaration(valueArray()[n])->name() == propertyName)
+ if (isPropertyMatch(metadataArray()[n], *valueArray()[n], id, property))
return n;
}
@@ -154,35 +164,35 @@ MutableStylePropertySet::MutableStylePropertySet(const StylePropertySet& other)
}
}
-String StylePropertySet::getPropertyValue(CSSPropertyID propertyID) const
+static String serializeShorthand(const StylePropertySet& propertySet, CSSPropertyID propertyID)
{
- RefPtrWillBeRawPtr<CSSValue> value = getPropertyCSSValue(propertyID);
- if (value)
- return value->cssText();
-
- return StylePropertySerializer(*this).getPropertyValue(propertyID);
+ return StylePropertySerializer(propertySet).getPropertyValue(propertyID);
}
-String StylePropertySet::getCustomPropertyValue(const AtomicString& propertyName) const
+static String serializeShorthand(const StylePropertySet&, const AtomicString& customPropertyName)
{
- RefPtrWillBeRawPtr<CSSValue> value = getCustomPropertyCSSValue(propertyName);
- return value ? value->cssText() : "";
+ // Custom properties are never shorthands.
+ return "";
}
-PassRefPtrWillBeRawPtr<CSSValue> StylePropertySet::getPropertyCSSValue(CSSPropertyID propertyID) const
+template<typename T>
+String StylePropertySet::getPropertyValue(T property) const
{
- int foundPropertyIndex = findPropertyIndex(propertyID);
- if (foundPropertyIndex == -1)
- return nullptr;
- return propertyAt(foundPropertyIndex).value();
+ RefPtrWillBeRawPtr<CSSValue> value = getPropertyCSSValue(property);
+ if (value)
+ return value->cssText();
+ return serializeShorthand(*this, property);
}
+template String StylePropertySet::getPropertyValue<CSSPropertyID>(CSSPropertyID) const;
+template String StylePropertySet::getPropertyValue<AtomicString>(AtomicString) const;
-PassRefPtrWillBeRawPtr<CSSCustomPropertyDeclaration> StylePropertySet::getCustomPropertyCSSValue(const AtomicString& propertyName) const
+template<typename T>
+PassRefPtrWillBeRawPtr<CSSValue> StylePropertySet::getPropertyCSSValue(T property) const
{
- int foundPropertyIndex = findCustomPropertyIndex(propertyName);
+ int foundPropertyIndex = findPropertyIndex(property);
if (foundPropertyIndex == -1)
return nullptr;
- return toCSSCustomPropertyDeclaration(propertyAt(foundPropertyIndex).value());
+ return propertyAt(foundPropertyIndex).value();
}
DEFINE_TRACE(StylePropertySet)
@@ -230,31 +240,33 @@ bool MutableStylePropertySet::removePropertyAtIndex(int propertyIndex, String* r
return true;
}
-bool MutableStylePropertySet::removeProperty(CSSPropertyID propertyID, String* returnText)
+template<typename T>
+bool MutableStylePropertySet::removeProperty(T property, String* returnText)
{
- if (removeShorthandProperty(propertyID)) {
+ if (removeShorthandProperty(property)) {
// FIXME: Return an equivalent shorthand when possible.
if (returnText)
*returnText = "";
return true;
}
- int foundPropertyIndex = findPropertyIndex(propertyID);
+ int foundPropertyIndex = findPropertyIndex(property);
return removePropertyAtIndex(foundPropertyIndex, returnText);
}
-bool MutableStylePropertySet::removeCustomProperty(const AtomicString& propertyName, String* returnText)
+template<typename T>
+bool StylePropertySet::propertyIsImportant(T property) const
{
- int foundPropertyIndex = findCustomPropertyIndex(propertyName);
- return removePropertyAtIndex(foundPropertyIndex, returnText);
-}
-
-bool StylePropertySet::propertyIsImportant(CSSPropertyID propertyID) const
-{
- int foundPropertyIndex = findPropertyIndex(propertyID);
+ int foundPropertyIndex = findPropertyIndex(property);
if (foundPropertyIndex != -1)
return propertyAt(foundPropertyIndex).isImportant();
+ return shorthandIsImportant(property);
+}
+template bool StylePropertySet::propertyIsImportant<CSSPropertyID>(CSSPropertyID) const;
+template bool StylePropertySet::propertyIsImportant<AtomicString>(AtomicString) const;
+bool StylePropertySet::shorthandIsImportant(CSSPropertyID propertyID) const
+{
StylePropertyShorthand shorthand = shorthandForProperty(propertyID);
if (!shorthand.length())
return false;
@@ -266,11 +278,9 @@ bool StylePropertySet::propertyIsImportant(CSSPropertyID propertyID) const
return true;
}
-bool StylePropertySet::customPropertyIsImportant(const AtomicString& propertyName) const
+bool StylePropertySet::shorthandIsImportant(const AtomicString& customPropertyName) const
{
- int foundPropertyIndex = findCustomPropertyIndex(propertyName);
- if (foundPropertyIndex != -1)
- return propertyAt(foundPropertyIndex).isImportant();
+ // Custom properties are never shorthands.
return false;
}
@@ -302,11 +312,11 @@ bool MutableStylePropertySet::setProperty(CSSPropertyID unresolvedProperty, cons
return CSSParser::parseValue(this, unresolvedProperty, value, important, contextStyleSheet);
}
-bool MutableStylePropertySet::setCustomProperty(const AtomicString& propertyName, const String& value, bool important, StyleSheetContents* contextStyleSheet)
+bool MutableStylePropertySet::setProperty(const AtomicString& customPropertyName, const String& value, bool important, StyleSheetContents* contextStyleSheet)
{
if (value.isEmpty())
- return removeCustomProperty(propertyName);
- return CSSParser::parseValueForCustomProperty(this, propertyName, value, important, contextStyleSheet);
+ return removeProperty(customPropertyName);
+ return CSSParser::parseValueForCustomProperty(this, customPropertyName, value, important, contextStyleSheet);
}
void MutableStylePropertySet::setProperty(CSSPropertyID propertyID, PassRefPtrWillBeRawPtr<CSSValue> prpValue, bool important)
@@ -520,44 +530,17 @@ CSSStyleDeclaration* MutableStylePropertySet::ensureCSSStyleDeclaration()
return m_cssomWrapper.get();
}
-int MutableStylePropertySet::findPropertyIndex(CSSPropertyID propertyID) const
-{
- const CSSProperty* begin = m_propertyVector.data();
- const CSSProperty* end = begin + m_propertyVector.size();
- // Convert here propertyID into an uint16_t to compare it with the metadata's m_propertyID to avoid
- // the compiler converting it to an int multiple times in the loop.
- uint16_t id = static_cast<uint16_t>(propertyID);
-
- auto compare = [propertyID, id](const CSSProperty& property) -> bool {
- if (property.metadata().m_propertyID == id) {
- // Only enabled properties should be part of the style.
- ASSERT(CSSPropertyMetadata::isEnabledProperty(propertyID));
- return true;
- }
- return false;
- };
-
- const CSSProperty* it = std::find_if(begin, end, compare);
-
- return (it == end) ? -1 : it - begin;
-}
-
-int MutableStylePropertySet::findCustomPropertyIndex(const AtomicString& propertyName) const
+template<typename T>
+int MutableStylePropertySet::findPropertyIndex(T property) const
{
const CSSProperty* begin = m_propertyVector.data();
const CSSProperty* end = begin + m_propertyVector.size();
- // Convert here propertyID into an uint16_t to compare it with the metadata's m_propertyID to avoid
- // the compiler converting it to an int multiple times in the loop.
- const uint16_t variableId = static_cast<uint16_t>(CSSPropertyVariable);
- auto compare = [variableId, propertyName](const CSSProperty& property) -> bool {
- if (property.metadata().m_propertyID == variableId) {
- return toCSSCustomPropertyDeclaration(property.value())->name() == propertyName;
- }
- return false;
- };
+ uint16_t id = getConvertedCSSPropertyID(property);
- const CSSProperty* it = std::find_if(begin, end, compare);
+ const CSSProperty* it = std::find_if(begin, end, [property, id](const CSSProperty& cssProperty) -> bool {
+ return isPropertyMatch(cssProperty.metadata(), *cssProperty.value(), id, property);
+ });
return (it == end) ? -1 : it - begin;
}
« no previous file with comments | « third_party/WebKit/Source/core/css/StylePropertySet.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698