Switch blink::HTMLElement to use new downcast helpers (5/n)

Bug: 891908
Change-Id: I076f80229a09f8ff8faec8b6d677fd0e5f5310f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1601621
Reviewed-by: Jeremy Roman <jbroman@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Abhijeet Kandalkar <abhijeet@igalia.com>
Cr-Commit-Position: refs/heads/master@{#658645}
diff --git a/third_party/blink/renderer/core/exported/web_view_test.cc b/third_party/blink/renderer/core/exported/web_view_test.cc
index 9b4e994..abb3a5b 100644
--- a/third_party/blink/renderer/core/exported/web_view_test.cc
+++ b/third_party/blink/renderer/core/exported/web_view_test.cc
@@ -2659,8 +2659,8 @@
             web_view->MainFrameWidget()->HandleInputEvent(
                 WebCoalescedInputEvent(event)));
 
-  HTMLElement* element = ToHTMLElement(
-      web_view->MainFrameImpl()->GetDocument().GetElementById("obj"));
+  auto* element = To<HTMLElement>(static_cast<Node*>(
+      web_view->MainFrameImpl()->GetDocument().GetElementById("obj")));
   EXPECT_FALSE(element->CanStartSelection());
 }
 
@@ -2684,8 +2684,8 @@
             web_view->MainFrameWidget()->HandleInputEvent(
                 WebCoalescedInputEvent(event)));
 
-  HTMLElement* element = ToHTMLElement(
-      web_view->MainFrameImpl()->GetDocument().GetElementById("obj"));
+  auto* element = To<HTMLElement>(static_cast<Node*>(
+      web_view->MainFrameImpl()->GetDocument().GetElementById("obj")));
   EXPECT_TRUE(element->CanStartSelection());
 }
 
diff --git a/third_party/blink/renderer/core/html/forms/html_field_set_element.cc b/third_party/blink/renderer/core/html/forms/html_field_set_element.cc
index 7f09324..0380724 100644
--- a/third_party/blink/renderer/core/html/forms/html_field_set_element.cc
+++ b/third_party/blink/renderer/core/html/forms/html_field_set_element.cc
@@ -52,9 +52,9 @@
     if (element->IsFormControlElement()) {
       if (!ToHTMLFormControlElement(element)->IsNotCandidateOrValid())
         return false;
-    } else if (element->IsHTMLElement() &&
-               blink::ToHTMLElement(element)->IsFormAssociatedCustomElement()) {
-      if (!element->EnsureElementInternals().IsNotCandidateOrValid())
+    } else if (auto* html_element = DynamicTo<HTMLElement>(element)) {
+      if (html_element->IsFormAssociatedCustomElement() &&
+          !element->EnsureElementInternals().IsNotCandidateOrValid())
         return false;
     }
   }
diff --git a/third_party/blink/renderer/core/html/forms/html_form_element.cc b/third_party/blink/renderer/core/html/forms/html_form_element.cc
index a9914db..1727da5 100644
--- a/third_party/blink/renderer/core/html/forms/html_form_element.cc
+++ b/third_party/blink/renderer/core/html/forms/html_form_element.cc
@@ -752,7 +752,7 @@
 #if DCHECK_IS_ON()
   if (!element)
     return nullptr;
