2016-10-18 07:29:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* representation of a declaration block in a CSS stylesheet, or of
|
|
|
|
* a style attribute
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef mozilla_DeclarationBlock_h
|
|
|
|
#define mozilla_DeclarationBlock_h
|
|
|
|
|
2017-09-20 06:30:08 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2018-05-30 19:15:25 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
2016-10-18 07:29:03 +03:00
|
|
|
|
2016-11-03 06:41:02 +03:00
|
|
|
#include "nsCSSPropertyID.h"
|
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
class nsHTMLCSSStyleSheet;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace css {
|
2016-10-18 07:29:03 +03:00
|
|
|
class Declaration;
|
2016-10-18 07:29:03 +03:00
|
|
|
class Rule;
|
|
|
|
} // namespace css
|
|
|
|
|
2018-05-30 19:15:25 +03:00
|
|
|
class DeclarationBlock final {
|
2018-06-05 22:22:14 +03:00
|
|
|
DeclarationBlock(const DeclarationBlock& aCopy)
|
|
|
|
: mRaw(Servo_DeclarationBlock_Clone(aCopy.mRaw).Consume()),
|
2018-05-31 11:31:44 +03:00
|
|
|
mImmutable(false),
|
2017-09-20 06:30:08 +03:00
|
|
|
mIsDirty(false) {
|
2017-05-31 02:33:03 +03:00
|
|
|
mContainer.mRaw = 0;
|
|
|
|
}
|
2016-10-18 07:29:03 +03:00
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
public:
|
|
|
|
explicit DeclarationBlock(already_AddRefed<RawServoDeclarationBlock> aRaw)
|
2018-05-31 11:31:44 +03:00
|
|
|
: mRaw(aRaw), mImmutable(false), mIsDirty(false) {
|
2018-05-30 19:15:25 +03:00
|
|
|
mContainer.mRaw = 0;
|
|
|
|
}
|
2016-10-18 07:29:03 +03:00
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
DeclarationBlock()
|
|
|
|
: DeclarationBlock(Servo_DeclarationBlock_CreateEmpty().Consume()) {}
|
|
|
|
|
2018-05-30 19:15:25 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DeclarationBlock)
|
2016-10-18 07:29:03 +03:00
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
already_AddRefed<DeclarationBlock> Clone() const {
|
2018-05-30 19:15:25 +03:00
|
|
|
return do_AddRef(new DeclarationBlock(*this));
|
|
|
|
}
|
2016-11-03 06:41:02 +03:00
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
/**
|
|
|
|
* Return whether |this| may be modified.
|
|
|
|
*/
|
2018-06-05 22:22:14 +03:00
|
|
|
bool IsMutable() const { return !mImmutable; }
|
2016-10-18 07:29:03 +03:00
|
|
|
|
|
|
|
/**
|
2018-06-05 22:22:14 +03:00
|
|
|
* Crash in debug builds if |this| cannot be modified.
|
2016-10-18 07:29:03 +03:00
|
|
|
*/
|
2018-06-05 22:22:14 +03:00
|
|
|
void AssertMutable() const {
|
2016-10-18 07:29:03 +03:00
|
|
|
MOZ_ASSERT(IsMutable(), "someone forgot to call EnsureMutable");
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!OwnerIsReadOnly(), "User Agent sheets shouldn't be modified");
|
2016-10-18 07:29:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-05 22:22:14 +03:00
|
|
|
* Mark this declaration as unmodifiable.
|
2016-10-18 07:29:03 +03:00
|
|
|
*/
|
2017-02-24 14:06:07 +03:00
|
|
|
void SetImmutable() { mImmutable = true; }
|
2016-10-18 07:29:03 +03:00
|
|
|
|
2017-05-31 02:33:03 +03:00
|
|
|
/**
|
|
|
|
* Return whether |this| has been restyled after modified.
|
|
|
|
*/
|
|
|
|
bool IsDirty() const { return mIsDirty; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark this declaration as dirty.
|
|
|
|
*/
|
|
|
|
void SetDirty() { mIsDirty = true; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark this declaration as not dirty.
|
|
|
|
*/
|
|
|
|
void UnsetDirty() { mIsDirty = false; }
|
|
|
|
|
2016-11-03 06:41:02 +03:00
|
|
|
/**
|
|
|
|
* Copy |this|, if necessary to ensure that it can be modified.
|
|
|
|
*/
|
2018-06-05 22:22:14 +03:00
|
|
|
already_AddRefed<DeclarationBlock> EnsureMutable() {
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!OwnerIsReadOnly());
|
|
|
|
|
2018-05-30 19:15:25 +03:00
|
|
|
if (!IsDirty()) {
|
|
|
|
// In stylo, the old DeclarationBlock is stored in element's rule node
|
|
|
|
// tree directly, to avoid new values replacing the DeclarationBlock in
|
|
|
|
// the tree directly, we need to copy the old one here if we haven't yet
|
|
|
|
// copied. As a result the new value does not replace rule node tree until
|
|
|
|
// traversal happens.
|
2018-06-05 22:22:14 +03:00
|
|
|
//
|
|
|
|
// FIXME(emilio): This is a hack for ::first-line and transitions starting
|
|
|
|
// due to CSSOM changes when other transitions are already running. Try
|
|
|
|
// to simplify this setup.
|
2018-05-30 19:15:25 +03:00
|
|
|
return Clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsMutable()) {
|
|
|
|
return Clone();
|
|
|
|
}
|
2018-06-05 22:22:14 +03:00
|
|
|
|
2018-05-30 19:15:25 +03:00
|
|
|
return do_AddRef(this);
|
|
|
|
}
|
2016-11-03 06:41:02 +03:00
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
void SetOwningRule(css::Rule* aRule) {
|
2016-10-18 07:29:03 +03:00
|
|
|
MOZ_ASSERT(!mContainer.mOwningRule || !aRule,
|
|
|
|
"should never overwrite one rule with another");
|
|
|
|
mContainer.mOwningRule = aRule;
|
|
|
|
}
|
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
css::Rule* GetOwningRule() const {
|
2016-10-18 07:29:03 +03:00
|
|
|
if (mContainer.mRaw & 0x1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mContainer.mOwningRule;
|
|
|
|
}
|
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
void SetHTMLCSSStyleSheet(nsHTMLCSSStyleSheet* aHTMLCSSStyleSheet) {
|
2016-10-18 07:29:03 +03:00
|
|
|
MOZ_ASSERT(!mContainer.mHTMLCSSStyleSheet || !aHTMLCSSStyleSheet,
|
|
|
|
"should never overwrite one sheet with another");
|
|
|
|
mContainer.mHTMLCSSStyleSheet = aHTMLCSSStyleSheet;
|
|
|
|
if (aHTMLCSSStyleSheet) {
|
|
|
|
mContainer.mRaw |= uintptr_t(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
nsHTMLCSSStyleSheet* GetHTMLCSSStyleSheet() const {
|
2016-10-18 07:29:03 +03:00
|
|
|
if (!(mContainer.mRaw & 0x1)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto c = mContainer;
|
|
|
|
c.mRaw &= ~uintptr_t(1);
|
|
|
|
return c.mHTMLCSSStyleSheet;
|
|
|
|
}
|
|
|
|
|
2019-04-29 08:34:06 +03:00
|
|
|
bool IsReadOnly() const;
|
|
|
|
|
2018-05-30 19:15:25 +03:00
|
|
|
static already_AddRefed<DeclarationBlock> FromCssText(
|
|
|
|
const nsAString& aCssText, URLExtraData* aExtraData,
|
|
|
|
nsCompatibility aMode, css::Loader* aLoader);
|
|
|
|
|
|
|
|
RawServoDeclarationBlock* Raw() const { return mRaw; }
|
|
|
|
RawServoDeclarationBlock* const* RefRaw() const {
|
|
|
|
static_assert(sizeof(RefPtr<RawServoDeclarationBlock>) ==
|
|
|
|
sizeof(RawServoDeclarationBlock*),
|
|
|
|
"RefPtr should just be a pointer");
|
|
|
|
return reinterpret_cast<RawServoDeclarationBlock* const*>(&mRaw);
|
|
|
|
}
|
2016-11-03 06:41:02 +03:00
|
|
|
|
2019-03-26 21:16:01 +03:00
|
|
|
const StyleStrong<RawServoDeclarationBlock>* RefRawStrong() const {
|
2018-05-30 19:15:25 +03:00
|
|
|
static_assert(sizeof(RefPtr<RawServoDeclarationBlock>) ==
|
|
|
|
sizeof(RawServoDeclarationBlock*),
|
|
|
|
"RefPtr should just be a pointer");
|
|
|
|
static_assert(
|
|
|
|
sizeof(RefPtr<RawServoDeclarationBlock>) ==
|
2019-03-26 21:16:01 +03:00
|
|
|
sizeof(StyleStrong<RawServoDeclarationBlock>),
|
2018-05-30 19:15:25 +03:00
|
|
|
"RawServoDeclarationBlockStrong should be the same as RefPtr");
|
2019-03-26 21:16:01 +03:00
|
|
|
return reinterpret_cast<const StyleStrong<RawServoDeclarationBlock>*>(
|
|
|
|
&mRaw);
|
2018-05-30 19:15:25 +03:00
|
|
|
}
|
2016-11-03 06:41:02 +03:00
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
void ToString(nsAString& aResult) const {
|
2018-05-30 19:15:25 +03:00
|
|
|
Servo_DeclarationBlock_GetCssText(mRaw, &aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Count() const { return Servo_DeclarationBlock_Count(mRaw); }
|
|
|
|
|
2018-06-05 22:22:14 +03:00
|
|
|
bool GetNthProperty(uint32_t aIndex, nsAString& aReturn) const {
|
2018-05-30 19:15:25 +03:00
|
|
|
aReturn.Truncate();
|
|
|
|
return Servo_DeclarationBlock_GetNthProperty(mRaw, aIndex, &aReturn);
|
|
|
|
}
|
|
|
|
|
2018-06-05 22:30:13 +03:00
|
|
|
void GetPropertyValue(const nsAString& aProperty, nsAString& aValue) const {
|
|
|
|
NS_ConvertUTF16toUTF8 property(aProperty);
|
|
|
|
Servo_DeclarationBlock_GetPropertyValue(mRaw, &property, &aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetPropertyValueByID(nsCSSPropertyID aPropID, nsAString& aValue) const {
|
|
|
|
Servo_DeclarationBlock_GetPropertyValueById(mRaw, aPropID, &aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetPropertyIsImportant(const nsAString& aProperty) const {
|
|
|
|
NS_ConvertUTF16toUTF8 property(aProperty);
|
|
|
|
return Servo_DeclarationBlock_GetPropertyIsImportant(mRaw, &property);
|
|
|
|
}
|
|
|
|
|
2018-02-05 08:07:44 +03:00
|
|
|
// Returns whether the property was removed.
|
2018-06-06 01:04:39 +03:00
|
|
|
bool RemoveProperty(const nsAString& aProperty,
|
|
|
|
DeclarationBlockMutationClosure aClosure = {}) {
|
2018-06-05 22:30:13 +03:00
|
|
|
AssertMutable();
|
|
|
|
NS_ConvertUTF16toUTF8 property(aProperty);
|
2018-06-06 01:04:39 +03:00
|
|
|
return Servo_DeclarationBlock_RemoveProperty(mRaw, &property, aClosure);
|
2018-06-05 22:30:13 +03:00
|
|
|
}
|
|
|
|
|
2017-10-19 10:51:12 +03:00
|
|
|
// Returns whether the property was removed.
|
2018-06-06 01:04:39 +03:00
|
|
|
bool RemovePropertyByID(nsCSSPropertyID aProperty,
|
|
|
|
DeclarationBlockMutationClosure aClosure = {}) {
|
2018-06-05 22:30:13 +03:00
|
|
|
AssertMutable();
|
2018-06-06 01:04:39 +03:00
|
|
|
return Servo_DeclarationBlock_RemovePropertyById(mRaw, aProperty, aClosure);
|
2018-06-05 22:30:13 +03:00
|
|
|
}
|
2016-11-03 06:41:02 +03:00
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
private:
|
2018-05-30 19:15:25 +03:00
|
|
|
~DeclarationBlock() = default;
|
2019-04-29 08:34:06 +03:00
|
|
|
|
|
|
|
bool OwnerIsReadOnly() const;
|
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
union {
|
|
|
|
// We only ever have one of these since we have an
|
|
|
|
// nsHTMLCSSStyleSheet only for style attributes, and style
|
|
|
|
// attributes never have an owning rule.
|
|
|
|
|
|
|
|
// It's an nsHTMLCSSStyleSheet if the low bit is set.
|
|
|
|
|
|
|
|
uintptr_t mRaw;
|
|
|
|
|
|
|
|
// The style rule that owns this declaration. May be null.
|
|
|
|
css::Rule* mOwningRule;
|
|
|
|
|
|
|
|
// The nsHTMLCSSStyleSheet that is responsible for this declaration.
|
|
|
|
// Only non-null for style attributes.
|
|
|
|
nsHTMLCSSStyleSheet* mHTMLCSSStyleSheet;
|
|
|
|
} mContainer;
|
|
|
|
|
2018-05-31 11:31:44 +03:00
|
|
|
RefPtr<RawServoDeclarationBlock> mRaw;
|
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
// set when declaration put in the rule tree;
|
2017-02-24 14:06:07 +03:00
|
|
|
bool mImmutable;
|
2016-10-18 07:29:03 +03:00
|
|
|
|
2017-09-20 06:30:08 +03:00
|
|
|
// True if this declaration has not been restyled after modified.
|
|
|
|
//
|
|
|
|
// Since we can clear this flag from style worker threads, we use an Atomic.
|
|
|
|
//
|
|
|
|
// Note that although a single DeclarationBlock can be shared between
|
|
|
|
// different rule nodes (due to the style="" attribute cache), whenever a
|
|
|
|
// DeclarationBlock has its mIsDirty flag set to true, we always clone it to
|
|
|
|
// a unique object first. So when we clear this flag during Servo traversal,
|
|
|
|
// we know that we are clearing it on a DeclarationBlock that has a single
|
|
|
|
// reference, and there is no problem with another user of the same
|
|
|
|
// DeclarationBlock thinking that it is not dirty.
|
|
|
|
Atomic<bool, MemoryOrdering::Relaxed> mIsDirty;
|
2016-10-18 07:29:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_DeclarationBlock_h
|