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 bool CSSStyleDeclaration::cssPropertyMatches(const CSSProperty
* property
) const
123 RefPtr
<CSSValue
> value
= getPropertyCSSValue(property
->id());
124 return value
&& value
->cssText() == property
->value()->cssText();
127 void CSSStyleDeclaration::diff(CSSMutableStyleDeclaration
* style
) const
132 Vector
<int> propertiesToRemove
;
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());
142 // FIXME: This should use mass removal.
143 for (unsigned i
= 0; i
< propertiesToRemove
.size(); i
++)
144 style
->removeProperty(propertiesToRemove
[i
]);
147 PassRefPtr
<CSSMutableStyleDeclaration
> CSSStyleDeclaration::copyPropertiesInSet(const int* set
, unsigned length
) const
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
]);
155 if (value
->isVariableDependentValue())
156 variableDependentValueCount
++;
157 list
.append(CSSProperty(set
[i
], value
.release(), false));
160 return CSSMutableStyleDeclaration::create(list
, variableDependentValueCount
);
163 } // namespace WebCore