2 * Copyright (C) 2007-2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "V8CSSStyleDeclaration.h"
34 #include "CSSParser.h"
35 #include "CSSStyleDeclaration.h"
37 #include "CSSPrimitiveValue.h"
38 #include "EventTarget.h"
40 #include "V8Binding.h"
43 #include <wtf/text/StringBuilder.h>
44 #include <wtf/text/StringConcatenate.h>
45 #include <wtf/ASCIICType.h>
46 #include <wtf/PassRefPtr.h>
47 #include <wtf/RefPtr.h>
48 #include <wtf/StdLibExtras.h>
49 #include <wtf/Vector.h>
53 // FIXME: Next two functions look lifted verbatim from JSCSSStyleDeclarationCustom. Please remove duplication.
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 lowercase.
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* prefix)
64 for (const char* p = prefix; *p; ++p)
65 ASSERT(WTF::isASCIILower(*p));
66 ASSERT(propertyName.length());
69 if (WTF::toASCIILower(propertyName[0]) != prefix[0])
72 unsigned length = propertyName.length();
73 for (unsigned i = 1; i < length; ++i) {
75 return WTF::isASCIIUpper(propertyName[i]);
76 if (propertyName[i] != prefix[i])
82 class CSSPropertyInfo {
85 bool hadPixelOrPosPrefix;
89 // When getting properties on CSSStyleDeclarations, the name used from
90 // Javascript and the actual name of the property are not the same, so
91 // we have to do the following translation. The translation turns upper
92 // case characters into lower case characters and inserts dashes to
95 // Example: 'backgroundPositionY' -> 'background-position-y'
97 // Also, certain prefixes such as 'pos', 'css-' and 'pixel-' are stripped
98 // and the hadPixelOrPosPrefix out parameter is used to indicate whether or
99 // not the property name was prefixed with 'pos-' or 'pixel-'.
100 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String>v8PropertyName)
102 String propertyName = toWebCoreString(v8PropertyName);
103 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap;
104 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ());
105 CSSPropertyInfo* propInfo = map.get(propertyName);
107 unsigned length = propertyName.length();
108 bool hadPixelOrPosPrefix = false;
112 StringBuilder builder;
113 builder.reserveCapacity(length);
117 if (hasCSSPropertyNamePrefix(propertyName, "css"))
119 else if (hasCSSPropertyNamePrefix(propertyName, "pixel")) {
121 hadPixelOrPosPrefix = true;
122 } else if (hasCSSPropertyNamePrefix(propertyName, "pos")) {
124 hadPixelOrPosPrefix = true;
125 } else if (hasCSSPropertyNamePrefix(propertyName, "webkit")
126 || hasCSSPropertyNamePrefix(propertyName, "khtml")
127 || hasCSSPropertyNamePrefix(propertyName, "apple"))
129 else if (WTF::isASCIIUpper(propertyName[0]))
132 builder.append(WTF::toASCIILower(propertyName[i++]));
134 for (; i < length; ++i) {
135 UChar c = propertyName[i];
136 if (!WTF::isASCIIUpper(c))
139 builder.append(makeString('-', toASCIILower(c)));
142 String propName = builder.toString();
143 int propertyID = cssPropertyID(propName);
145 propInfo = new CSSPropertyInfo();
146 propInfo->hadPixelOrPosPrefix = hadPixelOrPosPrefix;
147 propInfo->wasFilter = (propName == "filter");
148 propInfo->propID = propertyID;
149 map.add(propertyName, propInfo);
155 v8::Handle<v8::Value> V8CSSStyleDeclaration::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
157 INC_STATS("DOM.CSSStyleDeclaration.NamedPropertyGetter");
158 // First look for API defined attributes on the style declaration object.
159 if (info.Holder()->HasRealNamedCallbackProperty(name))
160 return notHandledByInterceptor();
162 // Search the style declaration.
163 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
164 CSSPropertyInfo* propInfo = cssPropertyInfo(name);
166 // Do not handle non-property names.
168 return notHandledByInterceptor();
171 RefPtr<CSSValue> cssValue = imp->getPropertyCSSValue(propInfo->propID);
173 if (propInfo->hadPixelOrPosPrefix &&
174 cssValue->isPrimitiveValue()) {
175 return v8::Number::New(static_cast<CSSPrimitiveValue*>(
176 cssValue.get())->getFloatValue(CSSPrimitiveValue::CSS_PX));
178 return v8StringOrNull(cssValue->cssText());
181 String result = imp->getPropertyValue(propInfo->propID);
183 result = ""; // convert null to empty string.
185 // The 'filter' attribute is made undetectable in KJS/WebKit
186 // to avoid confusion with IE's filter extension.
187 if (propInfo->wasFilter)
188 return v8UndetectableString(result);
190 return v8String(result);
193 v8::Handle<v8::Value> V8CSSStyleDeclaration::namedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
195 INC_STATS("DOM.CSSStyleDeclaration.NamedPropertySetter");
196 CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
197 CSSPropertyInfo* propInfo = cssPropertyInfo(name);
199 return notHandledByInterceptor();
201 String propertyValue = toWebCoreStringWithNullCheck(value);
202 if (propInfo->hadPixelOrPosPrefix)
203 propertyValue.append("px");
205 ExceptionCode ec = 0;
206 int importantIndex = propertyValue.find("!important", 0, false);
207 bool important = false;
208 if (importantIndex != -1) {
210 propertyValue = propertyValue.left(importantIndex - 1);
212 imp->setProperty(propInfo->propID, propertyValue, important, ec);
220 } // namespace WebCore