Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: Source/core/css/CSSStyleDeclaration.cpp

Issue 15834008: Auto-generate V8CSSStyleDeclaration::namedPropertyGetter (Closed) Base URL: https://chromium.googlesource.com/chromium/blink@master
Patch Set: rebased Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/css/CSSStyleDeclaration.h ('k') | Source/core/css/CSSStyleDeclaration.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007-2011 Google Inc. All rights reserved. 2 * Copyright (C) 2013 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
11 * copyright notice, this list of conditions and the following disclaimer 11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the 12 * in the documentation and/or other materials provided with the
13 * distribution. 13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its 14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from 15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission. 16 * this software without specific prior written permission.
17 * 17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "V8CSSStyleDeclaration.h" 32 #include "core/css/CSSStyleDeclaration.h"
33 33
34 #include "CSSPropertyNames.h"
35 #include "core/css/CSSParser.h" 34 #include "core/css/CSSParser.h"
36 #include "core/css/CSSPrimitiveValue.h" 35 #include "core/css/CSSPrimitiveValue.h"
37 #include "core/css/CSSStyleDeclaration.h" 36 #include "core/css/CSSStyleSheet.h"
38 #include "core/css/CSSValue.h" 37 #include "core/css/CSSValue.h"
38 #include "core/dom/Document.h"
39 #include "core/dom/DocumentStyleSheetCollection.h"
39 #include "core/dom/EventTarget.h" 40 #include "core/dom/EventTarget.h"
41 #include "core/html/HTMLStyleElement.h"
40 #include "core/page/RuntimeCSSEnabled.h" 42 #include "core/page/RuntimeCSSEnabled.h"
41 43
42 #include "bindings/v8/V8Binding.h"
43
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 { 44 namespace WebCore {
56 45
57 // FIXME: Next two functions look lifted verbatim from JSCSSStyleDeclarationCust om. Please remove duplication. 46 // FIXME: Next two functions look lifted verbatim from JSCSSStyleDeclarationCust om. Please remove duplication.
58 47
59 // Check for a CSS prefix. 48 // Check for a CSS prefix.
60 // Passed prefix is all lowercase. 49 // Passed prefix is all lowercase.
61 // First character of the prefix within the property name may be upper or lowerc ase. 50 // First character of the prefix within the property name may be upper or lowerc ase.
62 // Other characters in the prefix within the property name must be lowercase. 51 // Other characters in the prefix within the property name must be lowercase.
63 // The prefix within the property name must be followed by a capital letter. 52 // The prefix within the property name must be followed by a capital letter.
64 static bool hasCSSPropertyNamePrefix(const String& propertyName, const char* pre fix) 53 static bool hasCSSPropertyNamePrefix(const String& propertyName, const char* pre fix)
(...skipping 11 matching lines...) Expand all
76 unsigned length = propertyName.length(); 65 unsigned length = propertyName.length();
77 for (unsigned i = 1; i < length; ++i) { 66 for (unsigned i = 1; i < length; ++i) {
78 if (!prefix[i]) 67 if (!prefix[i])
79 return isASCIIUpper(propertyName[i]); 68 return isASCIIUpper(propertyName[i]);
80 if (propertyName[i] != prefix[i]) 69 if (propertyName[i] != prefix[i])
81 return false; 70 return false;
82 } 71 }
83 return false; 72 return false;
84 } 73 }
85 74
86 class CSSPropertyInfo {
87 public:
88 CSSPropertyID propID;
89 bool hadPixelOrPosPrefix;
90 };
91
92 // When getting properties on CSSStyleDeclarations, the name used from 75 // When getting properties on CSSStyleDeclarations, the name used from
93 // Javascript and the actual name of the property are not the same, so 76 // Javascript and the actual name of the property are not the same, so
94 // we have to do the following translation. The translation turns upper 77 // we have to do the following translation. The translation turns upper
95 // case characters into lower case characters and inserts dashes to 78 // case characters into lower case characters and inserts dashes to
96 // separate words. 79 // separate words.
97 // 80 //
98 // Example: 'backgroundPositionY' -> 'background-position-y' 81 // Example: 'backgroundPositionY' -> 'background-position-y'
99 // 82 //
100 // Also, certain prefixes such as 'pos', 'css-' and 'pixel-' are stripped 83 // Also, certain prefixes such as 'pos', 'css-' and 'pixel-' are stripped
101 // and the hadPixelOrPosPrefix out parameter is used to indicate whether or 84 // and the hadPixelOrPosPrefix out parameter is used to indicate whether or
102 // not the property name was prefixed with 'pos-' or 'pixel-'. 85 // not the property name was prefixed with 'pos-' or 'pixel-'.
103 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName) 86 CSSPropertyInfo* CSSStyleDeclaration::cssPropertyInfo(const String& propertyName )
104 { 87 {
105 String propertyName = toWebCoreString(v8PropertyName);
106 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap; 88 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap;
107 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ()); 89 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ());
108 CSSPropertyInfo* propInfo = map.get(propertyName); 90 CSSPropertyInfo* propInfo = map.get(propertyName);
109 if (!propInfo) { 91 if (!propInfo) {
110 unsigned length = propertyName.length(); 92 unsigned length = propertyName.length();
111 bool hadPixelOrPosPrefix = false; 93 bool hadPixelOrPosPrefix = false;
112 if (!length) 94 if (!length)
113 return 0; 95 return 0;
114 96
115 StringBuilder builder; 97 StringBuilder builder;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 if (propertyID && RuntimeCSSEnabled::isCSSPropertyEnabled(propertyID)) { 129 if (propertyID && RuntimeCSSEnabled::isCSSPropertyEnabled(propertyID)) {
148 propInfo = new CSSPropertyInfo(); 130 propInfo = new CSSPropertyInfo();
149 propInfo->hadPixelOrPosPrefix = hadPixelOrPosPrefix; 131 propInfo->hadPixelOrPosPrefix = hadPixelOrPosPrefix;
150 propInfo->propID = propertyID; 132 propInfo->propID = propertyID;
151 map.add(propertyName, propInfo); 133 map.add(propertyName, propInfo);
152 } 134 }
153 } 135 }
154 return propInfo; 136 return propInfo;
155 } 137 }
156 138
157 v8::Handle<v8::Array> V8CSSStyleDeclaration::namedPropertyEnumerator(const v8::A ccessorInfo& info) 139 void CSSStyleDeclaration::anonymousNamedGetter(const AtomicString& name, bool& r eturnValue1Enabled, String& returnValue1, bool& returnValue2Enabled, float& retu rnValue2)
158 { 140 {
159 typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector;
160 DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ());
161 static unsigned propertyNamesLength = 0;
162
163 if (propertyNames.isEmpty()) {
164 for (int id = firstCSSProperty; id <= lastCSSProperty; ++id) {
165 CSSPropertyID propertyId = static_cast<CSSPropertyID>(id);
166 if (RuntimeCSSEnabled::isCSSPropertyEnabled(propertyId))
167 propertyNames.append(getJSPropertyName(propertyId));
168 }
169 sort(propertyNames.begin(), propertyNames.end(), codePointCompareLessTha n);
170 propertyNamesLength = propertyNames.size();
171 }
172
173 v8::Handle<v8::Array> properties = v8::Array::New(propertyNamesLength);
174 for (unsigned i = 0; i < propertyNamesLength; ++i) {
175 String key = propertyNames.at(i);
176 ASSERT(!key.isNull());
177 properties->Set(v8Integer(i, info.GetIsolate()), v8String(key, info.GetI solate()));
178 }
179
180 return properties;
181 }
182
183 v8::Handle<v8::Integer> V8CSSStyleDeclaration::namedPropertyQuery(v8::Local<v8:: String> v8Name, const v8::AccessorInfo& info)
184 {
185 // NOTE: cssPropertyInfo lookups incur several mallocs.
186 // Successful lookups have the same cost the first time, but are cached.
187 if (cssPropertyInfo(v8Name))
188 return v8Integer(0, info.GetIsolate());
189
190 return v8::Handle<v8::Integer>();
191 }
192
193 v8::Handle<v8::Value> V8CSSStyleDeclaration::namedPropertyGetter(v8::Local<v8::S tring> name, const v8::AccessorInfo& info)
194 {
195 // First look for API defined attributes on the style declaration object.
196 if (info.Holder()->HasRealNamedCallbackProperty(name))
197 return v8Undefined();
198
199 // Search the style declaration. 141 // Search the style declaration.
200 CSSPropertyInfo* propInfo = cssPropertyInfo(name); 142 CSSPropertyInfo* propInfo = cssPropertyInfo(name);
201 143
202 // Do not handle non-property names. 144 // Do not handle non-property names.
203 if (!propInfo) 145 if (!propInfo)
204 return v8Undefined(); 146 return;
205 147
206 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder()); 148 RefPtr<CSSValue> cssValue = getPropertyCSSValueInternal(static_cast<CSSPrope rtyID>(propInfo->propID));
207 RefPtr<CSSValue> cssValue = imp->getPropertyCSSValueInternal(static_cast<CSS PropertyID>(propInfo->propID));
208 if (cssValue) { 149 if (cssValue) {
209 if (propInfo->hadPixelOrPosPrefix && 150 if (propInfo->hadPixelOrPosPrefix && cssValue->isPrimitiveValue()) {
210 cssValue->isPrimitiveValue()) { 151 returnValue2Enabled = true;
211 return v8::Number::New(static_cast<CSSPrimitiveValue*>( 152 returnValue2 = static_cast<CSSPrimitiveValue*>(cssValue.get())->getF loatValue(CSSPrimitiveValue::CSS_PX);
212 cssValue.get())->getFloatValue(CSSPrimitiveValue::CSS_PX)); 153 return;
213 } 154 }
214 return v8StringOrNull(cssValue->cssText(), info.GetIsolate()); 155 returnValue1Enabled = true;
156 returnValue1 = cssValue->cssText();
157 return;
215 } 158 }
216 159
217 String result = imp->getPropertyValueInternal(static_cast<CSSPropertyID>(pro pInfo->propID)); 160 String result = getPropertyValueInternal(static_cast<CSSPropertyID>(propInfo ->propID));
218 if (result.isNull()) 161 if (result.isNull())
219 result = ""; // convert null to empty string. 162 result = ""; // convert null to empty string.
220 163
221 return v8String(result, info.GetIsolate()); 164 returnValue1 = result;
222 } 165 returnValue1Enabled = true;
223
224 v8::Handle<v8::Value> V8CSSStyleDeclaration::namedPropertySetter(v8::Local<v8::S tring> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
225 {
226 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
227 CSSPropertyInfo* propInfo = cssPropertyInfo(name);
228 if (!propInfo)
229 return v8Undefined();
230
231 String propertyValue = toWebCoreStringWithNullCheck(value);
232 if (propInfo->hadPixelOrPosPrefix)
233 propertyValue.append("px");
234
235 ExceptionCode ec = 0;
236 imp->setPropertyInternal(static_cast<CSSPropertyID>(propInfo->propID), prope rtyValue, false, ec);
237
238 if (ec)
239 setDOMException(ec, info.GetIsolate());
240
241 return value;
242 } 166 }
243 167
244 } // namespace WebCore 168 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/css/CSSStyleDeclaration.h ('k') | Source/core/css/CSSStyleDeclaration.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698