1 /*
2  * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4  *
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.
9  *
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.
14  *
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.
19  */
20 
21 #ifndef CSSMutableStyleDeclaration_h
22 #define CSSMutableStyleDeclaration_h
23 
24 #include "CSSStyleDeclaration.h"
25 #include "CSSPrimitiveValue.h"
26 #include "CSSProperty.h"
27 #include "KURLHash.h"
28 #include "PlatformString.h"
29 #include <wtf/ListHashSet.h>
30 #include <wtf/Vector.h>
31 
32 namespace WebCore {
33 
34 class Node;
35 
36 class CSSMutableStyleDeclarationConstIterator {
37 public:
38     CSSMutableStyleDeclarationConstIterator(const CSSMutableStyleDeclaration* decl, CSSProperty* current);
39     CSSMutableStyleDeclarationConstIterator(const CSSMutableStyleDeclarationConstIterator& o);
40     ~CSSMutableStyleDeclarationConstIterator();
41 
42     const CSSProperty& operator*() const { return *m_current; }
43     const CSSProperty* operator->() const { return m_current; }
44 
45     bool operator!=(const CSSMutableStyleDeclarationConstIterator& o) { ASSERT(m_decl == o.m_decl); return m_current != o.m_current; }
46     bool operator==(const CSSMutableStyleDeclarationConstIterator& o) { ASSERT(m_decl == o.m_decl); return m_current == o.m_current; }
47 
48     CSSMutableStyleDeclarationConstIterator& operator=(const CSSMutableStyleDeclarationConstIterator& o);
49 
50     CSSMutableStyleDeclarationConstIterator& operator++();
51     CSSMutableStyleDeclarationConstIterator& operator--();
52 
53 private:
54     const CSSMutableStyleDeclaration* m_decl;
55     CSSProperty* m_current;
56 };
57 
58 class CSSMutableStyleDeclaration : public CSSStyleDeclaration {
59 public:
60     virtual ~CSSMutableStyleDeclaration();
61 
create()62     static PassRefPtr<CSSMutableStyleDeclaration> create()
63     {
64         return adoptRef(new CSSMutableStyleDeclaration);
65     }
create(CSSRule * parentRule)66     static PassRefPtr<CSSMutableStyleDeclaration> create(CSSRule* parentRule)
67     {
68         return adoptRef(new CSSMutableStyleDeclaration(parentRule));
69     }
create(CSSRule * parentRule,const CSSProperty * const * properties,int numProperties)70     static PassRefPtr<CSSMutableStyleDeclaration> create(CSSRule* parentRule, const CSSProperty* const* properties, int numProperties)
71     {
72         return adoptRef(new CSSMutableStyleDeclaration(parentRule, properties, numProperties));
73     }
create(const Vector<CSSProperty> & properties)74     static PassRefPtr<CSSMutableStyleDeclaration> create(const Vector<CSSProperty>& properties)
75     {
76         return adoptRef(new CSSMutableStyleDeclaration(0, properties));
77     }
78 
79     CSSMutableStyleDeclaration& operator=(const CSSMutableStyleDeclaration&);
80 
81     typedef CSSMutableStyleDeclarationConstIterator const_iterator;
82 
begin()83     const_iterator begin() { return const_iterator(this, m_properties.begin()); }
end()84     const_iterator end() { return const_iterator(this, m_properties.end()); }
85 
setNode(Node * node)86     void setNode(Node* node) { m_node = node; }
87 
node()88     Node* node() const { return m_node; }
89 
isMutableStyleDeclaration()90     virtual bool isMutableStyleDeclaration() const { return true; }
91 
92     virtual String cssText() const;
93     virtual void setCssText(const String&, ExceptionCode&);
94 
95     virtual unsigned virtualLength() const;
length()96     unsigned length() const { return m_properties.size(); }
97 
98     virtual String item(unsigned index) const;
99 
100     virtual PassRefPtr<CSSValue> getPropertyCSSValue(int propertyID) const;
101     virtual String getPropertyValue(int propertyID) const;
102     virtual bool getPropertyPriority(int propertyID) const;
103     virtual int getPropertyShorthand(int propertyID) const;
104     virtual bool isPropertyImplicit(int propertyID) const;
105 
106     virtual void setProperty(int propertyId, const String& value, bool important, ExceptionCode&);
107     virtual String removeProperty(int propertyID, ExceptionCode&);
108 
109     virtual PassRefPtr<CSSMutableStyleDeclaration> copy() const;
110 
111     bool setProperty(int propertyID, int value, bool important = false, bool notifyChanged = true);
112     bool setProperty(int propertyId, double value, CSSPrimitiveValue::UnitTypes, bool important = false, bool notifyChanged = true);
113     bool setProperty(int propertyID, const String& value, bool important = false, bool notifyChanged = true);
114 
115     String removeProperty(int propertyID, bool notifyChanged = true, bool returnText = false);
116 
117     // setLengthProperty treats integers as pixels! (Needed for conversion of HTML attributes.)
118     void setLengthProperty(int propertyId, const String& value, bool important, bool multiLength = false);
119     void setStringProperty(int propertyId, const String& value, CSSPrimitiveValue::UnitTypes, bool important = false); // parsed string value
120     void setImageProperty(int propertyId, const String& url, bool important = false);
121 
122     // The following parses an entire new style declaration.
123     void parseDeclaration(const String& styleDeclaration);
124 
125     // Besides adding the properties, this also removes any existing properties with these IDs.
126     // It does no notification since it's called by the parser.
127     void addParsedProperties(const CSSProperty* const *, int numProperties);
128     // This does no change notifications since it's only called by createMarkup.
129     void addParsedProperty(const CSSProperty&);
130 
131     PassRefPtr<CSSMutableStyleDeclaration> copyBlockProperties() const;
132     void removeBlockProperties();
133     void removePropertiesInSet(const int* set, unsigned length, bool notifyChanged = true);
134 
135     void merge(const CSSMutableStyleDeclaration*, bool argOverridesOnConflict = true);
136 
setStrictParsing(bool b)137     void setStrictParsing(bool b) { m_strictParsing = b; }
useStrictParsing()138     bool useStrictParsing() const { return m_strictParsing; }
139 
140     void addSubresourceStyleURLs(ListHashSet<KURL>&);
141 
propertiesEqual(const CSSMutableStyleDeclaration * o)142     bool propertiesEqual(const CSSMutableStyleDeclaration* o) const { return m_properties == o->m_properties; }
143 
144     bool isInlineStyleDeclaration();
145 
146 protected:
147     CSSMutableStyleDeclaration(CSSRule* parentRule);
148 
149 private:
150     CSSMutableStyleDeclaration();
151     CSSMutableStyleDeclaration(CSSRule* parentRule, const Vector<CSSProperty>&);
152     CSSMutableStyleDeclaration(CSSRule* parentRule, const CSSProperty* const *, int numProperties);
153 
154     virtual PassRefPtr<CSSMutableStyleDeclaration> makeMutable();
155 
156     void setNeedsStyleRecalc();
157 
158     String getShorthandValue(const int* properties, size_t) const;
159     String getCommonValue(const int* properties, size_t) const;
160     String getLayeredShorthandValue(const int* properties, size_t) const;
161     String get4Values(const int* properties) const;
162     String borderSpacingValue(const int properties[2]) const;
163 
getShorthandValue(const int (& properties)[size])164     template<size_t size> String getShorthandValue(const int (&properties)[size]) const { return getShorthandValue(properties, size); }
getCommonValue(const int (& properties)[size])165     template<size_t size> String getCommonValue(const int (&properties)[size]) const { return getCommonValue(properties, size); }
getLayeredShorthandValue(const int (& properties)[size])166     template<size_t size> String getLayeredShorthandValue(const int (&properties)[size]) const { return getLayeredShorthandValue(properties, size); }
167 
168     void setPropertyInternal(const CSSProperty&, CSSProperty* slot = 0);
169     bool removeShorthandProperty(int propertyID, bool notifyChanged);
170 
171     Vector<CSSProperty>::const_iterator findPropertyWithId(int propertyId) const;
172     Vector<CSSProperty>::iterator findPropertyWithId(int propertyId);
173 
174     Vector<CSSProperty, 4> m_properties;
175 
176     Node* m_node;
177     bool m_strictParsing : 1;
178 #ifndef NDEBUG
179     unsigned m_iteratorCount : 4;
180 #endif
181 
182     friend class CSSMutableStyleDeclarationConstIterator;
183 };
184 
CSSMutableStyleDeclarationConstIterator(const CSSMutableStyleDeclaration * decl,CSSProperty * current)185 inline CSSMutableStyleDeclarationConstIterator::CSSMutableStyleDeclarationConstIterator(const CSSMutableStyleDeclaration* decl, CSSProperty* current)
186 : m_decl(decl)
187 , m_current(current)
188 {
189 #ifndef NDEBUG
190     const_cast<CSSMutableStyleDeclaration*>(m_decl)->m_iteratorCount++;
191 #endif
192 }
193 
CSSMutableStyleDeclarationConstIterator(const CSSMutableStyleDeclarationConstIterator & o)194 inline CSSMutableStyleDeclarationConstIterator::CSSMutableStyleDeclarationConstIterator(const CSSMutableStyleDeclarationConstIterator& o)
195 : m_decl(o.m_decl)
196 , m_current(o.m_current)
197 {
198 #ifndef NDEBUG
199     const_cast<CSSMutableStyleDeclaration*>(m_decl)->m_iteratorCount++;
200 #endif
201 }
202 
~CSSMutableStyleDeclarationConstIterator()203 inline CSSMutableStyleDeclarationConstIterator::~CSSMutableStyleDeclarationConstIterator()
204 {
205 #ifndef NDEBUG
206     const_cast<CSSMutableStyleDeclaration*>(m_decl)->m_iteratorCount--;
207 #endif
208 }
209 
210 inline CSSMutableStyleDeclarationConstIterator& CSSMutableStyleDeclarationConstIterator::operator=(const CSSMutableStyleDeclarationConstIterator& o)
211 {
212     m_decl = o.m_decl;
213     m_current = o.m_current;
214 #ifndef NDEBUG
215     const_cast<CSSMutableStyleDeclaration*>(m_decl)->m_iteratorCount++;
216 #endif
217     return *this;
218 }
219 
220 inline CSSMutableStyleDeclarationConstIterator& CSSMutableStyleDeclarationConstIterator::operator++()
221 {
222     ASSERT(m_current != const_cast<CSSMutableStyleDeclaration*>(m_decl)->m_properties.end());
223     ++m_current;
224     return *this;
225 }
226 
227 inline CSSMutableStyleDeclarationConstIterator& CSSMutableStyleDeclarationConstIterator::operator--()
228 {
229     --m_current;
230     return *this;
231 }
232 
233 } // namespace WebCore
234 
235 #endif // CSSMutableStyleDeclaration_h
236