2 * Copyright (C) 2007-2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "V8CSSStyleDeclaration.h"
34 #include "CSSPropertyNames.h"
35 #include "bindings/v8/ExceptionState.h"
36 #include "bindings/v8/V8Binding.h"
37 #include "core/css/parser/BisonCSSParser.h"
38 #include "core/css/CSSPrimitiveValue.h"
39 #include "core/css/CSSStyleDeclaration.h"
40 #include "core/css/CSSValue.h"
41 #include "core/css/RuntimeCSSEnabled.h"
42 #include "core/events/EventTarget.h"
43 #include "core/frame/UseCounter.h"
44 #include "wtf/ASCIICType.h"
45 #include "wtf/PassRefPtr.h"
46 #include "wtf/RefPtr.h"
47 #include "wtf/StdLibExtras.h"
48 #include "wtf/Vector.h"
49 #include "wtf/text/StringBuilder.h"
50 #include "wtf/text/StringConcatenate.h"
57 // Check for a CSS prefix.
58 // Passed prefix is all lowercase.
59 // First character of the prefix within the property name may be upper or lowercase.
60 // Other characters in the prefix within the property name must be lowercase.
61 // The prefix within the property name must be followed by a capital letter.
62 static bool hasCSSPropertyNamePrefix(const String& propertyName, const char* prefix)
66 for (const char* p = prefix; *p; ++p)
67 ASSERT(isASCIILower(*p));
68 ASSERT(propertyName.length());
71 if (toASCIILower(propertyName[0]) != prefix[0])
74 unsigned length = propertyName.length();
75 for (unsigned i = 1; i < length; ++i) {
77 return isASCIIUpper(propertyName[i]);
78 if (propertyName[i] != prefix[i])
84 struct CSSPropertyInfo {
85 unsigned propID: 30; // CSSPropertyID
86 unsigned nameWithDash: 1;
87 unsigned nameWithCssPrefix: 1;
90 static inline void countCssPropertyInfoUsage(v8::Isolate* isolate, const CSSPropertyInfo& propInfo)
92 if (propInfo.nameWithDash)
93 UseCounter::count(activeExecutionContext(isolate), UseCounter::CSSStyleDeclarationPropertyName);
94 if (propInfo.propID == CSSPropertyFloat && !propInfo.nameWithCssPrefix)
95 UseCounter::count(activeExecutionContext(isolate), UseCounter::CSSStyleDeclarationFloatPropertyName);
98 // When getting properties on CSSStyleDeclarations, the name used from
99 // Javascript and the actual name of the property are not the same, so
100 // we have to do the following translation. The translation turns upper
101 // case characters into lower case characters and inserts dashes to
104 // Example: 'backgroundPositionY' -> 'background-position-y'
106 // Also, certain prefixes such as 'css-' are stripped.
107 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName)
109 String propertyName = toCoreString(v8PropertyName);
110 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap;
111 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ());
112 CSSPropertyInfo* propInfo = map.get(propertyName);
114 unsigned length = propertyName.length();
118 StringBuilder builder;
119 builder.reserveCapacity(length);
122 bool hasSeenDash = false;
123 bool hasSeenCssPrefix = false;
125 if (hasCSSPropertyNamePrefix(propertyName, "css")) {
126 hasSeenCssPrefix = true;
128 } else if (hasCSSPropertyNamePrefix(propertyName, "webkit")) {
130 } else if (isASCIIUpper(propertyName[0])) {
134 bool hasSeenUpper = isASCIIUpper(propertyName[i]);
136 builder.append(toASCIILower(propertyName[i++]));
138 for (; i < length; ++i) {
139 UChar c = propertyName[i];
140 if (!isASCIIUpper(c)) {
148 builder.append(toASCIILower(c));
152 // Reject names containing both dashes and upper-case characters, such as "border-rightColor".
153 if (hasSeenDash && hasSeenUpper)
156 String propName = builder.toString();
157 CSSPropertyID propertyID = cssPropertyID(propName);
158 if (propertyID && RuntimeCSSEnabled::isCSSPropertyEnabled(propertyID)) {
159 propInfo = new CSSPropertyInfo();
160 propInfo->propID = propertyID;
161 propInfo->nameWithDash = hasSeenDash;
162 propInfo->nameWithCssPrefix = hasSeenCssPrefix;
163 map.add(propertyName, propInfo);
169 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCallbackInfo<v8::Array>& info)
171 typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector;
172 DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ());
173 static unsigned propertyNamesLength = 0;
175 if (propertyNames.isEmpty()) {
176 for (int id = firstCSSProperty; id <= lastCSSProperty; ++id) {
177 CSSPropertyID propertyId = static_cast<CSSPropertyID>(id);
178 if (RuntimeCSSEnabled::isCSSPropertyEnabled(propertyId))
179 propertyNames.append(getJSPropertyName(propertyId));
181 sort(propertyNames.begin(), propertyNames.end(), codePointCompareLessThan);
182 propertyNamesLength = propertyNames.size();
185 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), propertyNamesLength);
186 for (unsigned i = 0; i < propertyNamesLength; ++i) {
187 String key = propertyNames.at(i);
188 ASSERT(!key.isNull());
189 properties->Set(v8::Integer::New(info.GetIsolate(), i), v8String(info.GetIsolate(), key));
192 v8SetReturnValue(info, properties);
195 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::String> v8Name, const v8::PropertyCallbackInfo<v8::Integer>& info)
197 // NOTE: cssPropertyInfo lookups incur several mallocs.
198 // Successful lookups have the same cost the first time, but are cached.
199 if (CSSPropertyInfo* propInfo = cssPropertyInfo(v8Name)) {
200 countCssPropertyInfoUsage(info.GetIsolate(), *propInfo);
201 v8SetReturnValueInt(info, 0);
206 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
208 // First look for API defined attributes on the style declaration object.
209 if (info.Holder()->HasRealNamedCallbackProperty(name))
212 // Search the style declaration.
213 CSSPropertyInfo* propInfo = cssPropertyInfo(name);
215 // Do not handle non-property names.
219 countCssPropertyInfoUsage(info.GetIsolate(), *propInfo);
220 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
221 RefPtr<CSSValue> cssValue = imp->getPropertyCSSValueInternal(static_cast<CSSPropertyID>(propInfo->propID));
223 v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate());
227 String result = imp->getPropertyValueInternal(static_cast<CSSPropertyID>(propInfo->propID));
229 result = ""; // convert null to empty string.
231 v8SetReturnValueString(info, result, info.GetIsolate());
234 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
236 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
237 CSSPropertyInfo* propInfo = cssPropertyInfo(name);
241 countCssPropertyInfoUsage(info.GetIsolate(), *propInfo);
242 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<WithNullCheck>, propertyValue, value);
243 ExceptionState exceptionState(ExceptionState::SetterContext, getPropertyName(static_cast<CSSPropertyID>(propInfo->propID)), "CSSStyleDeclaration", info.Holder(), info.GetIsolate());
244 imp->setPropertyInternal(static_cast<CSSPropertyID>(propInfo->propID), propertyValue, false, exceptionState);
246 if (exceptionState.throwIfNeeded())
249 v8SetReturnValue(info, value);
252 } // namespace WebCore