-  SECURITY_DCHECK(ToHTMLElement(element)->formOwner() == this);
+  SECURITY_DCHECK(To<HTMLElement>(element)->formOwner() == this);
   if (IsHTMLImageElement(*element)) {
     SECURITY_DCHECK(ImageElements().Find(element) != kNotFound);
   } else if (IsHTMLObjectElement(*element)) {
diff --git a/third_party/blink/renderer/core/html/forms/html_select_element.cc b/third_party/blink/renderer/core/html/forms/html_select_element.cc
index a1faaeae..924a852 100644
--- a/third_party/blink/renderer/core/html/forms/html_select_element.cc
+++ b/third_party/blink/renderer/core/html/forms/html_select_element.cc
@@ -752,33 +752,35 @@
 
   for (Element* current_element = ElementTraversal::FirstWithin(*this);
        current_element && list_items_.size() < kMaxListItems;) {
-    if (!current_element->IsHTMLElement()) {
+    auto* current_html_element = DynamicTo<HTMLElement>(current_element);
+    if (!current_html_element) {
       current_element =
           ElementTraversal::NextSkippingChildren(*current_element, this);
       continue;
     }
-    HTMLElement& current = blink::ToHTMLElement(*current_element);
 
     // We should ignore nested optgroup elements. The HTML parser flatten
     // them.  However we need to ignore nested optgroups built by DOM APIs.
     // This behavior matches to IE and Firefox.
-    if (IsHTMLOptGroupElement(current)) {
-      if (current.parentNode() != this) {
-        current_element = ElementTraversal::NextSkippingChildren(current, this);
+    if (IsHTMLOptGroupElement(*current_html_element)) {
+      if (current_html_element->parentNode() != this) {
+        current_element =
+            ElementTraversal::NextSkippingChildren(*current_html_element, this);
         continue;
       }
-      list_items_.push_back(&current);
-      if (Element* next_element = ElementTraversal::FirstWithin(current)) {
+      list_items_.push_back(current_html_element);
+      if (Element* next_element =
+              ElementTraversal::FirstWithin(*current_html_element)) {
         current_element = next_element;
         continue;
       }
     }
 
-    if (IsHTMLOptionElement(current))
-      list_items_.push_back(&current);
+    if (IsHTMLOptionElement(*current_html_element))
+      list_items_.push_back(current_html_element);
 
-    if (IsHTMLHRElement(current))
-      list_items_.push_back(&current);
+    if (IsHTMLHRElement(*current_html_element))
+      list_items_.push_back(current_html_element);
 
     // In conforming HTML code, only <optgroup> and <option> will be found
     // within a <select>. We call NodeTraversal::nextSkippingChildren so
diff --git a/third_party/blink/renderer/core/html/html_element.h b/third_party/blink/renderer/core/html/html_element.h
index 0695267c..a95fcef 100644
--- a/third_party/blink/renderer/core/html/html_element.h
+++ b/third_party/blink/renderer/core/html/html_element.h
@@ -261,25 +261,26 @@
 // This requires isHTML*Element(const Element&) and isHTML*Element(const
 // HTMLElement&).  When the input element is an HTMLElement, we don't need to
 // check the namespace URI, just the local name.
-#define DEFINE_HTMLELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType)                \
-  inline bool Is##thisType(const thisType* element);                         \
-  inline bool Is##thisType(const thisType& element);                         \
-  inline bool Is##thisType(const HTMLElement* element) {                     \
-    return element && Is##thisType(*element);                                \
-  }                                                                          \
-  inline bool Is##thisType(const Node& node) {                               \
-    return node.IsHTMLElement() ? Is##thisType(ToHTMLElement(node)) : false; \
-  }                                                                          \
-  inline bool Is##thisType(const Node* node) {                               \
-    return node && Is##thisType(*node);                                      \
-  }                                                                          \
-  inline bool Is##thisType(const Element* element) {                         \
-    return element && Is##thisType(*element);                                \
-  }                                                                          \
-  template <>                                                                \
-  inline bool IsElementOfType<const thisType>(const HTMLElement& element) {  \
-    return Is##thisType(element);                                            \
-  }                                                                          \
+#define DEFINE_HTMLELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType)               \
+  inline bool Is##thisType(const thisType* element);                        \
+  inline bool Is##thisType(const thisType& element);                        \
+  inline bool Is##thisType(const HTMLElement* element) {                    \
+    return element && Is##thisType(*element);                               \
+  }                                                                         \
+  inline bool Is##thisType(const Node& node) {                              \
+    auto* html_element = DynamicTo<HTMLElement>(node);                      \
+    return html_element ? Is##thisType(html_element) : false;               \
+  }                                                                         \
+  inline bool Is##thisType(const Node* node) {                              \
+    return node && Is##thisType(*node);                                     \
+  }                                                                         \
+  inline bool Is##thisType(const Element* element) {                        \
+    return element && Is##thisType(*element);                               \
+  }                                                                         \
+  template <>                                                               \
+  inline bool IsElementOfType<const thisType>(const HTMLElement& element) { \
+    return Is##thisType(element);                                           \
+  }                                                                         \
   DEFINE_ELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType)
 
 }  // namespace blink
