Skip to content

Commit

Permalink
Adopt dynamicDowncast<>() more in html/
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=265122

Reviewed by Dan Glastonbury.

* Source/WebCore/html/HTMLTrackElement.cpp:
(WebCore::HTMLTrackElement::insertedIntoAncestor):
(WebCore::HTMLTrackElement::removedFromAncestor):
(WebCore::HTMLTrackElement::mediaElement const):
* Source/WebCore/html/HTMLVideoElement.h:
(isType):
* Source/WebCore/html/ImageDocument.cpp:
(WebCore::ImageEventListener::handleEvent):
* Source/WebCore/html/ImageDocument.h:
(isType):
* Source/WebCore/html/ImageInputType.cpp:
(WebCore::ImageInputType::handleDOMActivateEvent):
(WebCore::ImageInputType::attributeChanged):
* Source/WebCore/html/LabelsNodeList.cpp:
(WebCore::LabelsNodeList::elementMatches const):
* Source/WebCore/html/LazyLoadFrameObserver.cpp:
* Source/WebCore/html/LazyLoadImageObserver.cpp:
* Source/WebCore/html/MediaDocument.cpp:
(WebCore::descendantVideoElement):
(WebCore::ancestorVideoElement):
(WebCore::MediaDocument::defaultEventHandler):
* Source/WebCore/html/MediaDocument.h:
(isType):
* Source/WebCore/html/MediaElementSession.cpp:
(WebCore::MediaElementSession::requiresFullscreenForVideoPlayback const):
* Source/WebCore/html/ModelDocument.h:
(isType):
* Source/WebCore/html/OffscreenCanvas.cpp:
(WebCore::OffscreenCanvas::setSize):
(WebCore::OffscreenCanvas::createContextWebGL):
(WebCore::OffscreenCanvas::getContext):
(WebCore::OffscreenCanvas::transferToImageBitmap):
(WebCore::OffscreenCanvas::securityOrigin const):
(WebCore::OffscreenCanvas::reset):
* Source/WebCore/html/PDFDocument.h:
(isType):
* Source/WebCore/html/PluginDocument.h:
(isType):
* Source/WebCore/html/RadioInputType.cpp:
(WebCore::RadioInputType::handleKeydownEvent):
(WebCore::RadioInputType::isKeyboardFocusable const):
* Source/WebCore/html/RadioNodeList.cpp:
(WebCore::nonEmptyRadioButton):
(WebCore::RadioNodeList::elementMatches const):
* Source/WebCore/html/RangeInputType.cpp:
(WebCore::RangeInputType::handleMouseDownEvent):
* Source/WebCore/html/SearchInputType.cpp:
(WebCore::SearchInputType::addSearchResult):
(WebCore::SearchInputType::didSetValueByUserEdit):
* Source/WebCore/html/TextFieldInputType.cpp:
(WebCore::TextFieldInputType::shouldSubmitImplicitly):
* Source/WebCore/html/ValidatedFormListedElement.cpp:
(WebCore::addInvalidElementToAncestorFromInsertionPoint):
(WebCore::removeInvalidElementToAncestorFromInsertionPoint):
(WebCore::ValidatedFormListedElement::computeIsDisabledByFieldsetAncestor const):
* Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp:
(WebCore::size):
(WebCore::CanvasRenderingContext2DBase::drawImage):
(WebCore::CanvasRenderingContext2DBase::getImageData const):
* Source/WebCore/html/canvas/CanvasStyle.cpp:
(WebCore::parseColor):
(WebCore::currentColor):
* Source/WebCore/html/canvas/GPUBasedCanvasRenderingContext.cpp:
(WebCore::GPUBasedCanvasRenderingContext::htmlCanvas const):
* Source/WebCore/html/canvas/GPUCanvasContextCocoa.mm:
(WebCore::GPUCanvasContextCocoa::htmlOrOffscreenCanvas const):
* Source/WebCore/html/canvas/ImageBitmapRenderingContext.cpp:
(WebCore::ImageBitmapRenderingContext::canvas):
* Source/WebCore/html/canvas/PlaceholderRenderingContext.cpp:
(WebCore::PlaceholderRenderingContext::canvas const):
* Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp:
(WebCore::WebGLRenderingContextBase::canvas):
(WebCore::WebGLRenderingContextBase::offscreenCanvas):
(WebCore::WebGLRenderingContextBase::useProgram):
* Source/WebCore/html/parser/HTMLConstructionSite.cpp:
(WebCore::shouldUseLengthLimit):
(WebCore::HTMLConstructionSite::insertHTMLTemplateElement):
(WebCore::HTMLConstructionSite::ownerDocumentForCurrentNode):
(WebCore::HTMLConstructionSite::createHTMLElementOrFindCustomElementInterface):
* Source/WebCore/html/parser/HTMLStackItem.h:
(WebCore::HTMLStackItem::HTMLStackItem):
* Source/WebCore/html/parser/HTMLTreeBuilder.cpp:
(WebCore::HTMLTreeBuilder::HTMLTreeBuilder):
(WebCore::HTMLTreeBuilder::processTemplateEndTag):
(WebCore::disallowTelephoneNumberParsing):
* Source/WebCore/html/shadow/AutoFillButtonElement.cpp:
(WebCore::AutoFillButtonElement::defaultEventHandler):
* Source/WebCore/html/shadow/DataListButtonElement.cpp:
(WebCore::DataListButtonElement::defaultEventHandler):
* Source/WebCore/html/shadow/DateTimeFieldElement.cpp:
(WebCore::DateTimeFieldElement::defaultEventHandler):
* Source/WebCore/html/shadow/MediaControlTextTrackContainerElement.cpp:
(WebCore::MediaControlTextTrackContainerElement::updateDisplay):
(WebCore::MediaControlTextTrackContainerElement::updateVideoDisplaySize):
* Source/WebCore/html/shadow/SliderThumbElement.cpp:
(WebCore::SliderThumbElement::defaultEventHandler):
* Source/WebCore/html/shadow/SliderThumbElement.h:
(isType):
* Source/WebCore/html/shadow/SpinButtonElement.cpp:
(WebCore::SpinButtonElement::defaultEventHandler):
(WebCore::SpinButtonElement::forwardEvent):
* Source/WebCore/html/shadow/SpinButtonElement.h:
(isType):
* Source/WebCore/html/shadow/TextControlInnerElements.cpp:
(WebCore::isStrongPasswordTextField):
(WebCore::TextControlPlaceholderElement::resolveCustomStyle):
(WebCore::SearchFieldResultsButtonElement::defaultEventHandler):
(WebCore::SearchFieldCancelButtonElement::defaultEventHandler):
(WebCore::SearchFieldCancelButtonElement::willRespondToMouseClickEventsWithEditability const):
* Source/WebCore/html/shadow/TextControlInnerElements.h:
(isType):
* Source/WebCore/html/shadow/TextPlaceholderElement.cpp:
(WebCore::TextPlaceholderElement::insertedIntoAncestor):
(WebCore::TextPlaceholderElement::removedFromAncestor):
* Source/WebCore/html/shadow/TextPlaceholderElement.h:
(isType):
* Source/WebCore/html/track/TextTrackCue.cpp:
(WebCore::tagPseudoObjects):
(WebCore::removePseudoAttributes):
* Source/WebCore/html/track/TextTrackCueGeneric.cpp:
(WebCore::TextTrackCueGeneric::isOrderedBefore const):
(WebCore::TextTrackCueGeneric::isPositionedAbove const):
* Source/WebCore/html/track/TextTrackList.cpp:
(WebCore::TextTrackList::getTrackIndex):
(WebCore::TextTrackList::append):
* Source/WebCore/html/track/VTTCue.cpp:
(WebCore::VTTCueBox::applyCSSPropertiesWithRegion):
(WebCore::VTTCueBox::applyCSSProperties):
(WebCore::copyWebVTTNodeToDOMTree):
(WebCore::VTTCue::markFutureAndPastNodes):

