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 "bindings/core/v8/V8CSSStyleDeclaration.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "bindings/core/v8/V8Binding.h"
36 #include "core/CSSPropertyNames.h"
37 #include "core/css/CSSPrimitiveValue.h"
38 #include "core/css/CSSPropertyMetadata.h"
39 #include "core/css/CSSStyleDeclaration.h"
40 #include "core/css/CSSValue.h"
41 #include "core/css/parser/CSSParser.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"
56 // Check for a CSS prefix.
57 // Passed prefix is all lowercase.
58 // First character of the prefix within the property name may be upper or lowercase.
59 // Other characters in the prefix within the property name must be lowercase.
60 // The prefix within the property name must be followed by a capital letter.
61 static bool hasCSSPropertyNamePrefix(const String& propertyName, const char* prefix)
65 for (const char* p = prefix; *p; ++p)
66 ASSERT(isASCIILower(*p));
67 ASSERT(propertyName.length());
70 if (toASCIILower(propertyName[0]) != prefix[0])
73 unsigned length = propertyName.length();
74 for (unsigned i = 1; i < length; ++i) {
76 return isASCIIUpper(propertyName[i]);
77 if (propertyName[i] != prefix[i])
83 struct CSSPropertyInfo {
87 static CSSPropertyID cssResolvedPropertyID(const String& propertyName, v8::Isolate* isolate)
89 unsigned length = propertyName.length();
91 return CSSPropertyInvalid;
93 StringBuilder builder;
94 builder.reserveCapacity(length);
97 bool hasSeenDash = false;
99 if (hasCSSPropertyNamePrefix(propertyName, "css")) {
101 // getComputedStyle(elem).cssX is a non-standard behaviour
102 // Measure this behaviour as CSSXGetComputedStyleQueries.
103 UseCounter::countIfNotPrivateScript(isolate, callingExecutionContext(isolate), UseCounter::CSSXGetComputedStyleQueries);
104 } else if (hasCSSPropertyNamePrefix(propertyName, "webkit"))
106 else if (isASCIIUpper(propertyName[0]))
107 return CSSPropertyInvalid;
109 bool hasSeenUpper = isASCIIUpper(propertyName[i]);
111 builder.append(toASCIILower(propertyName[i++]));
113 for (; i < length; ++i) {
114 UChar c = propertyName[i];
115 if (!isASCIIUpper(c)) {
122 builder.append(toASCIILower(c));
126 // Reject names containing both dashes and upper-case characters, such as "border-rightColor".
127 if (hasSeenDash && hasSeenUpper)
128 return CSSPropertyInvalid;
130 String propName = builder.toString();
131 return cssPropertyID(propName);
134 // When getting properties on CSSStyleDeclarations, the name used from
135 // Javascript and the actual name of the property are not the same, so
136 // we have to do the following translation. The translation turns upper
137 // case characters into lower case characters and inserts dashes to
140 // Example: 'backgroundPositionY' -> 'background-position-y'
142 // Also, certain prefixes such as 'css-' are stripped.
143 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName, v8::Isolate* isolate)
145 String propertyName = toCoreString(v8PropertyName);
146 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap;
147 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ());
148 CSSPropertyInfo* propInfo = map.get(propertyName);
150 propInfo = new CSSPropertyInfo();
151 propInfo->propID = cssResolvedPropertyID(propertyName, isolate);
152 map.add(propertyName, propInfo);
154 if (!propInfo->propID)
156 ASSERT(CSSPropertyMetadata::isEnabledProperty(propInfo->propID));
160 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCallbackInfo<v8::Array>& info)
162 typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector;
163 DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ());
164 static unsigned propertyNamesLength = 0;
166 if (propertyNames.isEmpty()) {
167 for (int id = firstCSSProperty; id <= lastCSSProperty; ++id) {
168 CSSPropertyID propertyId = static_cast<CSSPropertyID>(id);
169 if (CSSPropertyMetadata::isEnabledProperty(propertyId))
170 propertyNames.append(getJSPropertyName(propertyId));
172 std::sort(propertyNames.begin(), propertyNames.end(), codePointCompareLessThan);
173 propertyNamesLength = propertyNames.size();
176 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), propertyNamesLength);
177 for (unsigned i = 0; i < propertyNamesLength; ++i) {
178 String key = propertyNames.at(i);
179 ASSERT(!key.isNull());
180 properties->Set(v8::Integer::New(info.GetIsolate(), i), v8String(info.GetIsolate(), key));
183 v8SetReturnValue(info, properties);
186 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::String> v8Name, const v8::PropertyCallbackInfo<v8::Integer>& info)
188 // NOTE: cssPropertyInfo lookups incur several mallocs.
189 // Successful lookups have the same cost the first time, but are cached.
190 if (cssPropertyInfo(v8Name, info.GetIsolate())) {
191 v8SetReturnValueInt(info, 0);
196 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
198 // First look for API defined attributes on the style declaration object.
199 if (info.Holder()->HasRealNamedCallbackProperty(name))
202 // Search the style declaration.
203 CSSPropertyInfo* propInfo = cssPropertyInfo(name, info.GetIsolate());
205 // Do not handle non-property names.
209 CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toImpl(info.Holder());
210 RefPtrWillBeRawPtr<CSSValue> cssValue = impl->getPropertyCSSValueInternal(static_cast<CSSPropertyID>(propInfo->propID));
212 v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate());
216 String result = impl->getPropertyValueInternal(static_cast<CSSPropertyID>(propInfo->propID));
217 v8SetReturnValueString(info, result, info.GetIsolate());
220 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
222 CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toImpl(info.Holder());
223 CSSPropertyInfo* propInfo = cssPropertyInfo(name, info.GetIsolate());
227 TOSTRING_VOID(V8StringResource<TreatNullAsNullString>, propertyValue, value);
228 ExceptionState exceptionState(ExceptionState::SetterContext, getPropertyName(static_cast<CSSPropertyID>(propInfo->propID)), "CSSStyleDeclaration", info.Holder(), info.GetIsolate());
229 impl->setPropertyInternal(static_cast<CSSPropertyID>(propInfo->propID), propertyValue, false, exceptionState);
231 if (exceptionState.throwIfNeeded())
234 v8SetReturnValue(info, value);