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 "V8CSSStyleDeclaration.h"
33
34#include "CSSParser.h"
35#include "CSSStyleDeclaration.h"
36#include "CSSValue.h"
37#include "CSSPrimitiveValue.h"
38#include "EventTarget.h"
39
40#include "V8Binding.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
49namespace 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.
58static 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
80class CSSPropertyInfo {
81public:
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-'.
98static 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
155v8::Handle<v8::Value> V8CSSStyleDeclaration::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
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 = V8CSSStyleDeclaration::toNative(info.Holder());
164    CSSPropertyInfo* propInfo = cssPropertyInfo(name);
165
166    // Do not handle non-property names.
167    if (!propInfo)
168        return notHandledByInterceptor();
169
170
171    RefPtr<CSSValue> cssValue = imp->getPropertyCSSValue(propInfo->propID);
172    if (cssValue) {
173        if (propInfo->hadPixelOrPosPrefix &&
174            cssValue->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE) {
175            return v8::Number::New(static_cast<CSSPrimitiveValue*>(
176                cssValue.get())->getFloatValue(CSSPrimitiveValue::CSS_PX));
177        }
178        return v8StringOrNull(cssValue->cssText());
179    }
180
181    String result = imp->getPropertyValue(propInfo->propID);
182    if (result.isNull())
183        result = "";  // convert null to empty string.
184
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);
189
190    return v8String(result);
191}
192
193v8::Handle<v8::Value> V8CSSStyleDeclaration::namedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
194{
195    INC_STATS("DOM.CSSStyleDeclaration.NamedPropertySetter");
196    CSSStyleDeclaration* imp = V8CSSStyleDeclaration::toNative(info.Holder());
197    CSSPropertyInfo* propInfo = cssPropertyInfo(name);
198    if (!propInfo)
199        return notHandledByInterceptor();
200
201    String propertyValue = toWebCoreStringWithNullCheck(value);
202    if (propInfo->hadPixelOrPosPrefix)
203        propertyValue.append("px");
204
205    ExceptionCode ec = 0;
206    int importantIndex = propertyValue.find("!important", 0, false);
207    bool important = false;
208    if (importantIndex != -1) {
209        important = true;
210        propertyValue = propertyValue.left(importantIndex - 1);
211    }
212    imp->setProperty(propInfo->propID, propertyValue, important, ec);
213
214    if (ec)
215        throwError(ec);
216
217    return value;
218}
219
220} // namespace WebCore
221