Skip to content

Commit

Permalink
Add Element::hasClassName
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273925
rdar://127789854

Reviewed by Ryosuke Niwa.

This patch cherry-picks [1]. Simply this is good as a refactoring.

[1]: https://chromium-review.googlesource.com/c/chromium/src/+/5479574

* Source/WebCore/css/SelectorChecker.cpp:
(WebCore::SelectorChecker::checkOne const):
* Source/WebCore/dom/Element.h:
* Source/WebCore/dom/ElementInlines.h:
(WebCore::Element::hasClassName const):
* Source/WebCore/dom/ImageOverlay.cpp:
(WebCore::ImageOverlay::isDataDetectorResult):
(WebCore::ImageOverlay::updateSubtree):
* Source/WebCore/dom/SelectorQuery.cpp:
(WebCore::SelectorDataList::executeSingleClassNameSelectorData const):
* Source/WebCore/editing/TextManipulationController.cpp:
(WebCore::TextManipulationControllerExclusionRule::match const):
* Source/WebCore/page/Quirks.cpp:
(WebCore::Quirks::shouldTooltipPreventFromProceedingWithClick const):
(WebCore::Quirks::shouldBypassBackForwardCache const):
(WebCore::elementHasClassInClosestAncestors):
* Source/WebCore/style/StyleAdjuster.cpp:
(WebCore::Style::Adjuster::adjustForSiteSpecificQuirks const):

Canonical link: https://commits.webkit.org/278580@main
  • Loading branch information