diff --git a/third_party/blink/renderer/core/loader/image_loader.cc b/third_party/blink/renderer/core/loader/image_loader.cc
index c324730..1cb2e74 100644
--- a/third_party/blink/renderer/core/loader/image_loader.cc
+++ b/third_party/blink/renderer/core/loader/image_loader.cc
@@ -505,8 +505,8 @@
     // own origin checking logic that may get confused if service workers
     // respond with resources from another origin.
     // https://w3c.github.io/ServiceWorker/#implementer-concerns
-    if (GetElement()->IsHTMLElement() &&
-        ToHTMLElement(GetElement())->IsPluginElement()) {
+    auto* html_element = DynamicTo<HTMLElement>(GetElement());
+    if (html_element && html_element->IsPluginElement()) {
       resource_request.SetSkipServiceWorker(true);
     }
 
diff --git a/third_party/blink/renderer/core/page/context_menu_controller.cc b/third_party/blink/renderer/core/page/context_menu_controller.cc
index 3c8d095..8667c29 100644
--- a/third_party/blink/renderer/core/page/context_menu_controller.cc
+++ b/third_party/blink/renderer/core/page/context_menu_controller.cc
@@ -207,8 +207,8 @@
   // all else.
   data.link_url = result.AbsoluteLinkURL();
 
-  if (result.InnerNode()->IsHTMLElement()) {
-    HTMLElement* html_element = ToHTMLElement(result.InnerNode());
+  auto* html_element = DynamicTo<HTMLElement>(result.InnerNode());
+  if (html_element) {
     if (!html_element->title().IsEmpty()) {
       data.title_text = html_element->title();
     } else {
diff --git a/third_party/blink/renderer/core/page/drag_controller.cc b/third_party/blink/renderer/core/page/drag_controller.cc
index 46c7b599..5f2097a 100644
--- a/third_party/blink/renderer/core/page/drag_controller.cc
+++ b/third_party/blink/renderer/core/page/drag_controller.cc
@@ -801,8 +801,8 @@
     return true;
 
   for (Node& ancestor_node : NodeTraversal::InclusiveAncestorsOf(node)) {
-    if (ancestor_node.IsHTMLElement() &&
-        ToHTMLElement(&ancestor_node)->draggable())
+    auto* html_element = DynamicTo<HTMLElement>(ancestor_node);
+    if (html_element && html_element->draggable())
       return false;
   }
 
diff --git a/third_party/blink/renderer/core/page/focus_controller.cc b/third_party/blink/renderer/core/page/focus_controller.cc
index 8e8348d..856562e 100644
--- a/third_party/blink/renderer/core/page/focus_controller.cc
+++ b/third_party/blink/renderer/core/page/focus_controller.cc
@@ -413,8 +413,8 @@
 }
 
 inline bool HasCustomFocusLogic(const Element& element) {
-  return element.IsHTMLElement() &&
-         ToHTMLElement(element).HasCustomFocusLogic();
+  auto* html_element = DynamicTo<HTMLElement>(element);
+  return html_element && html_element->HasCustomFocusLogic();
 }
 
 inline bool IsShadowHostWithoutCustomFocusLogic(const Element& element) {
@@ -1123,15 +1123,16 @@
   // Will nvestigate further for a proper solution later.
   static const int kFocusTraversalThreshold = 50;
   element->GetDocument().UpdateStyleAndLayout();
-  if (!element->IsHTMLElement())
+  auto* html_element = DynamicTo<HTMLElement>(element);
+  if (!html_element)
     return nullptr;
 
   if (!element->IsFormControlElement() &&
-      !ToHTMLElement(element)->isContentEditableForBinding())
+      !html_element->isContentEditableForBinding())
     return nullptr;
 
   HTMLFormElement* form_owner = nullptr;
-  if (ToHTMLElement(element)->isContentEditableForBinding())
+  if (html_element->isContentEditableForBinding())
     form_owner = Traversal<HTMLFormElement>::FirstAncestor(*element);
   else
     form_owner = ToHTMLFormControlElement(element)->formOwner();
@@ -1146,9 +1147,10 @@
        next_element =
            FindFocusableElement(focus_type, *next_element, owner_map),
        ++traversal) {
-    if (!next_element->IsHTMLElement())
+    auto* next_html_element = DynamicTo<HTMLElement>(next_element);
+    if (!next_html_element)
       continue;
-    if (ToHTMLElement(next_element)->isContentEditableForBinding() &&
+    if (next_html_element->isContentEditableForBinding() &&
         next_element->IsDescendantOf(form_owner))
       return next_element;
     if (!next_element->IsFormControlElement())
diff --git a/third_party/blink/renderer/core/paint/paint_layer_painter_test.cc b/third_party/blink/renderer/core/paint/paint_layer_painter_test.cc
index 71bd564..80dcd81 100644
--- a/third_party/blink/renderer/core/paint/paint_layer_painter_test.cc
+++ b/third_party/blink/renderer/core/paint/paint_layer_painter_test.cc
@@ -139,7 +139,7 @@
 
   check_chunks();
 
-  ToHTMLElement(content1.GetNode())
+  To<HTMLElement>(content1.GetNode())
       ->setAttribute(html_names::kStyleAttr,
                      "position: absolute; width: 100px; height: 100px; "
                      "background-color: green");
@@ -303,7 +303,7 @@
                           IsSameId(&container2, kBackgroundType),
                           IsSameId(&content2, kBackgroundType)));
 
-  ToHTMLElement(GetElementById("content1"))
+  To<HTMLElement>(GetElementById("content1"))
       ->setAttribute(html_names::kStyleAttr,
                      "position: absolute; width: 100px; height: 100px; "
                      "background-color: green");
@@ -458,7 +458,7 @@
   )HTML");
   LayoutObject& outline_div =
       *GetDocument().getElementById("outline")->GetLayoutObject();
-  ToHTMLElement(outline_div.GetNode())
+  To<HTMLElement>(outline_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_without_outline);
   UpdateAllLifecyclePhasesForTest();
 
@@ -479,7 +479,7 @@
 
   // Outline on the self-painting-layer node itself doesn't affect
   // PaintPhaseDescendantOutlines.
-  ToHTMLElement(self_painting_layer_object.GetNode())
+  To<HTMLElement>(self_painting_layer_object.GetNode())
       ->setAttribute(html_names::kStyleAttr,
                      "position: absolute; outline: 1px solid green");
   UpdateAllLifecyclePhasesForTest();
@@ -491,7 +491,7 @@
 
   // needsPaintPhaseDescendantOutlines should be set when any descendant on the
   // same layer has outline.
-  ToHTMLElement(outline_div.GetNode())
+  To<HTMLElement>(outline_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_with_outline);
   GetDocument().View()->UpdateAllLifecyclePhasesExceptPaint();
   EXPECT_TRUE(self_painting_layer.NeedsPaintPhaseDescendantOutlines());
@@ -503,7 +503,7 @@
 
   // needsPaintPhaseDescendantOutlines should be reset when no outline is
   // actually painted.
-  ToHTMLElement(outline_div.GetNode())
+  To<HTMLElement>(outline_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_without_outline);
   UpdateAllLifecyclePhasesForTest();
   EXPECT_TRUE(self_painting_layer.NeedsPaintPhaseDescendantOutlines());
@@ -525,7 +525,7 @@
   )HTML");
   LayoutObject& float_div =
       *GetDocument().getElementById("float")->GetLayoutObject();