Canonical link: https://commits.webkit.org/270996@main
  • Loading branch information
cdumez committed Nov 21, 2023
1 parent bb71e93 commit a63f311
Show file tree
Hide file tree
Showing 47 changed files with 326 additions and 281 deletions.
19 changes: 10 additions & 9 deletions Source/WebCore/html/HTMLTrackElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ Node::InsertedIntoAncestorResult HTMLTrackElement::insertedIntoAncestor(Insertio
{
HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);

if (parentNode() == &parentOfInsertedTree && is<HTMLMediaElement>(parentOfInsertedTree)) {
downcast<HTMLMediaElement>(parentOfInsertedTree).didAddTextTrack(*this);
scheduleLoad();
if (parentNode() == &parentOfInsertedTree) {
if (auto* mediaElement = dynamicDowncast<HTMLMediaElement>(parentOfInsertedTree)) {
mediaElement->didAddTextTrack(*this);
scheduleLoad();
}
}

return InsertedIntoAncestorResult::Done;
Expand All @@ -100,8 +102,10 @@ void HTMLTrackElement::removedFromAncestor(RemovalType removalType, ContainerNod
{
HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree);

if (!parentNode() && is<HTMLMediaElement>(oldParentOfRemovedTree))
downcast<HTMLMediaElement>(oldParentOfRemovedTree).didRemoveTextTrack(*this);
if (!parentNode()) {
if (auto* mediaElement = dynamicDowncast<HTMLMediaElement>(oldParentOfRemovedTree))
mediaElement->didRemoveTextTrack(*this);
}
}

void HTMLTrackElement::attributeChanged(const QualifiedName& name, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason attributeModificationReason)
Expand Down Expand Up @@ -307,10 +311,7 @@ void HTMLTrackElement::textTrackModeChanged(TextTrack&)

RefPtr<HTMLMediaElement> HTMLTrackElement::mediaElement() const
{
RefPtr parent = parentElement();
if (!is<HTMLMediaElement>(parent))
return nullptr;
return downcast<HTMLMediaElement>(parent.get());
return dynamicDowncast<HTMLMediaElement>(parentElement());
}

const char* HTMLTrackElement::activeDOMObjectName() const
Expand Down
9 changes: 6 additions & 3 deletions Source/WebCore/html/HTMLVideoElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ class HTMLVideoElement final : public HTMLMediaElement, public Supplementable<HT
} // namespace WebCore

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::HTMLVideoElement)
static bool isType(const WebCore::HTMLMediaElement& element) { return element.hasTagName(WebCore::HTMLNames::videoTag); }
static bool isType(const WebCore::Element& element) { return is<WebCore::HTMLMediaElement>(element) && isType(downcast<WebCore::HTMLMediaElement>(element)); }
static bool isType(const WebCore::Node& node) { return is<WebCore::HTMLMediaElement>(node) && isType(downcast<WebCore::HTMLMediaElement>(node)); }
static bool isType(const WebCore::HTMLElement& element) { return element.hasTagName(WebCore::HTMLNames::videoTag); }
static bool isType(const WebCore::Node& node)
{
auto* element = dynamicDowncast<WebCore::HTMLElement>(node);
return element && isType(*element);
}
SPECIALIZE_TYPE_TRAITS_END()

