1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CSSStyleDeclaration.h"
#include "CSSPropertyNames.h"
#include "CSSPropertyParser.h"
#include "Document.h"
#include "Settings.h"
#include "StyledElement.h"
#include <variant>
#include <wtf/StdLibExtras.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/ParsingUtilities.h>
#include <wtf/text/StringParsingBuffer.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(CSSStyleDeclaration);
namespace {
enum class PropertyNamePrefix { None, Epub, WebKit };
static inline bool matchesCSSPropertyNamePrefix(const StringImpl& propertyName, ASCIILiteral prefix)
{
ASSERT(toASCIILower(propertyName[0]) == prefix[0]);
const size_t offset = 1;
#ifndef NDEBUG
for (auto character : prefix.span8())
ASSERT(isASCIILower(character));
ASSERT(propertyName.length());
#endif
// The prefix within the property name must be followed by a capital letter.
// Other characters in the prefix within the property name must be lowercase.
if (propertyName.length() < prefix.length() + 1)
return false;
for (size_t i = offset; i < prefix.length(); ++i) {
if (propertyName[i] != prefix[i])
return false;
}
if (!isASCIIUpper(propertyName[prefix.length()]))
return false;
return true;
}
static PropertyNamePrefix propertyNamePrefix(const StringImpl& propertyName)
{
ASSERT(propertyName.length());
// First character of the prefix within the property name may be upper or lowercase.
UChar firstChar = toASCIILower(propertyName[0]);
switch (firstChar) {
case 'e':
if (matchesCSSPropertyNamePrefix(propertyName, "epub"_s))
return PropertyNamePrefix::Epub;
break;
case 'w':
if (matchesCSSPropertyNamePrefix(propertyName, "webkit"_s))
return PropertyNamePrefix::WebKit;
break;
default:
break;
}
return PropertyNamePrefix::None;
}
static inline void writeWebKitPrefix(std::span<char>& buffer)
{
memcpySpan(consumeSpan(buffer, 8), "-webkit-"_span);
}
static inline void writeEpubPrefix(std::span<char>& buffer)
{
memcpySpan(consumeSpan(buffer, 6), "-epub-"_span);
}
static CSSPropertyID parseJavaScriptCSSPropertyName(const AtomString& propertyName)
{
using CSSPropertyIDMap = UncheckedKeyHashMap<AtomString, CSSPropertyID>;
static NeverDestroyed<CSSPropertyIDMap> propertyIDCache;
auto* propertyNameString = propertyName.impl();
if (!propertyNameString)
return CSSPropertyInvalid;
unsigned length = propertyNameString->length();
if (!length)
return CSSPropertyInvalid;
if (auto id = propertyIDCache.get().get(propertyName))
return id;
constexpr size_t bufferSize = maxCSSPropertyNameLength;
std::array<char, bufferSize> buffer;
std::span<char> bufferSpan { buffer };
const char* name = buffer.data();
unsigned i = 0;
switch (propertyNamePrefix(*propertyNameString)) {
case PropertyNamePrefix::None:
if (isASCIIUpper((*propertyNameString)[0]))
return CSSPropertyInvalid;
break;
case PropertyNamePrefix::Epub:
writeEpubPrefix(bufferSpan);
i += 4;
break;
case PropertyNamePrefix::WebKit:
writeWebKitPrefix(bufferSpan);
i += 6;
break;
}
consume(bufferSpan) = toASCIILower((*propertyNameString)[i++]);
char* stringEnd = std::to_address(std::span { buffer }.first(buffer.size() - 1).end());
size_t bufferSizeLeft = stringEnd - bufferSpan.data();
size_t propertySizeLeft = length - i;
if (propertySizeLeft > bufferSizeLeft)
return CSSPropertyInvalid;
for (; i < length; ++i) {
UChar c = (*propertyNameString)[i];
if (!c || !isASCII(c))
return CSSPropertyInvalid; // illegal character
if (isASCIIUpper(c)) {
size_t bufferSizeLeft = stringEnd - bufferSpan.data();
size_t propertySizeLeft = length - i + 1;
if (propertySizeLeft > bufferSizeLeft)
return CSSPropertyInvalid;
bufferSpan[0] = '-';
bufferSpan[1] = toASCIILowerUnchecked(c);
skip(bufferSpan, 2);
} else
consume(bufferSpan) = c;
ASSERT(!bufferSpan.empty());
}
ASSERT(!bufferSpan.empty());
unsigned outputLength = bufferSpan.data() - buffer.data();
auto id = findCSSProperty(name, outputLength);
// FIXME: Why aren't we memoizing CSS property names we fail to find?
if (id != CSSPropertyInvalid)
propertyIDCache.get().add(propertyName, id);
return id;
}
}
CSSPropertyID CSSStyleDeclaration::getCSSPropertyIDFromJavaScriptPropertyName(const AtomString& propertyName)
{
// FIXME: This exposes properties disabled by settings. Pass result of CSSStyleDeclaration::settings instead of null?
Settings* settings = nullptr;
auto property = parseJavaScriptCSSPropertyName(propertyName);
return isExposed(property, settings) ? property : CSSPropertyInvalid;
}
const Settings* CSSStyleDeclaration::settings() const
{
return parentElement() ? &parentElement()->document().settings() : nullptr;
}
enum class CSSPropertyLookupMode { ConvertUsingDashPrefix, ConvertUsingNoDashPrefix, NoConversion };
template<CSSPropertyLookupMode mode> static CSSPropertyID lookupCSSPropertyFromIDLAttribute(const AtomString& attribute)
{
static NeverDestroyed<UncheckedKeyHashMap<AtomString, CSSPropertyID>> cache;
if (auto id = cache.get().get(attribute))
return id;
std::array<char, maxCSSPropertyNameLength> outputBuffer;
size_t outputIndex = 0;
if constexpr (mode == CSSPropertyLookupMode::ConvertUsingDashPrefix || mode == CSSPropertyLookupMode::ConvertUsingNoDashPrefix) {
// Conversion is implementing the "IDL attribute to CSS property algorithm"
// from https://drafts.csswg.org/cssom/#idl-attribute-to-css-property.
if constexpr (mode == CSSPropertyLookupMode::ConvertUsingDashPrefix)
outputBuffer[outputIndex++] = '-';
readCharactersForParsing(attribute, [&](auto buffer) {
while (buffer.hasCharactersRemaining()) {
auto c = *buffer++;
ASSERT_WITH_MESSAGE(isASCII(c), "Invalid property name: %s", attribute.string().utf8().data());
if (isASCIIUpper(c)) {
outputBuffer[outputIndex++] = '-';
outputBuffer[outputIndex++] = toASCIILowerUnchecked(c);
} else
outputBuffer[outputIndex++] = c;
}
});
} else {
readCharactersForParsing(attribute, [&](auto buffer) {
while (buffer.hasCharactersRemaining()) {
auto c = *buffer++;
ASSERT_WITH_MESSAGE(c == '-' || isASCIILower(c), "Invalid property name: %s", attribute.string().utf8().data());
outputBuffer[outputIndex++] = c;
}
});
}
auto id = findCSSProperty(outputBuffer.data(), outputIndex);
ASSERT_WITH_MESSAGE(id != CSSPropertyInvalid, "Invalid property name: %s", attribute.string().utf8().data());
cache.get().add(attribute, id);
return id;
}
String CSSStyleDeclaration::propertyValueForCamelCasedIDLAttribute(const AtomString& attribute)
{
auto propertyID = lookupCSSPropertyFromIDLAttribute<CSSPropertyLookupMode::ConvertUsingNoDashPrefix>(attribute);
ASSERT_WITH_MESSAGE(propertyID != CSSPropertyInvalid, "Invalid attribute: %s", attribute.string().utf8().data());
return getPropertyValueInternal(propertyID);
}
ExceptionOr<void> CSSStyleDeclaration::setPropertyValueForCamelCasedIDLAttribute(const AtomString& attribute, const String& value)
{
auto propertyID = lookupCSSPropertyFromIDLAttribute<CSSPropertyLookupMode::ConvertUsingNoDashPrefix>(attribute);
ASSERT_WITH_MESSAGE(propertyID != CSSPropertyInvalid, "Invalid attribute: %s", attribute.string().utf8().data());
return setPropertyInternal(propertyID, value, IsImportant::No);
}
String CSSStyleDeclaration::propertyValueForWebKitCasedIDLAttribute(const AtomString& attribute)
{
auto propertyID = lookupCSSPropertyFromIDLAttribute<CSSPropertyLookupMode::ConvertUsingDashPrefix>(attribute);
ASSERT_WITH_MESSAGE(propertyID != CSSPropertyInvalid, "Invalid attribute: %s", attribute.string().utf8().data());
return getPropertyValueInternal(propertyID);
}
ExceptionOr<void> CSSStyleDeclaration::setPropertyValueForWebKitCasedIDLAttribute(const AtomString& attribute, const String& value)
{
auto propertyID = lookupCSSPropertyFromIDLAttribute<CSSPropertyLookupMode::ConvertUsingDashPrefix>(attribute);
ASSERT_WITH_MESSAGE(propertyID != CSSPropertyInvalid, "Invalid attribute: %s", attribute.string().utf8().data());
return setPropertyInternal(propertyID, value, IsImportant::No);
}
String CSSStyleDeclaration::propertyValueForDashedIDLAttribute(const AtomString& attribute)
{
auto propertyID = lookupCSSPropertyFromIDLAttribute<CSSPropertyLookupMode::NoConversion>(attribute);
ASSERT_WITH_MESSAGE(propertyID != CSSPropertyInvalid, "Invalid attribute: %s", attribute.string().utf8().data());
return getPropertyValueInternal(propertyID);
}
ExceptionOr<void> CSSStyleDeclaration::setPropertyValueForDashedIDLAttribute(const AtomString& attribute, const String& value)
{
auto propertyID = lookupCSSPropertyFromIDLAttribute<CSSPropertyLookupMode::NoConversion>(attribute);
ASSERT_WITH_MESSAGE(propertyID != CSSPropertyInvalid, "Invalid attribute: %s", attribute.string().utf8().data());
return setPropertyInternal(propertyID, value, IsImportant::No);
}
String CSSStyleDeclaration::propertyValueForEpubCasedIDLAttribute(const AtomString& attribute)
{
auto propertyID = lookupCSSPropertyFromIDLAttribute<CSSPropertyLookupMode::ConvertUsingDashPrefix>(attribute);
ASSERT_WITH_MESSAGE(propertyID != CSSPropertyInvalid, "Invalid attribute: %s", attribute.string().utf8().data());
return getPropertyValueInternal(propertyID);
}
ExceptionOr<void> CSSStyleDeclaration::setPropertyValueForEpubCasedIDLAttribute(const AtomString& attribute, const String& value)
{
auto propertyID = lookupCSSPropertyFromIDLAttribute<CSSPropertyLookupMode::ConvertUsingDashPrefix>(attribute);
ASSERT_WITH_MESSAGE(propertyID != CSSPropertyInvalid, "Invalid attribute: %s", attribute.string().utf8().data());
return setPropertyInternal(propertyID, value, IsImportant::No);
}
String CSSStyleDeclaration::cssFloat()
{
return getPropertyValueInternal(CSSPropertyFloat);
}
ExceptionOr<void> CSSStyleDeclaration::setCssFloat(const String& value)
{
return setPropertyInternal(CSSPropertyFloat, value, IsImportant::No);
}
}
|