Skip to content

Commit

Permalink
Make most CSSSelector setters only available to MutableCSSSelector
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=267060
rdar://120434084

Reviewed by Simon Fraser.

CSSSelector isn't meant to be mutated in the majority of cases, restrict most setters to MutableCSSSelector.

* Source/WebCore/css/CSSSelector.h:
(WebCore::CSSSelector::hasDescendantRelation const):
(WebCore::CSSSelector::relation const):
(WebCore::CSSSelector::match const):
(WebCore::CSSSelector::isLastInSelectorList const):
(WebCore::CSSSelector::isFirstInTagHistory const):
(WebCore::CSSSelector::setLastInSelectorList):
(WebCore::CSSSelector::setNotFirstInTagHistory):
(WebCore::CSSSelector::setNotLastInTagHistory):
(WebCore::CSSSelector::isForPage const):
(WebCore::CSSSelector::setForPage):
(WebCore::CSSSelector::setImplicit):
(WebCore::CSSSelector::tagHistory):
(WebCore::CSSSelector::setNotLastInSelectorList): Deleted.
(WebCore::CSSSelector::setLastInTagHistory): Deleted.
* Source/WebCore/css/parser/CSSParserImpl.cpp:
(WebCore::appendImplicitSelectorPseudoClassScopeIfNeeded):
(WebCore::CSSParserImpl::createNestingParentRule):
* Source/WebCore/css/parser/MutableCSSSelector.h:
(WebCore::MutableCSSSelector::setImplicit):

Canonical link: https://commits.webkit.org/272635@main
  • Loading branch information
nt1m committed Jan 4, 2024
1 parent d9cb0be commit 4a02d79
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
58 changes: 29 additions & 29 deletions Source/WebCore/css/CSSSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class CSSSelector {
Right,
};

enum AttributeMatchType { CaseSensitive, CaseInsensitive };

