OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007-2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2007-2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 #include "CSSPropertyNames.h" | 34 #include "CSSPropertyNames.h" |
35 #include "bindings/v8/ExceptionState.h" | 35 #include "bindings/v8/ExceptionState.h" |
36 #include "bindings/v8/V8Binding.h" | 36 #include "bindings/v8/V8Binding.h" |
37 #include "core/css/parser/BisonCSSParser.h" | 37 #include "core/css/parser/BisonCSSParser.h" |
38 #include "core/css/CSSPrimitiveValue.h" | 38 #include "core/css/CSSPrimitiveValue.h" |
39 #include "core/css/CSSStyleDeclaration.h" | 39 #include "core/css/CSSStyleDeclaration.h" |
40 #include "core/css/CSSValue.h" | 40 #include "core/css/CSSValue.h" |
41 #include "core/css/RuntimeCSSEnabled.h" | 41 #include "core/css/RuntimeCSSEnabled.h" |
42 #include "core/events/EventTarget.h" | 42 #include "core/events/EventTarget.h" |
43 #include "core/frame/UseCounter.h" | |
44 #include "wtf/ASCIICType.h" | 43 #include "wtf/ASCIICType.h" |
45 #include "wtf/PassRefPtr.h" | 44 #include "wtf/PassRefPtr.h" |
46 #include "wtf/RefPtr.h" | 45 #include "wtf/RefPtr.h" |
47 #include "wtf/StdLibExtras.h" | 46 #include "wtf/StdLibExtras.h" |
48 #include "wtf/Vector.h" | 47 #include "wtf/Vector.h" |
49 #include "wtf/text/StringBuilder.h" | 48 #include "wtf/text/StringBuilder.h" |
50 #include "wtf/text/StringConcatenate.h" | 49 #include "wtf/text/StringConcatenate.h" |
51 | 50 |
52 using namespace WTF; | 51 using namespace WTF; |
53 using namespace std; | 52 using namespace std; |
(...skipping 21 matching lines...) Expand all Loading... |
75 for (unsigned i = 1; i < length; ++i) { | 74 for (unsigned i = 1; i < length; ++i) { |
76 if (!prefix[i]) | 75 if (!prefix[i]) |
77 return isASCIIUpper(propertyName[i]); | 76 return isASCIIUpper(propertyName[i]); |
78 if (propertyName[i] != prefix[i]) | 77 if (propertyName[i] != prefix[i]) |
79 return false; | 78 return false; |
80 } | 79 } |
81 return false; | 80 return false; |
82 } | 81 } |
83 | 82 |
84 struct CSSPropertyInfo { | 83 struct CSSPropertyInfo { |
85 unsigned propID: 30; // CSSPropertyID | 84 CSSPropertyID propID; |
86 unsigned nameWithDash: 1; | |
87 unsigned nameWithCssPrefix: 1; | |
88 }; | 85 }; |
89 | 86 |
90 static inline void countCssPropertyInfoUsage(v8::Isolate* isolate, const CSSProp
ertyInfo& propInfo) | |
91 { | |
92 if (propInfo.nameWithDash) | |
93 UseCounter::count(callingExecutionContext(isolate), UseCounter::CSSStyle
DeclarationPropertyName); | |
94 if (propInfo.propID == CSSPropertyFloat && !propInfo.nameWithCssPrefix) | |
95 UseCounter::count(callingExecutionContext(isolate), UseCounter::CSSStyle
DeclarationFloatPropertyName); | |
96 } | |
97 | |
98 // When getting properties on CSSStyleDeclarations, the name used from | 87 // When getting properties on CSSStyleDeclarations, the name used from |
99 // Javascript and the actual name of the property are not the same, so | 88 // 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 | 89 // we have to do the following translation. The translation turns upper |
101 // case characters into lower case characters and inserts dashes to | 90 // case characters into lower case characters and inserts dashes to |
102 // separate words. | 91 // separate words. |
103 // | 92 // |
104 // Example: 'backgroundPositionY' -> 'background-position-y' | 93 // Example: 'backgroundPositionY' -> 'background-position-y' |
105 // | 94 // |
106 // Also, certain prefixes such as 'css-' are stripped. | 95 // Also, certain prefixes such as 'css-' are stripped. |
107 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName) | 96 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName) |
108 { | 97 { |
109 String propertyName = toCoreString(v8PropertyName); | 98 String propertyName = toCoreString(v8PropertyName); |
110 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap; | 99 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap; |
111 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ()); | 100 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ()); |
112 CSSPropertyInfo* propInfo = map.get(propertyName); | 101 CSSPropertyInfo* propInfo = map.get(propertyName); |
113 if (!propInfo) { | 102 if (!propInfo) { |
114 unsigned length = propertyName.length(); | 103 unsigned length = propertyName.length(); |
115 if (!length) | 104 if (!length) |
116 return 0; | 105 return 0; |
117 | 106 |
118 StringBuilder builder; | 107 StringBuilder builder; |
119 builder.reserveCapacity(length); | 108 builder.reserveCapacity(length); |
120 | 109 |
121 unsigned i = 0; | 110 unsigned i = 0; |
122 bool hasSeenDash = false; | 111 bool hasSeenDash = false; |
123 bool hasSeenCssPrefix = false; | |
124 | 112 |
125 if (hasCSSPropertyNamePrefix(propertyName, "css")) { | 113 if (hasCSSPropertyNamePrefix(propertyName, "css")) |
126 hasSeenCssPrefix = true; | |
127 i += 3; | 114 i += 3; |
128 } else if (hasCSSPropertyNamePrefix(propertyName, "webkit")) { | 115 else if (hasCSSPropertyNamePrefix(propertyName, "webkit")) |
129 builder.append('-'); | 116 builder.append('-'); |
130 } else if (isASCIIUpper(propertyName[0])) { | 117 else if (isASCIIUpper(propertyName[0])) |
131 return 0; | 118 return 0; |
132 } | |
133 | 119 |
134 bool hasSeenUpper = isASCIIUpper(propertyName[i]); | 120 bool hasSeenUpper = isASCIIUpper(propertyName[i]); |
135 | 121 |
136 builder.append(toASCIILower(propertyName[i++])); | 122 builder.append(toASCIILower(propertyName[i++])); |
137 | 123 |
138 for (; i < length; ++i) { | 124 for (; i < length; ++i) { |
139 UChar c = propertyName[i]; | 125 UChar c = propertyName[i]; |
140 if (!isASCIIUpper(c)) { | 126 if (!isASCIIUpper(c)) { |
141 if (c == '-') | 127 if (c == '-') |
142 hasSeenDash = true; | 128 hasSeenDash = true; |
143 builder.append(c); | 129 builder.append(c); |
144 } | 130 } |
145 else { | 131 else { |
146 hasSeenUpper = true; | 132 hasSeenUpper = true; |
147 builder.append('-'); | 133 builder.append('-'); |
148 builder.append(toASCIILower(c)); | 134 builder.append(toASCIILower(c)); |
149 } | 135 } |
150 } | 136 } |
151 | 137 |
152 // Reject names containing both dashes and upper-case characters, such a
s "border-rightColor". | 138 // Reject names containing both dashes and upper-case characters, such a
s "border-rightColor". |
153 if (hasSeenDash && hasSeenUpper) | 139 if (hasSeenDash && hasSeenUpper) |
154 return 0; | 140 return 0; |
155 | 141 |
156 String propName = builder.toString(); | 142 String propName = builder.toString(); |
157 CSSPropertyID propertyID = cssPropertyID(propName); | 143 CSSPropertyID propertyID = cssPropertyID(propName); |
158 if (propertyID && RuntimeCSSEnabled::isCSSPropertyEnabled(propertyID)) { | 144 if (propertyID && RuntimeCSSEnabled::isCSSPropertyEnabled(propertyID)) { |
159 propInfo = new CSSPropertyInfo(); | 145 propInfo = new CSSPropertyInfo(); |
160 propInfo->propID = propertyID; | 146 propInfo->propID = propertyID; |
161 propInfo->nameWithDash = hasSeenDash; | |
162 propInfo->nameWithCssPrefix = hasSeenCssPrefix; | |
163 map.add(propertyName, propInfo); | 147 map.add(propertyName, propInfo); |
164 } | 148 } |
165 } | 149 } |
166 return propInfo; | 150 return propInfo; |
167 } | 151 } |
168 | 152 |
169 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCall
backInfo<v8::Array>& info) | 153 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCall
backInfo<v8::Array>& info) |
170 { | 154 { |
171 typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector; | 155 typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector; |
172 DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ()); | 156 DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ()); |
(...skipping 16 matching lines...) Expand all Loading... |
189 properties->Set(v8::Integer::New(info.GetIsolate(), i), v8String(info.Ge
tIsolate(), key)); | 173 properties->Set(v8::Integer::New(info.GetIsolate(), i), v8String(info.Ge
tIsolate(), key)); |
190 } | 174 } |
191 | 175 |
192 v8SetReturnValue(info, properties); | 176 v8SetReturnValue(info, properties); |
193 } | 177 } |
194 | 178 |
195 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::String> v8Nam
e, const v8::PropertyCallbackInfo<v8::Integer>& info) | 179 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::String> v8Nam
e, const v8::PropertyCallbackInfo<v8::Integer>& info) |
196 { | 180 { |
197 // NOTE: cssPropertyInfo lookups incur several mallocs. | 181 // NOTE: cssPropertyInfo lookups incur several mallocs. |
198 // Successful lookups have the same cost the first time, but are cached. | 182 // Successful lookups have the same cost the first time, but are cached. |
199 if (CSSPropertyInfo* propInfo = cssPropertyInfo(v8Name)) { | 183 if (cssPropertyInfo(v8Name)) { |
200 countCssPropertyInfoUsage(info.GetIsolate(), *propInfo); | |
201 v8SetReturnValueInt(info, 0); | 184 v8SetReturnValueInt(info, 0); |
202 return; | 185 return; |
203 } | 186 } |
204 } | 187 } |
205 | 188 |
206 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::String> name
, const v8::PropertyCallbackInfo<v8::Value>& info) | 189 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::String> name
, const v8::PropertyCallbackInfo<v8::Value>& info) |
207 { | 190 { |
208 // First look for API defined attributes on the style declaration object. | 191 // First look for API defined attributes on the style declaration object. |
209 if (info.Holder()->HasRealNamedCallbackProperty(name)) | 192 if (info.Holder()->HasRealNamedCallbackProperty(name)) |
210 return; | 193 return; |
211 | 194 |
212 // Search the style declaration. | 195 // Search the style declaration. |
213 CSSPropertyInfo* propInfo = cssPropertyInfo(name); | 196 CSSPropertyInfo* propInfo = cssPropertyInfo(name); |
214 | 197 |
215 // Do not handle non-property names. | 198 // Do not handle non-property names. |
216 if (!propInfo) | 199 if (!propInfo) |
217 return; | 200 return; |
218 | 201 |
219 countCssPropertyInfoUsage(info.GetIsolate(), *propInfo); | |
220 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder()); | 202 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder()); |
221 RefPtrWillBeRawPtr<CSSValue> cssValue = imp->getPropertyCSSValueInternal(sta
tic_cast<CSSPropertyID>(propInfo->propID)); | 203 RefPtrWillBeRawPtr<CSSValue> cssValue = imp->getPropertyCSSValueInternal(sta
tic_cast<CSSPropertyID>(propInfo->propID)); |
222 if (cssValue) { | 204 if (cssValue) { |
223 v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate(
)); | 205 v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate(
)); |
224 return; | 206 return; |
225 } | 207 } |
226 | 208 |
227 String result = imp->getPropertyValueInternal(static_cast<CSSPropertyID>(pro
pInfo->propID)); | 209 String result = imp->getPropertyValueInternal(static_cast<CSSPropertyID>(pro
pInfo->propID)); |
228 if (result.isNull()) | 210 if (result.isNull()) |
229 result = ""; // convert null to empty string. | 211 result = ""; // convert null to empty string. |
230 | 212 |
231 v8SetReturnValueString(info, result, info.GetIsolate()); | 213 v8SetReturnValueString(info, result, info.GetIsolate()); |
232 } | 214 } |
233 | 215 |
234 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::String> name
, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) | 216 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::String> name
, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) |
235 { | 217 { |
236 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder()); | 218 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder()); |
237 CSSPropertyInfo* propInfo = cssPropertyInfo(name); | 219 CSSPropertyInfo* propInfo = cssPropertyInfo(name); |
238 if (!propInfo) | 220 if (!propInfo) |
239 return; | 221 return; |
240 | 222 |
241 countCssPropertyInfoUsage(info.GetIsolate(), *propInfo); | |
242 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<WithNullCheck>, proper
tyValue, value); | 223 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<WithNullCheck>, proper
tyValue, value); |
243 ExceptionState exceptionState(ExceptionState::SetterContext, getPropertyName
(static_cast<CSSPropertyID>(propInfo->propID)), "CSSStyleDeclaration", info.Hold
er(), info.GetIsolate()); | 224 ExceptionState exceptionState(ExceptionState::SetterContext, getPropertyName
(static_cast<CSSPropertyID>(propInfo->propID)), "CSSStyleDeclaration", info.Hold
er(), info.GetIsolate()); |
244 imp->setPropertyInternal(static_cast<CSSPropertyID>(propInfo->propID), prope
rtyValue, false, exceptionState); | 225 imp->setPropertyInternal(static_cast<CSSPropertyID>(propInfo->propID), prope
rtyValue, false, exceptionState); |
245 | 226 |
246 if (exceptionState.throwIfNeeded()) | 227 if (exceptionState.throwIfNeeded()) |
247 return; | 228 return; |
248 | 229 |
249 v8SetReturnValue(info, value); | 230 v8SetReturnValue(info, value); |
250 } | 231 } |
251 | 232 |
252 } // namespace WebCore | 233 } // namespace WebCore |
OLD | NEW |