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 "CSSMutableValue.h"
26 #include "CSSParser.h"
27 #include "CSSProperty.h"
28 #include "CSSPropertyNames.h"
31 #include "SVGElement.h"
32 #include <wtf/ASCIICType.h>
33 #include <wtf/text/CString.h>
42 CSSStyleDeclaration::CSSStyleDeclaration(CSSRule* parent)
47 PassRefPtr<CSSValue> CSSStyleDeclaration::getPropertyCSSValue(const String& propertyName)
49 int propID = cssPropertyID(propertyName);
53 // Short-cut, not involving any change to the refcount.
54 if (!isMutableStyleDeclaration())
55 return getPropertyCSSValue(propID);
58 RefPtr<CSSValue> value = getPropertyCSSValue(propID);
59 if (!value || !value->isMutableValue())
60 return value.release();
62 Node* node = static_cast<CSSMutableStyleDeclaration*>(this)->node();
63 if (!node || !node->isStyledElement())
64 return value.release();
66 Node* associatedNode = static_cast<CSSMutableValue*>(value.get())->node();
68 ASSERT(associatedNode == node);
69 return value.release();
72 static_cast<CSSMutableValue*>(value.get())->setNode(node);
73 return value.release();
76 String CSSStyleDeclaration::getPropertyValue(const String &propertyName)
78 int propID = cssPropertyID(propertyName);
81 return getPropertyValue(propID);
84 String CSSStyleDeclaration::getPropertyPriority(const String& propertyName)
86 int propID = cssPropertyID(propertyName);
89 return getPropertyPriority(propID) ? "important" : "";
92 String CSSStyleDeclaration::getPropertyShorthand(const String& propertyName)
94 int propID = cssPropertyID(propertyName);
97 int shorthandID = getPropertyShorthand(propID);
100 return getPropertyName(static_cast<CSSPropertyID>(shorthandID));
103 bool CSSStyleDeclaration::isPropertyImplicit(const String& propertyName)
105 int propID = cssPropertyID(propertyName);
108 return isPropertyImplicit(propID);
111 void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, ExceptionCode& ec)
113 size_t important = value.find("!important", 0, false);
114 int propertyID = cssPropertyID(propertyName);
117 if (important == notFound)
118 setProperty(propertyID, value, false, ec);
120 setProperty(propertyID, value.left(important - 1), true, ec);
123 void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, const String& priority, ExceptionCode& ec)
125 int propID = cssPropertyID(propertyName);
127 // FIXME: Should we raise an exception here?
130 bool important = priority.find("important", 0, false) != notFound;
131 setProperty(propID, value, important, ec);
134 String CSSStyleDeclaration::removeProperty(const String& propertyName, ExceptionCode& ec)
136 int propID = cssPropertyID(propertyName);
139 return removeProperty(propID, ec);
142 bool CSSStyleDeclaration::isPropertyName(const String& propertyName)
144 return cssPropertyID(propertyName);
147 CSSRule* CSSStyleDeclaration::parentRule() const
149 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
152 bool CSSStyleDeclaration::cssPropertyMatches(const CSSProperty* property) const
154 RefPtr<CSSValue> value = getPropertyCSSValue(property->id());
155 return value && value->cssText() == property->value()->cssText();
158 void CSSStyleDeclaration::diff(CSSMutableStyleDeclaration* style) const
163 Vector<int> propertiesToRemove;
165 CSSMutableStyleDeclaration::const_iterator end = style->end();
166 for (CSSMutableStyleDeclaration::const_iterator it = style->begin(); it != end; ++it) {
167 const CSSProperty& property = *it;
168 if (cssPropertyMatches(&property))
169 propertiesToRemove.append(property.id());
173 // FIXME: This should use mass removal.
174 for (unsigned i = 0; i < propertiesToRemove.size(); i++)
175 style->removeProperty(propertiesToRemove[i]);
178 PassRefPtr<CSSMutableStyleDeclaration> CSSStyleDeclaration::copyPropertiesInSet(const int* set, unsigned length) const
180 Vector<CSSProperty> list;
181 list.reserveInitialCapacity(length);
182 for (unsigned i = 0; i < length; i++) {
183 RefPtr<CSSValue> value = getPropertyCSSValue(set[i]);
185 list.append(CSSProperty(set[i], value.release(), false));
187 return CSSMutableStyleDeclaration::create(list);
191 void CSSStyleDeclaration::showStyle()
193 fprintf(stderr, "%s\n", cssText().ascii().data());
197 } // namespace WebCore