Skip to content

Commit

Permalink
Rename some pseudo-class/element parsing functions
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=267084
rdar://120517489

Reviewed by Antti Koivisto.

- Add "Name" suffix to functions that parse names without the colons.
- Change the map lookup functions to use the `find` prefix instead of `parse`, to prevent folks from mistakingly using them to parse strings
- Rename `parseStandalonePseudoElement` to `parsePseudoElement` since it parses a pseudo-element with the colons.

The end result is:
parsePseudoElement -> parsePseudoElementName
parsePseudoElementString -> findPseudoElementName
parseStandalonePseudoElement -> parsePseudoElement
parsePseudoClassAndCompatibilityElementString -> findPseudoClassAndCompatibilityElementName

* Source/WebCore/animation/WebAnimationUtilities.cpp:
(WebCore::pseudoIdFromString):
* Source/WebCore/css/CSSSelector.cpp:
(WebCore::CSSSelector::parsePseudoElementName):
(WebCore::CSSSelector::parsePseudoElement):
(WebCore::CSSSelector::parseStandalonePseudoElement): Deleted.
* Source/WebCore/css/CSSSelector.h:
* Source/WebCore/css/SelectorPseudoTypeMap.h:
* Source/WebCore/css/parser/CSSSelectorParser.cpp:
(WebCore::CSSSelectorParser::containsUnknownWebKitPseudoElements):
* Source/WebCore/css/parser/MutableCSSSelector.cpp:
(WebCore::MutableCSSSelector::parsePseudoElementSelector):
(WebCore::MutableCSSSelector::parsePseudoClassSelector):
* Source/WebCore/css/process-css-pseudo-selectors.py:
* Source/WebCore/page/LocalDOMWindow.cpp:
(WebCore::LocalDOMWindow::getComputedStyle const):
(WebCore::LocalDOMWindow::getMatchedCSSRules const):

Canonical link: https://commits.webkit.org/272682@main
  • Loading branch information
nt1m committed Jan 5, 2024
1 parent 05f0643 commit 02d2839
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/animation/WebAnimationUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ std::optional<PseudoId> pseudoIdFromString(const String& pseudoElement)

// FIXME: This parserContext should include a document to get the proper settings.
CSSSelectorParserContext parserContext { CSSParserContext { HTMLStandardMode } };
return CSSSelector::parseStandalonePseudoElement(pseudoElement, parserContext);
return CSSSelector::parsePseudoElement(pseudoElement, parserContext);
}

AtomString animatablePropertyAsString(AnimatableCSSProperty property)
Expand Down
10 changes: 5 additions & 5 deletions Source/WebCore/css/CSSSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ PseudoId CSSSelector::pseudoId(PseudoElement type)
return PseudoId::None;
}

