• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2009 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 "CSSStyleDeclaration.h"
33 
34 #include "CSSParser.h"
35 #include "CSSValue.h"
36 #include "CSSPrimitiveValue.h"
37 #include "EventTarget.h"
38 
39 #include "V8Binding.h"
40 #include "V8CustomBinding.h"
41 #include "V8Proxy.h"
42 
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 
49 namespace WebCore {
50 
51 // FIXME: Next two functions look lifted verbatim from JSCSSStyleDeclarationCustom. Please remove duplication.
52 
53 // Check for a CSS prefix.
54 // Passed prefix is all lowercase.
55 // First character of the prefix within the property name may be upper or lowercase.
56 // Other characters in the prefix within the property name must be lowercase.
57 // The prefix within the property name must be followed by a capital letter.
hasCSSPropertyNamePrefix(const String & propertyName,const char * prefix)58 static bool hasCSSPropertyNamePrefix(const String& propertyName, const char* prefix)
59 {
60 #ifndef NDEBUG
61     ASSERT(*prefix);
62     for (const char* p = prefix; *p; ++p)
63         ASSERT(WTF::isASCIILower(*p));
64     ASSERT(propertyName.length());
65 #endif
66 
67     if (WTF::toASCIILower(propertyName[0]) != prefix[0])
68         return false;
69 
70     unsigned length = propertyName.length();
71     for (unsigned i = 1; i < length; ++i) {
72         if (!prefix[i])
73             return WTF::isASCIIUpper(propertyName[i]);
74         if (propertyName[i] != prefix[i])
75             return false;
76     }
77     return false;
78 }
79 
80 class CSSPropertyInfo {
81 public:
82     int propID;
83     bool hadPixelOrPosPrefix;
84     bool wasFilter;
85 };
86 
87 // When getting properties on CSSStyleDeclarations, the name used from
88 // Javascript and the actual name of the property are not the same, so
89 // we have to do the following translation.  The translation turns upper
90 // case characters into lower case characters and inserts dashes to
91 // separate words.
92 //
93 // Example: 'backgroundPositionY' -> 'background-position-y'
94 //
95 // Also, certain prefixes such as 'pos', 'css-' and 'pixel-' are stripped
96 // and the hadPixelOrPosPrefix out parameter is used to indicate whether or
97 // not the property name was prefixed with 'pos-' or 'pixel-'.
cssPropertyInfo(v8::Handle<v8::String> v8PropertyName)98 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String>v8PropertyName)
99 {
100     String propertyName = toWebCoreString(v8PropertyName);
101     typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap;
102     DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ());
103     CSSPropertyInfo* propInfo = map.get(propertyName);
104     if (!propInfo) {
105         unsigned length = propertyName.length();
106         bool hadPixelOrPosPrefix = false;
107         if (!length)
108             return 0;
109 
110         Vector<UChar> name;
111         name.reserveCapacity(length);
112 
113         unsigned i = 0;
114 
115         if (hasCSSPropertyNamePrefix(propertyName, "css"))
116             i += 3;
117         else if (hasCSSPropertyNamePrefix(propertyName, "pixel")) {
118             i += 5;
119             hadPixelOrPosPrefix = true;
120         } else if (hasCSSPropertyNamePrefix(propertyName, "pos")) {
121             i += 3;
122             hadPixelOrPosPrefix = true;
123         } else if (hasCSSPropertyNamePrefix(propertyName, "webkit")
124                 || hasCSSPropertyNamePrefix(propertyName, "khtml")
125                 || hasCSSPropertyNamePrefix(propertyName, "apple"))
126             name.append('-');
127         else if (WTF::isASCIIUpper(propertyName[0]))
128             return 0;
129 
130         name.append(WTF::toASCIILower(propertyName[i++]));
131 
132         for (; i < length; ++i) {
133             UChar c = propertyName[i];
134             if (!WTF::isASCIIUpper(c))
135                 name.append(c);
136             else {
137                 name.append('-');
138                 name.append(WTF::toASCIILower(c));
139             }
140         }
141 
142         String propName = String::adopt(name);
143         int propertyID = cssPropertyID(propName);
144         if (propertyID) {
145             propInfo = new CSSPropertyInfo();
146             propInfo->hadPixelOrPosPrefix = hadPixelOrPosPrefix;
147             propInfo->wasFilter = (propName == "filter");
148             propInfo->propID = propertyID;
149             map.add(propertyName, propInfo);
150         }
151     }
152     return propInfo;
153 }
154 
NAMED_PROPERTY_GETTER(CSSStyleDeclaration)155 NAMED_PROPERTY_GETTER(CSSStyleDeclaration)
156 {
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();
161 
162     // Search the style declaration.
163     CSSStyleDeclaration* imp =
164         V8DOMWrapper::convertToNativeObject<CSSStyleDeclaration>(V8ClassIndex::CSSSTYLEDECLARATION, info.Holder());
165     CSSPropertyInfo* propInfo = cssPropertyInfo(name);
166 
167     // Do not handle non-property names.
168     if (!propInfo)
169         return notHandledByInterceptor();
170 
171 
172     RefPtr<CSSValue> cssValue = imp->getPropertyCSSValue(propInfo->propID);
173     if (cssValue) {
174         if (propInfo->hadPixelOrPosPrefix &&
175             cssValue->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE) {
176             return v8::Number::New(static_cast<CSSPrimitiveValue*>(
177                 cssValue.get())->getFloatValue(CSSPrimitiveValue::CSS_PX));
178         }
179         return v8StringOrNull(cssValue->cssText());
180     }
181 
182     String result = imp->getPropertyValue(propInfo->propID);
183     if (result.isNull())
184         result = "";  // convert null to empty string.
185 
186     // The 'filter' attribute is made undetectable in KJS/WebKit
187     // to avoid confusion with IE's filter extension.
188     if (propInfo->wasFilter)
189         return v8UndetectableString(result);
190 
191     return v8String(result);
192 }
193 
NAMED_PROPERTY_SETTER(CSSStyleDeclaration)194 NAMED_PROPERTY_SETTER(CSSStyleDeclaration)
195 {
196     INC_STATS("DOM.CSSStyleDeclaration.NamedPropertySetter");
197     CSSStyleDeclaration* imp =
198         V8DOMWrapper::convertToNativeObject<CSSStyleDeclaration>(
199             V8ClassIndex::CSSSTYLEDECLARATION, info.Holder());
200     CSSPropertyInfo* propInfo = cssPropertyInfo(name);
201     if (!propInfo)
202         return notHandledByInterceptor();
203 
204     String propertyValue = toWebCoreStringWithNullCheck(value);
205     if (propInfo->hadPixelOrPosPrefix)
206         propertyValue.append("px");
207 
208     ExceptionCode ec = 0;
209     int importantIndex = propertyValue.find("!important", 0, false);
210     bool important = false;
211     if (importantIndex != -1) {
212         important = true;
213         propertyValue = propertyValue.left(importantIndex - 1);
214     }
215     imp->setProperty(propInfo->propID, propertyValue, important, ec);
216 
217     if (ec)
218         throwError(ec);
219 
220     return value;
221 }
222 
223 } // namespace WebCore
224