Skip to content

Commit

Permalink
[view-transitions] Register user agent @keyframes rules
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=269504
rdar://123033600

Reviewed by Matthieu Dubet.

Add static storage for user agent @Keyframes, and use it for the viewTransitions.css stylesheet.

This is the same pattern used as `CounterStyleRegistry::userAgentCounterStyles`.

* Source/WebCore/style/StyleResolver.cpp:
(WebCore::Style::Resolver::userAgentKeyframes):
(WebCore::Style::Resolver::addUserAgentKeyframeStyle):
(WebCore::Style::Resolver::isAnimationNameValid):
(WebCore::Style::Resolver::keyframeRulesForName const):
* Source/WebCore/style/StyleResolver.h:
* Source/WebCore/style/UserAgentStyle.cpp:
(WebCore::Style::addUserAgentKeyframes):
(WebCore::Style::UserAgentStyle::ensureDefaultStyleSheetsForElement):

Canonical link: https://commits.webkit.org/274789@main
  • Loading branch information
nt1m committed Feb 16, 2024
1 parent 729aa50 commit 3054951
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
25 changes: 21 additions & 4 deletions Source/WebCore/style/StyleResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,22 @@ void Resolver::appendAuthorStyleSheets(const Vector<RefPtr<CSSStyleSheet>>& styl
renderView->style().fontCascade().update(&document().fontSelector());
}

KeyframesRuleMap& Resolver::userAgentKeyframes()
{
static NeverDestroyed<KeyframesRuleMap> keyframes;
return keyframes;
}

void Resolver::addUserAgentKeyframeStyle(Ref<StyleRuleKeyframes>&& rule)
{
const auto& animationName = rule->name();
userAgentKeyframes().set(animationName, WTFMove(rule));
}

// This is a simplified style setting function for keyframe styles
void Resolver::addKeyframeStyle(Ref<StyleRuleKeyframes>&& rule)
{
auto& animationName = rule->name();
const auto& animationName = rule->name();
m_keyframesRuleMap.set(animationName, WTFMove(rule));
document().keyframesRuleDidChange(animationName);
}
Expand Down Expand Up @@ -347,7 +359,8 @@ std::unique_ptr<RenderStyle> Resolver::styleForKeyframe(const Element& element,

bool Resolver::isAnimationNameValid(const String& name)
{
return m_keyframesRuleMap.find(AtomString(name)) != m_keyframesRuleMap.end();
return m_keyframesRuleMap.find(AtomString(name)) != m_keyframesRuleMap.end()
|| userAgentKeyframes().find(AtomString(name)) != userAgentKeyframes().end();
}

Vector<Ref<StyleRuleKeyframe>> Resolver::keyframeRulesForName(const AtomString& animationName) const
Expand All @@ -357,9 +370,13 @@ Vector<Ref<StyleRuleKeyframe>> Resolver::keyframeRulesForName(const AtomString&

m_keyframesRuleMap.checkConsistency();

// Check author map first then check user-agent map.
auto it = m_keyframesRuleMap.find(animationName);
if (it == m_keyframesRuleMap.end())
return { };
if (it == m_keyframesRuleMap.end()) {
it = userAgentKeyframes().find(animationName);
if (it == userAgentKeyframes().end())
return { };
}

auto compositeOperationForKeyframe = [](Ref<StyleRuleKeyframe> keyframe) -> CompositeOperation {
if (auto compositeOperationCSSValue = keyframe->properties().getPropertyCSSValue(CSSPropertyAnimationComposition)) {
Expand Down
5 changes: 4 additions & 1 deletion Source/WebCore/style/StyleResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ struct ResolutionContext {
bool isSVGUseTreeRoot { false };
};

using KeyframesRuleMap = HashMap<AtomString, RefPtr<StyleRuleKeyframes>>;

class Resolver : public RefCounted<Resolver>, public CanMakeSingleThreadWeakPtr<Resolver> {
WTF_MAKE_ISO_ALLOCATED(Resolver);
public:
Expand Down Expand Up @@ -133,6 +135,8 @@ class Resolver : public RefCounted<Resolver>, public CanMakeSingleThreadWeakPtr<
bool hasViewportDependentMediaQueries() const;
std::optional<DynamicMediaQueryEvaluationChanges> evaluateDynamicMediaQueries();

static KeyframesRuleMap& userAgentKeyframes();
static void addUserAgentKeyframeStyle(Ref<StyleRuleKeyframes>&&);
void addKeyframeStyle(Ref<StyleRuleKeyframes>&&);
Vector<Ref<StyleRuleKeyframe>> keyframeRulesForName(const AtomString&) const;

Expand Down Expand Up @@ -164,7 +168,6 @@ class Resolver : public RefCounted<Resolver>, public CanMakeSingleThreadWeakPtr<

ScopeRuleSets m_ruleSets;

typedef HashMap<AtomString, RefPtr<StyleRuleKeyframes>> KeyframesRuleMap;
KeyframesRuleMap m_keyframesRuleMap;

MQ::MediaQueryEvaluator m_mediaQueryEvaluator;
Expand Down
12 changes: 12 additions & 0 deletions Source/WebCore/style/UserAgentStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "CSSCounterStyleRegistry.h"
#include "CSSCounterStyleRule.h"
#include "CSSKeyframesRule.h"
#include "CSSValuePool.h"
#include "Chrome.h"
#include "ChromeClient.h"
Expand Down Expand Up @@ -60,6 +61,7 @@
#include "RenderTheme.h"
#include "RuleSetBuilder.h"
#include "SVGElement.h"
#include "StyleResolver.h"
#include "StyleSheetContents.h"
#include "UserAgentStyleSheets.h"
#include <wtf/NeverDestroyed.h>
Expand Down Expand Up @@ -130,6 +132,15 @@ void static addToCounterStyleRegistry(StyleSheetContents& sheet)
CSSCounterStyleRegistry::resolveUserAgentReferences();
}

void static addUserAgentKeyframes(StyleSheetContents& sheet)
{
// This does not handle nested rules.
for (auto& rule : sheet.childRules()) {
if (auto* styleRuleKeyframes = dynamicDowncast<StyleRuleKeyframes>(rule.get()))
Style::Resolver::addUserAgentKeyframeStyle(*styleRuleKeyframes);
}
}

void UserAgentStyle::addToDefaultStyle(StyleSheetContents& sheet)
{
RuleSetBuilder screenBuilder(*defaultStyle, screenEval());
Expand Down Expand Up @@ -276,6 +287,7 @@ void UserAgentStyle::ensureDefaultStyleSheetsForElement(const Element& element)
if (!viewTransitionsStyleSheet && element.document().settings().viewTransitionsEnabled()) {
viewTransitionsStyleSheet = parseUASheet(StringImpl::createWithoutCopying(viewTransitionsUserAgentStyleSheet, sizeof(viewTransitionsUserAgentStyleSheet)));
addToDefaultStyle(*viewTransitionsStyleSheet);
addUserAgentKeyframes(*viewTransitionsStyleSheet);
}

ASSERT(defaultStyle->features().idsInRules.isEmpty());
Expand Down

0 comments on commit 3054951

Please sign in to comment.