1 /* 2 * Copyright (C) 2007-2011 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 "bindings/core/v8/V8CSSStyleDeclaration.h" 33 34 #include "bindings/core/v8/ExceptionState.h" 35 #include "bindings/core/v8/V8Binding.h" 36 #include "core/CSSPropertyNames.h" 37 #include "core/css/CSSPrimitiveValue.h" 38 #include "core/css/CSSPropertyMetadata.h" 39 #include "core/css/CSSStyleDeclaration.h" 40 #include "core/css/CSSValue.h" 41 #include "core/css/parser/CSSParser.h" 42 #include "core/events/EventTarget.h" 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 #include "wtf/text/StringBuilder.h" 49 #include "wtf/text/StringConcatenate.h" 50 51 using namespace WTF; 52 53 namespace blink { 54 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) 61 { 62 #if ENABLE(ASSERT) 63 ASSERT(*prefix); 64 for (const char* p = prefix; *p; ++p) 65 ASSERT(isASCIILower(*p)); 66 ASSERT(propertyName.length()); 67 #endif 68 69 if (toASCIILower(propertyName[0]) != prefix[0]) 70 return false; 71 72 unsigned length = propertyName.length(); 73 for (unsigned i = 1; i < length; ++i) { 74 if (!prefix[i]) 75 return isASCIIUpper(propertyName[i]); 76 if (propertyName[i] != prefix[i]) 77 return false; 78 } 79 return false; 80 } 81 82 struct CSSPropertyInfo { 83 CSSPropertyID propID; 84 }; 85 86 static CSSPropertyID cssResolvedPropertyID(const String& propertyName) 87 { 88 unsigned length = propertyName.length(); 89 if (!length) 90 return CSSPropertyInvalid; 91 92 StringBuilder builder; 93 builder.reserveCapacity(length); 94 95 unsigned i = 0; 96 bool hasSeenDash = false; 97 98 if (hasCSSPropertyNamePrefix(propertyName, "css")) 99 i += 3; 100 else if (hasCSSPropertyNamePrefix(propertyName, "webkit")) 101 builder.append('-'); 102 else if (isASCIIUpper(propertyName[0])) 103 return CSSPropertyInvalid; 104 105 bool hasSeenUpper = isASCIIUpper(propertyName[i]); 106 107 builder.append(toASCIILower(propertyName[i++])); 108 109 for (; i < length; ++i) { 110 UChar c = propertyName[i]; 111 if (!isASCIIUpper(c)) { 112 if (c == '-') 113 hasSeenDash = true; 114 builder.append(c); 115 } else { 116 hasSeenUpper = true; 117 builder.append('-'); 118 builder.append(toASCIILower(c)); 119 } 120 } 121 122 // Reject names containing both dashes and upper-case characters, such as "border-rightColor". 123 if (hasSeenDash && hasSeenUpper) 124 return CSSPropertyInvalid; 125 126 String propName = builder.toString(); 127 return cssPropertyID(propName); 128 } 129 130 // When getting properties on CSSStyleDeclarations, the name used from 131 // Javascript and the actual name of the property are not the same, so 132 // we have to do the following translation. The translation turns upper 133 // case characters into lower case characters and inserts dashes to 134 // separate words. 135 // 136 // Example: 'backgroundPositionY' -> 'background-position-y' 137 // 138 // Also, certain prefixes such as 'css-' are stripped. 139 static CSSPropertyInfo* cssPropertyInfo(v8::Handle<v8::String> v8PropertyName) 140 { 141 String propertyName = toCoreString(v8PropertyName); 142 typedef HashMap<String, CSSPropertyInfo*> CSSPropertyInfoMap; 143 DEFINE_STATIC_LOCAL(CSSPropertyInfoMap, map, ()); 144 CSSPropertyInfo* propInfo = map.get(propertyName); 145 if (!propInfo) { 146 propInfo = new CSSPropertyInfo(); 147 propInfo->propID = cssResolvedPropertyID(propertyName); 148 map.add(propertyName, propInfo); 149 } 150 if (!propInfo->propID) 151 return 0; 152 ASSERT(CSSPropertyMetadata::isEnabledProperty(propInfo->propID)); 153 return propInfo; 154 } 155 156 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCallbackInfo<v8::Array>& info) 157 { 158 typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector; 159 DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ()); 160 static unsigned propertyNamesLength = 0; 161 162 if (propertyNames.isEmpty()) { 163 for (int id = firstCSSProperty; id <= lastCSSProperty; ++id) { 164 CSSPropertyID propertyId = static_cast<CSSPropertyID>(id); 165 if (CSSPropertyMetadata::isEnabledProperty(propertyId)) 166 propertyNames.append(getJSPropertyName(propertyId)); 167 } 168 std::sort(propertyNames.begin(), propertyNames.end(), codePointCompareLessThan); 169 propertyNamesLength = propertyNames.size(); 170 } 171 172 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), propertyNamesLength); 173 for (unsigned i = 0; i < propertyNamesLength; ++i) { 174 String key = propertyNames.at(i); 175 ASSERT(!key.isNull()); 176 properties->Set(v8::Integer::New(info.GetIsolate(), i), v8String(info.GetIsolate(), key)); 177 } 178 179 v8SetReturnValue(info, properties); 180 } 181 182 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::String> v8Name, const v8::PropertyCallbackInfo<v8::Integer>& info) 183 { 184 // NOTE: cssPropertyInfo lookups incur several mallocs. 185 // Successful lookups have the same cost the first time, but are cached. 186 if (cssPropertyInfo(v8Name)) { 187 v8SetReturnValueInt(info, 0); 188 return; 189 } 190 } 191 192 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info) 193 { 194 // First look for API defined attributes on the style declaration object. 195 if (info.Holder()->HasRealNamedCallbackProperty(name)) 196 return; 197 198 // Search the style declaration. 199 CSSPropertyInfo* propInfo = cssPropertyInfo(name); 200 201 // Do not handle non-property names. 202 if (!propInfo) 203 return; 204 205 CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toImpl(info.Holder()); 206 RefPtrWillBeRawPtr<CSSValue> cssValue = impl->getPropertyCSSValueInternal(static_cast<CSSPropertyID>(propInfo->propID)); 207 if (cssValue) { 208 v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate()); 209 return; 210 } 211 212 String result = impl->getPropertyValueInternal(static_cast<CSSPropertyID>(propInfo->propID)); 213 v8SetReturnValueString(info, result, info.GetIsolate()); 214 } 215 216 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) 217 { 218 CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toImpl(info.Holder()); 219 CSSPropertyInfo* propInfo = cssPropertyInfo(name); 220 if (!propInfo) 221 return; 222 223 TOSTRING_VOID(V8StringResource<TreatNullAsNullString>, propertyValue, value); 224 ExceptionState exceptionState(ExceptionState::SetterContext, getPropertyName(static_cast<CSSPropertyID>(propInfo->propID)), "CSSStyleDeclaration", info.Holder(), info.GetIsolate()); 225 impl->setPropertyInternal(static_cast<CSSPropertyID>(propInfo->propID), propertyValue, false, exceptionState); 226 227 if (exceptionState.throwIfNeeded()) 228 return; 229 230 v8SetReturnValue(info, value); 231 } 232 233 } // namespace blink 234