2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 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.
22 #include "CSSStyleDeclaration.h"
24 #include "CSSMutableStyleDeclaration.h"
25 #include "CSSParser.h"
26 #include "CSSProperty.h"
27 #include "CSSPropertyNames.h"
29 #include <wtf/ASCIICType.h>
35 CSSStyleDeclaration::CSSStyleDeclaration(CSSRule* parent)
40 PassRefPtr<CSSValue> CSSStyleDeclaration::getPropertyCSSValue(const String& propertyName)
42 int propID = cssPropertyID(propertyName);
45 return getPropertyCSSValue(propID);
48 String CSSStyleDeclaration::getPropertyValue(const String &propertyName)
50 int propID = cssPropertyID(propertyName);
53 return getPropertyValue(propID);
56 String CSSStyleDeclaration::getPropertyPriority(const String& propertyName)
58 int propID = cssPropertyID(propertyName);
61 return getPropertyPriority(propID) ? "important" : "";
64 String CSSStyleDeclaration::getPropertyShorthand(const String& propertyName)
66 int propID = cssPropertyID(propertyName);
69 int shorthandID = getPropertyShorthand(propID);
72 return getPropertyName(static_cast<CSSPropertyID>(shorthandID));
75 bool CSSStyleDeclaration::isPropertyImplicit(const String& propertyName)
77 int propID = cssPropertyID(propertyName);
80 return isPropertyImplicit(propID);
83 void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, ExceptionCode& ec)
85 int important = value.find("!important", 0, false);
87 setProperty(propertyName, value, "", ec);
89 setProperty(propertyName, value.left(important - 1), "important", ec);
92 void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, const String& priority, ExceptionCode& ec)
94 int propID = cssPropertyID(propertyName);
96 // FIXME: Should we raise an exception here?
99 bool important = priority.find("important", 0, false) != -1;
100 setProperty(propID, value, important, ec);
103 String CSSStyleDeclaration::removeProperty(const String& propertyName, ExceptionCode& ec)
105 int propID = cssPropertyID(propertyName);
108 return removeProperty(propID, ec);
111 bool CSSStyleDeclaration::isPropertyName(const String& propertyName)
113 return cssPropertyID(propertyName);
116 CSSRule* CSSStyleDeclaration::parentRule() const
118 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
121 void CSSStyleDeclaration::diff(CSSMutableStyleDeclaration* style) const
126 Vector<int> propertiesToRemove;
128 CSSMutableStyleDeclaration::const_iterator end = style->end();
129 for (CSSMutableStyleDeclaration::const_iterator it = style->begin(); it != end; ++it) {
130 const CSSProperty& property = *it;
131 RefPtr<CSSValue> value = getPropertyCSSValue(property.id());
132 if (value && (value->cssText() == property.value()->cssText()))
133 propertiesToRemove.append(property.id());
137 // FIXME: This should use mass removal.
138 for (unsigned i = 0; i < propertiesToRemove.size(); i++)
139 style->removeProperty(propertiesToRemove[i]);
142 PassRefPtr<CSSMutableStyleDeclaration> CSSStyleDeclaration::copyPropertiesInSet(const int* set, unsigned length) const
144 Vector<CSSProperty> list;
145 list.reserveInitialCapacity(length);
146 unsigned variableDependentValueCount = 0;
147 for (unsigned i = 0; i < length; i++) {
148 RefPtr<CSSValue> value = getPropertyCSSValue(set[i]);
150 if (value->isVariableDependentValue())
151 variableDependentValueCount++;
152 list.append(CSSProperty(set[i], value.release(), false));
155 return CSSMutableStyleDeclaration::create(list, variableDependentValueCount);
158 } // namespace WebCore