Skip to content

Commit

Permalink
Use dynamicDowncast<T> even more in html/
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=265226

Reviewed by Dan Glastonbury.

* Source/WebCore/html/HTMLFrameSetElement.cpp:
(WebCore::HTMLFrameSetElement::defaultEventHandler):
* Source/WebCore/html/HTMLObjectElement.cpp:
(WebCore::HTMLObjectElement::hasFallbackContent const):
* Source/WebCore/html/HTMLSlotElement.cpp:
(WebCore::flattenAssignedNodes):
* Source/WebCore/html/HTMLSourceElement.cpp:
(WebCore::HTMLSourceElement::insertedIntoAncestor):
(WebCore::HTMLSourceElement::removedFromAncestor):
* Source/WebCore/html/HTMLSummaryElement.cpp:
(WebCore::HTMLSummaryElement::detailsElement const):
(WebCore::isInSummaryInteractiveContent):
(WebCore::HTMLSummaryElement::defaultEventHandler):
* Source/WebCore/html/HTMLTableCellElement.cpp:
(WebCore::HTMLTableCellElement::attributeChanged):
(WebCore::HTMLTableCellElement::cellAbove const):
* Source/WebCore/html/HTMLTableCellElement.h:
(isType):
* Source/WebCore/html/HTMLTableColElement.cpp:
(WebCore::HTMLTableColElement::attributeChanged):
* Source/WebCore/html/HTMLTablePartElement.h:
(isType):
* Source/WebCore/html/HTMLTableRowElement.cpp:
(WebCore::findTable):
(WebCore::findRows):
* Source/WebCore/html/HTMLTableRowsCollection.cpp:
(WebCore::HTMLTableRowsCollection::rowAfter):
(WebCore::HTMLTableRowsCollection::lastRow):
* Source/WebCore/html/HTMLTableSectionElement.h:
(isType):
* Source/WebCore/html/HTMLTemplateElement.cpp:
(WebCore::HTMLTemplateElement::attachAsDeclarativeShadowRootIfNeeded):
* Source/WebCore/html/HTMLTextAreaElement.cpp:
(WebCore::HTMLTextAreaElement::defaultEventHandler):
* Source/WebCore/html/HTMLTextFormControlElement.cpp:
(WebCore::HTMLTextFormControlElement::selection const):
(WebCore::innerTextValueFrom):
(WebCore::HTMLTextFormControlElement::setInnerTextValue):
(WebCore::positionForIndex):
(WebCore::HTMLTextFormControlElement::indexForPosition const):
(WebCore::HTMLTextFormControlElement::valueWithHardLineBreaks const):
* Source/WebCore/html/HTMLTextFormControlElement.h:
(isType):
* Source/WebCore/html/InputType.cpp:
(WebCore::InputType::shouldSubmitImplicitly):
* Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp:
(WebCore::WebGLRenderingContextBase::useProgram):

Canonical link: https://commits.webkit.org/271060@main
  • Loading branch information
cdumez committed Nov 22, 2023
1 parent 67d8cd8 commit 9381085
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 94 deletions.
10 changes: 6 additions & 4 deletions Source/WebCore/html/HTMLFrameSetElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ void HTMLFrameSetElement::willAttachRenderers()

