2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008, 2012, 2013 Apple Inc. All rights reserved.
4 * Copyright (C) 2013 Intel Corporation. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "CSSParserMode.h"
25 #include "CSSParserTokenRange.h"
26 #include "CSSProperty.h"
27 #include "CSSValueKeywords.h"
29 #include <wtf/ListHashSet.h>
30 #include <wtf/TypeCasts.h>
31 #include <wtf/Vector.h>
32 #include <wtf/text/WTFString.h>
36 class CSSDeferredParser;
37 class CSSStyleDeclaration;
39 class ImmutableStyleProperties;
41 class MutableStyleProperties;
42 class PropertySetCSSStyleDeclaration;
44 class StylePropertyShorthand;
45 class StyleSheetContents;
47 enum StylePropertiesType { ImmutablePropertiesType, MutablePropertiesType, DeferredPropertiesType };
49 class StylePropertiesBase : public RefCounted<StylePropertiesBase> {
51 // Override RefCounted's deref() to ensure operator delete is called on
52 // the appropriate subclass type.
55 StylePropertiesType type() const { return static_cast<StylePropertiesType>(m_type); }
57 CSSParserMode cssParserMode() const { return static_cast<CSSParserMode>(m_cssParserMode); }
60 StylePropertiesBase(CSSParserMode cssParserMode, StylePropertiesType type)
61 : m_cssParserMode(cssParserMode)
66 StylePropertiesBase(CSSParserMode cssParserMode, unsigned immutableArraySize)
67 : m_cssParserMode(cssParserMode)
68 , m_type(ImmutablePropertiesType)
69 , m_arraySize(immutableArraySize)
72 unsigned m_cssParserMode : 3;
73 mutable unsigned m_type : 2;
74 unsigned m_arraySize : 27;
77 class StyleProperties : public StylePropertiesBase {
78 friend class PropertyReference;
80 class PropertyReference {
82 PropertyReference(const StylePropertyMetadata& metadata, const CSSValue* value)
83 : m_metadata(metadata)
87 CSSPropertyID id() const { return static_cast<CSSPropertyID>(m_metadata.m_propertyID); }
88 CSSPropertyID shorthandID() const { return m_metadata.shorthandID(); }
90 bool isImportant() const { return m_metadata.m_important; }
91 bool isInherited() const { return m_metadata.m_inherited; }
92 bool isImplicit() const { return m_metadata.m_implicit; }
94 String cssName() const;
95 String cssText() const;
97 const CSSValue* value() const { return m_value; }
98 // FIXME: We should try to remove this mutable overload.
99 CSSValue* value() { return const_cast<CSSValue*>(m_value); }
101 // FIXME: Remove this.
102 CSSProperty toCSSProperty() const { return CSSProperty(id(), const_cast<CSSValue*>(m_value), isImportant(), m_metadata.m_isSetFromShorthand, m_metadata.m_indexInShorthandsVector, isImplicit()); }
105 const StylePropertyMetadata& m_metadata;
106 const CSSValue* m_value;
109 unsigned propertyCount() const;
110 bool isEmpty() const { return !propertyCount(); }
111 PropertyReference propertyAt(unsigned) const;
113 WEBCORE_EXPORT RefPtr<CSSValue> getPropertyCSSValue(CSSPropertyID) const;
114 WEBCORE_EXPORT String getPropertyValue(CSSPropertyID) const;
115 bool propertyIsImportant(CSSPropertyID) const;
116 String getPropertyShorthand(CSSPropertyID) const;
117 bool isPropertyImplicit(CSSPropertyID) const;
119 RefPtr<CSSValue> getCustomPropertyCSSValue(const String& propertyName) const;
120 String getCustomPropertyValue(const String& propertyName) const;
121 bool customPropertyIsImportant(const String& propertyName) const;
123 Ref<MutableStyleProperties> copyBlockProperties() const;
125 WEBCORE_EXPORT Ref<MutableStyleProperties> mutableCopy() const;
126 Ref<ImmutableStyleProperties> immutableCopyIfNeeded() const;
128 Ref<MutableStyleProperties> copyPropertiesInSet(const CSSPropertyID* set, unsigned length) const;
130 String asText() const;
132 bool hasCSSOMWrapper() const;
133 bool isMutable() const { return type() == MutablePropertiesType; }
135 bool traverseSubresources(const std::function<bool (const CachedResource&)>& handler) const;
137 static unsigned averageSizeInBytes();
143 bool propertyMatches(CSSPropertyID, const CSSValue*) const;
146 StyleProperties(CSSParserMode cssParserMode, StylePropertiesType type)
147 : StylePropertiesBase(cssParserMode, type)
150 StyleProperties(CSSParserMode cssParserMode, unsigned immutableArraySize)
151 : StylePropertiesBase(cssParserMode, immutableArraySize)
154 int findPropertyIndex(CSSPropertyID) const;
155 int findCustomPropertyIndex(const String& propertyName) const;
158 String getShorthandValue(const StylePropertyShorthand&) const;
159 String getCommonValue(const StylePropertyShorthand&) const;
160 String getAlignmentShorthandValue(const StylePropertyShorthand&) const;
161 enum CommonValueMode { OmitUncommonValues, ReturnNullOnUncommonValues };
162 String borderPropertyValue(CommonValueMode) const;
163 String getLayeredShorthandValue(const StylePropertyShorthand&) const;
164 String get4Values(const StylePropertyShorthand&) const;
165 String borderSpacingValue(const StylePropertyShorthand&) const;
166 String fontValue() const;
167 void appendFontLonghandValueIfExplicit(CSSPropertyID, StringBuilder& result, String& value) const;
169 RefPtr<CSSValue> getPropertyCSSValueInternal(CSSPropertyID) const;
171 friend class PropertySetCSSStyleDeclaration;
174 class ImmutableStyleProperties final : public StyleProperties {
176 WEBCORE_EXPORT ~ImmutableStyleProperties();
177 static Ref<ImmutableStyleProperties> create(const CSSProperty* properties, unsigned count, CSSParserMode);
179 unsigned propertyCount() const { return m_arraySize; }
180 bool isEmpty() const { return !propertyCount(); }
181 PropertyReference propertyAt(unsigned index) const;
183 const CSSValue** valueArray() const;
184 const StylePropertyMetadata* metadataArray() const;
185 int findPropertyIndex(CSSPropertyID) const;
186 int findCustomPropertyIndex(const String& propertyName) const;
191 ImmutableStyleProperties(const CSSProperty*, unsigned count, CSSParserMode);
194 inline const CSSValue** ImmutableStyleProperties::valueArray() const
196 return reinterpret_cast<const CSSValue**>(const_cast<const void**>((&(this->m_storage))));
199 inline const StylePropertyMetadata* ImmutableStyleProperties::metadataArray() const
201 return reinterpret_cast_ptr<const StylePropertyMetadata*>(&reinterpret_cast_ptr<const char*>(&(this->m_storage))[m_arraySize * sizeof(CSSValue*)]);
204 class MutableStyleProperties final : public StyleProperties {
206 WEBCORE_EXPORT static Ref<MutableStyleProperties> create(CSSParserMode = HTMLQuirksMode);
207 static Ref<MutableStyleProperties> create(const CSSProperty* properties, unsigned count);
209 WEBCORE_EXPORT ~MutableStyleProperties();
211 unsigned propertyCount() const { return m_propertyVector.size(); }
212 bool isEmpty() const { return !propertyCount(); }
213 PropertyReference propertyAt(unsigned index) const;
215 PropertySetCSSStyleDeclaration* cssStyleDeclaration();
217 bool addParsedProperties(const ParsedPropertyVector&);
218 bool addParsedProperty(const CSSProperty&);
220 // These expand shorthand properties into multiple properties.
221 bool setProperty(CSSPropertyID, const String& value, bool important, CSSParserContext);
222 bool setProperty(CSSPropertyID, const String& value, bool important = false);
223 void setProperty(CSSPropertyID, RefPtr<CSSValue>&&, bool important = false);
225 // These do not. FIXME: This is too messy, we can do better.
226 bool setProperty(CSSPropertyID, CSSValueID identifier, bool important = false);
227 bool setProperty(CSSPropertyID, CSSPropertyID identifier, bool important = false);
228 bool setProperty(const CSSProperty&, CSSProperty* slot = nullptr);
230 bool removeProperty(CSSPropertyID, String* returnText = nullptr);
231 void removeBlockProperties();
232 bool removePropertiesInSet(const CSSPropertyID* set, unsigned length);
234 void mergeAndOverrideOnConflict(const StyleProperties&);
237 bool parseDeclaration(const String& styleDeclaration, CSSParserContext);
239 WEBCORE_EXPORT CSSStyleDeclaration* ensureCSSStyleDeclaration();
240 CSSStyleDeclaration* ensureInlineCSSStyleDeclaration(StyledElement* parentElement);
242 int findPropertyIndex(CSSPropertyID) const;
243 int findCustomPropertyIndex(const String& propertyName) const;
245 Vector<CSSProperty, 4> m_propertyVector;
247 // Methods for querying and altering CSS custom properties.
248 bool setCustomProperty(const String& propertyName, const String& value, bool important, CSSParserContext);
249 bool removeCustomProperty(const String& propertyName, String* returnText = nullptr);
252 explicit MutableStyleProperties(CSSParserMode);
253 explicit MutableStyleProperties(const StyleProperties&);
254 MutableStyleProperties(const CSSProperty* properties, unsigned count);
256 bool removeShorthandProperty(CSSPropertyID);
257 CSSProperty* findCSSPropertyWithID(CSSPropertyID);
258 CSSProperty* findCustomCSSPropertyWithName(const String&);
259 std::unique_ptr<PropertySetCSSStyleDeclaration> m_cssomWrapper;
261 friend class StyleProperties;
264 class DeferredStyleProperties final : public StylePropertiesBase {
266 WEBCORE_EXPORT ~DeferredStyleProperties();
267 static Ref<DeferredStyleProperties> create(const CSSParserTokenRange&, CSSDeferredParser&);
269 Ref<ImmutableStyleProperties> parseDeferredProperties();
272 DeferredStyleProperties(const CSSParserTokenRange&, CSSDeferredParser&);
274 Vector<CSSParserToken> m_tokens;
275 Ref<CSSDeferredParser> m_parser;
278 inline ImmutableStyleProperties::PropertyReference ImmutableStyleProperties::propertyAt(unsigned index) const
280 return PropertyReference(metadataArray()[index], valueArray()[index]);
283 inline MutableStyleProperties::PropertyReference MutableStyleProperties::propertyAt(unsigned index) const
285 const CSSProperty& property = m_propertyVector[index];
286 return PropertyReference(property.metadata(), property.value());
289 inline StyleProperties::PropertyReference StyleProperties::propertyAt(unsigned index) const
291 if (is<MutableStyleProperties>(*this))
292 return downcast<MutableStyleProperties>(*this).propertyAt(index);
293 return downcast<ImmutableStyleProperties>(*this).propertyAt(index);
296 inline unsigned StyleProperties::propertyCount() const
298 if (is<MutableStyleProperties>(*this))
299 return downcast<MutableStyleProperties>(*this).propertyCount();
300 return downcast<ImmutableStyleProperties>(*this).propertyCount();
303 inline void StylePropertiesBase::deref()
308 if (is<MutableStyleProperties>(*this))
309 delete downcast<MutableStyleProperties>(this);
310 else if (is<ImmutableStyleProperties>(*this))
311 delete downcast<ImmutableStyleProperties>(this);
313 delete downcast<DeferredStyleProperties>(this);
316 inline int StyleProperties::findPropertyIndex(CSSPropertyID propertyID) const
318 if (is<MutableStyleProperties>(*this))
319 return downcast<MutableStyleProperties>(*this).findPropertyIndex(propertyID);
320 return downcast<ImmutableStyleProperties>(*this).findPropertyIndex(propertyID);
323 inline int StyleProperties::findCustomPropertyIndex(const String& propertyName) const
325 if (is<MutableStyleProperties>(*this))
326 return downcast<MutableStyleProperties>(*this).findCustomPropertyIndex(propertyName);
327 return downcast<ImmutableStyleProperties>(*this).findCustomPropertyIndex(propertyName);
330 } // namespace WebCore
332 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleProperties)
333 static bool isType(const WebCore::StylePropertiesBase& set) { return set.type() != WebCore::DeferredPropertiesType; }
334 SPECIALIZE_TYPE_TRAITS_END()
336 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::MutableStyleProperties)
337 static bool isType(const WebCore::StylePropertiesBase& set) { return set.type() == WebCore::MutablePropertiesType; }
338 SPECIALIZE_TYPE_TRAITS_END()
340 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ImmutableStyleProperties)
341 static bool isType(const WebCore::StylePropertiesBase& set) { return set.type() == WebCore::ImmutablePropertiesType; }
342 SPECIALIZE_TYPE_TRAITS_END()
344 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::DeferredStyleProperties)
345 static bool isType(const WebCore::StylePropertiesBase& set) { return set.type() == WebCore::DeferredPropertiesType; }
346 SPECIALIZE_TYPE_TRAITS_END()