OLD | NEW |
| (Empty) |
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/v8/ExceptionState.h" | |
35 #include "bindings/v8/V8Binding.h" | |
36 #include "core/CSSPropertyNames.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 "wtf/ASCIICType.h" | |
44 #include "wtf/PassRefPtr.h" | |
45 #include "wtf/RefPtr.h" | |
46 #include "wtf/StdLibExtras.h" | |
47 #include "wtf/Vector.h" | |
48 #include "wtf/text/StringBuilder.h" | |
49 #include "wtf/text/StringConcatenate.h" | |
50 | |
51 using namespace WTF; | |
52 | |
53 namespace WebCore { | |
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 lowerc
ase. | |
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. | |
60 static bool hasCSSPropertyNamePrefix(const String& propertyName, const char* pre
fix) | |
61 { | |
62 #ifndef NDEBUG | |
63 ASSERT(*prefix); | |
64 for (const char* p = prefix; *p; ++p) | |
65 ASSERT(isASCIILower(*p)); | |
66 ASSERT(propertyName.length()); | |
67 #endif | |
68 | |
69 if (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 isASCIIUpper(propertyName[i]); | |
76 if (propertyName[i] != prefix[i]) | |
77 return false; | |
78 } | |
79 return false; | |
80 } | |
81 | |
82 struct CSSPropertyInfo { | |
83 CSSPropertyID propID; | |
84 }; | |
85 | |
86 static CSSPropertyID cssResolvedPropertyID(const String& propertyName) | |
87 { | |
88 unsigned length = propertyName.length(); | |
89 if (!length) | |
90 return CSSPropertyInvalid; | |
91 | |
92 StringBuilder builder; | |
93 builder.reserveCapacity(length); | |
94 | |
95 unsigned i = 0; | |
96 bool hasSeenDash = false; | |
97 | |
98 if (hasCSSPropertyNamePrefix(propertyName, "css")) | |
99 i += 3; | |
100 else if (hasCSSPropertyNamePrefix(propertyName, "webkit")) | |
101 builder.append('-'); | |
102 else if (isASCIIUpper(propertyName[0])) | |
103 return CSSPropertyInvalid; | |
104 | |
105 bool hasSeenUpper = isASCIIUpper(propertyName[i]); | |
106 | |
107 builder.append(toASCIILower(propertyName[i++])); | |
108 | |
109 for (; i < length; ++i) { | |
110 UChar c = propertyName[i]; | |
111 if (!isASCIIUpper(c)) { | |
112 if (c == '-') | |
113 hasSeenDash = true; | |
114 builder.append(c); | |
115 } else { | |
116 hasSeenUpper = true; | |
117 builder.append('-'); | |
118 builder.append(toASCIILower(c)); | |
119 } | |
120 } | |
121 | |
122 // Reject names containing both dashes and upper-case characters, such as "b
order-rightColor". | |
123 if (hasSeenDash && hasSeenUpper) | |
124 return CSSPropertyInvalid; | |
125 | |
126 String propName = builder.toString(); | |
127 return cssPropertyID(propName); | |
128 } | |
129 | |
130 // When getting properties on CSSStyleDeclarations, the name used from | |
131 // Javascript and the actual name of the property are not the same, so | |
132 // we have to do the following translation. The translation turns upper | |
133 // case characters into lower case characters and inserts dashes to | |
134 // separate words. | |
135 // | |
136 // Example: 'backgroundPositionY' -> 'background-position-y' | |
137 // | |
138 // Also, certain prefixes such as 'css-' are stripped. | |
139 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName) | |
140 { | |
141 String propertyName = toCoreString(v8PropertyName); | |
142 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap; | |
143 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ()); | |
144 CSSPropertyInfo* propInfo = map.get(propertyName); | |
145 if (!propInfo) { | |
146 propInfo = new CSSPropertyInfo(); | |
147 propInfo->propID = cssResolvedPropertyID(propertyName); | |
148 map.add(propertyName, propInfo); | |
149 } | |
150 if (propInfo->propID && RuntimeCSSEnabled::isCSSPropertyEnabled(propInfo->pr
opID)) | |
151 return propInfo; | |
152 return 0; | |
153 } | |
154 | |
155 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCall
backInfo<v8::Array>& info) | |
156 { | |
157 typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector; | |
158 DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ()); | |
159 static unsigned propertyNamesLength = 0; | |
160 | |
161 if (propertyNames.isEmpty()) { | |
162 for (int id = firstCSSProperty; id <= lastCSSProperty; ++id) { | |
163 CSSPropertyID propertyId = static_cast<CSSPropertyID>(id); | |
164 if (RuntimeCSSEnabled::isCSSPropertyEnabled(propertyId)) | |
165 propertyNames.append(getJSPropertyName(propertyId)); | |
166 } | |
167 std::sort(propertyNames.begin(), propertyNames.end(), codePointCompareLe
ssThan); | |
168 propertyNamesLength = propertyNames.size(); | |
169 } | |
170 | |
171 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), propert
yNamesLength); | |
172 for (unsigned i = 0; i < propertyNamesLength; ++i) { | |
173 String key = propertyNames.at(i); | |
174 ASSERT(!key.isNull()); | |
175 properties->Set(v8::Integer::New(info.GetIsolate(), i), v8String(info.Ge
tIsolate(), key)); | |
176 } | |
177 | |
178 v8SetReturnValue(info, properties); | |
179 } | |
180 | |
181 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::String> v8Nam
e, const v8::PropertyCallbackInfo<v8::Integer>& info) | |
182 { | |
183 // NOTE: cssPropertyInfo lookups incur several mallocs. | |
184 // Successful lookups have the same cost the first time, but are cached. | |
185 if (cssPropertyInfo(v8Name)) { | |
186 v8SetReturnValueInt(info, 0); | |
187 return; | |
188 } | |
189 } | |
190 | |
191 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::String> name
, const v8::PropertyCallbackInfo<v8::Value>& info) | |
192 { | |
193 // First look for API defined attributes on the style declaration object. | |
194 if (info.Holder()->HasRealNamedCallbackProperty(name)) | |
195 return; | |
196 | |
197 // Search the style declaration. | |
198 CSSPropertyInfo* propInfo = cssPropertyInfo(name); | |
199 | |
200 // Do not handle non-property names. | |
201 if (!propInfo) | |
202 return; | |
203 | |
204 CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toNative(info.Holder()); | |
205 RefPtrWillBeRawPtr<CSSValue> cssValue = impl->getPropertyCSSValueInternal(st
atic_cast<CSSPropertyID>(propInfo->propID)); | |
206 if (cssValue) { | |
207 v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate(
)); | |
208 return; | |
209 } | |
210 | |
211 String result = impl->getPropertyValueInternal(static_cast<CSSPropertyID>(pr
opInfo->propID)); | |
212 v8SetReturnValueString(info, result, info.GetIsolate()); | |
213 } | |
214 | |
215 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::String> name
, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) | |
216 { | |
217 CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toNative(info.Holder()); | |
218 CSSPropertyInfo* propInfo = cssPropertyInfo(name); | |
219 if (!propInfo) | |
220 return; | |
221 | |
222 TOSTRING_VOID(V8StringResource<WithNullCheck>, propertyValue, value); | |
223 ExceptionState exceptionState(ExceptionState::SetterContext, getPropertyName
(static_cast<CSSPropertyID>(propInfo->propID)), "CSSStyleDeclaration", info.Hold
er(), info.GetIsolate()); | |
224 impl->setPropertyInternal(static_cast<CSSPropertyID>(propInfo->propID), prop
ertyValue, false, exceptionState); | |
225 | |
226 if (exceptionState.throwIfNeeded()) | |
227 return; | |
228 | |
229 v8SetReturnValue(info, value); | |
230 } | |
231 | |
232 } // namespace WebCore | |
OLD | NEW |