Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / 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 "bindings/core/v8/V8CSSStyleDeclaration.h"
33
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"
51
52 using namespace WTF;
53
54 namespace blink {
55
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)
62 {
63 #if ENABLE(ASSERT)
64     ASSERT(*prefix);
65     for (const char* p = prefix; *p; ++p)
66         ASSERT(isASCIILower(*p));
67     ASSERT(propertyName.length());
68 #endif
69
70     if (toASCIILower(propertyName[0]) != prefix[0])
71         return false;
72
73     unsigned length = propertyName.length();
74     for (unsigned i = 1; i < length; ++i) {
75         if (!prefix[i])
76             return isASCIIUpper(propertyName[i]);
77         if (propertyName[i] != prefix[i])
78             return false;
79     }
80     return false;
81 }
82
83 struct CSSPropertyInfo {
84     CSSPropertyID propID;
85 };
86
87 static CSSPropertyID cssResolvedPropertyID(const String& propertyName, v8::Isolate* isolate)
88 {
89     unsigned length = propertyName.length();
90     if (!length)
91         return CSSPropertyInvalid;
92
93     StringBuilder builder;
94     builder.reserveCapacity(length);
95
96     unsigned i = 0;
97     bool hasSeenDash = false;
98
99     if (hasCSSPropertyNamePrefix(propertyName, "css")) {
100         i += 3;
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"))
105         builder.append('-');
106     else if (isASCIIUpper(propertyName[0]))
107         return CSSPropertyInvalid;
108
109     bool hasSeenUpper = isASCIIUpper(propertyName[i]);
110
111     builder.append(toASCIILower(propertyName[i++]));
112
113     for (; i < length; ++i) {
114         UChar c = propertyName[i];
115         if (!isASCIIUpper(c)) {
116             if (c == '-')
117                 hasSeenDash = true;
118             builder.append(c);
119         } else {
120             hasSeenUpper = true;
121             builder.append('-');
122             builder.append(toASCIILower(c));
123         }
124     }
125
126     // Reject names containing both dashes and upper-case characters, such as "border-rightColor".
127     if (hasSeenDash && hasSeenUpper)
128         return CSSPropertyInvalid;
129
130     String propName = builder.toString();
131     return cssPropertyID(propName);
132 }
133
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
138 // separate words.
139 //
140 // Example: 'backgroundPositionY' -> 'background-position-y'
141 //
142 // Also, certain prefixes such as 'css-' are stripped.
143 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName, v8::Isolate* isolate)
144 {
145     String propertyName = toCoreString(v8PropertyName);
146     typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap;
147     DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ());
148     CSSPropertyInfo* propInfo = map.get(propertyName);
149     if (!propInfo) {
150         propInfo = new CSSPropertyInfo();
151         propInfo->propID = cssResolvedPropertyID(propertyName, isolate);
152         map.add(propertyName, propInfo);
153     }
154     if (!propInfo->propID)
155         return 0;
156     ASSERT(CSSPropertyMetadata::isEnabledProperty(propInfo->propID));
157     return propInfo;
158 }
159
160 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCallbackInfo<v8::Array>& info)
161 {
162     typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector;
163     DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ());
164     static unsigned propertyNamesLength = 0;
165
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));
171         }
172         std::sort(propertyNames.begin(), propertyNames.end(), codePointCompareLessThan);
173         propertyNamesLength = propertyNames.size();
174     }
175
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));
181     }
182
183     v8SetReturnValue(info, properties);
184 }
185
186 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::String> v8Name, const v8::PropertyCallbackInfo<v8::Integer>& info)
187 {
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);
192         return;
193     }
194 }
195
196 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
197 {
198     // First look for API defined attributes on the style declaration object.
199     if (info.Holder()->HasRealNamedCallbackProperty(name))
200         return;
201
202     // Search the style declaration.
203     CSSPropertyInfo* propInfo = cssPropertyInfo(name, info.GetIsolate());
204
205     // Do not handle non-property names.
206     if (!propInfo)
207         return;
208
209     CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toImpl(info.Holder());
210     RefPtrWillBeRawPtr<CSSValue> cssValue = impl->getPropertyCSSValueInternal(static_cast<CSSPropertyID>(propInfo->propID));
211     if (cssValue) {
212         v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate());
213         return;
214     }
215
216     String result = impl->getPropertyValueInternal(static_cast<CSSPropertyID>(propInfo->propID));
217     v8SetReturnValueString(info, result, info.GetIsolate());
218 }
219
220 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
221 {
222     CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toImpl(info.Holder());
223     CSSPropertyInfo* propInfo = cssPropertyInfo(name, info.GetIsolate());
224     if (!propInfo)
225         return;
226
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);
230
231     if (exceptionState.throwIfNeeded())
232         return;
233
234     v8SetReturnValue(info, value);
235 }
236
237 } // namespace blink