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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
/**
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004-2023 Apple Inc. All rights reserved.
* Copyright (C) 2011 Research In Motion Limited. All rights reserved.
* Copyright (C) 2013 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "StyleProperties.h"
#include "CSSColorValue.h"
#include "CSSCustomPropertyValue.h"
#include "CSSParser.h"
#include "CSSPendingSubstitutionValue.h"
#include "CSSPrimitiveValue.h"
#include "CSSPropertyNames.h"
#include "CSSPropertyParserConsumer+Font.h"
#include "CSSSerializationContext.h"
#include "CSSValueKeywords.h"
#include "CSSValueList.h"
#include "Color.h"
#include "PropertySetCSSStyleDeclaration.h"
#include "ShorthandSerializer.h"
#include "StylePropertiesInlines.h"
#include "StylePropertyShorthand.h"
#include <bitset>
#include <wtf/text/MakeString.h>
#include <wtf/text/StringBuilder.h>
#ifndef NDEBUG
#include <stdio.h>
#include <wtf/text/CString.h>
#endif
namespace WebCore {
DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(StyleProperties);
constexpr unsigned maxShorthandsForLonghand = 4; // FIXME: Generate this from CSSProperties.json and use it for StylePropertyShorthandVector too.
Ref<ImmutableStyleProperties> StyleProperties::immutableCopyIfNeeded() const
{
if (m_isMutable)
return uncheckedDowncast<MutableStyleProperties>(*this).immutableDeduplicatedCopy();
return const_cast<ImmutableStyleProperties&>(uncheckedDowncast<ImmutableStyleProperties>(*this));
}
String serializeLonghandValue(const CSS::SerializationContext& context, CSSPropertyID property, const CSSValue& value)
{
switch (property) {
case CSSPropertyFillOpacity:
case CSSPropertyFloodOpacity:
case CSSPropertyOpacity:
case CSSPropertyStopOpacity:
case CSSPropertyStrokeOpacity:
// FIXME: Handle this when creating the CSSValue for opacity, to be consistent with other CSS value serialization quirks.
// Opacity percentage values serialize as a fraction in the range 0-1, not "%".
if (auto* primitive = dynamicDowncast<CSSPrimitiveValue>(value); primitive && primitive->isPercentage())
return makeString(primitive->resolveAsPercentageDeprecated() / 100);
break;
default:
break;
}
// Longhands set by mask and background shorthands can have comma-separated lists with implicit initial values in them.
// We need to serialize those lists with the actual values, not as "initial".
// Doing this for all CSSValueList with comma separators is better than checking the property is one of those longhands.
// Serializing this way is harmless for other properties; those won't have any implicit initial values.
if (auto* list = dynamicDowncast<CSSValueList>(value); list && list->separator() == CSSValueList::CommaSeparator) {
StringBuilder result;
auto separator = ""_s;
for (auto& individualValue : *list)
result.append(std::exchange(separator, ", "_s), serializeLonghandValue(context, property, individualValue));
return result.toString();
}
return value.isImplicitInitialValue() ? initialValueTextForLonghand(property) : value.cssText(context);
}
inline String StyleProperties::serializeLonghandValue(const CSS::SerializationContext& context, CSSPropertyID propertyID) const
{
return WebCore::serializeLonghandValue(context, propertyID, getPropertyCSSValue(propertyID).get());
}
inline String StyleProperties::serializeShorthandValue(const CSS::SerializationContext& context, CSSPropertyID propertyID) const
{
return WebCore::serializeShorthandValue(context, *this, propertyID);
}
String StyleProperties::getPropertyValue(CSSPropertyID propertyID) const
{
return isLonghand(propertyID) ? serializeLonghandValue(CSS::defaultSerializationContext(), propertyID) : serializeShorthandValue(CSS::defaultSerializationContext(), propertyID);
}
std::optional<Color> StyleProperties::propertyAsColor(CSSPropertyID property) const
{
auto value = getPropertyCSSValue(property);
if (!value)
return std::nullopt;
return value->isColor()
? CSSColorValue::absoluteColor(*value)
: CSSParser::parseColorWithoutContext(WebCore::serializeLonghandValue(CSS::defaultSerializationContext(), property, *value));
}
std::optional<CSSValueID> StyleProperties::propertyAsValueID(CSSPropertyID property) const
{
return longhandValueID(property, getPropertyCSSValue(property).get());
}
String StyleProperties::getCustomPropertyValue(const String& propertyName) const
{
RefPtr<CSSValue> value = getCustomPropertyCSSValue(propertyName);
if (value)
return value->cssText(CSS::defaultSerializationContext());
return String();
}
RefPtr<CSSValue> StyleProperties::getPropertyCSSValue(CSSPropertyID propertyID) const
{
int foundPropertyIndex = findPropertyIndex(propertyID);
if (foundPropertyIndex == -1)
return nullptr;
auto property = propertyAt(foundPropertyIndex);
auto value = property.value();
// System fonts are represented as CSSPrimitiveValue for various font subproperties, but these must serialize as the empty string.
// It might be better to implement this as a special CSSValue type instead of turning them into null here.
if (property.shorthandID() == CSSPropertyFont && CSSPropertyParserHelpers::isSystemFontShorthand(valueID(value)))
return nullptr;
return value;
}
RefPtr<CSSValue> StyleProperties::getCustomPropertyCSSValue(const String& propertyName) const
{
int foundPropertyIndex = findCustomPropertyIndex(propertyName);
if (foundPropertyIndex == -1)
return nullptr;
return propertyAt(foundPropertyIndex).value();
}
bool StyleProperties::propertyIsImportant(CSSPropertyID propertyID) const
{
if (isLonghand(propertyID)) {
int foundPropertyIndex = findPropertyIndex(propertyID);
if (foundPropertyIndex != -1)
return propertyAt(foundPropertyIndex).isImportant();
return false;
}
ASSERT(shorthandForProperty(propertyID).length());
for (auto longhand : shorthandForProperty(propertyID)) {
if (!propertyIsImportant(longhand))
return false;
}
return true;
}
bool StyleProperties::customPropertyIsImportant(const String& propertyName) const
{
int foundPropertyIndex = findCustomPropertyIndex(propertyName);
if (foundPropertyIndex != -1)
return propertyAt(foundPropertyIndex).isImportant();
return false;
}
String StyleProperties::getPropertyShorthand(CSSPropertyID propertyID) const
{
int foundPropertyIndex = findPropertyIndex(propertyID);
if (foundPropertyIndex == -1)
return String();
return nameString(propertyAt(foundPropertyIndex).shorthandID());
}
bool StyleProperties::isPropertyImplicit(CSSPropertyID propertyID) const
{
int foundPropertyIndex = findPropertyIndex(propertyID);
if (foundPropertyIndex == -1)
return false;
return propertyAt(foundPropertyIndex).isImplicit();
}
String StyleProperties::asText(const CSS::SerializationContext& context) const
{
return asTextInternal(context).toString();
}
AtomString StyleProperties::asTextAtom(const CSS::SerializationContext& context) const
{
return asTextInternal(context).toAtomString();
}
static constexpr bool canUseShorthandForLonghand(CSSPropertyID shorthandID, CSSPropertyID longhandID)
{
ASSERT(isShorthand(shorthandID));
ASSERT(isLonghand(longhandID));
switch (shorthandID) {
// We are not yet using the CSSPropertyFont shorthand here because our editing code is currently incompatible.
case CSSPropertyFont:
return false;
// Avoid legacy shorthands according to https://www.w3.org/TR/css-cascade-5/#legacy-shorthand
case CSSPropertyPageBreakAfter:
case CSSPropertyPageBreakBefore:
case CSSPropertyPageBreakInside:
case CSSPropertyWebkitBackgroundSize:
case CSSPropertyWebkitBorderRadius:
case CSSPropertyWebkitColumnBreakAfter:
case CSSPropertyWebkitColumnBreakBefore:
case CSSPropertyWebkitColumnBreakInside:
case CSSPropertyWebkitMaskPosition:
case CSSPropertyWebkitPerspective:
case CSSPropertyWebkitTextDecoration:
case CSSPropertyWebkitTextOrientation:
return false;
// No other browser currently supports text-decoration-skip, so it's currently more web
// compatible to avoid collapsing text-decoration-skip-ink, its only longhand.
case CSSPropertyTextDecorationSkip:
return false;
// FIXME: -webkit-mask is a legacy shorthand but it's used to serialize -webkit-mask-clip,
// which should be a legacy shorthand of mask-clip, but it's implemented as a longhand.
case CSSPropertyWebkitMask:
return longhandID == CSSPropertyWebkitMaskClip;
// FIXME: more mask nonsense.
case CSSPropertyMask:
return longhandID != CSSPropertyMaskComposite && longhandID != CSSPropertyMaskMode && longhandID != CSSPropertyMaskSize;
// FIXME: If font-variant-ligatures is none, this depends on the value of the longhand.
case CSSPropertyFontVariant:
// FIXME: These shorthands are avoided for unknown legacy reasons, probably shouldn't be avoided.
case CSSPropertyBorderBlockEnd:
case CSSPropertyBorderBlockStart:
case CSSPropertyBorderBottom:
case CSSPropertyBorderInlineEnd:
case CSSPropertyBorderInlineStart:
case CSSPropertyBorderLeft:
case CSSPropertyBorderRight:
case CSSPropertyBorderTop:
case CSSPropertyColumnRule:
case CSSPropertyColumns:
case CSSPropertyContainer:
case CSSPropertyFontSynthesis:
case CSSPropertyGridArea:
case CSSPropertyGridColumn:
case CSSPropertyGridRow:
case CSSPropertyMaskPosition:
case CSSPropertyOffset:
case CSSPropertyTextEmphasis:
case CSSPropertyWebkitTextStroke:
return false;
default:
return true;
}
}
StringBuilder StyleProperties::asTextInternal(const CSS::SerializationContext& context) const
{
StringBuilder result;
constexpr unsigned shorthandPropertyCount = lastShorthandProperty - firstShorthandProperty + 1;
std::bitset<shorthandPropertyCount> shorthandPropertyUsed;
std::bitset<shorthandPropertyCount> shorthandPropertyAppeared;
unsigned numDecls = 0;
for (auto property : *this) {
auto propertyID = property.id();
ASSERT(isLonghand(propertyID) || propertyID == CSSPropertyCustom);
Vector<CSSPropertyID, maxShorthandsForLonghand> shorthands;
if (auto* substitutionValue = dynamicDowncast<CSSPendingSubstitutionValue>(property.value()))
shorthands.append(substitutionValue->shorthandPropertyId());
else {
for (auto& shorthand : matchingShorthandsForLonghand(propertyID)) {
if (canUseShorthandForLonghand(shorthand.id(), propertyID))
shorthands.append(shorthand.id());
}
}
String value;
bool alreadyUsedShorthand = false;
for (auto& shorthandPropertyID : shorthands) {
ASSERT(isShorthand(shorthandPropertyID));
unsigned shorthandPropertyIndex = shorthandPropertyID - firstShorthandProperty;
ASSERT(shorthandPropertyIndex < shorthandPropertyUsed.size());
if (shorthandPropertyUsed[shorthandPropertyIndex]) {
alreadyUsedShorthand = true;
break;
}
if (shorthandPropertyAppeared[shorthandPropertyIndex])
continue;
shorthandPropertyAppeared.set(shorthandPropertyIndex);
value = serializeShorthandValue(context, shorthandPropertyID);
if (!value.isNull()) {
propertyID = shorthandPropertyID;
shorthandPropertyUsed.set(shorthandPropertyIndex);
break;
}
}
if (alreadyUsedShorthand)
continue;
if (value.isNull())
value = WebCore::serializeLonghandValue(context, propertyID, *property.value());
if (numDecls++)
result.append(' ');
if (propertyID == CSSPropertyCustom)
result.append(downcast<CSSCustomPropertyValue>(*property.value()).name());
else
result.append(nameLiteral(propertyID));
result.append(": "_s, value, property.isImportant() ? " !important"_s : ""_s, ';');
}
ASSERT(!numDecls ^ !result.isEmpty());
return result;
}
bool StyleProperties::hasCSSOMWrapper() const
{
auto* mutableProperties = dynamicDowncast<MutableStyleProperties>(*this);
return mutableProperties && mutableProperties->m_cssomWrapper;
}
bool StyleProperties::traverseSubresources(NOESCAPE const Function<bool(const CachedResource&)>& handler) const
{
for (auto property : *this) {
if (property.value()->traverseSubresources(handler))
return true;
}
return false;
}
bool StyleProperties::mayDependOnBaseURL() const
{
bool result = false;
Function<IterationStatus(CSSValue&)> func = [&](CSSValue& value) -> IterationStatus {
if (value.mayDependOnBaseURL()) {
result = true;
return IterationStatus::Done;
}
return value.visitChildren(func);
};
for (auto property : *this) {
if (func(*property.value()) == IterationStatus::Done)
return result;
}
return false;
}
bool StyleProperties::propertyMatches(CSSPropertyID propertyID, const CSSValue* propertyValue) const
{
int foundPropertyIndex = findPropertyIndex(propertyID);
if (foundPropertyIndex == -1)
return false;
return propertyAt(foundPropertyIndex).value()->equals(*propertyValue);
}
Ref<MutableStyleProperties> StyleProperties::mutableCopy() const
{
return adoptRef(*new MutableStyleProperties(*this));
}
Ref<MutableStyleProperties> StyleProperties::copyProperties(std::span<const CSSPropertyID> properties) const
{
auto vector = WTF::compactMap(properties, [&](auto& property) -> std::optional<CSSProperty> {
if (auto value = getPropertyCSSValue(property))
return CSSProperty(property, value.releaseNonNull());
return std::nullopt;
});
return MutableStyleProperties::create(WTFMove(vector));
}
PropertySetCSSStyleDeclaration* MutableStyleProperties::cssStyleDeclaration()
{
return m_cssomWrapper.get();
}
CSSStyleDeclaration& MutableStyleProperties::ensureCSSStyleDeclaration()
{
if (m_cssomWrapper) {
ASSERT(!static_cast<CSSStyleDeclaration*>(m_cssomWrapper.get())->parentRule());
ASSERT(!m_cssomWrapper->parentElement());
return *m_cssomWrapper;
}
m_cssomWrapper = makeUniqueWithoutRefCountedCheck<PropertySetCSSStyleDeclaration>(*this);
return *m_cssomWrapper;
}
unsigned StyleProperties::averageSizeInBytes()
{
// Please update this if the storage scheme changes so that this longer reflects the actual size.
return ImmutableStyleProperties::objectSize(4);
}
// See the function above if you need to update this.
struct SameSizeAsStyleProperties : public RefCounted<SameSizeAsStyleProperties> {
unsigned bitfield;
};
static_assert(sizeof(StyleProperties) == sizeof(SameSizeAsStyleProperties), "style property set should stay small");
#ifndef NDEBUG
void StyleProperties::showStyle()
{
SAFE_FPRINTF(stderr, "%s\n", asText(CSS::defaultSerializationContext()).ascii());
}
#endif
String StyleProperties::PropertyReference::cssName() const
{
if (id() == CSSPropertyCustom)
return downcast<CSSCustomPropertyValue>(*value()).name();
return nameString(id());
}
String StyleProperties::PropertyReference::cssText(const CSS::SerializationContext& context) const
{
return makeString(cssName(), ": "_s, WebCore::serializeLonghandValue(context, id(), *m_value), isImportant() ? " !important;"_s : ";"_s);
}
} // namespace WebCore
|