Skip to content

Commit

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

Reviewed by Jean-Yves Avenard.

* Source/WebCore/dom/Document.cpp:
(WebCore::Document::updateLayoutIfDimensionsOutOfDate):
(WebCore::Document::beginLoadingFontSoon):
(WebCore::Document::enqueueOverflowEvent):
* Source/WebCore/dom/Document.h:
(isType):
* Source/WebCore/dom/Element.cpp:
(WebCore::Node::setCustomElementState):
(WebCore::Element::ensureFormAssociatedCustomElement):
* Source/WebCore/dom/Element.h:
(isType):
* Source/WebCore/dom/ElementAncestorIteratorInlines.h:
(WebCore::lineageOfType):
* Source/WebCore/dom/ElementData.cpp:
(WebCore::ElementData::destroy):
* Source/WebCore/dom/ElementData.h:
(WebCore::ElementData::length const):
(WebCore::ElementData::attributeBase const):
(WebCore::ElementData::presentationalHintStyle const):
(WebCore::ElementData::attributesIterator const):
* Source/WebCore/dom/ElementInlines.h:
(WebCore::Node::hasAttributes const):
(WebCore::Node::attributes const):
* Source/WebCore/dom/ElementIteratorInlines.h:
(WebCore::findElementAncestorOfType):
* Source/WebCore/dom/ElementRareData.h:
(WebCore::Node::shadowRoot const):
* Source/WebCore/dom/EventDispatcher.cpp:
(WebCore::isInShadowTree):
(WebCore::shouldSuppressEventDispatchInDOM):
(WebCore::findInputElementInEventPath):
* Source/WebCore/dom/TreeScopeOrderedMap.cpp:
(WebCore::TreeScopeOrderedMap::get const):
* Source/WebCore/dom/XMLDocument.h:
(isType):
* Source/WebCore/editing/AlternativeTextController.cpp:
(WebCore::AlternativeTextController::canEnableAutomaticSpellingCorrection const):
* Source/WebCore/editing/ApplyBlockElementCommand.cpp:
(WebCore::ApplyBlockElementCommand::endOfNextParagraphSplittingTextNodesIfNeeded):

Canonical link: https://commits.webkit.org/270959@main
  • Loading branch information
