Skip to content

Commit

Permalink
Introduce PseudoElementIdentifier
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=268412
rdar://121959648

Reviewed by Darin Adler.

We want to separate this from PseudoElementRequest so we can re-use it in other places.

* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/rendering/RenderElement.cpp:
(WebCore::RenderElement::getUncachedPseudoStyle const):
* Source/WebCore/style/ElementRuleCollector.cpp:
(WebCore::Style::ElementRuleCollector::collectMatchingRules):
(WebCore::Style::ElementRuleCollector::ruleMatches):
(WebCore::Style::ElementRuleCollector::collectMatchingRulesForList):
(WebCore::Style::ElementRuleCollector::addAuthorKeyframeRules):
* Source/WebCore/style/PseudoElementIdentifier.h: Copied from Source/WebCore/style/PseudoElementRequest.h.
(WebCore::Style::PseudoElementIdentifier::PseudoElementIdentifier):
* Source/WebCore/style/PseudoElementRequest.h:
(WebCore::Style::PseudoElementRequest::PseudoElementRequest):
(WebCore::Style::PseudoElementRequest::identifier const):
(WebCore::Style::PseudoElementRequest::pseudoId const):
(WebCore::Style::PseudoElementRequest::pseudoElementNameArgument const):
(WebCore::Style::PseudoElementRequest::scrollbarState const):
* Source/WebCore/style/StyleResolver.cpp:
(WebCore::Style::Resolver::styleForPseudoElement):

Canonical link: https://commits.webkit.org/273809@main
  • Loading branch information
