1 /*
2 * Copyright (C) 2007-2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "V8CSSStyleDeclaration.h"
33
34 #include "CSSParser.h"
35 #include "CSSStyleDeclaration.h"
36 #include "CSSValue.h"
37 #include "CSSPrimitiveValue.h"
38 #include "EventTarget.h"
39
40 #include "V8Binding.h"
41 #include "V8Proxy.h"
42
43 #include <wtf/text/StringBuilder.h>
44 #include <wtf/text/StringConcatenate.h>
45 #include <wtf/ASCIICType.h>
46 #include <wtf/PassRefPtr.h>
47 #include <wtf/RefPtr.h>
48 #include <wtf/StdLibExtras.h>
49 #include <wtf/Vector.h>
50
51 namespace WebCore {
52
53 // FIXME: Next two functions look lifted verbatim from JSCSSStyleDeclarationCustom. Please remove duplication.
54
55 // Check for a CSS prefix.
56 // Passed prefix is all lowercase.
57 // First character of the prefix within the property name may be upper or lowercase.
58 // Other characters in the prefix within the property name must be lowercase.
59 // The prefix within the property name must be followed by a capital letter.
hasCSSPropertyNamePrefix(const String & propertyName,const char * prefix)60 static bool hasCSSPropertyNamePrefix(const String& propertyName, const char* prefix)
61 {
62 #ifndef NDEBUG
63 ASSERT(*prefix);
64 for (const char* p = prefix; *p; ++p)
65 ASSERT(WTF::isASCIILower(*p));
66 ASSERT(propertyName.length());
67 #endif
68
69 if (WTF::toASCIILower(propertyName[0]) != prefix[0])
70 return false;
71
72 unsigned length = propertyName.length();
73 for (unsigned i = 1; i < length; ++i) {
74 if (!prefix[i])
75 return WTF::isASCIIUpper(propertyName[i]);
76 if (propertyName[i] != prefix[i])
77 return false;
78 }
79 return false;
80 }
81
82 class CSSPropertyInfo {
83 public:
84 int propID;
85 bool hadPixelOrPosPrefix;
86 bool wasFilter;
87 };
88
89 // When getting properties on CSSStyleDeclarations, the name used from
90 // Javascript and the actual name of the property are not the same, so
91 // we have to do the following translation. The translation turns upper
92 // case characters into lower case characters and inserts dashes to
93 // separate words.
94 //
95 // Example: 'backgroundPositionY' -> 'background-position-y'
96 //
97 // Also, certain prefixes such as 'pos', 'css-' and 'pixel-' are stripped
98 // and the hadPixelOrPosPrefix out parameter is used to indicate whether or
99 // not the property name was prefixed with 'pos-' or 'pixel-'.
cssPropertyInfo(v8::Handle<v8::String> v8PropertyName)100 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String>v8PropertyName)
101 {
102 String propertyName = toWebCoreString(v8PropertyName);
103 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap;
104 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ());
105 CSSPropertyInfo* propInfo = map.get(propertyName);
106 if (!propInfo) {
107 unsigned length = propertyName.length();
108 bool hadPixelOrPosPrefix = false;
109 if (!length)
110 return 0;
111
112 StringBuilder builder;
113 builder.reserveCapacity(length);
114
115 unsigned i = 0;
116
117 if (hasCSSPropertyNamePrefix(propertyName, "css"))
118 i += 3;
119 else if (hasCSSPropertyNamePrefix(propertyName, "pixel")) {
120 i += 5;
121 hadPixelOrPosPrefix = true;
122 } else if (hasCSSPropertyNamePrefix(propertyName, "pos")) {
123 i += 3;
124 hadPixelOrPosPrefix = true;
125 } else if (hasCSSPropertyNamePrefix(propertyName, "webkit")
126 || hasCSSPropertyNamePrefix(propertyName, "khtml")
127 || hasCSSPropertyNamePrefix(propertyName, "apple"))
128 builder.append('-');
129 else if (WTF::isASCIIUpper(propertyName[0]))
130 return 0;
131
132 builder.append(WTF::toASCIILower(propertyName[i++]));
133
134 for (; i < length; ++i) {
135 UChar c = propertyName[i];
136 if (!WTF::isASCIIUpper(c))
137 builder.append(c);
138 else
139 builder.append(makeString('-', toASCIILower(c)));
140 }
141
142 String propName = builder.toString();
143 int propertyID = cssPropertyID(propName);
144 if (propertyID) {
145 propInfo = new CSSPropertyInfo();
146 propInfo->hadPixelOrPosPrefix = hadPixelOrPosPrefix;
147 propInfo->wasFilter = (propName == "filter");
148 propInfo->propID = propertyID;
149 map.add(propertyName, propInfo);
150 }
151 }
152 return propInfo;
153 }
154
namedPropertyGetter(v8::Local<v8::String> name,const v8::AccessorInfo & info)155 v8::Handle<v8::Value> V8CSSStyleDeclaration::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
156 {
157 INC_STATS("DOM.CSSStyleDeclaration.NamedPropertyGetter");
158 // First look for API defined attributes on the style declaration object.
159 if (info.Holder()->HasRealNamedCallbackProperty(name))
160 return notHandledByInterceptor();
161
162 // Search the style declaration.
163 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
164 CSSPropertyInfo* propInfo = cssPropertyInfo(name);
165
166 // Do not handle non-property names.
167 if (!propInfo)
168 return notHandledByInterceptor();
169
170
171 RefPtr<CSSValue> cssValue = imp->getPropertyCSSValue(propInfo->propID);
172 if (cssValue) {
173 if (propInfo->hadPixelOrPosPrefix &&
174 cssValue->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE) {
175 return v8::Number::New(static_cast<CSSPrimitiveValue*>(
176 cssValue.get())->getFloatValue(CSSPrimitiveValue::CSS_PX));
177 }
178 return v8StringOrNull(cssValue->cssText());
179 }
180
181 String result = imp->getPropertyValue(propInfo->propID);
182 if (result.isNull())
183 result = ""; // convert null to empty string.
184
185 // The 'filter' attribute is made undetectable in KJS/WebKit
186 // to avoid confusion with IE's filter extension.
187 if (propInfo->wasFilter)
188 return v8UndetectableString(result);
189
190 return v8String(result);
191 }
192
namedPropertySetter(v8::Local<v8::String> name,v8::Local<v8::Value> value,const v8::AccessorInfo & info)193 v8::Handle<v8::Value> V8CSSStyleDeclaration::namedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
194 {
195 INC_STATS("DOM.CSSStyleDeclaration.NamedPropertySetter");
196 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
197 CSSPropertyInfo* propInfo = cssPropertyInfo(name);
198 if (!propInfo)
199 return notHandledByInterceptor();
200
201 String propertyValue = toWebCoreStringWithNullCheck(value);
202 if (propInfo->hadPixelOrPosPrefix)
203 propertyValue.append("px");
204
205 ExceptionCode ec = 0;
206 int importantIndex = propertyValue.find("!important", 0, false);
207 bool important = false;
208 if (importantIndex != -1) {
209 important = true;
210 propertyValue = propertyValue.left(importantIndex - 1);
211 }
212 imp->setProperty(propInfo->propID, propertyValue, important, ec);
213
214 if (ec)
215 throwError(ec);
216
217 return value;
218 }
219
220 } // namespace WebCore
221