-  ToHTMLElement(float_div.GetNode())
+  To<HTMLElement>(float_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_without_float);
   UpdateAllLifecyclePhasesForTest();
 
@@ -546,7 +546,7 @@
 
   // needsPaintPhaseFloat should be set when any descendant on the same layer
   // has float.
-  ToHTMLElement(float_div.GetNode())
+  To<HTMLElement>(float_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_with_float);
   GetDocument().View()->UpdateAllLifecyclePhasesExceptPaint();
   EXPECT_TRUE(self_painting_layer.NeedsPaintPhaseFloat());
@@ -558,7 +558,7 @@
 
   // needsPaintPhaseFloat should be reset when there is no float actually
   // painted.
-  ToHTMLElement(float_div.GetNode())
+  To<HTMLElement>(float_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_without_float);
   UpdateAllLifecyclePhasesForTest();
   EXPECT_TRUE(self_painting_layer.NeedsPaintPhaseFloat());
@@ -627,7 +627,7 @@
   )HTML");
   LayoutObject& background_div =
       *GetDocument().getElementById("background")->GetLayoutObject();
-  ToHTMLElement(background_div.GetNode())
+  To<HTMLElement>(background_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_without_background);
   UpdateAllLifecyclePhasesForTest();
 
@@ -649,7 +649,7 @@
 
   // Background on the self-painting-layer node itself doesn't affect
   // PaintPhaseDescendantBlockBackgrounds.
