~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/bindings/js/JSCSSStyleDeclarationCustom.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Apple 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
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 
14
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
18
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
19
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
20
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
21
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
22
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
23
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "JSCSSStyleDeclaration.h"
 
28
 
 
29
#include "AtomicString.h"
 
30
#include "CSSPrimitiveValue.h"
 
31
#include "CSSStyleDeclaration.h"
 
32
#include "CSSValue.h"
 
33
#include "DeprecatedString.h"
 
34
#include "PlatformString.h"
 
35
#include <kjs/string_object.h>
 
36
 
 
37
namespace WebCore {
 
38
 
 
39
using namespace KJS;
 
40
 
 
41
static String cssPropertyName(const Identifier& propertyName, bool* hadPixelOrPosPrefix = 0)
 
42
{
 
43
    DeprecatedString prop = propertyName;
 
44
 
 
45
    int i = prop.length();
 
46
    while (--i) {
 
47
        ::UChar c = prop[i].unicode();
 
48
        if (c >= 'A' && c <= 'Z')
 
49
            prop.insert(i, '-');
 
50
    }
 
51
 
 
52
    prop = prop.lower();
 
53
 
 
54
    if (hadPixelOrPosPrefix)
 
55
        *hadPixelOrPosPrefix = false;
 
56
 
 
57
    if (prop.startsWith("css-"))
 
58
        prop = prop.mid(4);
 
59
    else if (prop.startsWith("pixel-")) {
 
60
        prop = prop.mid(6);
 
61
        if (hadPixelOrPosPrefix)
 
62
            *hadPixelOrPosPrefix = true;
 
63
    } else if (prop.startsWith("pos-")) {
 
64
        prop = prop.mid(4);
 
65
        if (hadPixelOrPosPrefix)
 
66
            *hadPixelOrPosPrefix = true;
 
67
    } else if (prop.startsWith("khtml-") || prop.startsWith("apple-") || prop.startsWith("webkit-"))
 
68
        prop.insert(0, '-');
 
69
 
 
70
    return prop;
 
71
}
 
72
 
 
73
static bool isCSSPropertyName(const Identifier& propertyName)
 
74
{
 
75
    return CSSStyleDeclaration::isPropertyName(cssPropertyName(propertyName));
 
76
}
 
77
 
 
78
bool JSCSSStyleDeclaration::canGetItemsForName(ExecState*, CSSStyleDeclaration*, const Identifier& propertyName)
 
79
{
 
80
    return isCSSPropertyName(propertyName);
 
81
}
 
82
 
 
83
JSValue* JSCSSStyleDeclaration::nameGetter(ExecState* exec, JSObject* originalObject, const Identifier& propertyName, const PropertySlot& slot)
 
84
{
 
85
    JSCSSStyleDeclaration* thisObj = static_cast<JSCSSStyleDeclaration*>(slot.slotBase());
 
86
 
 
87
    // Set up pixelOrPos boolean to handle the fact that
 
88
    // pixelTop returns "CSS Top" as number value in unit pixels
 
89
    // posTop returns "CSS top" as number value in unit pixels _if_ its a
 
90
    // positioned element. if it is not a positioned element, return 0
 
91
    // from MSIE documentation FIXME: IMPLEMENT THAT (Dirk)
 
92
    bool pixelOrPos;
 
93
    String prop = cssPropertyName(propertyName, &pixelOrPos);
 
94
    RefPtr<CSSValue> v = thisObj->impl()->getPropertyCSSValue(prop);
 
95
    if (v) {
 
96
        if (pixelOrPos && v->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE)
 
97
            return jsNumber(static_pointer_cast<CSSPrimitiveValue>(v)->getFloatValue(CSSPrimitiveValue::CSS_PX));
 
98
        return jsStringOrNull(v->cssText());
 
99
    }
 
100
 
 
101
    // If the property is a shorthand property (such as "padding"), 
 
102
    // it can only be accessed using getPropertyValue.
 
103
 
 
104
    // Make the SVG 'filter' attribute undetectable, to avoid confusion with the IE 'filter' attribute.
 
105
    if (propertyName == "filter")
 
106
        return new StringInstanceThatMasqueradesAsUndefined(exec->lexicalInterpreter()->builtinStringPrototype(), thisObj->impl()->getPropertyValue(prop));
 
107
 
 
108
    return jsString(thisObj->impl()->getPropertyValue(prop));
 
109
}
 
110
 
 
111
 
 
112
bool JSCSSStyleDeclaration::customPut(ExecState* exec, const Identifier& propertyName, JSValue* value, int /*attr*/)
 
113
{
 
114
    if (!isCSSPropertyName(propertyName))
 
115
        return false;
 
116
 
 
117
    DOMExceptionTranslator exception(exec);
 
118
    bool pixelOrPos;
 
119
    String prop = cssPropertyName(propertyName, &pixelOrPos);
 
120
    String propValue = valueToStringWithNullCheck(exec, value);
 
121
    if (pixelOrPos)
 
122
        propValue += "px";
 
123
    impl()->setProperty(prop, propValue, exception);
 
124
    return true;
 
125
}
 
126
 
 
127
} // namespace WebCore