Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / custom / V8CSSStyleDeclarationCustom.cpp
1 /*
2  * Copyright (C) 2007-2011 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 "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"
51
52 using namespace WTF;
53 using namespace std;
54
55 namespace WebCore {
56
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)
63 {
64 #ifndef NDEBUG
65     ASSERT(*prefix);
66     for (const char* p = prefix; *p; ++p)
67         ASSERT(isASCIILower(*p));
68     ASSERT(propertyName.length());
69 #endif
70
71     if (toASCIILower(propertyName[0]) != prefix[0])
72         return false;
73
74     unsigned length = propertyName.length();
75     for (unsigned i = 1; i < length; ++i) {
76         if (!prefix[i])
77             return isASCIIUpper(propertyName[i]);
78         if (propertyName[i] != prefix[i])
79             return false;
80     }
81     return false;
82 }
83
84 struct CSSPropertyInfo {
85     unsigned propID: 30; // CSSPropertyID
86     unsigned nameWithDash: 1;
87     unsigned nameWithCssPrefix: 1;
88 };
89
90 static inline void countCssPropertyInfoUsage(v8::Isolate* isolate, const CSSPropertyInfo& propInfo)
91 {
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);
96 }
97
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
102 // separate words.
103 //
104 // Example: 'backgroundPositionY' -> 'background-position-y'
105 //
106 // Also, certain prefixes such as 'css-' are stripped.
107 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName)
108 {
109     String propertyName = toCoreString(v8PropertyName);
110     typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap;
111     DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ());
112     CSSPropertyInfo* propInfo = map.get(propertyName);
113     if (!propInfo) {
114         unsigned length = propertyName.length();
115         if (!length)
116             return 0;
117
118         StringBuilder builder;
119         builder.reserveCapacity(length);
120
121         unsigned i = 0;
122         bool hasSeenDash = false;
123         bool hasSeenCssPrefix = false;
124
125         if (hasCSSPropertyNamePrefix(propertyName, "css")) {
126             hasSeenCssPrefix = true;
127             i += 3;
128         } else if (hasCSSPropertyNamePrefix(propertyName, "webkit")) {
129             builder.append('-');
130         } else if (isASCIIUpper(propertyName[0])) {
131             return 0;
132         }
133
134         bool hasSeenUpper = isASCIIUpper(propertyName[i]);
135
136         builder.append(toASCIILower(propertyName[i++]));
137
138         for (; i < length; ++i) {
139             UChar c = propertyName[i];
140             if (!isASCIIUpper(c)) {
141                 if (c == '-')
142                     hasSeenDash = true;
143                 builder.append(c);
144             }
145             else {
146                 hasSeenUpper = true;
147                 builder.append('-');
148                 builder.append(toASCIILower(c));
149             }
150         }
151
152         // Reject names containing both dashes and upper-case characters, such as "border-rightColor".
153         if (hasSeenDash && hasSeenUpper)
154             return 0;
155
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);
164         }
165     }
166     return propInfo;
167 }
168
169 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCallbackInfo<v8::Array>& info)
170 {
171     typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector;
172     DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ());
173     static unsigned propertyNamesLength = 0;
174
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));
180         }
181         sort(propertyNames.begin(), propertyNames.end(), codePointCompareLessThan);
182         propertyNamesLength = propertyNames.size();
183     }
184
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));
190     }
191
192     v8SetReturnValue(info, properties);
193 }
194
195 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::String> v8Name, const v8::PropertyCallbackInfo<v8::Integer>& info)
196 {
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);
202         return;
203     }
204 }
205
206 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
207 {
208     // First look for API defined attributes on the style declaration object.
209     if (info.Holder()->HasRealNamedCallbackProperty(name))
210         return;
211
212     // Search the style declaration.
213     CSSPropertyInfo* propInfo = cssPropertyInfo(name);
214
215     // Do not handle non-property names.
216     if (!propInfo)
217         return;
218
219     countCssPropertyInfoUsage(info.GetIsolate(), *propInfo);
220     CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
221     RefPtr<CSSValue> cssValue = imp->getPropertyCSSValueInternal(static_cast<CSSPropertyID>(propInfo->propID));
222     if (cssValue) {
223         v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate());
224         return;
225     }
226
227     String result = imp->getPropertyValueInternal(static_cast<CSSPropertyID>(propInfo->propID));
228     if (result.isNull())
229         result = ""; // convert null to empty string.
230
231     v8SetReturnValueString(info, result, info.GetIsolate());
232 }
233
234 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
235 {
236     CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
237     CSSPropertyInfo* propInfo = cssPropertyInfo(name);
238     if (!propInfo)
239         return;
240
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);
245
246     if (exceptionState.throwIfNeeded())
247         return;
248
249     v8SetReturnValue(info, value);
250 }
251
252 } // namespace WebCore