Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "core/frame/UseCounter.h" | |
| 44 #include "wtf/ASCIICType.h" | |
| 45 #include "wtf/PassRefPtr.h" | |
| 46 #include "wtf/RefPtr.h" | |
| 47 #include "wtf/StdLibExtras.h" | |
| 48 #include "wtf/Vector.h" | |
| 49 #include "wtf/text/StringBuilder.h" | |
| 50 #include "wtf/text/StringConcatenate.h" | |
| 51 | |
| 52 using namespace WTF; | |
| 53 | |
| 54 namespace blink { | |
| 55 | |
| 56 // Check for a CSS prefix. | |
| 57 // Passed prefix is all lowercase. | |
| 58 // First character of the prefix within the property name may be upper or lowerc ase. | |
| 59 // Other characters in the prefix within the property name must be lowercase. | |
| 60 // The prefix within the property name must be followed by a capital letter. | |
| 61 static bool hasCSSPropertyNamePrefix(const String& propertyName, const char* pre fix) | |
| 62 { | |
| 63 #if ENABLE(ASSERT) | |
| 64 ASSERT(*prefix); | |
| 65 for (const char* p = prefix; *p; ++p) | |
| 66 ASSERT(isASCIILower(*p)); | |
| 67 ASSERT(propertyName.length()); | |
| 68 #endif | |
| 69 | |
| 70 if (toASCIILower(propertyName[0]) != prefix[0]) | |
| 71 return false; | |
| 72 | |
| 73 unsigned length = propertyName.length(); | |
| 74 for (unsigned i = 1; i < length; ++i) { | |
| 75 if (!prefix[i]) | |
| 76 return isASCIIUpper(propertyName[i]); | |
| 77 if (propertyName[i] != prefix[i]) | |
| 78 return false; | |
| 79 } | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 static CSSPropertyID parseCSSPropertyID(v8::Isolate* isolate, const String& prop ertyName) | |
| 84 { | |
| 85 unsigned length = propertyName.length(); | |
| 86 if (!length) | |
| 87 return CSSPropertyInvalid; | |
| 88 | |
| 89 StringBuilder builder; | |
| 90 builder.reserveCapacity(length); | |
| 91 | |
| 92 unsigned i = 0; | |
| 93 bool hasSeenDash = false; | |
| 94 | |
| 95 if (hasCSSPropertyNamePrefix(propertyName, "css")) { | |
| 96 i += 3; | |
| 97 // getComputedStyle(elem).cssX is a non-standard behaviour | |
| 98 // Measure this behaviour as CSSXGetComputedStyleQueries. | |
| 99 UseCounter::countIfNotPrivateScript(isolate, callingExecutionContext(iso late), UseCounter::CSSXGetComputedStyleQueries); | |
| 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 "b order-rightColor". | |
| 123 if (hasSeenDash && hasSeenUpper) | |
| 124 return CSSPropertyInvalid; | |
| 125 | |
| 126 String propName = builder.toString(); | |
| 127 return unresolvedCSSPropertyID(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. | |
|
haraken
2015/04/29 15:53:45
Keep this comment.
| |
| 139 static CSSPropertyID cssPropertyInfo(v8::Local<v8::String> v8PropertyName, v8::I solate* isolate) | |
| 140 { | |
| 141 String propertyName = toCoreString(v8PropertyName); | |
| 142 typedef HashMap<String, CSSPropertyID> CSSPropertyIDMap; | |
| 143 DEFINE_STATIC_LOCAL(CSSPropertyIDMap, map, ()); | |
| 144 CSSPropertyIDMap::iterator iter = map.find(propertyName); | |
| 145 if (iter != map.end()) | |
| 146 return iter->value; | |
| 147 | |
| 148 CSSPropertyID unresolvedProperty = parseCSSPropertyID(isolate, propertyName) ; | |
| 149 map.add(propertyName, unresolvedProperty); | |
| 150 ASSERT(!unresolvedProperty || CSSPropertyMetadata::isEnabledProperty(unresol vedProperty)); | |
| 151 return unresolvedProperty; | |
| 152 } | |
| 153 | |
| 154 void V8CSSStyleDeclaration::namedPropertyEnumeratorCustom(const v8::PropertyCall backInfo<v8::Array>& info) | |
| 155 { | |
| 156 typedef Vector<String, numCSSProperties - 1> PreAllocatedPropertyVector; | |
| 157 DEFINE_STATIC_LOCAL(PreAllocatedPropertyVector, propertyNames, ()); | |
| 158 static unsigned propertyNamesLength = 0; | |
| 159 | |
| 160 if (propertyNames.isEmpty()) { | |
| 161 for (int id = firstCSSProperty; id <= lastCSSProperty; ++id) { | |
| 162 CSSPropertyID propertyId = static_cast<CSSPropertyID>(id); | |
| 163 if (CSSPropertyMetadata::isEnabledProperty(propertyId)) | |
| 164 propertyNames.append(getJSPropertyName(propertyId)); | |
| 165 } | |
| 166 std::sort(propertyNames.begin(), propertyNames.end(), codePointCompareLe ssThan); | |
| 167 propertyNamesLength = propertyNames.size(); | |
| 168 } | |
| 169 | |
| 170 v8::Local<v8::Array> properties = v8::Array::New(info.GetIsolate(), property NamesLength); | |
| 171 for (unsigned i = 0; i < propertyNamesLength; ++i) { | |
| 172 String key = propertyNames.at(i); | |
| 173 ASSERT(!key.isNull()); | |
| 174 properties->Set(v8::Integer::New(info.GetIsolate(), i), v8String(info.Ge tIsolate(), key)); | |
| 175 } | |
| 176 | |
| 177 v8SetReturnValue(info, properties); | |
| 178 } | |
| 179 | |
| 180 void V8CSSStyleDeclaration::namedPropertyQueryCustom(v8::Local<v8::Name> v8Name, const v8::PropertyCallbackInfo<v8::Integer>& info) | |
| 181 { | |
| 182 if (!v8Name->IsString()) | |
| 183 return; | |
| 184 // NOTE: cssPropertyInfo lookups incur several mallocs. | |
| 185 // Successful lookups have the same cost the first time, but are cached. | |
| 186 if (cssPropertyInfo(v8Name.As<v8::String>(), info.GetIsolate())) { | |
| 187 v8SetReturnValueInt(info, 0); | |
| 188 return; | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 void V8CSSStyleDeclaration::namedPropertyGetterCustom(v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) | |
| 193 { | |
| 194 // Search the style declaration. | |
| 195 CSSPropertyID unresolvedProperty = cssPropertyInfo(name.As<v8::String>(), in fo.GetIsolate()); | |
| 196 | |
| 197 // Do not handle non-property names. | |
| 198 if (!unresolvedProperty) | |
| 199 return; | |
| 200 CSSPropertyID resolvedProperty = resolveCSSPropertyID(unresolvedProperty); | |
| 201 | |
| 202 CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toImpl(info.Holder()); | |
| 203 RefPtrWillBeRawPtr<CSSValue> cssValue = impl->getPropertyCSSValueInternal(re solvedProperty); | |
| 204 if (cssValue) { | |
| 205 v8SetReturnValueStringOrNull(info, cssValue->cssText(), info.GetIsolate( )); | |
| 206 return; | |
| 207 } | |
| 208 | |
| 209 String result = impl->getPropertyValueInternal(resolvedProperty); | |
| 210 v8SetReturnValueString(info, result, info.GetIsolate()); | |
| 211 } | |
| 212 | |
| 213 void V8CSSStyleDeclaration::namedPropertySetterCustom(v8::Local<v8::Name> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) | |
| 214 { | |
| 215 if (!name->IsString()) | |
| 216 return; | |
| 217 CSSStyleDeclaration* impl = V8CSSStyleDeclaration::toImpl(info.Holder()); | |
| 218 CSSPropertyID unresolvedProperty = cssPropertyInfo(name.As<v8::String>(), in fo.GetIsolate()); | |
| 219 if (!unresolvedProperty) | |
| 220 return; | |
| 221 | |
| 222 TOSTRING_VOID(V8StringResource<TreatNullAsNullString>, propertyValue, value) ; | |
| 223 ExceptionState exceptionState(ExceptionState::SetterContext, getPropertyName (resolveCSSPropertyID(unresolvedProperty)), "CSSStyleDeclaration", info.Holder() , info.GetIsolate()); | |
| 224 impl->setPropertyInternal(unresolvedProperty, propertyValue, false, exceptio nState); | |
| 225 | |
| 226 if (exceptionState.throwIfNeeded()) | |
| 227 return; | |
| 228 | |
| 229 v8SetReturnValue(info, value); | |
| 230 } | |
| 231 | |
| 232 } // namespace blink | |
| OLD | NEW |