-  ToHTMLElement(self_painting_layer_object.GetNode())
+  To<HTMLElement>(self_painting_layer_object.GetNode())
       ->setAttribute(html_names::kStyleAttr,
                      "position: absolute; background: green");
   UpdateAllLifecyclePhasesForTest();
@@ -662,7 +662,7 @@
 
   // needsPaintPhaseDescendantBlockBackgrounds should be set when any descendant
   // on the same layer has Background.
-  ToHTMLElement(background_div.GetNode())
+  To<HTMLElement>(background_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_with_background);
   GetDocument().View()->UpdateAllLifecyclePhasesExceptPaint();
   EXPECT_TRUE(self_painting_layer.NeedsPaintPhaseDescendantBlockBackgrounds());
@@ -675,7 +675,7 @@
 
   // needsPaintPhaseDescendantBlockBackgrounds should be reset when no outline
   // is actually painted.
-  ToHTMLElement(background_div.GetNode())
+  To<HTMLElement>(background_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, style_without_background);
   UpdateAllLifecyclePhasesForTest();
   EXPECT_TRUE(self_painting_layer.NeedsPaintPhaseDescendantBlockBackgrounds());
@@ -704,7 +704,7 @@
   EXPECT_TRUE(html_layer.NeedsPaintPhaseFloat());
   EXPECT_TRUE(html_layer.NeedsPaintPhaseDescendantBlockBackgrounds());
 
-  ToHTMLElement(layer_div.GetNode())
+  To<HTMLElement>(layer_div.GetNode())
       ->setAttribute(html_names::kStyleAttr, "position: relative");
   UpdateAllLifecyclePhasesForTest();
   ASSERT_TRUE(layer_div.HasLayer());
@@ -739,7 +739,7 @@
   EXPECT_TRUE(html_layer.NeedsPaintPhaseDescendantOutlines());
   EXPECT_TRUE(html_layer.NeedsPaintPhaseDescendantBlockBackgrounds());
 
-  ToHTMLElement(layer_div.GetNode())
+  To<HTMLElement>(layer_div.GetNode())
       ->setAttribute(
           html_names::kStyleAttr,
           "width: 100px; height: 100px; overflow: hidden; position: relative");
@@ -779,7 +779,7 @@
   EXPECT_FALSE(html_layer.NeedsPaintPhaseDescendantOutlines());
   EXPECT_FALSE(html_layer.NeedsPaintPhaseDescendantBlockBackgrounds());
 
-  ToHTMLElement(layer_div.GetNode())
+  To<HTMLElement>(layer_div.GetNode())
       ->setAttribute(html_names::kStyleAttr,
                      "width: 100px; height: 100px; overflow: hidden");
   UpdateAllLifecyclePhasesForTest();
@@ -826,7 +826,7 @@
   EXPECT_TRUE(layer.IsSelfPaintingLayer());
   EXPECT_FALSE(layer.NeedsPaintPhaseDescendantBlockBackgrounds());
 
-  ToHTMLElement(table.GetNode())
+  To<HTMLElement>(table.GetNode())
       ->setAttribute(html_names::kStyleAttr,
                      "position: relative; border-collapse: collapse");
   UpdateAllLifecyclePhasesForTest();
diff --git a/third_party/blink/renderer/modules/accessibility/ax_layout_object.cc b/third_party/blink/renderer/modules/accessibility/ax_layout_object.cc
index 2996fd78..4a19b67 100644
--- a/third_party/blink/renderer/modules/accessibility/ax_layout_object.cc
+++ b/third_party/blink/renderer/modules/accessibility/ax_layout_object.cc
@@ -2511,7 +2511,7 @@
     ExceptionState exception_state(v8::Isolate::GetCurrent(),
                                    ExceptionState::kExecutionContext, nullptr,
                                    nullptr);
