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 size_t important = value.find("!important", 0, false);
86 int propertyID = cssPropertyID(propertyName);
87 if (!propertyID)
88 return;
89 if (important == notFound)
90 setProperty(propertyID, value, false, ec);
91 else
92 setProperty(propertyID, value.left(important - 1), true, ec);
93 }
94
setProperty(const String & propertyName,const String & value,const String & priority,ExceptionCode & ec)95 void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, const String& priority, ExceptionCode& ec)
96 {
97 int propID = cssPropertyID(propertyName);
98 if (!propID) {
99 // FIXME: Should we raise an exception here?
100 return;
101 }
102 bool important = priority.find("important", 0, false) != notFound;
103 setProperty(propID, value, important, ec);
104 }
105
removeProperty(const String & propertyName,ExceptionCode & ec)106 String CSSStyleDeclaration::removeProperty(const String& propertyName, ExceptionCode& ec)
107 {
108 int propID = cssPropertyID(propertyName);
109 if (!propID)
110 return String();
111 return removeProperty(propID, ec);
112 }
113
isPropertyName(const String & propertyName)114 bool CSSStyleDeclaration::isPropertyName(const String& propertyName)
115 {
116 return cssPropertyID(propertyName);
117 }
118
parentRule() const119 CSSRule* CSSStyleDeclaration::parentRule() const
120 {
121 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
122 }
123
cssPropertyMatches(const CSSProperty * property) const124 bool CSSStyleDeclaration::cssPropertyMatches(const CSSProperty* property) const
125 {
126 RefPtr<CSSValue> value = getPropertyCSSValue(property->id());
127 return value && value->cssText() == property->value()->cssText();
128 }
129
diff(CSSMutableStyleDeclaration * style) const130 void CSSStyleDeclaration::diff(CSSMutableStyleDeclaration* style) const
131 {
132 if (!style)
133 return;
134
135 Vector<int> propertiesToRemove;
136 {
137 CSSMutableStyleDeclaration::const_iterator end = style->end();
138 for (CSSMutableStyleDeclaration::const_iterator it = style->begin(); it != end; ++it) {
139 const CSSProperty& property = *it;
140 if (cssPropertyMatches(&property))
141 propertiesToRemove.append(property.id());
142 }
143 }
144
145 // FIXME: This should use mass removal.
146 for (unsigned i = 0; i < propertiesToRemove.size(); i++)
147 style->removeProperty(propertiesToRemove[i]);
148 }
149
copyPropertiesInSet(const int * set,unsigned length) const150 PassRefPtr<CSSMutableStyleDeclaration> CSSStyleDeclaration::copyPropertiesInSet(const int* set, unsigned length) const
151 {
152 Vector<CSSProperty> list;
153 list.reserveInitialCapacity(length);
154 for (unsigned i = 0; i < length; i++) {
155 RefPtr<CSSValue> value = getPropertyCSSValue(set[i]);
156 if (value)
157 list.append(CSSProperty(set[i], value.release(), false));
158 }
159 return CSSMutableStyleDeclaration::create(list);
160 }
161
162 } // namespace WebCore
163