nt1m committed Jan 31, 2024
1 parent b31165e commit 412734d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Source/WebCore/WebCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,7 @@
48B0242A2B2A65AA0083D122 /* AXTextRun.h in Headers */ = {isa = PBXBuildFile; fileRef = 48B024282B2A65AA0083D122 /* AXTextRun.h */; settings = {ATTRIBUTES = (Private, ); }; };
48B5BF3729CA691700FEBE3B /* AccessibilityRegionContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 48B5BF3529CA691700FEBE3B /* AccessibilityRegionContext.h */; settings = {ATTRIBUTES = (Private, ); }; };
48D0E12929F7329E00263857 /* ElementAnimationContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 48D0E12829F7322900263857 /* ElementAnimationContext.h */; settings = {ATTRIBUTES = (Private, ); }; };
490D51012B698A8300C6B266 /* PseudoElementIdentifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 490D51002B698A8300C6B266 /* PseudoElementIdentifier.h */; };
491342E32B43FFEF00FEEF18 /* CSSSelectorInlines.h in Headers */ = {isa = PBXBuildFile; fileRef = 491342DA2B43FAA800FEEF18 /* CSSSelectorInlines.h */; settings = {ATTRIBUTES = (Private, ); }; };
491342E42B43FFEF00FEEF18 /* CSSSelectorEnums.h in Headers */ = {isa = PBXBuildFile; fileRef = 491342D92B43FAA700FEEF18 /* CSSSelectorEnums.h */; settings = {ATTRIBUTES = (Private, ); }; };
493E5E0912D6420500020081 /* PlatformCALayerClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 493E5E0812D6420500020081 /* PlatformCALayerClient.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -10802,6 +10803,7 @@
48B5BF3629CA691700FEBE3B /* AccessibilityRegionContext.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AccessibilityRegionContext.cpp; sourceTree = "<group>"; };
48C4E83C2B34D8380083460A /* AXTextRun.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AXTextRun.cpp; sourceTree = "<group>"; };
48D0E12829F7322900263857 /* ElementAnimationContext.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ElementAnimationContext.h; sourceTree = "<group>"; };
490D51002B698A8300C6B266 /* PseudoElementIdentifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PseudoElementIdentifier.h; sourceTree = "<group>"; };
491342962B425C0100FEEF18 /* CSSPseudoSelectors.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = CSSPseudoSelectors.json; sourceTree = "<group>"; };
491342972B425C0200FEEF18 /* process-css-pseudo-selectors.py */ = {isa = PBXFileReference; lastKnownFileType = text.script.python; path = "process-css-pseudo-selectors.py"; sourceTree = "<group>"; };
491342D92B43FAA700FEEF18 /* CSSSelectorEnums.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 4; path = CSSSelectorEnums.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -35305,6 +35307,7 @@
E4ABABE22361A32900FA4345 /* PropertyCascade.h */,
E45A6C732417BA4C006E4CD5 /* PseudoClassChangeInvalidation.cpp */,
E45A6C762417BA59006E4CD5 /* PseudoClassChangeInvalidation.h */,
490D51002B698A8300C6B266 /* PseudoElementIdentifier.h */,
494265302B61F7E9000446D5 /* PseudoElementRequest.h */,
930A9D8629DA3EFF006CBF0E /* ResolvedStyle.h */,
E4863CFA23842E8700972158 /* RuleData.cpp */,
Expand Down Expand Up @@ -41244,6 +41247,7 @@
51A052561058874000CC9E95 /* ProtectionSpaceHash.h in Headers */,
E45A6C772417BA59006E4CD5 /* PseudoClassChangeInvalidation.h in Headers */,
FF945ECC161F7F3600971BC8 /* PseudoElement.h in Headers */,
490D51012B698A8300C6B266 /* PseudoElementIdentifier.h in Headers */,
494265312B61F7EA000446D5 /* PseudoElementRequest.h in Headers */,
57D8462E1FEAF69900CA3682 /* PublicKeyCredential.h in Headers */,
57303BE92009748D00355965 /* PublicKeyCredentialCreationOptions.h in Headers */,
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/rendering/RenderElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ const RenderStyle* RenderElement::getCachedPseudoStyle(PseudoId pseudo, const Re

std::unique_ptr<RenderStyle> RenderElement::getUncachedPseudoStyle(const Style::PseudoElementRequest& pseudoElementRequest, const RenderStyle* parentStyle, const RenderStyle* ownStyle) const
{
if (pseudoElementRequest.pseudoId < PseudoId::FirstInternalPseudoId && !ownStyle && !style().hasPseudoStyle(pseudoElementRequest.pseudoId))
if (pseudoElementRequest.pseudoId() < PseudoId::FirstInternalPseudoId && !ownStyle && !style().hasPseudoStyle(pseudoElementRequest.pseudoId()))
return nullptr;

if (!parentStyle) {
Expand Down
16 changes: 8 additions & 8 deletions Source/WebCore/style/ElementRuleCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void ElementRuleCollector::collectMatchingRules(CascadeLevel level)

void ElementRuleCollector::collectMatchingRules(const MatchRequest& matchRequest)
{
ASSERT_WITH_MESSAGE(!(m_mode == SelectorChecker::Mode::CollectingRulesIgnoringVirtualPseudoElements && m_pseudoElementRequest.pseudoId != PseudoId::None), "When in StyleInvalidation or SharingRules, SelectorChecker does not try to match the pseudo ID. While ElementRuleCollector supports matching a particular pseudoId in this case, this would indicate a error at the call site since matching a particular element should be unnecessary.");
ASSERT_WITH_MESSAGE(!(m_mode == SelectorChecker::Mode::CollectingRulesIgnoringVirtualPseudoElements && m_pseudoElementRequest.pseudoId() != PseudoId::None), "When in StyleInvalidation or SharingRules, SelectorChecker does not try to match the pseudo ID. While ElementRuleCollector supports matching a particular pseudoId in this case, this would indicate a error at the call site since matching a particular element should be unnecessary.");

auto& element = this->element();
auto* shadowRoot = element.containingShadowRoot();
Expand Down Expand Up @@ -446,7 +446,7 @@ inline bool ElementRuleCollector::ruleMatches(const RuleData& ruleData, unsigned
// This is limited to HTML only so we don't need to check the namespace (because of tag name match).
auto matchBasedOnRuleHash = ruleData.matchBasedOnRuleHash();
if (matchBasedOnRuleHash != MatchBasedOnRuleHash::None && element().isHTMLElement()) {
ASSERT_WITH_MESSAGE(m_pseudoElementRequest.pseudoId == PseudoId::None, "If we match based on the rule hash while collecting for a particular pseudo element ID, we would add incorrect rules for that pseudo element ID. We should never end in ruleMatches() with a pseudo element if the ruleData cannot match any pseudo element.");
ASSERT_WITH_MESSAGE(m_pseudoElementRequest.pseudoId() == PseudoId::None, "If we match based on the rule hash while collecting for a particular pseudo element ID, we would add incorrect rules for that pseudo element ID. We should never end in ruleMatches() with a pseudo element if the ruleData cannot match any pseudo element.");

switch (matchBasedOnRuleHash) {
case MatchBasedOnRuleHash::None:
Expand Down Expand Up @@ -479,7 +479,7 @@ inline bool ElementRuleCollector::ruleMatches(const RuleData& ruleData, unsigned

#if !ASSERT_MSG_DISABLED
unsigned ignoreSpecificity;
ASSERT_WITH_MESSAGE(!SelectorCompiler::ruleCollectorSimpleSelectorChecker(compiledSelector, &element(), &ignoreSpecificity) || m_pseudoElementRequest.pseudoId == PseudoId::None, "When matching pseudo elements, we should never compile a selector checker without context unless it cannot match anything.");
ASSERT_WITH_MESSAGE(!SelectorCompiler::ruleCollectorSimpleSelectorChecker(compiledSelector, &element(), &ignoreSpecificity) || m_pseudoElementRequest.pseudoId() == PseudoId::None, "When matching pseudo elements, we should never compile a selector checker without context unless it cannot match anything.");
#endif
bool selectorMatches = SelectorCompiler::ruleCollectorSimpleSelectorChecker(compiledSelector, &element(), &specificity);

Expand All @@ -491,9 +491,9 @@ inline bool ElementRuleCollector::ruleMatches(const RuleData& ruleData, unsigned
#endif // ENABLE(CSS_SELECTOR_JIT)

SelectorChecker::CheckingContext context(m_mode);
context.pseudoId = m_pseudoElementRequest.pseudoId;
context.scrollbarState = m_pseudoElementRequest.scrollbarState;
context.pseudoElementNameArgument = m_pseudoElementRequest.pseudoElementNameArgument;
context.pseudoId = m_pseudoElementRequest.pseudoId();
context.scrollbarState = m_pseudoElementRequest.scrollbarState();
context.pseudoElementNameArgument = m_pseudoElementRequest.pseudoElementNameArgument();
context.styleScopeOrdinal = styleScopeOrdinal;
context.selectorMatchingState = m_selectorMatchingState;
context.scope = scopingRoot;
Expand Down Expand Up @@ -535,7 +535,7 @@ void ElementRuleCollector::collectMatchingRulesForList(const RuleSet::RuleDataVe
if (UNLIKELY(!ruleData.isEnabled()))
continue;

if (!ruleData.canMatchPseudoElement() && m_pseudoElementRequest.pseudoId != PseudoId::None)
if (!ruleData.canMatchPseudoElement() && m_pseudoElementRequest.pseudoId() != PseudoId::None)
continue;

if (m_selectorMatchingState && m_selectorMatchingState->selectorFilter.fastRejectSelector(ruleData.descendantSelectorIdentifierHashes()))
Expand Down Expand Up @@ -843,7 +843,7 @@ void ElementRuleCollector::addMatchedProperties(MatchedProperties&& matchedPrope
void ElementRuleCollector::addAuthorKeyframeRules(const StyleRuleKeyframe& keyframe)
{
ASSERT(m_result->authorDeclarations.isEmpty());
m_result->authorDeclarations.append({ keyframe.properties(), SelectorChecker::MatchAll, propertyAllowlistForPseudoId(m_pseudoElementRequest.pseudoId) });
m_result->authorDeclarations.append({ keyframe.properties(), SelectorChecker::MatchAll, propertyAllowlistForPseudoId(m_pseudoElementRequest.pseudoId()) });
}

}
Expand Down
40 changes: 40 additions & 0 deletions Source/WebCore/style/PseudoElementIdentifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "RenderStyleConstants.h"
#include <wtf/text/AtomString.h>

namespace WebCore::Style {

struct PseudoElementIdentifier {
PseudoId pseudoId;

// highlight name for ::highlight or view transition name for view transition pseudo elements.
AtomString pseudoElementNameArgument { nullAtom() };
};

} // namespace WebCore
24 changes: 16 additions & 8 deletions Source/WebCore/style/PseudoElementRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#pragma once

#include "PseudoElementIdentifier.h"
#include "RenderStyleConstants.h"
#include <wtf/text/AtomString.h>

Expand All @@ -33,23 +34,30 @@ namespace WebCore::Style {
class PseudoElementRequest {
public:
PseudoElementRequest(PseudoId pseudoId, std::optional<StyleScrollbarState> scrollbarState = std::nullopt)
: pseudoId(pseudoId)
, scrollbarState(scrollbarState)
: m_identifier({ pseudoId })
, m_scrollbarState(scrollbarState)
{
}

PseudoElementRequest(PseudoId pseudoId, const AtomString& pseudoElementNameArgument)
: pseudoId(pseudoId)
, pseudoElementNameArgument(pseudoElementNameArgument)
: m_identifier({ pseudoId, pseudoElementNameArgument })
{
ASSERT(pseudoId == PseudoId::Highlight || pseudoId == PseudoId::ViewTransitionGroup || pseudoId == PseudoId::ViewTransitionImagePair || pseudoId == PseudoId::ViewTransitionOld || pseudoId == PseudoId::ViewTransitionNew);
}

PseudoId pseudoId;
std::optional<StyleScrollbarState> scrollbarState;
PseudoElementRequest(const PseudoElementIdentifier& pseudoElementIdentifier)
: m_identifier(pseudoElementIdentifier)
{
}

const PseudoElementIdentifier& identifier() const { return m_identifier; }
PseudoId pseudoId() const { return m_identifier.pseudoId; }
const AtomString& pseudoElementNameArgument() const { return m_identifier.pseudoElementNameArgument; }
const std::optional<StyleScrollbarState>& scrollbarState() const { return m_scrollbarState; }

// highlight name for ::highlight or view transition name for view transition pseudo elements.
AtomString pseudoElementNameArgument;
private:
PseudoElementIdentifier m_identifier;
std::optional<StyleScrollbarState> m_scrollbarState;
};

} // namespace WebCore
6 changes: 3 additions & 3 deletions Source/WebCore/style/StyleResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,9 @@ std::optional<ResolvedStyle> Resolver::styleForPseudoElement(const Element& elem
if (collector.matchResult().isEmpty())
return { };

state.style()->setPseudoElementType(pseudoElementRequest.pseudoId);
if (!pseudoElementRequest.pseudoElementNameArgument.isNull())
state.style()->setPseudoElementNameArgument(pseudoElementRequest.pseudoElementNameArgument);
state.style()->setPseudoElementType(pseudoElementRequest.pseudoId());
if (!pseudoElementRequest.pseudoElementNameArgument().isNull())
state.style()->setPseudoElementNameArgument(pseudoElementRequest.pseudoElementNameArgument());

applyMatchedProperties(state, collector.matchResult());

Expand Down

0 comments on commit 412734d

Please sign in to comment.