void HTMLFrameSetElement::defaultEventHandler(Event& event)
{
if (is<MouseEvent>(event) && !m_noresize && is<RenderFrameSet>(renderer())) {
if (downcast<RenderFrameSet>(*renderer()).userResize(downcast<MouseEvent>(event))) {
event.setDefaultHandled();
return;
if (auto* mouseEvent = dynamicDowncast<MouseEvent>(event); mouseEvent && !m_noresize) {
if (CheckedPtr renderFrameSet = dynamicDowncast<RenderFrameSet>(renderer())) {
if (renderFrameSet->userResize(*mouseEvent)) {
event.setDefaultHandled();
return;
}
}
}
HTMLElement::defaultEventHandler(event);
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/html/HTMLObjectElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ bool HTMLObjectElement::hasFallbackContent() const
{
for (RefPtr<Node> child = firstChild(); child; child = child->nextSibling()) {
// Ignore whitespace-only text, and <param> tags, any other content is fallback content.
if (is<Text>(*child)) {
if (!downcast<Text>(*child).data().containsOnly<isASCIIWhitespace>())
if (auto* textChild = dynamicDowncast<Text>(*child)) {
if (!textChild->data().containsOnly<isASCIIWhitespace>())
return true;
} else if (!is<HTMLParamElement>(*child))
return true;
Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/html/HTMLSlotElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ static void flattenAssignedNodes(Vector<Ref<Node>>& nodes, const HTMLSlotElement
auto* assignedNodes = slot.assignedNodes();
if (!assignedNodes) {
for (RefPtr<Node> child = slot.firstChild(); child; child = child->nextSibling()) {
if (is<HTMLSlotElement>(*child))
flattenAssignedNodes(nodes, downcast<HTMLSlotElement>(*child));
if (auto* slot = dynamicDowncast<HTMLSlotElement>(*child))
flattenAssignedNodes(nodes, *slot);
else if (is<Text>(*child) || is<Element>(*child))
nodes.append(*child);
}
Expand All @@ -131,8 +131,8 @@ static void flattenAssignedNodes(Vector<Ref<Node>>& nodes, const HTMLSlotElement
ASSERT_NOT_REACHED();
continue;
}
if (is<HTMLSlotElement>(*node) && downcast<HTMLSlotElement>(*node).containingShadowRoot())
flattenAssignedNodes(nodes, downcast<HTMLSlotElement>(*node));
if (auto* slot = dynamicDowncast<HTMLSlotElement>(*node); slot && slot->containingShadowRoot())
flattenAssignedNodes(nodes, *slot);
else
nodes.append(*node);
}
Expand Down
20 changes: 10 additions & 10 deletions Source/WebCore/html/HTMLSourceElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,24 @@ Node::InsertedIntoAncestorResult HTMLSourceElement::insertedIntoAncestor(Inserti
RefPtr<Element> parent = parentElement();
if (parent == &parentOfInsertedTree) {
#if ENABLE(VIDEO)
if (is<HTMLMediaElement>(*parent))
downcast<HTMLMediaElement>(*parent).sourceWasAdded(*this);
if (auto* mediaElement = dynamicDowncast<HTMLMediaElement>(*parent))
mediaElement->sourceWasAdded(*this);
else
#endif
#if ENABLE(MODEL_ELEMENT)
if (is<HTMLModelElement>(*parent))
downcast<HTMLModelElement>(*parent).sourcesChanged();
if (auto* modelElement = dynamicDowncast<HTMLModelElement>(*parent))
modelElement->sourcesChanged();
else
#endif
if (is<HTMLPictureElement>(*parent)) {
if (auto* pictureElement = dynamicDowncast<HTMLPictureElement>(*parent)) {
// The new source element only is a relevant mutation if it precedes any img element.
m_shouldCallSourcesChanged = true;
for (const Node* node = previousSibling(); node; node = node->previousSibling()) {
if (is<HTMLImageElement>(*node))
m_shouldCallSourcesChanged = false;
}
if (m_shouldCallSourcesChanged)
downcast<HTMLPictureElement>(*parent).sourcesChanged();
pictureElement->sourcesChanged();
}
}
return InsertedIntoAncestorResult::Done;
Expand All @@ -107,13 +107,13 @@ void HTMLSourceElement::removedFromAncestor(RemovalType removalType, ContainerNo
HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
if (!parentNode() && is<Element>(oldParentOfRemovedTree)) {
#if ENABLE(VIDEO)
if (is<HTMLMediaElement>(oldParentOfRemovedTree))
downcast<HTMLMediaElement>(oldParentOfRemovedTree).sourceWasRemoved(*this);
if (auto* medialElement = dynamicDowncast<HTMLMediaElement>(oldParentOfRemovedTree))
medialElement->sourceWasRemoved(*this);
else
#endif
#if ENABLE(MODEL_ELEMENT)
if (is<HTMLModelElement>(oldParentOfRemovedTree))
downcast<HTMLModelElement>(oldParentOfRemovedTree).sourcesChanged();
if (auto* model = dynamicDowncast<HTMLModelElement>(oldParentOfRemovedTree))
model->sourcesChanged();
else
#endif
if (m_shouldCallSourcesChanged) {
Expand Down
34 changes: 15 additions & 19 deletions Source/WebCore/html/HTMLSummaryElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot& root)

RefPtr<HTMLDetailsElement> HTMLSummaryElement::detailsElement() const
{
auto* parent = parentElement();
if (parent && is<HTMLDetailsElement>(*parent))
return downcast<HTMLDetailsElement>(parent);
if (auto* parent = dynamicDowncast<HTMLDetailsElement>(parentElement()))
return parent;
// Fallback summary element is in the shadow tree.
auto* host = shadowHost();
if (host && is<HTMLDetailsElement>(*host))
return downcast<HTMLDetailsElement>(host);
if (auto* details = dynamicDowncast<HTMLDetailsElement>(shadowHost()))
return details;
return nullptr;
}

Expand All @@ -100,10 +98,9 @@ bool HTMLSummaryElement::isActiveSummary() const

static bool isInSummaryInteractiveContent(EventTarget* target)
{
if (!is<Element>(target))
return false;
for (RefPtr element = downcast<Element>(target); element && !is<HTMLSummaryElement>(element); element = element->parentOrShadowHostElement()) {
if ((is<HTMLElement>(element) && downcast<HTMLElement>(element)->isInteractiveContent()) || is<SVGAElement>(element))
for (RefPtr element = dynamicDowncast<Element>(target); element && !is<HTMLSummaryElement>(element); element = element->parentOrShadowHostElement()) {
auto* htmlElement = dynamicDowncast<HTMLElement>(*element);
if ((htmlElement && htmlElement->isInteractiveContent()) || is<SVGAElement>(element))
return true;
}
return false;
Expand All @@ -130,29 +127,28 @@ void HTMLSummaryElement::defaultEventHandler(Event& event)
return;
}

if (is<KeyboardEvent>(event)) {
KeyboardEvent& keyboardEvent = downcast<KeyboardEvent>(event);
if (keyboardEvent.type() == eventNames.keydownEvent && keyboardEvent.keyIdentifier() == "U+0020"_s) {
if (auto* keyboardEvent = dynamicDowncast<KeyboardEvent>(event)) {
if (keyboardEvent->type() == eventNames.keydownEvent && keyboardEvent->keyIdentifier() == "U+0020"_s) {
setActive(true);
// No setDefaultHandled() - IE dispatches a keypress in this case.
return;
}
if (keyboardEvent.type() == eventNames.keypressEvent) {
switch (keyboardEvent.charCode()) {
if (keyboardEvent->type() == eventNames.keypressEvent) {
switch (keyboardEvent->charCode()) {
case '\r':
dispatchSimulatedClick(&event);
keyboardEvent.setDefaultHandled();
keyboardEvent->setDefaultHandled();
return;
case ' ':
// Prevent scrolling down the page.
keyboardEvent.setDefaultHandled();
keyboardEvent->setDefaultHandled();
return;
}
}
if (keyboardEvent.type() == eventNames.keyupEvent && keyboardEvent.keyIdentifier() == "U+0020"_s) {
if (keyboardEvent->type() == eventNames.keyupEvent && keyboardEvent->keyIdentifier() == "U+0020"_s) {
if (active())
dispatchSimulatedClick(&event);
keyboardEvent.setDefaultHandled();
keyboardEvent->setDefaultHandled();
return;
}
}
Expand Down
11 changes: 5 additions & 6 deletions Source/WebCore/html/HTMLTableCellElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ void HTMLTableCellElement::attributeChanged(const QualifiedName& name, const Ato
HTMLTablePartElement::attributeChanged(name, oldValue, newValue, attributeModificationReason);

if (name == rowspanAttr || name == colspanAttr) {
if (is<RenderTableCell>(renderer()))
downcast<RenderTableCell>(*renderer()).colSpanOrRowSpanChanged();
if (CheckedPtr tableCell = dynamicDowncast<RenderTableCell>(renderer()))
tableCell->colSpanOrRowSpanChanged();
}
}

Expand Down Expand Up @@ -208,12 +208,11 @@ void HTMLTableCellElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) c

HTMLTableCellElement* HTMLTableCellElement::cellAbove() const
{
auto* cellRenderer = renderer();
if (!is<RenderTableCell>(cellRenderer))
auto* tableCellRenderer = dynamicDowncast<RenderTableCell>(renderer());
if (!tableCellRenderer)
return nullptr;

auto& tableCellRenderer = downcast<RenderTableCell>(*cellRenderer);
auto* cellAboveRenderer = tableCellRenderer.table()->cellAbove(&tableCellRenderer);
auto* cellAboveRenderer = tableCellRenderer->table()->cellAbove(tableCellRenderer);
if (!cellAboveRenderer)
return nullptr;

Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/html/HTMLTableCellElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,9 @@ class HTMLTableCellElement final : public HTMLTablePartElement {

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::HTMLTableCellElement)
static bool isType(const WebCore::HTMLElement& element) { return element.hasTagName(WebCore::HTMLNames::tdTag) || element.hasTagName(WebCore::HTMLNames::thTag); }
static bool isType(const WebCore::Node& node) { return is<WebCore::HTMLElement>(node) && isType(downcast<WebCore::HTMLElement>(node)); }
static bool isType(const WebCore::Node& node)
{
auto* htmlElement = dynamicDowncast<WebCore::HTMLElement>(node);
return htmlElement && isType(*htmlElement);
}
SPECIALIZE_TYPE_TRAITS_END()
11 changes: 5 additions & 6 deletions Source/WebCore/html/HTMLTableColElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ void HTMLTableColElement::attributeChanged(const QualifiedName& name, const Atom

if (name == spanAttr) {
m_span = clampHTMLNonNegativeIntegerToRange(newValue, minSpan, maxSpan, defaultSpan);
if (is<RenderTableCol>(renderer()))
downcast<RenderTableCol>(*renderer()).updateFromElement();
if (CheckedPtr col = dynamicDowncast<RenderTableCol>(renderer()))
col->updateFromElement();
} else if (name == widthAttr) {
if (!newValue.isEmpty()) {
if (is<RenderTableCol>(renderer())) {
auto& col = downcast<RenderTableCol>(*renderer());
if (CheckedPtr col = dynamicDowncast<RenderTableCol>(renderer())) {
int newWidth = parseHTMLInteger(newValue).value_or(0);
if (newWidth != col.width())
col.setNeedsLayoutAndPrefWidthsRecalc();
if (newWidth != col->width())
col->setNeedsLayoutAndPrefWidthsRecalc();
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/html/HTMLTablePartElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@ class HTMLTablePartElement : public HTMLElement {

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::HTMLTablePartElement)
static bool isType(const WebCore::Element& element) { return element.isHTMLTablePartElement(); }
static bool isType(const WebCore::Node& node) { return is<WebCore::Element>(node) && isType(downcast<WebCore::Element>(node)); }
static bool isType(const WebCore::Node& node)
{
auto* element = dynamicDowncast<WebCore::Element>(node);
return element && isType(*element);
}
SPECIALIZE_TYPE_TRAITS_END()
16 changes: 8 additions & 8 deletions Source/WebCore/html/HTMLTableRowElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ Ref<HTMLTableRowElement> HTMLTableRowElement::create(const QualifiedName& tagNam
static inline RefPtr<HTMLTableElement> findTable(const HTMLTableRowElement& row)
{
auto* parent = row.parentNode();
if (is<HTMLTableElement>(parent))
return downcast<HTMLTableElement>(parent);
if (auto* table = dynamicDowncast<HTMLTableElement>(parent))
return table;
if (is<HTMLTableSectionElement>(parent)) {
auto* grandparent = parent->parentNode();
if (is<HTMLTableElement>(grandparent))
return downcast<HTMLTableElement>(grandparent);
if (auto* table = dynamicDowncast<HTMLTableElement>(grandparent))
return table;
}
return nullptr;
}
Expand All @@ -90,10 +90,10 @@ int HTMLTableRowElement::rowIndex() const
static inline RefPtr<HTMLCollection> findRows(const HTMLTableRowElement& row)
{
RefPtr parent = row.parentNode();
if (is<HTMLTableSectionElement>(parent))
return downcast<HTMLTableSectionElement>(*parent).rows();
if (is<HTMLTableElement>(parent))
return downcast<HTMLTableElement>(*parent).rows();
if (auto* section = dynamicDowncast<HTMLTableSectionElement>(parent.get()))
return section->rows();
if (auto* table = dynamicDowncast<HTMLTableElement>(parent.get()))
return table->rows();
return nullptr;
}

Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/html/HTMLTableRowsCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ HTMLTableRowElement* HTMLTableRowsCollection::rowAfter(HTMLTableElement& table,
else if (isInSection(*previous, tbodyTag))
child = ElementTraversal::nextSibling(*previous->parentNode());
for (; child; child = ElementTraversal::nextSibling(*child)) {
if (is<HTMLTableRowElement>(*child))
return downcast<HTMLTableRowElement>(child.get());
if (auto* row = dynamicDowncast<HTMLTableRowElement>(*child))
return row;
if (child->hasTagName(tbodyTag)) {
if (auto row = childrenOfType<HTMLTableRowElement>(*child).first())
return row;
Expand Down Expand Up @@ -135,8 +135,8 @@ HTMLTableRowElement* HTMLTableRowsCollection::lastRow(HTMLTableElement& table)
}

for (auto* child = ElementTraversal::lastChild(table); child; child = ElementTraversal::previousSibling(*child)) {
if (is<HTMLTableRowElement>(*child))
return downcast<HTMLTableRowElement>(child);
if (auto* row = dynamicDowncast<HTMLTableRowElement>(*child))
return row;
if (child->hasTagName(tbodyTag)) {
if (auto* row = childrenOfType<HTMLTableRowElement>(*child).last())
return row;
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/html/HTMLTableSectionElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ class HTMLTableSectionElement final : public HTMLTablePartElement {

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::HTMLTableSectionElement)
static bool isType(const WebCore::HTMLElement& element) { return element.hasTagName(WebCore::HTMLNames::theadTag) || element.hasTagName(WebCore::HTMLNames::tfootTag) || element.hasTagName(WebCore::HTMLNames::tbodyTag); }
static bool isType(const WebCore::Node& node) { return is<WebCore::HTMLElement>(node) && isType(downcast<WebCore::HTMLElement>(node)); }
static bool isType(const WebCore::Node& node)
{
auto* htmlElement = dynamicDowncast<WebCore::HTMLElement>(node);
return htmlElement && isType(*htmlElement);
}
SPECIALIZE_TYPE_TRAITS_END()
8 changes: 4 additions & 4 deletions Source/WebCore/html/HTMLTemplateElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ void HTMLTemplateElement::attachAsDeclarativeShadowRootIfNeeded(Element& host)
auto importedContent = document().importNode(content(), /* deep */ true).releaseReturnValue();
for (RefPtr<Node> node = NodeTraversal::next(importedContent), next; node; node = next) {
next = NodeTraversal::next(*node);
if (!is<HTMLTemplateElement>(*node))
continue;
if (RefPtr parentElement = node->parentElement())
downcast<HTMLTemplateElement>(*node).attachAsDeclarativeShadowRootIfNeeded(*parentElement);
if (auto* templateElement = dynamicDowncast<HTMLTemplateElement>(*node)) {
if (RefPtr parentElement = node->parentElement())
templateElement->attachAsDeclarativeShadowRootIfNeeded(*parentElement);
}
}

Ref shadowRoot = exceptionOrShadowRoot.releaseReturnValue();
Expand Down
10 changes: 6 additions & 4 deletions Source/WebCore/html/HTMLTextAreaElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ void HTMLTextAreaElement::updateFocusAppearance(SelectionRestorationMode restora

void HTMLTextAreaElement::defaultEventHandler(Event& event)
{
if (renderer() && (event.isMouseEvent() || event.type() == eventNames().blurEvent))
forwardEvent(event);
else if (renderer() && is<BeforeTextInsertedEvent>(event))
handleBeforeTextInsertedEvent(downcast<BeforeTextInsertedEvent>(event));
if (renderer()) {
if (event.isMouseEvent() || event.type() == eventNames().blurEvent)
forwardEvent(event);
else if (auto* insertedEvent = dynamicDowncast<BeforeTextInsertedEvent>(event))
handleBeforeTextInsertedEvent(*insertedEvent);
}

HTMLTextFormControlElement::defaultEventHandler(event);
}
Expand Down
Loading

0 comments on commit 9381085

Please sign in to comment.