-    ToHTMLElement(GetNode())->setInnerText(string, exception_state);
+    To<HTMLElement>(GetNode())->setInnerText(string, exception_state);
     if (exception_state.HadException()) {
       exception_state.ClearException();
       return false;
diff --git a/third_party/blink/renderer/modules/accessibility/ax_node_object.cc b/third_party/blink/renderer/modules/accessibility/ax_node_object.cc
index cf78117..46e820a 100644
--- a/third_party/blink/renderer/modules/accessibility/ax_node_object.cc
+++ b/third_party/blink/renderer/modules/accessibility/ax_node_object.cc
@@ -371,7 +371,8 @@
                                    ax::mojom::Role current_role,
                                    HTMLElement* current_element) {
   Node* parent_node = parent->GetNode();
-  if (!parent_node || !parent_node->IsHTMLElement())
+  auto* parent_html_element = DynamicTo<HTMLElement>(parent_node);
+  if (!parent_html_element)
     return false;
 
   if (current_role == ax::mojom::Role::kListItem)
@@ -388,7 +389,7 @@
   if (IsHTMLTableCellElement(*current_element))
     return IsHTMLTableRowElement(*parent_node);
   if (IsHTMLTableRowElement(*current_element))
-    return IsHTMLTableSectionElement(ToHTMLElement(*parent_node));
+    return IsHTMLTableSectionElement(parent_html_element);
 
   // In case of ListboxRole and its child, ListBoxOptionRole, inheritance of
   // presentation role is handled in AXListBoxOption because ListBoxOption Role
@@ -417,9 +418,7 @@
   if (!parent)
     return nullptr;
 
-  HTMLElement* element = nullptr;
-  if (GetNode() && GetNode()->IsHTMLElement())
-    element = ToHTMLElement(GetNode());
+  auto* element = DynamicTo<HTMLElement>(GetNode());
   if (!parent->HasInheritedPresentationalRole())
     return nullptr;
 
@@ -1279,26 +1278,26 @@
     }
   }
 
-  if (!node->IsHTMLElement())
+  auto* element = DynamicTo<HTMLElement>(node);
+  if (!element)
     return 0;
 
-  HTMLElement& element = ToHTMLElement(*node);
-  if (element.HasTagName(kH1Tag))
+  if (element->HasTagName(kH1Tag))
     return 1;
 
-  if (element.HasTagName(kH2Tag))
+  if (element->HasTagName(kH2Tag))
     return 2;
 
-  if (element.HasTagName(kH3Tag))
+  if (element->HasTagName(kH3Tag))
     return 3;
 
-  if (element.HasTagName(kH4Tag))
+  if (element->HasTagName(kH4Tag))
     return 4;
 
-  if (element.HasTagName(kH5Tag))
+  if (element->HasTagName(kH5Tag))
     return 5;
 
-  if (element.HasTagName(kH6Tag))
+  if (element->HasTagName(kH6Tag))
     return 6;
 
   if (RoleValue() == ax::mojom::Role::kHeading)
@@ -2187,9 +2186,7 @@
   // Based on
   // http://rawgit.com/w3c/aria/master/html-aam/html-aam.html#accessible-name-and-description-calculation
   // 5.1/5.5 Text inputs, Other labelable Elements
-  HTMLElement* html_element = nullptr;
-  if (GetNode()->IsHTMLElement())
-    html_element = ToHTMLElement(GetNode());
+  auto* html_element = DynamicTo<HTMLElement>(GetNode());
   if (html_element && html_element->IsLabelable()) {
     if (html_element->labels() && html_element->labels()->length() > 0)
       return true;
@@ -2827,10 +2824,7 @@
 
   // 5.1/5.5 Text inputs, Other labelable Elements
   // If you change this logic, update AXNodeObject::nameFromLabelElement, too.
-  HTMLElement* html_element = nullptr;
-  if (GetNode()->IsHTMLElement())
-    html_element = ToHTMLElement(GetNode());
-
+  auto* html_element = DynamicTo<HTMLElement>(GetNode());
   if (html_element && html_element->IsLabelable()) {
     name_from = ax::mojom::NameFrom::kRelatedElement;
     if (name_sources) {