#endif // ENABLE(VIDEO)
6 changes: 2 additions & 4 deletions Source/WebCore/html/ImageDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,8 @@ void ImageDocument::imageClicked(int x, int y)

void ImageEventListener::handleEvent(ScriptExecutionContext&, Event& event)
{
if (event.type() == eventNames().clickEvent && is<MouseEvent>(event)) {
MouseEvent& mouseEvent = downcast<MouseEvent>(event);
m_document.imageClicked(mouseEvent.offsetX(), mouseEvent.offsetY());
}
if (auto* mouseEvent = dynamicDowncast<MouseEvent>(event); mouseEvent && event.type() == eventNames().clickEvent)
m_document.imageClicked(mouseEvent->offsetX(), mouseEvent->offsetY());
}

bool ImageEventListener::operator==(const EventListener& other) const
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/html/ImageDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,9 @@ class ImageDocument final : public HTMLDocument {

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ImageDocument)
static bool isType(const WebCore::Document& document) { return document.isImageDocument(); }
static bool isType(const WebCore::Node& node) { return is<WebCore::Document>(node) && isType(downcast<WebCore::Document>(node)); }
static bool isType(const WebCore::Node& node)
{
auto* document = dynamicDowncast<WebCore::Document>(node);
return document && isType(*document);
}
SPECIALIZE_TYPE_TRAITS_END()
11 changes: 5 additions & 6 deletions Source/WebCore/html/ImageInputType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ void ImageInputType::handleDOMActivateEvent(Event& event)
m_clickLocation = IntPoint();
if (event.underlyingEvent()) {
Event& underlyingEvent = *event.underlyingEvent();
if (is<MouseEvent>(underlyingEvent)) {
MouseEvent& mouseEvent = downcast<MouseEvent>(underlyingEvent);
if (!mouseEvent.isSimulated())
m_clickLocation = IntPoint(mouseEvent.offsetX(), mouseEvent.offsetY());
if (auto* mouseEvent = dynamicDowncast<MouseEvent>(underlyingEvent)) {
if (!mouseEvent->isSimulated())
m_clickLocation = IntPoint(mouseEvent->offsetX(), mouseEvent->offsetY());
}
}

Expand All @@ -119,8 +118,8 @@ void ImageInputType::attributeChanged(const QualifiedName& name)
if (name == altAttr) {
if (auto* element = this->element()) {
auto* renderer = element->renderer();
if (is<RenderImage>(renderer))
downcast<RenderImage>(*renderer).updateAltText();
if (auto* renderImage = dynamicDowncast<RenderImage>(renderer))
renderImage->updateAltText();
}
} else if (name == srcAttr) {
if (auto* element = this->element()) {
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/html/LabelsNodeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ LabelsNodeList::~LabelsNodeList()

bool LabelsNodeList::elementMatches(Element& testNode) const
{
return is<HTMLLabelElement>(testNode) && downcast<HTMLLabelElement>(testNode).control() == &ownerNode();
auto* label = dynamicDowncast<HTMLLabelElement>(testNode);
return label && label->control() == &ownerNode();
}

} // namespace WebCore
7 changes: 3 additions & 4 deletions Source/WebCore/html/LazyLoadFrameObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ class LazyFrameLoadIntersectionObserverCallback final : public IntersectionObser
for (auto& entry : entries) {
if (!entry->isIntersecting())
continue;
auto* element = entry->target();
if (is<HTMLIFrameElement>(element)) {
downcast<HTMLIFrameElement>(*element).lazyLoadFrameObserver().unobserve();
downcast<HTMLIFrameElement>(*element).loadDeferredFrame();
if (RefPtr iframe = dynamicDowncast<HTMLIFrameElement>(entry->target())) {
iframe->lazyLoadFrameObserver().unobserve();
iframe->loadDeferredFrame();
}
}
return { };
Expand Down
5 changes: 2 additions & 3 deletions Source/WebCore/html/LazyLoadImageObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ class LazyImageLoadIntersectionObserverCallback final : public IntersectionObser
for (auto& entry : entries) {
if (!entry->isIntersecting())
continue;
auto* element = entry->target();
if (is<HTMLImageElement>(element)) {
downcast<HTMLImageElement>(*element).loadDeferredImage();
if (RefPtr element = dynamicDowncast<HTMLImageElement>(entry->target())) {
element->loadDeferredImage();
element->document().lazyLoadImageObserver().unobserve(*element, element->document());
}
}
Expand Down
32 changes: 16 additions & 16 deletions Source/WebCore/html/MediaDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,20 @@ Ref<DocumentParser> MediaDocument::createParser()

static inline HTMLVideoElement* descendantVideoElement(ContainerNode& node)
{
if (is<HTMLVideoElement>(node))
return downcast<HTMLVideoElement>(&node);
if (auto* video = dynamicDowncast<HTMLVideoElement>(node))
return video;

return descendantsOfType<HTMLVideoElement>(node).first();
}

#if !ENABLE(MODERN_MEDIA_CONTROLS)
static inline HTMLVideoElement* ancestorVideoElement(Node* node)
{
while (node && !is<HTMLVideoElement>(*node))
node = node->parentOrShadowHostNode();

return downcast<HTMLVideoElement>(node);
for (; node; node = node->parentOrShadowHostNode()) {
if (auto* video = dynamicDowncast<HTMLVideoElement>(node))
return video;
}
return nullptr;
}
#endif

Expand All @@ -179,11 +180,11 @@ void MediaDocument::defaultEventHandler(Event& event)
#if !ENABLE(MODERN_MEDIA_CONTROLS)
// Match the default Quicktime plugin behavior to allow
// clicking and double-clicking to pause and play the media.
if (!is<Node>(event.target()))
auto* targetNode = dynamicDowncast<Node>(*event.target());
if (!targetNode)
return;
auto& targetNode = downcast<Node>(*event.target());

if (RefPtr video = ancestorVideoElement(&targetNode)) {
if (RefPtr video = ancestorVideoElement(targetNode)) {
if (event.type() == eventNames().clickEvent) {
if (!video->canPlay()) {
video->pause();
Expand All @@ -197,23 +198,22 @@ void MediaDocument::defaultEventHandler(Event& event)
}
}

if (!is<ContainerNode>(targetNode))
auto* targetContainer = dynamicDowncast<ContainerNode>(*targetNode);
if (!targetContainer)
return;
auto& targetContainer = downcast<ContainerNode>(targetNode);

if (event.type() == eventNames().keydownEvent && is<KeyboardEvent>(event)) {
RefPtr video = descendantVideoElement(targetContainer);
if (auto* keyboardEvent = dynamicDowncast<KeyboardEvent>(event); keyboardEvent && event.type() == eventNames().keydownEvent) {
RefPtr video = descendantVideoElement(*targetContainer);
if (!video)
return;

auto& keyboardEvent = downcast<KeyboardEvent>(event);
if (keyboardEvent.keyIdentifier() == "U+0020"_s) { // space
if (keyboardEvent->keyIdentifier() == "U+0020"_s) { // space
if (video->paused()) {
if (video->canPlay())
video->play();
} else
video->pause();
keyboardEvent.setDefaultHandled();
keyboardEvent->setDefaultHandled();
}
}
#else // !ENABLE(MODERN_MEDIA_CONTROLS)
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/html/MediaDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class MediaDocument final : public HTMLDocument {

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::MediaDocument)
static bool isType(const WebCore::Document& document) { return document.isMediaDocument(); }
static bool isType(const WebCore::Node& node) { return is<WebCore::Document>(node) && isType(downcast<WebCore::Document>(node)); }
static bool isType(const WebCore::Node& node)
{
auto* document = dynamicDowncast<WebCore::Document>(node);
return document && isType(*document);
}
SPECIALIZE_TYPE_TRAITS_END()

#endif // ENABLE(VIDEO)
1 change: 0 additions & 1 deletion Source/WebCore/html/MediaElementSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,6 @@ bool MediaElementSession::requiresFullscreenForVideoPlayback() const
return false;

if (m_element.document().isMediaDocument()) {
ASSERT(is<HTMLVideoElement>(m_element));
const HTMLVideoElement& videoElement = *downcast<const HTMLVideoElement>(&m_element);
if (m_element.readyState() < HTMLVideoElement::HAVE_METADATA || !videoElement.hasEverHadVideo())
return false;
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/html/ModelDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ class ModelDocument final : public HTMLDocument {

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ModelDocument)
static bool isType(const WebCore::Document& document) { return document.isModelDocument(); }
static bool isType(const WebCore::Node& node) { return is<WebCore::Document>(node) && isType(downcast<WebCore::Document>(node)); }
static bool isType(const WebCore::Node& node)
{
auto* document = dynamicDowncast<WebCore::Document>(node);
return document && isType(*document);
}
SPECIALIZE_TYPE_TRAITS_END()

#endif // ENABLE(MODEL_ELEMENT)
Loading

0 comments on commit a63f311

Please sign in to comment.