Constellation committed May 9, 2024
1 parent f1fd721 commit f2ae3f6
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/css/SelectorChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ bool SelectorChecker::checkOne(CheckingContext& checkingContext, const LocalCont
return tagMatches(element, selector);

if (selector.match() == CSSSelector::Match::Class)
return element.hasClass() && element.classNames().contains(selector.value());
return element.hasClassName(selector.value());

if (selector.match() == CSSSelector::Match::Id) {
ASSERT(!selector.value().isNull());
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/dom/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ class Element : public ContainerNode {
inline bool hasClass() const;
inline bool hasName() const;
inline const SpaceSplitString& classNames() const;
inline bool hasClassName(const AtomString& className) const;

ScrollPosition savedLayerScrollPosition() const;
void setSavedLayerScrollPosition(const ScrollPosition&);
Expand Down
7 changes: 7 additions & 0 deletions Source/WebCore/dom/ElementInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ inline const SpaceSplitString& Element::classNames() const
return elementData()->classNames();
}

inline bool Element::hasClassName(const AtomString& className) const
{
if (!elementData())
return false;
return elementData()->classNames().contains(className);
}

inline unsigned Element::attributeCount() const
{
ASSERT(elementData());
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/dom/ImageOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static RefPtr<HTMLElement> imageOverlayHost(const Node& node)

bool isDataDetectorResult(const HTMLElement& element)
{
return imageOverlayHost(element) && element.hasClass() && element.classNames().contains(imageOverlayDataDetectorClass());
return imageOverlayHost(element) && element.hasClassName(imageOverlayDataDetectorClass());
}

std::optional<CharacterRange> characterRange(const VisibleSelection& selection)
Expand Down Expand Up @@ -269,7 +269,7 @@ static Elements updateSubtree(HTMLElement& element, const TextRecognitionResult&

auto& containerClass = controlsHost->mediaControlsContainerClassName();
for (auto& child : childrenOfType<HTMLDivElement>(shadowRoot.get())) {
if (child.hasClass() && child.classNames().contains(containerClass))
if (child.hasClassName(containerClass))
return &child;
}
ASSERT_NOT_REACHED();
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/SelectorQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ ALWAYS_INLINE void SelectorDataList::executeSingleClassNameSelectorData(const Co

const AtomString& className = selectorData.selector->value();
for (auto& element : descendantsOfType<Element>(const_cast<ContainerNode&>(rootNode))) {
if (element.hasClass() && element.classNames().contains(className)) {
if (element.hasClassName(className)) {
appendOutputForElement(output, element);
if constexpr (std::is_same_v<OutputType, Element*>)
return;
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/editing/TextManipulationController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ inline bool TextManipulationControllerExclusionRule::match(const Element& elemen
}, [&element] (AttributeRule rule) {
return equalIgnoringASCIICase(element.getAttribute(rule.name), rule.value);
}, [&element] (ClassRule rule) {
return element.hasClass() && element.classNames().contains(rule.className);
return element.hasClassName(rule.className);
});
}

Expand Down
12 changes: 7 additions & 5 deletions Source/WebCore/page/Quirks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ bool Quirks::shouldTooltipPreventFromProceedingWithClick(const Element& element)

if (!isDomain("covid.cdc.gov"_s))
return false;
return element.hasClass() && element.classNames().contains("tooltip"_s);

static MainThreadNeverDestroyed<const AtomString> tooltipClass("tooltip"_s);
return element.hasClassName(tooltipClass.get());
}

// google.com https://bugs.webkit.org/show_bug.cgi?id=223700
Expand Down Expand Up @@ -941,7 +943,7 @@ bool Quirks::shouldBypassBackForwardCache() const
static MainThreadNeverDestroyed<const AtomString> signInId("signIn"_s);
static MainThreadNeverDestroyed<const AtomString> loadingClass("loading"_s);
RefPtr signinButton = document->getElementById(signInId.get());
return signinButton && signinButton->classNames().contains(loadingClass.get());
return signinButton && signinButton->hasClassName(loadingClass.get());
}
}
}
Expand All @@ -954,7 +956,7 @@ bool Quirks::shouldBypassBackForwardCache() const
static MainThreadNeverDestroyed<const AtomString> googleDocsOverlayDivClass("docs-homescreen-freeze-el-full"_s);
auto* firstChildInBody = document->body() ? document->body()->firstChild() : nullptr;
if (RefPtr div = dynamicDowncast<HTMLDivElement>(firstChildInBody)) {
if (div->hasClass() && div->classNames().contains(googleDocsOverlayDivClass))
if (div->hasClassName(googleDocsOverlayDivClass))
return true;
}

Expand Down Expand Up @@ -1120,14 +1122,14 @@ bool Quirks::isMicrosoftTeamsRedirectURL(const URL& url)

static bool elementHasClassInClosestAncestors(const Element& element, const AtomString& className, unsigned distance)
{
if (element.hasClass() && element.classNames().contains(className))
if (element.hasClassName(className))
return true;

unsigned currentDistance = 0;
RefPtr ancestor = dynamicDowncast<Element>(element.parentNode());
while (ancestor && currentDistance < distance) {
++currentDistance;
if (ancestor->hasClass() && ancestor->classNames().contains(className))
if (ancestor->hasClassName(className))
return true;

ancestor = dynamicDowncast<Element>(ancestor->parentNode());
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/style/StyleAdjuster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ void Adjuster::adjustForSiteSpecificQuirks(RenderStyle& style) const
static MainThreadNeverDestroyed<const AtomString> instreamNativeVideoDivClass("instream-native-video--mobile"_s);
static MainThreadNeverDestroyed<const AtomString> videoElementID("vjs_video_3_html5_api"_s);

if (div->hasClass() && div->classNames().contains(instreamNativeVideoDivClass)) {
if (div->hasClassName(instreamNativeVideoDivClass)) {
RefPtr video = dynamicDowncast<HTMLVideoElement>(div->treeScope().getElementById(videoElementID));
if (video && video->isFullscreen())
style.setEffectiveDisplay(DisplayType::Block);
Expand All @@ -966,7 +966,7 @@ void Adjuster::adjustForSiteSpecificQuirks(RenderStyle& style) const
static MainThreadNeverDestroyed<const AtomString> playerClassName("top-player-video-element"_s);
bool isFullscreen = fullscreenManager->isFullscreen();
RefPtr video = dynamicDowncast<HTMLVideoElement>(m_element);
if (video && isFullscreen && video->hasClass() && video->classNames().contains(playerClassName) && style.objectFit() == ObjectFit::Fill)
if (video && isFullscreen && video->hasClassName(playerClassName) && style.objectFit() == ObjectFit::Fill)
style.setObjectFit(ObjectFit::Contain);
}
#endif
Expand Down

0 comments on commit f2ae3f6

Please sign in to comment.