2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008, 2012 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #ifndef StylePropertySet_h
22 #define StylePropertySet_h
24 #include "CSSPropertyNames.h"
25 #include "core/css/CSSParserMode.h"
26 #include "core/css/CSSPrimitiveValue.h"
27 #include "core/css/CSSProperty.h"
28 #include "core/css/PropertySetCSSStyleDeclaration.h"
29 #include "wtf/ListHashSet.h"
30 #include "wtf/Vector.h"
31 #include "wtf/text/WTFString.h"
36 class CSSStyleDeclaration;
38 class ImmutableStylePropertySet;
40 class MutableStylePropertySet;
41 class StylePropertyShorthand;
42 class StyleSheetContents;
44 class StylePropertySet : public RefCounted<StylePropertySet> {
45 friend class PropertyReference;
47 // Override RefCounted's deref() to ensure operator delete is called on
48 // the appropriate subclass type.
51 class PropertyReference {
53 PropertyReference(const StylePropertySet& propertySet, unsigned index)
54 : m_propertySet(propertySet)
59 CSSPropertyID id() const { return static_cast<CSSPropertyID>(propertyMetadata().m_propertyID); }
60 CSSPropertyID shorthandID() const { return propertyMetadata().shorthandID(); }
62 bool isImportant() const { return propertyMetadata().m_important; }
63 bool isInherited() const { return propertyMetadata().m_inherited; }
64 bool isImplicit() const { return propertyMetadata().m_implicit; }
66 String cssName() const;
67 String cssText() const;
69 const CSSValue* value() const { return propertyValue(); }
70 // FIXME: We should try to remove this mutable overload.
71 CSSValue* value() { return const_cast<CSSValue*>(propertyValue()); }
73 // FIXME: Remove this.
74 CSSProperty toCSSProperty() const { return CSSProperty(propertyMetadata(), const_cast<CSSValue*>(propertyValue())); }
76 const StylePropertyMetadata& propertyMetadata() const;
79 const CSSValue* propertyValue() const;
81 const StylePropertySet& m_propertySet;
85 unsigned propertyCount() const;
87 PropertyReference propertyAt(unsigned index) const { return PropertyReference(*this, index); }
88 int findPropertyIndex(CSSPropertyID) const;
90 PassRefPtr<CSSValue> getPropertyCSSValue(CSSPropertyID) const;
91 String getPropertyValue(CSSPropertyID) const;
93 bool propertyIsImportant(CSSPropertyID) const;
94 CSSPropertyID getPropertyShorthand(CSSPropertyID) const;
95 bool isPropertyImplicit(CSSPropertyID) const;
97 PassRefPtr<MutableStylePropertySet> copyBlockProperties() const;
99 CSSParserMode cssParserMode() const { return static_cast<CSSParserMode>(m_cssParserMode); }
101 PassRefPtr<MutableStylePropertySet> mutableCopy() const;
102 PassRefPtr<ImmutableStylePropertySet> immutableCopyIfNeeded() const;
104 PassRefPtr<MutableStylePropertySet> copyPropertiesInSet(const Vector<CSSPropertyID>&) const;
106 String asText() const;
108 bool isMutable() const { return m_isMutable; }
110 bool hasFailedOrCanceledSubresources() const;
112 static unsigned averageSizeInBytes();
118 bool propertyMatches(CSSPropertyID, const CSSValue*) const;
122 enum { MaxArraySize = (1 << 28) - 1 };
124 StylePropertySet(CSSParserMode cssParserMode)
125 : m_cssParserMode(cssParserMode)
130 StylePropertySet(CSSParserMode cssParserMode, unsigned immutableArraySize)
131 : m_cssParserMode(cssParserMode)
133 , m_arraySize(std::min(immutableArraySize, unsigned(MaxArraySize)))
136 unsigned m_cssParserMode : 3;
137 mutable unsigned m_isMutable : 1;
138 unsigned m_arraySize : 28;
140 friend class PropertySetCSSStyleDeclaration;
143 class ImmutableStylePropertySet : public StylePropertySet {
145 ~ImmutableStylePropertySet();
146 static PassRefPtr<ImmutableStylePropertySet> create(const CSSProperty* properties, unsigned count, CSSParserMode);
148 unsigned propertyCount() const { return m_arraySize; }
150 const CSSValue** valueArray() const;
151 const StylePropertyMetadata* metadataArray() const;
156 ImmutableStylePropertySet(const CSSProperty*, unsigned count, CSSParserMode);
159 inline const CSSValue** ImmutableStylePropertySet::valueArray() const
161 return reinterpret_cast<const CSSValue**>(const_cast<const void**>(&(this->m_storage)));
164 inline const StylePropertyMetadata* ImmutableStylePropertySet::metadataArray() const
166 return reinterpret_cast<const StylePropertyMetadata*>(&reinterpret_cast<const char*>(&(this->m_storage))[m_arraySize * sizeof(CSSValue*)]);
169 DEFINE_TYPE_CASTS(ImmutableStylePropertySet, StylePropertySet, set, !set->isMutable(), !set.isMutable());
171 inline ImmutableStylePropertySet* toImmutableStylePropertySet(const RefPtr<StylePropertySet>& set)
173 return toImmutableStylePropertySet(set.get());
176 class MutableStylePropertySet : public StylePropertySet {
178 ~MutableStylePropertySet() { }
179 static PassRefPtr<MutableStylePropertySet> create(CSSParserMode = HTMLQuirksMode);
180 static PassRefPtr<MutableStylePropertySet> create(const CSSProperty* properties, unsigned count);
182 unsigned propertyCount() const { return m_propertyVector.size(); }
184 void addParsedProperties(const Vector<CSSProperty, 256>&);
185 void addParsedProperty(const CSSProperty&);
187 // These expand shorthand properties into multiple properties.
188 bool setProperty(CSSPropertyID, const String& value, bool important = false, StyleSheetContents* contextStyleSheet = 0);
189 void setProperty(CSSPropertyID, PassRefPtr<CSSValue>, bool important = false);
191 // These do not. FIXME: This is too messy, we can do better.
192 bool setProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
193 bool setProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
194 void appendPrefixingVariantProperty(const CSSProperty&);
195 void setPrefixingVariantProperty(const CSSProperty&);
196 void setProperty(const CSSProperty&, CSSProperty* slot = 0);
198 bool removeProperty(CSSPropertyID, String* returnText = 0);
199 void removePrefixedOrUnprefixedProperty(CSSPropertyID);
200 void removeBlockProperties();
201 bool removePropertiesInSet(const CSSPropertyID* set, unsigned length);
202 void removeEquivalentProperties(const StylePropertySet*);
203 void removeEquivalentProperties(const CSSStyleDeclaration*);
205 void mergeAndOverrideOnConflict(const StylePropertySet*);
208 void parseDeclaration(const String& styleDeclaration, StyleSheetContents* contextStyleSheet);
210 CSSStyleDeclaration* ensureCSSStyleDeclaration();
212 Vector<CSSProperty, 4> m_propertyVector;
215 explicit MutableStylePropertySet(CSSParserMode);
216 explicit MutableStylePropertySet(const StylePropertySet&);
217 MutableStylePropertySet(const CSSProperty* properties, unsigned count);
219 bool removeShorthandProperty(CSSPropertyID);
220 CSSProperty* findCSSPropertyWithID(CSSPropertyID);
221 OwnPtr<PropertySetCSSStyleDeclaration> m_cssomWrapper;
223 friend class StylePropertySet;
226 DEFINE_TYPE_CASTS(MutableStylePropertySet, StylePropertySet, set, set->isMutable(), set.isMutable());
228 inline MutableStylePropertySet* toMutableStylePropertySet(const RefPtr<StylePropertySet>& set)
230 return toMutableStylePropertySet(set.get());
233 inline const StylePropertyMetadata& StylePropertySet::PropertyReference::propertyMetadata() const
235 if (m_propertySet.isMutable())
236 return toMutableStylePropertySet(m_propertySet).m_propertyVector.at(m_index).metadata();
237 return toImmutableStylePropertySet(m_propertySet).metadataArray()[m_index];
240 inline const CSSValue* StylePropertySet::PropertyReference::propertyValue() const
242 if (m_propertySet.isMutable())
243 return toMutableStylePropertySet(m_propertySet).m_propertyVector.at(m_index).value();
244 return toImmutableStylePropertySet(m_propertySet).valueArray()[m_index];
247 inline unsigned StylePropertySet::propertyCount() const
250 return toMutableStylePropertySet(this)->m_propertyVector.size();
254 inline bool StylePropertySet::isEmpty() const
256 return !propertyCount();
259 inline void StylePropertySet::deref()
265 delete toMutableStylePropertySet(this);
267 delete toImmutableStylePropertySet(this);
270 } // namespace WebCore
272 #endif // StylePropertySet_h