static PseudoId pseudoId(PseudoElement);
static bool isPseudoClassEnabled(PseudoClass, const CSSSelectorParserContext&);
static bool isPseudoElementEnabled(PseudoElement, StringView, const CSSSelectorParserContext&);
Expand Down Expand Up @@ -143,62 +145,63 @@ class CSSSelector {
const CSSSelectorList* selectorList() const { return m_hasRareData ? m_data.rareData->selectorList.get() : nullptr; }
CSSSelectorList* selectorList() { return m_hasRareData ? m_data.rareData->selectorList.get() : nullptr; }

void setValue(const AtomString&, bool matchLowerCase = false);

enum AttributeMatchType { CaseSensitive, CaseInsensitive };
void setAttribute(const QualifiedName&, AttributeMatchType);
void setNth(int a, int b);
void setArgument(const AtomString&);
void setArgumentList(FixedVector<PossiblyQuotedIdentifier>);
void setSelectorList(std::unique_ptr<CSSSelectorList>);

bool matchNth(int count) const;
int nthA() const;
int nthB() const;

bool hasDescendantRelation() const { return relation() == Relation::DescendantSpace; }

bool hasDescendantOrChildRelation() const { return relation() == Relation::Child || hasDescendantRelation(); }

PseudoClass pseudoClass() const;
void setPseudoClass(PseudoClass);

PseudoElement pseudoElement() const;
void setPseudoElement(PseudoElement);

PagePseudoClass pagePseudoClass() const;
void setPagePseudoClass(PagePseudoClass);

bool matchesPseudoElement() const;
bool isUserAgentPartPseudoElement() const;
bool isSiblingSelector() const;
bool isAttributeSelector() const;

Relation relation() const { return static_cast<Relation>(m_relation); }
void setRelation(Relation);

Match match() const { return static_cast<Match>(m_match); }
void setMatch(Match);

bool isLastInSelectorList() const { return m_isLastInSelectorList; }
void setLastInSelectorList() { m_isLastInSelectorList = true; }
void setNotLastInSelectorList() { m_isLastInSelectorList = false; }

bool isFirstInTagHistory() const { return m_isFirstInTagHistory; }
void setNotFirstInTagHistory() { m_isFirstInTagHistory = false; }

bool isLastInTagHistory() const { return m_isLastInTagHistory; }

// FIXME: These should ideally be private, but CSSSelectorList and StyleRule use them.
void setLastInSelectorList() { m_isLastInSelectorList = true; }
void setNotFirstInTagHistory() { m_isFirstInTagHistory = false; }
void setNotLastInTagHistory() { m_isLastInTagHistory = false; }
void setLastInTagHistory() { m_isLastInTagHistory = true; }

bool isForPage() const { return m_isForPage; }
void setForPage() { m_isForPage = true; }

void setImplicit() { m_isImplicit = true; }
// Implicit means that this selector is not author/UA written.
bool isImplicit() const { return m_isImplicit; }

private:
friend class MutableCSSSelector;

void setValue(const AtomString&, bool matchLowerCase = false);

void setAttribute(const QualifiedName&, AttributeMatchType);
void setNth(int a, int b);
void setArgument(const AtomString&);
void setArgumentList(FixedVector<PossiblyQuotedIdentifier>);
void setSelectorList(std::unique_ptr<CSSSelectorList>);

void setPseudoClass(PseudoClass);
void setPseudoElement(PseudoElement);
void setPagePseudoClass(PagePseudoClass);

void setRelation(Relation);
void setMatch(Match);

void setForPage() { m_isForPage = true; }
void setImplicit() { m_isImplicit = true; }

unsigned simpleSelectorSpecificityForPage() const;
CSSSelector* tagHistory() { return m_isLastInTagHistory ? nullptr : this + 1; }

unsigned m_relation : 4 { enumToUnderlyingType(Relation::DescendantSpace) };
mutable unsigned m_match : 5 { enumToUnderlyingType(Match::Unknown) };
mutable unsigned m_pseudoType : 8 { 0 }; // PseudoType.
Expand All @@ -216,9 +219,6 @@ class CSSSelector {
unsigned m_destructorHasBeenCalled : 1 { false };
#endif

unsigned simpleSelectorSpecificityForPage() const;
CSSSelector* tagHistory() { return m_isLastInTagHistory ? nullptr : this + 1; }

CSSSelector& operator=(const CSSSelector&) = delete;
CSSSelector(CSSSelector&&) = delete;

Expand Down
9 changes: 4 additions & 5 deletions Source/WebCore/css/parser/CSSParserImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void appendImplicitSelectorPseudoClassScopeIfNeeded(MutableCSSSelector& s
auto scopeSelector = makeUnique<MutableCSSSelector>();
scopeSelector->setMatch(CSSSelector::Match::PseudoClass);
scopeSelector->setPseudoClass(CSSSelector::PseudoClass::Scope);
scopeSelector->selector()->setImplicit();
scopeSelector->setImplicit();
selector.appendTagHistoryAsRelative(WTFMove(scopeSelector));
}
}
Expand Down Expand Up @@ -615,11 +615,10 @@ void CSSParserImpl::runInNewNestingContext(auto&& run)

Ref<StyleRuleBase> CSSParserImpl::createNestingParentRule()
{
CSSSelector nestingParentSelector;
nestingParentSelector.setMatch(CSSSelector::Match::NestingParent);
auto parserSelector = makeUnique<MutableCSSSelector>(nestingParentSelector);
auto nestingParentSelector = makeUnique<MutableCSSSelector>();
nestingParentSelector->setMatch(CSSSelector::Match::NestingParent);
Vector<std::unique_ptr<MutableCSSSelector>> selectorList;
selectorList.append(WTFMove(parserSelector));
selectorList.append(WTFMove(nestingParentSelector));
auto properties = createStyleProperties(topContext().m_parsedProperties, m_context.mode);
return StyleRuleWithNesting::create(WTFMove(properties), m_context.hasDocumentSecurityOrigin, CSSSelectorList { WTFMove(selectorList) }, { });
}
Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/css/parser/MutableCSSSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class MutableCSSSelector {
void setArgumentList(FixedVector<PossiblyQuotedIdentifier>);
void setSelectorList(std::unique_ptr<CSSSelectorList>);

void setImplicit() { m_selector->setImplicit(); }

CSSSelector::PseudoClass pseudoClass() const { return m_selector->pseudoClass(); }

bool matchesPseudoElement() const;
Expand Down

0 comments on commit 4a02d79

Please sign in to comment.