Bug 1921553 - Use PseudoStyleRequest in nsCSSPseudoElements. r=view-transitions-reviewers,layout-reviewers,emilio

And some related code, such as `KeyframeEffectParams` and
`IsSupportedPseudoForAnimations()`.

Note: `GetPseudoAtom()` and `GetPseudoType()` are redundant, so remove them.

Differential Revision: https://phabricator.services.mozilla.com/D228224
This commit is contained in:
Boris Chiou
2024-11-25 23:46:31 +00:00
parent 30af2312c9
commit b6c240ef4c
10 changed files with 49 additions and 57 deletions

View File

@@ -93,6 +93,10 @@ class AnimationUtils {
return aType == PseudoStyleType::before ||
aType == PseudoStyleType::after || aType == PseudoStyleType::marker;
}
static bool IsSupportedPseudoForAnimations(
const PseudoStyleRequest& aRequest) {
return IsSupportedPseudoForAnimations(aRequest.mType);
}
/**
* Returns true if the difference between |aFirst| and |aSecond| is within

View File

@@ -102,8 +102,10 @@ KeyframeEffect::KeyframeEffect(Document* aDocument,
const KeyframeEffect& aOther)
: AnimationEffect(aDocument, TimingParams{aOther.SpecifiedTiming()}),
mTarget(std::move(aTarget)),
// TODO: We will use PseudoStyleReqeust in OwningAnimationTarget in the
// following patches.
mEffectOptions{aOther.IterationComposite(), aOther.Composite(),
mTarget.mPseudoType},
PseudoStyleRequest(mTarget.mPseudoType)},
mKeyframes(aOther.mKeyframes.Clone()),
mProperties(aOther.mProperties.Clone()),
mBaseValues(aOther.mBaseValues.Clone()) {}
@@ -787,17 +789,17 @@ static KeyframeEffectParams KeyframeEffectParamsFromUnion(
const KeyframeEffectOptions& options =
KeyframeEffectOptionsFromUnion(aOptions);
// |result.mPseudoRequest| uses the default value, i.e. NotPseudo.
result.mIterationComposite = options.mIterationComposite;
result.mComposite = options.mComposite;
result.mPseudoType = PseudoStyleType::NotPseudo;
if (DOMStringIsNull(options.mPseudoElement)) {
return result;
}
Maybe<PseudoStyleType> pseudoType =
nsCSSPseudoElements::GetPseudoType(options.mPseudoElement);
if (!pseudoType) {
Maybe<PseudoStyleRequest> pseudoRequest =
nsCSSPseudoElements::ParsePseudoElement(options.mPseudoElement);
if (!pseudoRequest) {
// Per the spec, we throw SyntaxError for syntactically invalid pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is a syntactically invalid pseudo-element.",
@@ -805,8 +807,8 @@ static KeyframeEffectParams KeyframeEffectParamsFromUnion(
return result;
}
result.mPseudoType = *pseudoType;
if (!AnimationUtils::IsSupportedPseudoForAnimations(result.mPseudoType)) {
result.mPseudoRequest = std::move(*pseudoRequest);
if (!AnimationUtils::IsSupportedPseudoForAnimations(result.mPseudoRequest)) {
// Per the spec, we throw SyntaxError for unsupported pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is an unsupported pseudo-element.",
@@ -849,8 +851,10 @@ already_AddRefed<KeyframeEffect> KeyframeEffect::ConstructKeyframeEffect(
return nullptr;
}
// TODO: We will use PseudoStyleRequest in OwningAnimaitonTarget in the
// following patches.
RefPtr<KeyframeEffect> effect = new KeyframeEffect(
doc, OwningAnimationTarget(aTarget, effectOptions.mPseudoType),
doc, OwningAnimationTarget(aTarget, effectOptions.mPseudoRequest.mType),
std::move(timingParams), effectOptions);
effect->SetKeyframes(aGlobal.Context(), aKeyframes, aRv);
@@ -1102,11 +1106,11 @@ void KeyframeEffect::SetPseudoElement(const nsAString& aPseudoElement,
return;
}
// Note: GetPseudoType() returns Some(NotPseudo) for the null string,
// Note: ParsePseudoELement() returns Some(NotPseudo) for the null string,
// so we handle null case before this.
Maybe<PseudoStyleType> pseudoType =
nsCSSPseudoElements::GetPseudoType(aPseudoElement);
if (!pseudoType || *pseudoType == PseudoStyleType::NotPseudo) {
Maybe<PseudoStyleRequest> pseudoRequest =
nsCSSPseudoElements::ParsePseudoElement(aPseudoElement);
if (!pseudoRequest || pseudoRequest->IsNotPseudo()) {
// Per the spec, we throw SyntaxError for syntactically invalid pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is a syntactically invalid pseudo-element.",
@@ -1114,7 +1118,7 @@ void KeyframeEffect::SetPseudoElement(const nsAString& aPseudoElement,
return;
}
if (!AnimationUtils::IsSupportedPseudoForAnimations(*pseudoType)) {
if (!AnimationUtils::IsSupportedPseudoForAnimations(*pseudoRequest)) {
// Per the spec, we throw SyntaxError for unsupported pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is an unsupported pseudo-element.",
@@ -1122,7 +1126,7 @@ void KeyframeEffect::SetPseudoElement(const nsAString& aPseudoElement,
return;
}
UpdateTarget(mTarget.mElement, *pseudoType);
UpdateTarget(mTarget.mElement, pseudoRequest->mType);
}
static void CreatePropertyValue(

View File

@@ -8,7 +8,7 @@
#define mozilla_KeyframeEffectParams_h
#include "mozilla/dom/KeyframeEffectBinding.h" // IterationCompositeOperation
#include "mozilla/PseudoStyleType.h" // PseudoStyleType
#include "mozilla/PseudoStyleType.h" // PseudoStyleRequest
namespace mozilla {
@@ -16,17 +16,17 @@ struct KeyframeEffectParams {
KeyframeEffectParams() = default;
KeyframeEffectParams(dom::IterationCompositeOperation aIterationComposite,
dom::CompositeOperation aComposite,
PseudoStyleType aPseudoType)
const PseudoStyleRequest& aPseudoRequest)
: mIterationComposite(aIterationComposite),
mComposite(aComposite),
mPseudoType(aPseudoType) {}
mPseudoRequest(aPseudoRequest) {}
explicit KeyframeEffectParams(dom::CompositeOperation aComposite)
: mComposite(aComposite) {}
dom::IterationCompositeOperation mIterationComposite =
dom::IterationCompositeOperation::Replace;
dom::CompositeOperation mComposite = dom::CompositeOperation::Replace;
PseudoStyleType mPseudoType = PseudoStyleType::NotPseudo;
PseudoStyleRequest mPseudoRequest;
};
} // namespace mozilla

View File

@@ -8779,14 +8779,14 @@ already_AddRefed<Element> Document::CreateElement(
// Check 'pseudo' and throw an exception if it's not one allowed
// with CSS_PSEUDO_ELEMENT_IS_JS_CREATED_NAC.
if (options.mPseudo.WasPassed()) {
Maybe<PseudoStyleType> type =
nsCSSPseudoElements::GetPseudoType(options.mPseudo.Value());
if (!type || *type == PseudoStyleType::NotPseudo ||
!nsCSSPseudoElements::PseudoElementIsJSCreatedNAC(*type)) {
Maybe<PseudoStyleRequest> request =
nsCSSPseudoElements::ParsePseudoElement(options.mPseudo.Value());
if (!request || request->IsNotPseudo() ||
!nsCSSPseudoElements::PseudoElementIsJSCreatedNAC(request->mType)) {
rv.ThrowNotSupportedError("Invalid pseudo-element");
return nullptr;
}
pseudoType = *type;
pseudoType = request->mType;
}
}

View File

@@ -3319,14 +3319,13 @@ nsDOMWindowUtils::GetUnanimatedComputedStyle(Element* aElement,
return NS_ERROR_FAILURE;
}
Maybe<PseudoStyleType> pseudo =
nsCSSPseudoElements::GetPseudoType(aPseudoElement);
Maybe<PseudoStyleRequest> pseudo =
nsCSSPseudoElements::ParsePseudoElement(aPseudoElement);
if (!pseudo) {
return NS_ERROR_FAILURE;
}
RefPtr<const ComputedStyle> computedStyle =
nsComputedDOMStyle::GetUnanimatedComputedStyleNoFlush(
aElement, PseudoStyleRequest(*pseudo));
nsComputedDOMStyle::GetUnanimatedComputedStyleNoFlush(aElement, *pseudo);
if (!computedStyle) {
return NS_ERROR_FAILURE;
}

View File

@@ -254,9 +254,9 @@ bool CSSStyleRule::SelectorMatchesElement(uint32_t aSelectorIndex,
Element& aElement,
const nsAString& aPseudo,
bool aRelevantLinkVisited) {
Maybe<PseudoStyleType> pseudoType = nsCSSPseudoElements::GetPseudoType(
Maybe<PseudoStyleRequest> pseudo = nsCSSPseudoElements::ParsePseudoElement(
aPseudo, CSSEnabledState::IgnoreEnabledState);
if (!pseudoType) {
if (!pseudo) {
return false;
}
@@ -288,8 +288,10 @@ bool CSSStyleRule::SelectorMatchesElement(uint32_t aSelectorIndex,
AutoTArray<const StyleLockedStyleRule*, 8> rules;
CollectStyleRules(*this, /* aDesugared = */ true, rules);
// FIXME: Bug 1909173. This function is used for the devtool, so we may need
// to revist here once we finish the support of view-transitions.
return Servo_StyleRule_SelectorMatchesElement(
&rules, &aElement, aSelectorIndex, host, *pseudoType,
&rules, &aElement, aSelectorIndex, host, pseudo->mType,
aRelevantLinkVisited);
}

