OSDN Git Service

am 870c405: Merge change 587 into donut
[android-x86/external-webkit.git] / WebCore / css / CSSStyleDeclaration.cpp
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
35 CSSStyleDeclaration::CSSStyleDeclaration(CSSRule* parent)
36     : StyleBase(parent)
37 {
38 }
39
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
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
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
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
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
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
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
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
111 bool CSSStyleDeclaration::isPropertyName(const String& propertyName)
112 {
113     return cssPropertyID(propertyName);
114 }
115
116 CSSRule* CSSStyleDeclaration::parentRule() const
117 {
118     return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
119 }
120
121 void CSSStyleDeclaration::diff(CSSMutableStyleDeclaration* style) const
122 {
123     if (!style)
124         return;
125
126     Vector<int> propertiesToRemove;
127     {
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());
134         }
135     }
136
137     // FIXME: This should use mass removal.
138     for (unsigned i = 0; i < propertiesToRemove.size(); i++)
139         style->removeProperty(propertiesToRemove[i]);
140 }
141
142 PassRefPtr<CSSMutableStyleDeclaration> CSSStyleDeclaration::copyPropertiesInSet(const int* set, unsigned length) const
143 {
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]);
149         if (value) {
150             if (value->isVariableDependentValue())
151                 variableDependentValueCount++;
152             list.append(CSSProperty(set[i], value.release(), false));
153         }
154     }
155     return CSSMutableStyleDeclaration::create(list, variableDependentValueCount);
156 }
157
158 } // namespace WebCore