cdumez committed Nov 20, 2023
1 parent 01fd632 commit b8691e2
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 60 deletions.
15 changes: 7 additions & 8 deletions Source/WebCore/dom/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2527,11 +2527,12 @@ bool Document::updateLayoutIfDimensionsOutOfDate(Element& element, OptionSet<Dim

// If a block contains floats and the child's height isn't specified, then
// give up also, since our height could end up being influenced by the floats.
if (checkingLogicalHeight && !hasSpecifiedLogicalHeight && currentBox->isRenderBlockFlow()) {
CheckedRef currentBlockFlow = downcast<RenderBlockFlow>(*currentBox);
if (currentBlockFlow->containsFloats() && previousBox && !previousBox->isFloatingOrOutOfFlowPositioned()) {
requireFullLayout = true;
break;
if (checkingLogicalHeight && !hasSpecifiedLogicalHeight) {
if (CheckedPtr currentBlockFlow = dynamicDowncast<RenderBlockFlow>(*currentBox)) {
if (currentBlockFlow->containsFloats() && previousBox && !previousBox->isFloatingOrOutOfFlowPositioned()) {
requireFullLayout = true;
break;
}
}
}

Expand Down Expand Up @@ -3289,7 +3290,6 @@ std::unique_ptr<FontLoadRequest> Document::fontLoadRequest(const String& url, bo

void Document::beginLoadingFontSoon(FontLoadRequest& request)
{
ASSERT(is<CachedFontLoadRequest>(request));
CachedResourceHandle font = downcast<CachedFontLoadRequest>(request).cachedFont();
m_fontLoader->beginLoadingFontSoon(*font);
}
Expand Down Expand Up @@ -5600,8 +5600,7 @@ void Document::enqueueOverflowEvent(Ref<Event>&& event)
// FIXME: This event is totally unspecified.
RefPtr target = event->target();
RELEASE_ASSERT(target);
RELEASE_ASSERT(is<Node>(target));
eventLoop().queueTask(TaskSource::DOMManipulation, [protectedTarget = GCReachableRef<Node>(downcast<Node>(*target)), event = WTFMove(event)] {
eventLoop().queueTask(TaskSource::DOMManipulation, [protectedTarget = GCReachableRef<Node>(checkedDowncast<Node>(*target)), event = WTFMove(event)] {
protectedTarget->dispatchEvent(event);
});
}
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/dom/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -2498,5 +2498,9 @@ WTF::TextStream& operator<<(WTF::TextStream&, const Document&);
SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::Document)
static bool isType(const WebCore::ScriptExecutionContext& context) { return context.isDocument(); }
static bool isType(const WebCore::Node& node) { return node.isDocumentNode(); }
static bool isType(const WebCore::EventTarget& target) { return is<WebCore::Node>(target) && isType(downcast<WebCore::Node>(target)); }
static bool isType(const WebCore::EventTarget& target)
{
auto* node = dynamicDowncast<WebCore::Node>(target);
return node && isType(*node);
}
SPECIALIZE_TYPE_TRAITS_END()
7 changes: 3 additions & 4 deletions Source/WebCore/dom/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3004,8 +3004,7 @@ ShadowRoot& Element::createUserAgentShadowRoot()

inline void Node::setCustomElementState(CustomElementState state)
{
RELEASE_ASSERT(is<Element>(this));
Style::PseudoClassChangeInvalidation styleInvalidation(downcast<Element>(*this),
Style::PseudoClassChangeInvalidation styleInvalidation(checkedDowncast<Element>(*this),
CSSSelector::PseudoClassType::Defined,
state == CustomElementState::Custom || state == CustomElementState::Uncustomized
);
Expand Down Expand Up @@ -5421,10 +5420,10 @@ void Element::setAttributeStyleMap(Ref<StylePropertyMap>&& map)

void Element::ensureFormAssociatedCustomElement()
{
RELEASE_ASSERT(is<HTMLMaybeFormAssociatedCustomElement>(*this));
auto& customElement = checkedDowncast<HTMLMaybeFormAssociatedCustomElement>(*this);
auto& data = ensureElementRareData();
if (!data.formAssociatedCustomElement())
data.setFormAssociatedCustomElement(makeUniqueWithoutRefCountedCheck<FormAssociatedCustomElement>(downcast<HTMLMaybeFormAssociatedCustomElement>(*this)));
data.setFormAssociatedCustomElement(makeUniqueWithoutRefCountedCheck<FormAssociatedCustomElement>(customElement));
}

FormAssociatedCustomElement& Element::formAssociatedCustomElementUnsafe() const
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/dom/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -950,5 +950,9 @@ inline bool isInTopLayerOrBackdrop(const RenderStyle&, const Element*);

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::Element)
static bool isType(const WebCore::Node& node) { return node.isElementNode(); }
static bool isType(const WebCore::EventTarget& target) { return is<WebCore::Node>(target) && isType(downcast<WebCore::Node>(target)); }
static bool isType(const WebCore::EventTarget& target)
{
auto* node = dynamicDowncast<WebCore::Node>(target);
return node && isType(*node);
}
SPECIALIZE_TYPE_TRAITS_END()
16 changes: 8 additions & 8 deletions Source/WebCore/dom/ElementAncestorIteratorInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ template<> inline ElementAncestorRange<Element> lineageOfType<Element>(Element&
template <typename ElementType>
inline ElementAncestorRange<ElementType> lineageOfType(Element& first)
{
if (is<ElementType>(first))
return ElementAncestorRange<ElementType>(&downcast<ElementType>(first));
if (auto* element = dynamicDowncast<ElementType>(first))
return ElementAncestorRange<ElementType>(element);
return ancestorsOfType<ElementType>(first);
}

Expand All @@ -70,24 +70,24 @@ template<> inline ElementAncestorRange<const Element> lineageOfType<Element>(con
template <typename ElementType>
inline ElementAncestorRange<const ElementType> lineageOfType(const Element& first)
{
if (is<ElementType>(first))
return ElementAncestorRange<const ElementType>(&downcast<ElementType>(first));
if (auto* element = dynamicDowncast<ElementType>(first))
return ElementAncestorRange<const ElementType>(element);
return ancestorsOfType<ElementType>(first);
}

template <typename ElementType>
inline ElementAncestorRange<ElementType> lineageOfType(Node& first)
{
if (is<ElementType>(first))
return ElementAncestorRange<ElementType>(&downcast<ElementType>(first));
if (auto* element = dynamicDowncast<ElementType>(first))
return ElementAncestorRange<ElementType>(element);
return ancestorsOfType<ElementType>(first);
}

template <typename ElementType>
inline ElementAncestorRange<const ElementType> lineageOfType(const Node& first)
{
if (is<ElementType>(first))
return ElementAncestorRange<const ElementType>(&downcast<ElementType>(first));
if (auto* element = dynamicDowncast<ElementType>(first))
return ElementAncestorRange<const ElementType>(element);
return ancestorsOfType<ElementType>(first);
}

Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/dom/ElementData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(ShareableElementData);

void ElementData::destroy()
{
if (is<UniqueElementData>(*this))
delete downcast<UniqueElementData>(this);
if (auto* uniqueData = dynamicDowncast<UniqueElementData>(*this))
delete uniqueData;
else
delete downcast<ShareableElementData>(this);
}
Expand Down
18 changes: 9 additions & 9 deletions Source/WebCore/dom/ElementData.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,29 +243,29 @@ inline void ElementData::deref()

inline unsigned ElementData::length() const
{
if (is<UniqueElementData>(*this))
return downcast<UniqueElementData>(*this).m_attributeVector.size();
if (auto* uniqueData = dynamicDowncast<UniqueElementData>(*this))
return uniqueData->m_attributeVector.size();
return arraySize();
}

inline const Attribute* ElementData::attributeBase() const
{
if (is<UniqueElementData>(*this))
return downcast<UniqueElementData>(*this).m_attributeVector.data();
if (auto* uniqueData = dynamicDowncast<UniqueElementData>(*this))
return uniqueData->m_attributeVector.data();
return downcast<ShareableElementData>(*this).m_attributeArray;
}

inline const ImmutableStyleProperties* ElementData::presentationalHintStyle() const
{
if (!is<UniqueElementData>(*this))
return nullptr;
return downcast<UniqueElementData>(*this).m_presentationalHintStyle.get();
if (auto* uniqueData = dynamicDowncast<UniqueElementData>(*this))
return uniqueData->m_presentationalHintStyle.get();
return nullptr;
}

inline AttributeIteratorAccessor ElementData::attributesIterator() const
{
if (is<UniqueElementData>(*this)) {
const Vector<Attribute, 4>& attributeVector = downcast<UniqueElementData>(*this).m_attributeVector;
if (auto* uniqueData = dynamicDowncast<UniqueElementData>(*this)) {
auto& attributeVector = uniqueData->m_attributeVector;
return AttributeIteratorAccessor(attributeVector.data(), attributeVector.size());
}
return AttributeIteratorAccessor(downcast<ShareableElementData>(*this).m_attributeArray, arraySize());
Expand Down
7 changes: 5 additions & 2 deletions Source/WebCore/dom/ElementInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ inline unsigned Element::findAttributeIndexByName(const AtomString& name, bool s

inline bool Node::hasAttributes() const
{
return is<Element>(*this) && downcast<Element>(*this).hasAttributes();
auto* element = dynamicDowncast<Element>(*this);
return element && element->hasAttributes();
}

inline NamedNodeMap* Node::attributes() const
{
return is<Element>(*this) ? &downcast<Element>(*this).attributes() : nullptr;
if (auto* element = dynamicDowncast<Element>(*this))
return &element->attributes();
return nullptr;
}

inline Element* Node::parentElement() const
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/dom/ElementIteratorInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ template <typename ElementType>
inline ElementType* findElementAncestorOfType(const Node& current)
{
for (Element* ancestor = current.parentElement(); ancestor; ancestor = ancestor->parentElement()) {
if (is<ElementType>(*ancestor))
return downcast<ElementType>(ancestor);
if (auto* element = dynamicDowncast<ElementType>(*ancestor))
return element;
}
return nullptr;
}
Expand Down
6 changes: 3 additions & 3 deletions Source/WebCore/dom/ElementRareData.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ inline ElementRareData* Element::elementRareData() const

inline ShadowRoot* Node::shadowRoot() const
{
if (!is<Element>(*this))
return nullptr;
return downcast<Element>(*this).shadowRoot();
if (auto* element = dynamicDowncast<Element>(*this))
return element->shadowRoot();
return nullptr;
}

inline ShadowRoot* Element::shadowRoot() const
Expand Down
15 changes: 7 additions & 8 deletions Source/WebCore/dom/EventDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ static void callDefaultEventHandlersInBubblingOrder(Event& event, const EventPat

static bool isInShadowTree(EventTarget* target)
{
return is<Node>(target) && downcast<Node>(*target).isInShadowTree();
auto* node = dynamicDowncast<Node>(target);
return node && node->isInShadowTree();
}

static void dispatchEventInDOM(Event& event, const EventPath& path)
Expand Down Expand Up @@ -127,10 +128,8 @@ static bool shouldSuppressEventDispatchInDOM(Node& node, Event& event)
if (!localFrame->checkedLoader()->shouldSuppressTextInputFromEditing())
return false;

if (is<TextEvent>(event)) {
auto& textEvent = downcast<TextEvent>(event);
return textEvent.isKeyboard() || textEvent.isComposition();
}
if (auto* textEvent = dynamicDowncast<TextEvent>(event))
return textEvent->isKeyboard() || textEvent->isComposition();

return is<CompositionEvent>(event) || is<InputEvent>(event) || is<KeyboardEvent>(event);
}
Expand All @@ -139,9 +138,9 @@ static HTMLInputElement* findInputElementInEventPath(const EventPath& path)
{
size_t size = path.size();
for (size_t i = 0; i < size; ++i) {
const EventContext& eventContext = path.contextAt(i);
if (is<HTMLInputElement>(eventContext.currentTarget()))
return downcast<HTMLInputElement>(eventContext.currentTarget());
auto& eventContext = path.contextAt(i);
if (auto* inputElement = dynamicDowncast<HTMLInputElement>(eventContext.currentTarget()))
return inputElement;
}
return nullptr;
}
Expand Down
1 change: 0 additions & 1 deletion Source/WebCore/dom/TreeScopeOrderedMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ inline RefPtr<Element> TreeScopeOrderedMap::get(const AtomString& key, const Tre
if (auto* currentScope = ContainerChildRemovalScope::currentScope()) {
ASSERT(&scope.rootNode() == &currentScope->parentOfRemovedTree().rootNode());
Ref removedTree = currentScope->removedChild();
ASSERT(is<ContainerNode>(removedTree));
for (Ref element : descendantsOfType<Element>(downcast<ContainerNode>(removedTree.get()))) {
if (!keyMatches(key, element))
continue;
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/dom/XMLDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,9 @@ class XMLDocument : public Document {

SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::XMLDocument)
static bool isType(const WebCore::Document& document) { return document.isXMLDocument(); }
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()
2 changes: 1 addition & 1 deletion Source/WebCore/editing/AlternativeTextController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ bool AlternativeTextController::canEnableAutomaticSpellingCorrection() const
if (RefPtr control = enclosingTextFormControl(position)) {
if (!control->shouldAutocorrect())
return false;
} else if (RefPtr editableRoot = position.rootEditableElement(); is<HTMLElement>(editableRoot) && !downcast<HTMLElement>(*editableRoot).shouldAutocorrect())
} else if (RefPtr editableRoot = dynamicDowncast<HTMLElement>(position.rootEditableElement()); editableRoot && !editableRoot->shouldAutocorrect())
return false;
#endif

Expand Down
23 changes: 14 additions & 9 deletions Source/WebCore/editing/ApplyBlockElementCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,25 @@ VisiblePosition ApplyBlockElementCommand::endOfNextParagraphSplittingTextNodesIf
splitTextNode(*text, 1);
RefPtr previousSiblingOfText { text->previousSibling() };

if (text == start.containerNode() && previousSiblingOfText && is<Text>(previousSiblingOfText)) {
ASSERT(start.offsetInContainerNode() < position.offsetInContainerNode());
start = Position(downcast<Text>(text->previousSibling()), start.offsetInContainerNode());
if (text == start.containerNode() && previousSiblingOfText) {
if (auto* previousText = dynamicDowncast<Text>(*previousSiblingOfText)) {
ASSERT(start.offsetInContainerNode() < position.offsetInContainerNode());
start = Position(previousText, start.offsetInContainerNode());
}
}
if (text == end.containerNode() && previousSiblingOfText && is<Text>(previousSiblingOfText)) {
ASSERT(end.offsetInContainerNode() < position.offsetInContainerNode());
end = Position(downcast<Text>(text->previousSibling()), end.offsetInContainerNode());
if (text == end.containerNode() && previousSiblingOfText) {
if (auto* previousText = dynamicDowncast<Text>(*previousSiblingOfText)) {
ASSERT(end.offsetInContainerNode() < position.offsetInContainerNode());
end = Position(previousText, end.offsetInContainerNode());
}
}
if (text == m_endOfLastParagraph.containerNode()) {
if (m_endOfLastParagraph.offsetInContainerNode() < position.offsetInContainerNode()) {
// We can only fix endOfLastParagraph if the previous node was still text and hasn't been modified by script.
if (previousSiblingOfText && is<Text>(previousSiblingOfText)
&& static_cast<unsigned>(m_endOfLastParagraph.offsetInContainerNode()) <= downcast<Text>(text->previousSibling())->length())
m_endOfLastParagraph = Position(downcast<Text>(text->previousSibling()), m_endOfLastParagraph.offsetInContainerNode());
if (auto* previousText = dynamicDowncast<Text>(previousSiblingOfText.get())) {
if (static_cast<unsigned>(m_endOfLastParagraph.offsetInContainerNode()) <= previousText->length())
m_endOfLastParagraph = Position(previousText, m_endOfLastParagraph.offsetInContainerNode());
}
} else
m_endOfLastParagraph = Position(text.get(), m_endOfLastParagraph.offsetInContainerNode() - 1);
}
Expand Down

0 comments on commit b8691e2

Please sign in to comment.