View File

@@ -130,15 +130,17 @@ class PseudoStyle final {
struct PseudoStyleRequest {
PseudoStyleRequest() = default;
PseudoStyleRequest(PseudoStyleRequest&&) = default;
PseudoStyleRequest(const PseudoStyleRequest&) = default;
PseudoStyleRequest& operator=(PseudoStyleRequest&&) = default;
PseudoStyleRequest& operator=(const PseudoStyleRequest&) = default;
explicit PseudoStyleRequest(PseudoStyleType aType) : mType(aType) {}
PseudoStyleRequest(PseudoStyleType aType, nsAtom* aIdentifier)
: mType(aType), mIdentifier(aIdentifier) {}
bool IsNotPseudo() const { return mType == PseudoStyleType::NotPseudo; }
bool IsPseudoElementOrNotPseudo() const {
return mType == PseudoStyleType::NotPseudo ||
PseudoStyle::IsPseudoElement(mType);
return IsNotPseudo() || PseudoStyle::IsPseudoElement(mType);
}
static PseudoStyleRequest NotPseudo() { return PseudoStyleRequest(); }

View File

@@ -104,13 +104,6 @@ Maybe<PseudoStyleRequest> nsCSSPseudoElements::ParsePseudoElement(
return Nothing();
}
/* static */
mozilla::Maybe<PseudoStyleType> nsCSSPseudoElements::GetPseudoType(
const nsAString& aPseudoElement, CSSEnabledState aEnabledState) {
return ParsePseudoElement(aPseudoElement, aEnabledState)
.map([](const auto& r) { return r.mType; });
}
/* static */
bool nsCSSPseudoElements::PseudoElementSupportsUserActionState(
const Type aType) {

View File

@@ -58,8 +58,9 @@
#define CSS_PSEUDO_ELEMENT_IS_FLEX_OR_GRID_ITEM (1 << 7)
class nsCSSPseudoElements {
typedef mozilla::PseudoStyleType Type;
typedef mozilla::CSSEnabledState EnabledState;
using EnabledState = mozilla::CSSEnabledState;
using Request = mozilla::PseudoStyleRequest;
using Type = mozilla::PseudoStyleType;
public:
static bool IsEagerlyCascadedInServo(const Type aType) {
@@ -81,17 +82,9 @@ class nsCSSPseudoElements {
#include "nsCSSPseudoElementList.h"
#undef CSS_PSEUDO_ELEMENT
// Returns an empty tuple for a syntactically invalid pseudo-element, and
// Returns an empty Request for a syntactically invalid pseudo-element, and
// NotPseudo for the empty / null string.
// The second element of the tuple (functional pseudo parameter) is currently
// only used for `::highlight()` pseudos and is `nullptr` otherwise.
static mozilla::Maybe<mozilla::PseudoStyleRequest> ParsePseudoElement(
const nsAString& aPseudoElement,
EnabledState = EnabledState::ForAllContent);
// Returns Nothing() for a syntactically invalid pseudo-element, and NotPseudo
// for the empty / null string.
static mozilla::Maybe<Type> GetPseudoType(
static mozilla::Maybe<Request> ParsePseudoElement(
const nsAString& aPseudoElement,
EnabledState = EnabledState::ForAllContent);
@@ -100,11 +93,6 @@ class nsCSSPseudoElements {
// This only ever returns static atoms, so it's fine to return a raw pointer.
static nsAtom* GetPseudoAtom(Type aType);
// Get the atom for a given pseudo-element string (e.g. "::before"). This can
// return dynamic atoms, for unrecognized pseudo-elements.
static already_AddRefed<nsAtom> GetPseudoAtom(
const nsAString& aPseudoElement);
static bool PseudoElementContainsElements(const Type aType) {
return PseudoElementHasFlags(aType, CSS_PSEUDO_ELEMENT_CONTAINS_ELEMENTS);
}

View File

@@ -133,7 +133,7 @@ static bool ElementNeedsRestyle(Element* aElement,
// If the pseudo-element is animating, make sure to flush.
if (aElement->MayHaveAnimations() &&
aPseudo.mType != PseudoStyleType::NotPseudo &&
AnimationUtils::IsSupportedPseudoForAnimations(aPseudo.mType)) {
AnimationUtils::IsSupportedPseudoForAnimations(aPseudo)) {
if (EffectSet::Get(aElement, aPseudo.mType)) {
return true;
}