1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 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 #include "config.h"
22 #include "CSSStyleDeclaration.h"
23
24 #include "CSSMutableStyleDeclaration.h"
25 #include "CSSParser.h"
26 #include "CSSProperty.h"
27 #include "CSSPropertyNames.h"
28 #include "CSSRule.h"
29 #include <wtf/ASCIICType.h>
30
31 using namespace WTF;
32
33 namespace WebCore {
34
CSSStyleDeclaration(CSSRule * parent)35 CSSStyleDeclaration::CSSStyleDeclaration(CSSRule* parent)
36 : StyleBase(parent)
37 {
38 }
39
getPropertyCSSValue(const String & propertyName)40 PassRefPtr<CSSValue> CSSStyleDeclaration::getPropertyCSSValue(const String& propertyName)
41 {
42 int propID = cssPropertyID(propertyName);
43 if (!propID)
44 return 0;
45 return getPropertyCSSValue(propID);
46 }
47
getPropertyValue(const String & propertyName)48 String CSSStyleDeclaration::getPropertyValue(const String &propertyName)
49 {
50 int propID = cssPropertyID(propertyName);
51 if (!propID)
52 return String();
53 return getPropertyValue(propID);
54 }
55
getPropertyPriority(const String & propertyName)56 String CSSStyleDeclaration::getPropertyPriority(const String& propertyName)
57 {
58 int propID = cssPropertyID(propertyName);
59 if (!propID)
60 return String();
61 return getPropertyPriority(propID) ? "important" : "";
62 }
63
getPropertyShorthand(const String & propertyName)64 String CSSStyleDeclaration::getPropertyShorthand(const String& propertyName)
65 {
66 int propID = cssPropertyID(propertyName);
67 if (!propID)
68 return String();
69 int shorthandID = getPropertyShorthand(propID);
70 if (!shorthandID)
71 return String();
72 return getPropertyName(static_cast<CSSPropertyID>(shorthandID));
73 }
74
isPropertyImplicit(const String & propertyName)75 bool CSSStyleDeclaration::isPropertyImplicit(const String& propertyName)
76 {
77 int propID = cssPropertyID(propertyName);
78 if (!propID)
79 return false;
80 return isPropertyImplicit(propID);
81 }
82
setProperty(const String & propertyName,const String & value,ExceptionCode & ec)83 void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, ExceptionCode& ec)
84 {
85 int important = value.find("!important", 0, false);
86 if (important == -1)
87 setProperty(propertyName, value, "", ec);
88 else
89 setProperty(propertyName, value.left(important - 1), "important", ec);
90 }
91
setProperty(const String & propertyName,const String & value,const String & priority,ExceptionCode & ec)92 void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, const String& priority, ExceptionCode& ec)
93 {
94 int propID = cssPropertyID(propertyName);
95 if (!propID) {
96 // FIXME: Should we raise an exception here?
97 return;
98 }
99 bool important = priority.find("important", 0, false) != -1;
100 setProperty(propID, value, important, ec);
101 }
102
removeProperty(const String & propertyName,ExceptionCode & ec)103 String CSSStyleDeclaration::removeProperty(const String& propertyName, ExceptionCode& ec)
104 {
105 int propID = cssPropertyID(propertyName);
106 if (!propID)
107 return String();
108 return removeProperty(propID, ec);
109 }
110
isPropertyName(const String & propertyName)111 bool CSSStyleDeclaration::isPropertyName(const String& propertyName)
112 {
113 return cssPropertyID(propertyName);
114 }
115
parentRule() const116 CSSRule* CSSStyleDeclaration::parentRule() const
117 {
118 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
119 }
120
cssPropertyMatches(const CSSProperty * property) const121 bool CSSStyleDeclaration::cssPropertyMatches(const CSSProperty* property) const
122 {
123 RefPtr<CSSValue> value = getPropertyCSSValue(property->id());
124 return value && value->cssText() == property->value()->cssText();
125 }
126
diff(CSSMutableStyleDeclaration * style) const127 void CSSStyleDeclaration::diff(CSSMutableStyleDeclaration* style) const
128 {
129 if (!style)
130 return;
131
132 Vector<int> propertiesToRemove;
133 {
134 CSSMutableStyleDeclaration::const_iterator end = style->end();
135 for (CSSMutableStyleDeclaration::const_iterator it = style->begin(); it != end; ++it) {
136 const CSSProperty& property = *it;
137 if (cssPropertyMatches(&property))
138 propertiesToRemove.append(property.id());
139 }
140 }
141
142 // FIXME: This should use mass removal.
143 for (unsigned i = 0; i < propertiesToRemove.size(); i++)
144 style->removeProperty(propertiesToRemove[i]);
145 }
146
copyPropertiesInSet(const int * set,unsigned length) const147 PassRefPtr<CSSMutableStyleDeclaration> CSSStyleDeclaration::copyPropertiesInSet(const int* set, unsigned length) const
148 {
149 Vector<CSSProperty> list;
150 list.reserveInitialCapacity(length);
151 unsigned variableDependentValueCount = 0;
152 for (unsigned i = 0; i < length; i++) {
153 RefPtr<CSSValue> value = getPropertyCSSValue(set[i]);
154 if (value) {
155 if (value->isVariableDependentValue())
156 variableDependentValueCount++;
157 list.append(CSSProperty(set[i], value.release(), false));
158 }
159 }
160 return CSSMutableStyleDeclaration::create(list, variableDependentValueCount);
161 }
162
163 } // namespace WebCore
164