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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
/*
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 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 "core/css/StylePropertySet.h"
#include "core/StylePropertyShorthand.h"
#include "core/css/CSSCustomPropertyDeclaration.h"
#include "core/css/CSSIdentifierValue.h"
#include "core/css/CSSPropertyMetadata.h"
#include "core/css/StylePropertySerializer.h"
#include "core/css/StyleSheetContents.h"
#include "core/css/parser/CSSParser.h"
#include "core/css/parser/CSSParserContext.h"
#include "core/frame/UseCounter.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "wtf/text/StringBuilder.h"
#ifndef NDEBUG
#include "wtf/text/CString.h"
#include <stdio.h>
#endif
namespace blink {
static size_t sizeForImmutableStylePropertySetWithPropertyCount(
unsigned count) {
return sizeof(ImmutableStylePropertySet) - sizeof(void*) +
sizeof(Member<CSSValue>) * count +
sizeof(StylePropertyMetadata) * count;
}
ImmutableStylePropertySet* ImmutableStylePropertySet::create(
const CSSProperty* properties,
unsigned count,
CSSParserMode cssParserMode) {
ASSERT(count <= MaxArraySize);
void* slot = ThreadHeap::allocate<StylePropertySet>(
sizeForImmutableStylePropertySetWithPropertyCount(count));
return new (slot) ImmutableStylePropertySet(properties, count, cssParserMode);
}
ImmutableStylePropertySet* StylePropertySet::immutableCopyIfNeeded() const {
if (!isMutable())
return toImmutableStylePropertySet(const_cast<StylePropertySet*>(this));
const MutableStylePropertySet* mutableThis = toMutableStylePropertySet(this);
return ImmutableStylePropertySet::create(mutableThis->m_propertyVector.data(),
mutableThis->m_propertyVector.size(),
cssParserMode());
}
MutableStylePropertySet::MutableStylePropertySet(CSSParserMode cssParserMode)
: StylePropertySet(cssParserMode) {}
MutableStylePropertySet::MutableStylePropertySet(const CSSProperty* properties,
unsigned length)
: StylePropertySet(HTMLStandardMode) {
m_propertyVector.reserveInitialCapacity(length);
for (unsigned i = 0; i < length; ++i)
m_propertyVector.uncheckedAppend(properties[i]);
}
ImmutableStylePropertySet::ImmutableStylePropertySet(
const CSSProperty* properties,
unsigned length,
CSSParserMode cssParserMode)
: StylePropertySet(cssParserMode, length) {
StylePropertyMetadata* metadataArray =
const_cast<StylePropertyMetadata*>(this->metadataArray());
Member<const CSSValue>* valueArray =
const_cast<Member<const CSSValue>*>(this->valueArray());
for (unsigned i = 0; i < m_arraySize; ++i) {
metadataArray[i] = properties[i].metadata();
valueArray[i] = properties[i].value();
}
}
ImmutableStylePropertySet::~ImmutableStylePropertySet() {}
// Convert property into an uint16_t for comparison with metadata's m_propertyID
// to avoid the compiler converting it to an int multiple times in a loop.
static uint16_t getConvertedCSSPropertyID(CSSPropertyID propertyID) {
return static_cast<uint16_t>(propertyID);
}
static uint16_t getConvertedCSSPropertyID(const AtomicString&) {
return static_cast<uint16_t>(CSSPropertyVariable);
}
static bool isPropertyMatch(const StylePropertyMetadata& metadata,
const CSSValue&,
uint16_t id,
CSSPropertyID propertyID) {
ASSERT(id == propertyID);
bool result = metadata.m_propertyID == id;
// Only enabled properties should be part of the style.
ASSERT(!result || CSSPropertyMetadata::isEnabledProperty(propertyID));
return result;
}
static bool isPropertyMatch(const StylePropertyMetadata& metadata,
const CSSValue& value,
uint16_t id,
const AtomicString& customPropertyName) {
ASSERT(id == CSSPropertyVariable);
return metadata.m_propertyID == id &&
toCSSCustomPropertyDeclaration(value).name() == customPropertyName;
}
template <typename T>
int ImmutableStylePropertySet::findPropertyIndex(T property) const {
uint16_t id = getConvertedCSSPropertyID(property);
for (int n = m_arraySize - 1; n >= 0; --n) {
if (isPropertyMatch(metadataArray()[n], *valueArray()[n], id, property))
return n;
}
return -1;
}
template CORE_EXPORT int ImmutableStylePropertySet::findPropertyIndex(
CSSPropertyID) const;
template CORE_EXPORT int ImmutableStylePropertySet::findPropertyIndex(
AtomicString) const;
DEFINE_TRACE_AFTER_DISPATCH(ImmutableStylePropertySet) {
const Member<const CSSValue>* values = valueArray();
for (unsigned i = 0; i < m_arraySize; i++)
visitor->trace(values[i]);
StylePropertySet::traceAfterDispatch(visitor);
}
MutableStylePropertySet::MutableStylePropertySet(const StylePropertySet& other)
: StylePropertySet(other.cssParserMode()) {
if (other.isMutable()) {
m_propertyVector = toMutableStylePropertySet(other).m_propertyVector;
} else {
m_propertyVector.reserveInitialCapacity(other.propertyCount());
for (unsigned i = 0; i < other.propertyCount(); ++i)
m_propertyVector.uncheckedAppend(other.propertyAt(i).toCSSProperty());
}
}
static String serializeShorthand(const StylePropertySet& propertySet,
CSSPropertyID propertyID) {
return StylePropertySerializer(propertySet).getPropertyValue(propertyID);
}
static String serializeShorthand(const StylePropertySet&,
const AtomicString& customPropertyName) {
// Custom properties are never shorthands.
return "";
}
template <typename T>
String StylePropertySet::getPropertyValue(T property) const {
const CSSValue* value = getPropertyCSSValue(property);
if (value)
return value->cssText();
return serializeShorthand(*this, property);
}
template CORE_EXPORT String
StylePropertySet::getPropertyValue<CSSPropertyID>(CSSPropertyID) const;
template CORE_EXPORT String
StylePropertySet::getPropertyValue<AtomicString>(AtomicString) const;
template <typename T>
const CSSValue* StylePropertySet::getPropertyCSSValue(T property) const {
int foundPropertyIndex = findPropertyIndex(property);
if (foundPropertyIndex == -1)
return nullptr;
return &propertyAt(foundPropertyIndex).value();
}
template CORE_EXPORT const CSSValue*
StylePropertySet::getPropertyCSSValue<CSSPropertyID>(CSSPropertyID) const;
template CORE_EXPORT const CSSValue*
StylePropertySet::getPropertyCSSValue<AtomicString>(AtomicString) const;
DEFINE_TRACE(StylePropertySet) {
if (m_isMutable)
toMutableStylePropertySet(this)->traceAfterDispatch(visitor);
else
toImmutableStylePropertySet(this)->traceAfterDispatch(visitor);
}
void StylePropertySet::finalizeGarbageCollectedObject() {
if (m_isMutable)
toMutableStylePropertySet(this)->~MutableStylePropertySet();
else
toImmutableStylePropertySet(this)->~ImmutableStylePropertySet();
}
bool MutableStylePropertySet::removeShorthandProperty(
CSSPropertyID propertyID) {
StylePropertyShorthand shorthand = shorthandForProperty(propertyID);
if (!shorthand.length())
return false;
return removePropertiesInSet(shorthand.properties(), shorthand.length());
}
bool MutableStylePropertySet::removePropertyAtIndex(int propertyIndex,
String* returnText) {
if (propertyIndex == -1) {
if (returnText)
*returnText = "";
return false;
}
if (returnText)
*returnText = propertyAt(propertyIndex).value().cssText();
// A more efficient removal strategy would involve marking entries as empty
// and sweeping them when the vector grows too big.
m_propertyVector.remove(propertyIndex);
return true;
}
template <typename T>
bool MutableStylePropertySet::removeProperty(T property, String* returnText) {
if (removeShorthandProperty(property)) {
// FIXME: Return an equivalent shorthand when possible.
if (returnText)
*returnText = "";
return true;
}
int foundPropertyIndex = findPropertyIndex(property);
return removePropertyAtIndex(foundPropertyIndex, returnText);
}
template CORE_EXPORT bool MutableStylePropertySet::removeProperty(CSSPropertyID,
String*);
template CORE_EXPORT bool MutableStylePropertySet::removeProperty(AtomicString,
String*);
template <typename T>
bool StylePropertySet::propertyIsImportant(T property) const {
int foundPropertyIndex = findPropertyIndex(property);
if (foundPropertyIndex != -1)
return propertyAt(foundPropertyIndex).isImportant();
return shorthandIsImportant(property);
}
template bool StylePropertySet::propertyIsImportant<CSSPropertyID>(
CSSPropertyID) const;
template bool StylePropertySet::propertyIsImportant<AtomicString>(
AtomicString) const;
bool StylePropertySet::shorthandIsImportant(CSSPropertyID propertyID) const {
StylePropertyShorthand shorthand = shorthandForProperty(propertyID);
if (!shorthand.length())
return false;
for (unsigned i = 0; i < shorthand.length(); ++i) {
if (!propertyIsImportant(shorthand.properties()[i]))
return false;
}
return true;
}
bool StylePropertySet::shorthandIsImportant(
AtomicString customPropertyName) const {
// Custom properties are never shorthands.
return false;
}
CSSPropertyID StylePropertySet::getPropertyShorthand(
CSSPropertyID propertyID) const {
int foundPropertyIndex = findPropertyIndex(propertyID);
if (foundPropertyIndex == -1)
return CSSPropertyInvalid;
return propertyAt(foundPropertyIndex).shorthandID();
}
bool StylePropertySet::isPropertyImplicit(CSSPropertyID propertyID) const {
int foundPropertyIndex = findPropertyIndex(propertyID);
if (foundPropertyIndex == -1)
return false;
return propertyAt(foundPropertyIndex).isImplicit();
}
MutableStylePropertySet::SetResult MutableStylePropertySet::setProperty(
CSSPropertyID unresolvedProperty,
const String& value,
bool important,
StyleSheetContents* contextStyleSheet) {
DCHECK_GE(unresolvedProperty, firstCSSProperty);
// Setting the value to an empty string just removes the property in both IE
// and Gecko. Setting it to null seems to produce less consistent results, but
// we treat it just the same.
if (value.isEmpty()) {
bool didParse = true;
bool didChange = removeProperty(resolveCSSPropertyID(unresolvedProperty));
return SetResult{didParse, didChange};
}
// When replacing an existing property value, this moves the property to the
// end of the list. Firefox preserves the position, and MSIE moves the
// property to the beginning.
return CSSParser::parseValue(this, unresolvedProperty, value, important,
contextStyleSheet);
}
MutableStylePropertySet::SetResult MutableStylePropertySet::setProperty(
const AtomicString& customPropertyName,
const PropertyRegistry* registry,
const String& value,
bool important,
StyleSheetContents* contextStyleSheet,
bool isAnimationTainted) {
if (value.isEmpty()) {
bool didParse = true;
bool didChange = removeProperty(customPropertyName);
return MutableStylePropertySet::SetResult{didParse, didChange};
}
return CSSParser::parseValueForCustomProperty(
this, customPropertyName, registry, value, important, contextStyleSheet,
isAnimationTainted);
}
void MutableStylePropertySet::setProperty(CSSPropertyID propertyID,
const CSSValue& value,
bool important) {
StylePropertyShorthand shorthand = shorthandForProperty(propertyID);
if (!shorthand.length()) {
setProperty(CSSProperty(propertyID, value, important));
return;
}
removePropertiesInSet(shorthand.properties(), shorthand.length());
for (unsigned i = 0; i < shorthand.length(); ++i)
m_propertyVector.push_back(
CSSProperty(shorthand.properties()[i], value, important));
}
bool MutableStylePropertySet::setProperty(const CSSProperty& property,
CSSProperty* slot) {
const AtomicString& name =
(property.id() == CSSPropertyVariable)
? toCSSCustomPropertyDeclaration(property.value())->name()
: nullAtom;
CSSProperty* toReplace =
slot ? slot : findCSSPropertyWithID(property.id(), name);
if (toReplace && *toReplace == property)
return false;
if (toReplace) {
*toReplace = property;
return true;
}
m_propertyVector.push_back(property);
return true;
}
bool MutableStylePropertySet::setProperty(CSSPropertyID propertyID,
CSSValueID identifier,
bool important) {
setProperty(CSSProperty(propertyID, *CSSIdentifierValue::create(identifier),
important));
return true;
}
void MutableStylePropertySet::parseDeclarationList(
const String& styleDeclaration,
StyleSheetContents* contextStyleSheet) {
m_propertyVector.clear();
CSSParserContext* context;
if (contextStyleSheet) {
context = CSSParserContext::createWithStyleSheetContents(
contextStyleSheet->parserContext(), contextStyleSheet);
context->setMode(cssParserMode());
} else {
context = CSSParserContext::create(cssParserMode());
}
CSSParser::parseDeclarationList(context, this, styleDeclaration);
}
bool MutableStylePropertySet::addParsedProperties(
const HeapVector<CSSProperty, 256>& properties) {
bool changed = false;
m_propertyVector.reserveCapacity(m_propertyVector.size() + properties.size());
for (unsigned i = 0; i < properties.size(); ++i)
changed |= setProperty(properties[i]);
return changed;
}
bool MutableStylePropertySet::addRespectingCascade(
const CSSProperty& property) {
// Only add properties that have no !important counterpart present
if (!propertyIsImportant(property.id()) || property.isImportant())
return setProperty(property);
return false;
}
String StylePropertySet::asText() const {
return StylePropertySerializer(*this).asText();
}
void MutableStylePropertySet::mergeAndOverrideOnConflict(
const StylePropertySet* other) {
unsigned size = other->propertyCount();
for (unsigned n = 0; n < size; ++n) {
PropertyReference toMerge = other->propertyAt(n);
// TODO(leviw): This probably doesn't work correctly with Custom Properties
CSSProperty* old = findCSSPropertyWithID(toMerge.id());
if (old)
setProperty(toMerge.toCSSProperty(), old);
else
m_propertyVector.push_back(toMerge.toCSSProperty());
}
}
bool StylePropertySet::hasFailedOrCanceledSubresources() const {
unsigned size = propertyCount();
for (unsigned i = 0; i < size; ++i) {
if (propertyAt(i).value().hasFailedOrCanceledSubresources())
return true;
}
return false;
}
void MutableStylePropertySet::clear() {
m_propertyVector.clear();
}
inline bool containsId(const CSSPropertyID* set,
unsigned length,
CSSPropertyID id) {
for (unsigned i = 0; i < length; ++i) {
if (set[i] == id)
return true;
}
return false;
}
bool MutableStylePropertySet::removePropertiesInSet(const CSSPropertyID* set,
unsigned length) {
if (m_propertyVector.isEmpty())
return false;
CSSProperty* properties = m_propertyVector.data();
unsigned oldSize = m_propertyVector.size();
unsigned newIndex = 0;
for (unsigned oldIndex = 0; oldIndex < oldSize; ++oldIndex) {
const CSSProperty& property = properties[oldIndex];
if (containsId(set, length, property.id()))
continue;
// Modify m_propertyVector in-place since this method is
// performance-sensitive.
properties[newIndex++] = properties[oldIndex];
}
if (newIndex != oldSize) {
m_propertyVector.shrink(newIndex);
return true;
}
return false;
}
CSSProperty* MutableStylePropertySet::findCSSPropertyWithID(
CSSPropertyID propertyID,
const AtomicString& customPropertyName) {
int foundPropertyIndex = -1;
if (propertyID == CSSPropertyVariable && !customPropertyName.isNull()) {
// TODO(shanestephens): fix call sites so we always have a
// customPropertyName here.
foundPropertyIndex = findPropertyIndex(customPropertyName);
} else {
ASSERT(customPropertyName.isNull());
foundPropertyIndex = findPropertyIndex(propertyID);
}
if (foundPropertyIndex == -1)
return nullptr;
return &m_propertyVector.at(foundPropertyIndex);
}
bool StylePropertySet::propertyMatches(CSSPropertyID propertyID,
const CSSValue& propertyValue) const {
int foundPropertyIndex = findPropertyIndex(propertyID);
if (foundPropertyIndex == -1)
return false;
return propertyAt(foundPropertyIndex).value().equals(propertyValue);
}
void MutableStylePropertySet::removeEquivalentProperties(
const StylePropertySet* style) {
Vector<CSSPropertyID> propertiesToRemove;
unsigned size = m_propertyVector.size();
for (unsigned i = 0; i < size; ++i) {
PropertyReference property = propertyAt(i);
if (style->propertyMatches(property.id(), property.value()))
propertiesToRemove.push_back(property.id());
}
// FIXME: This should use mass removal.
for (unsigned i = 0; i < propertiesToRemove.size(); ++i)
removeProperty(propertiesToRemove[i]);
}
void MutableStylePropertySet::removeEquivalentProperties(
const CSSStyleDeclaration* style) {
Vector<CSSPropertyID> propertiesToRemove;
unsigned size = m_propertyVector.size();
for (unsigned i = 0; i < size; ++i) {
PropertyReference property = propertyAt(i);
if (style->cssPropertyMatches(property.id(), &property.value()))
propertiesToRemove.push_back(property.id());
}
// FIXME: This should use mass removal.
for (unsigned i = 0; i < propertiesToRemove.size(); ++i)
removeProperty(propertiesToRemove[i]);
}
MutableStylePropertySet* StylePropertySet::mutableCopy() const {
return new MutableStylePropertySet(*this);
}
MutableStylePropertySet* StylePropertySet::copyPropertiesInSet(
const Vector<CSSPropertyID>& properties) const {
HeapVector<CSSProperty, 256> list;
list.reserveInitialCapacity(properties.size());
for (unsigned i = 0; i < properties.size(); ++i) {
const CSSValue* value = getPropertyCSSValue(properties[i]);
if (value)
list.push_back(CSSProperty(properties[i], *value, false));
}
return MutableStylePropertySet::create(list.data(), list.size());
}
CSSStyleDeclaration* MutableStylePropertySet::ensureCSSStyleDeclaration() {
// FIXME: get rid of this weirdness of a CSSStyleDeclaration inside of a
// style property set.
if (m_cssomWrapper) {
ASSERT(
!static_cast<CSSStyleDeclaration*>(m_cssomWrapper.get())->parentRule());
ASSERT(!m_cssomWrapper->parentElement());
return m_cssomWrapper.get();
}
m_cssomWrapper = new PropertySetCSSStyleDeclaration(*this);
return m_cssomWrapper.get();
}
template <typename T>
int MutableStylePropertySet::findPropertyIndex(T property) const {
const CSSProperty* begin = m_propertyVector.data();
const CSSProperty* end = begin + m_propertyVector.size();
uint16_t id = getConvertedCSSPropertyID(property);
const CSSProperty* it = std::find_if(
begin, end, [property, id](const CSSProperty& cssProperty) -> bool {
return isPropertyMatch(cssProperty.metadata(), *cssProperty.value(), id,
property);
});
return (it == end) ? -1 : it - begin;
}
template CORE_EXPORT int MutableStylePropertySet::findPropertyIndex(
CSSPropertyID) const;
template CORE_EXPORT int MutableStylePropertySet::findPropertyIndex(
AtomicString) const;
DEFINE_TRACE_AFTER_DISPATCH(MutableStylePropertySet) {
visitor->trace(m_cssomWrapper);
visitor->trace(m_propertyVector);
StylePropertySet::traceAfterDispatch(visitor);
}
unsigned StylePropertySet::averageSizeInBytes() {
// Please update this if the storage scheme changes so that this longer
// reflects the actual size.
return sizeForImmutableStylePropertySetWithPropertyCount(4);
}
// See the function above if you need to update this.
struct SameSizeAsStylePropertySet
: public GarbageCollectedFinalized<SameSizeAsStylePropertySet> {
unsigned bitfield;
};
static_assert(sizeof(StylePropertySet) == sizeof(SameSizeAsStylePropertySet),
"StylePropertySet should stay small");
#ifndef NDEBUG
void StylePropertySet::showStyle() {
fprintf(stderr, "%s\n", asText().ascii().data());
}
#endif
MutableStylePropertySet* MutableStylePropertySet::create(
CSSParserMode cssParserMode) {
return new MutableStylePropertySet(cssParserMode);
}
MutableStylePropertySet* MutableStylePropertySet::create(
const CSSProperty* properties,
unsigned count) {
return new MutableStylePropertySet(properties, count);
}
DEFINE_TRACE(CSSLazyPropertyParser) {}
} // namespace blink
|