std::optional<CSSSelector::PseudoElement> CSSSelector::parsePseudoElement(StringView name, const CSSSelectorParserContext& context)
std::optional<CSSSelector::PseudoElement> CSSSelector::parsePseudoElementName(StringView name, const CSSSelectorParserContext& context)
{
if (name.isEmpty())
return std::nullopt;

auto type = parsePseudoElementString(name);
auto type = findPseudoElementName(name);
if (!type) {
// FIXME: Put all known UA parts in CSSPseudoSelectors.json and split out the unknown case (webkit.org/b/266947).
if (name.startsWithIgnoringASCIICase("-webkit-"_s))
Expand All @@ -336,7 +336,7 @@ std::optional<CSSSelector::PseudoElement> CSSSelector::parsePseudoElement(String
}

// FIXME: We should eventually deduplicate this with CSSSelectorParser::consumePseudo() somehow.
std::optional<PseudoId> CSSSelector::parseStandalonePseudoElement(const String& input, const CSSSelectorParserContext& context)
std::optional<PseudoId> CSSSelector::parsePseudoElement(const String& input, const CSSSelectorParserContext& context)
{
// FIXME: Add support for FunctionToken (webkit.org/b/264103).
auto tokenizer = CSSTokenizer { input };
Expand All @@ -348,7 +348,7 @@ std::optional<PseudoId> CSSSelector::parseStandalonePseudoElement(const String&
if (token.type() == IdentToken) {
if (!range.atEnd())
return std::nullopt;
auto pseudoClassOrElement = parsePseudoClassAndCompatibilityElementString(token.value());
auto pseudoClassOrElement = findPseudoClassAndCompatibilityElementName(token.value());
if (!pseudoClassOrElement.compatibilityPseudoElement)
return std::nullopt;
ASSERT(CSSSelector::isPseudoElementEnabled(*pseudoClassOrElement.compatibilityPseudoElement, token.value(), context));
Expand All @@ -359,7 +359,7 @@ std::optional<PseudoId> CSSSelector::parseStandalonePseudoElement(const String&
token = range.consume();
if (token.type() != IdentToken || !range.atEnd())
return std::nullopt;
if (auto pseudoElement = parsePseudoElement(token.value(), context))
if (auto pseudoElement = parsePseudoElementName(token.value(), context))
return pseudoId(*pseudoElement);
return std::nullopt;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/css/CSSSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class CSSSelector {
static PseudoId pseudoId(PseudoElement);
static bool isPseudoClassEnabled(PseudoClass, const CSSSelectorParserContext&);
static bool isPseudoElementEnabled(PseudoElement, StringView, const CSSSelectorParserContext&);
static std::optional<PseudoElement> parsePseudoElement(StringView, const CSSSelectorParserContext&);
static std::optional<PseudoId> parseStandalonePseudoElement(const String&, const CSSSelectorParserContext&);
static std::optional<PseudoId> parsePseudoElement(const String&, const CSSSelectorParserContext&);
static std::optional<PseudoElement> parsePseudoElementName(StringView, const CSSSelectorParserContext&);
static bool pseudoClassRequiresArgument(PseudoClass);
static bool pseudoElementRequiresArgument(PseudoElement);
static bool pseudoClassMayHaveArgument(PseudoClass);
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/css/SelectorPseudoTypeMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct PseudoClassOrCompatibilityPseudoElement {
std::optional<CSSSelector::PseudoElement> compatibilityPseudoElement;
};

PseudoClassOrCompatibilityPseudoElement parsePseudoClassAndCompatibilityElementString(StringView pseudoTypeString);
std::optional<CSSSelector::PseudoElement> parsePseudoElementString(StringView pseudoTypeString);
PseudoClassOrCompatibilityPseudoElement findPseudoClassAndCompatibilityElementName(StringView);
std::optional<CSSSelector::PseudoElement> findPseudoElementName(StringView);

} // namespace WebCore
2 changes: 1 addition & 1 deletion Source/WebCore/css/parser/CSSSelectorParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ bool CSSSelectorParser::containsUnknownWebKitPseudoElements(const CSSSelector& c
continue;

// FIXME: Stop attempting parsing once the unknown "-webkit" pseudo-elements are behind a different type. (webkit.org/b/266947)
if (!parsePseudoElementString(StringView(current->value())))
if (!findPseudoElementName(StringView(current->value())))
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/css/parser/MutableCSSSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::unique_ptr<MutableCSSSelector> MutableCSSSelector::parsePagePseudoSelector(

std::unique_ptr<MutableCSSSelector> MutableCSSSelector::parsePseudoElementSelector(StringView pseudoTypeString, const CSSSelectorParserContext& context)
{
auto pseudoType = CSSSelector::parsePseudoElement(pseudoTypeString, context);
auto pseudoType = CSSSelector::parsePseudoElementName(pseudoTypeString, context);
if (!pseudoType)
return nullptr;

Expand All @@ -71,7 +71,7 @@ std::unique_ptr<MutableCSSSelector> MutableCSSSelector::parsePseudoElementSelect

std::unique_ptr<MutableCSSSelector> MutableCSSSelector::parsePseudoClassSelector(StringView pseudoTypeString, const CSSSelectorParserContext& context)
{
auto pseudoType = parsePseudoClassAndCompatibilityElementString(pseudoTypeString);
auto pseudoType = findPseudoClassAndCompatibilityElementName(pseudoTypeString);
if (pseudoType.pseudoClass) {
if (!CSSSelector::isPseudoClassEnabled(*pseudoType.pseudoClass, context))
return nullptr;
Expand Down
28 changes: 14 additions & 14 deletions Source/WebCore/css/process-css-pseudo-selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,13 @@ def prefix_value(prefix, value):
def write_parsing_function_definitions_for_pseudo_class(self, writer):
longest_keyword_length = len(max(self.mapping, key=len))
writer.write_block("""
static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* parsePseudoClassAndCompatibilityElementString(const LChar* characters, unsigned length)
static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* findPseudoClassAndCompatibilityElementName(const LChar* characters, unsigned length)
{
return SelectorPseudoClassAndCompatibilityElementMapHash::in_word_set(reinterpret_cast<const char*>(characters), length);
}""")

writer.write_block("""
static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* parsePseudoClassAndCompatibilityElementString(const UChar* characters, unsigned length)
static inline const SelectorPseudoClassOrCompatibilityPseudoElementEntry* findPseudoClassAndCompatibilityElementName(const UChar* characters, unsigned length)
{{
constexpr unsigned maxKeywordLength = {};
LChar buffer[maxKeywordLength];
Expand All @@ -394,17 +394,17 @@ def write_parsing_function_definitions_for_pseudo_class(self, writer):
buffer[i] = static_cast<LChar>(character);
}}
return parsePseudoClassAndCompatibilityElementString(buffer, length);
return findPseudoClassAndCompatibilityElementName(buffer, length);
}}""".format(longest_keyword_length))

writer.write_block("""
PseudoClassOrCompatibilityPseudoElement parsePseudoClassAndCompatibilityElementString(StringView pseudoTypeString)
PseudoClassOrCompatibilityPseudoElement findPseudoClassAndCompatibilityElementName(StringView name)
{
const SelectorPseudoClassOrCompatibilityPseudoElementEntry* entry;
if (pseudoTypeString.is8Bit())
entry = parsePseudoClassAndCompatibilityElementString(pseudoTypeString.characters8(), pseudoTypeString.length());
if (name.is8Bit())
entry = findPseudoClassAndCompatibilityElementName(name.characters8(), name.length());
else
entry = parsePseudoClassAndCompatibilityElementString(pseudoTypeString.characters16(), pseudoTypeString.length());
entry = findPseudoClassAndCompatibilityElementName(name.characters16(), name.length());
if (entry)
return entry->pseudoTypes;
Expand All @@ -414,15 +414,15 @@ def write_parsing_function_definitions_for_pseudo_class(self, writer):
def write_parsing_function_definitions_for_pseudo_element(self, writer):
longest_keyword_length = len(max(self.mapping, key=len))
writer.write_block("""
static inline std::optional<CSSSelector::PseudoElement> parsePseudoElementString(const LChar* characters, unsigned length)
static inline std::optional<CSSSelector::PseudoElement> findPseudoElementName(const LChar* characters, unsigned length)
{
if (const SelectorPseudoTypeEntry* entry = SelectorPseudoElementMapHash::in_word_set(reinterpret_cast<const char*>(characters), length))
return entry->type;
return std::nullopt;
}""")

writer.write_block("""
static inline std::optional<CSSSelector::PseudoElement> parsePseudoElementString(const UChar* characters, unsigned length)
static inline std::optional<CSSSelector::PseudoElement> findPseudoElementName(const UChar* characters, unsigned length)
{{
constexpr unsigned maxKeywordLength = {};
LChar buffer[maxKeywordLength];
Expand All @@ -436,15 +436,15 @@ def write_parsing_function_definitions_for_pseudo_element(self, writer):
buffer[i] = static_cast<LChar>(character);
}}
return parsePseudoElementString(buffer, length);
return findPseudoElementName(buffer, length);
}}""".format(longest_keyword_length))

writer.write_block("""
std::optional<CSSSelector::PseudoElement> parsePseudoElementString(StringView pseudoTypeString)
std::optional<CSSSelector::PseudoElement> findPseudoElementName(StringView name)
{
if (pseudoTypeString.is8Bit())
return parsePseudoElementString(pseudoTypeString.characters8(), pseudoTypeString.length());
return parsePseudoElementString(pseudoTypeString.characters16(), pseudoTypeString.length());
if (name.is8Bit())
return findPseudoElementName(name.characters8(), name.length());
return findPseudoElementName(name.characters16(), name.length());
}""")

def write_end_ignore_warning(self, writer):
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/page/LocalDOMWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,7 @@ Ref<CSSStyleDeclaration> LocalDOMWindow::getComputedStyle(Element& element, cons
std::optional<PseudoId> pseudoId = PseudoId::None;
// FIXME: This does not work for pseudo-elements that take arguments (webkit.org/b/264103).
if (pseudoElt.startsWith(":"_s))
pseudoId = CSSSelector::parseStandalonePseudoElement(pseudoElt, CSSSelectorParserContext { element.document() });
pseudoId = CSSSelector::parsePseudoElement(pseudoElt, CSSSelectorParserContext { element.document() });
return CSSComputedStyleDeclaration::create(element, pseudoId);
}

Expand All @@ -1696,7 +1696,7 @@ RefPtr<CSSRuleList> LocalDOMWindow::getMatchedCSSRules(Element* element, const S

// FIXME: This parser context won't get the right settings without a document.
auto parserContext = document() ? CSSSelectorParserContext { *document() } : CSSSelectorParserContext { CSSParserContext { HTMLStandardMode } };
auto optionalPseudoId = CSSSelector::parseStandalonePseudoElement(pseudoElement, parserContext);
auto optionalPseudoId = CSSSelector::parsePseudoElement(pseudoElement, parserContext);
if (!optionalPseudoId && !pseudoElement.isEmpty())
return nullptr;
auto pseudoId = optionalPseudoId ? *optionalPseudoId : PseudoId::None;
Expand Down

0 comments on commit 02d2839

Please sign in to comment.