Use moar ToXMLElement() in place of static_cast<>.

Introduces checks in a few new places, but mainly just consolidates
checking/casting logic.

Change-Id: I634a03060d254db099972c6978249992367e146c
Reviewed-on: https://pdfium-review.googlesource.com/38900
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_metadata.cpp b/core/fpdfdoc/cpdf_metadata.cpp
index 323de4f..5ef7312 100644
--- a/core/fpdfdoc/cpdf_metadata.cpp
+++ b/core/fpdfdoc/cpdf_metadata.cpp
@@ -49,11 +49,9 @@
 
   for (auto* child = element->GetFirstChild(); child;
        child = child->GetNextSibling()) {
-    if (child->GetType() != FX_XMLNODE_Element)
-      continue;
-
-    CheckForSharedFormInternal(static_cast<CFX_XMLElement*>(child),
-                               unsupported);
+    CFX_XMLElement* pElement = ToXMLElement(child);
+    if (pElement)
+      CheckForSharedFormInternal(pElement, unsupported);
   }
 }
 
diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp
index 74351b8..4bb4eae 100644
--- a/core/fxcrt/xml/cfx_xmlelement.cpp
+++ b/core/fxcrt/xml/cfx_xmlelement.cpp
@@ -125,11 +125,8 @@
 CFX_XMLElement* CFX_XMLElement::GetNthChildNamed(const WideStringView& name,
                                                  size_t idx) const {
   for (auto* child = GetFirstChild(); child; child = child->GetNextSibling()) {
-    if (child->GetType() != FX_XMLNODE_Element)
-      continue;
-
-    CFX_XMLElement* elem = static_cast<CFX_XMLElement*>(child);
-    if (elem->name_ != name)
+    CFX_XMLElement* elem = ToXMLElement(child);
+    if (!elem || elem->name_ != name)
       continue;
     if (idx == 0)
       return elem;
diff --git a/core/fxcrt/xml/cfx_xmlelement_unittest.cpp b/core/fxcrt/xml/cfx_xmlelement_unittest.cpp
index 1e53ef3..dfc60a1 100644
--- a/core/fxcrt/xml/cfx_xmlelement_unittest.cpp
+++ b/core/fxcrt/xml/cfx_xmlelement_unittest.cpp
@@ -73,7 +73,6 @@
 
 TEST(CFX_XMLElementTest, Clone) {
   CFX_XMLDocument doc;
-
   CFX_XMLElement node(L"test:node");
   node.SetAttribute(L"first", L"one");
   node.SetAttribute(L"second", L"two");
@@ -87,10 +86,9 @@
 
   CFX_XMLNode* clone = node.Clone(&doc);
   EXPECT_TRUE(clone != nullptr);
-
   ASSERT_EQ(FX_XMLNODE_Element, clone->GetType());
-  CFX_XMLElement* inst = static_cast<CFX_XMLElement*>(clone);
 
+  CFX_XMLElement* inst = ToXMLElement(clone);
   EXPECT_EQ(L"test:node", inst->GetName());
   EXPECT_EQ(L"node", inst->GetLocalTagName());
   EXPECT_EQ(L"test", inst->GetNamespacePrefix());
diff --git a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
index 74ef87e..b25a18b 100644
--- a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
+++ b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
@@ -132,7 +132,7 @@
   ASSERT_TRUE(root->GetFirstChild() != nullptr);
   ASSERT_TRUE(root->GetFirstChild()->GetType() == FX_XMLNODE_Element);
 
-  CFX_XMLElement* node = static_cast<CFX_XMLElement*>(root->GetFirstChild());
+  CFX_XMLElement* node = ToXMLElement(root->GetFirstChild());
   EXPECT_EQ(L"node", node->GetName());
 
   CFX_XMLInstruction* instruction = nullptr;
diff --git a/core/fxcrt/xml/cfx_xmlparser.cpp b/core/fxcrt/xml/cfx_xmlparser.cpp
index 268774a..dd28cf8 100644
--- a/core/fxcrt/xml/cfx_xmlparser.cpp
+++ b/core/fxcrt/xml/cfx_xmlparser.cpp
@@ -262,11 +262,10 @@
             current_buffer_idx++;
             current_parser_state = FDE_XmlSyntaxState::AttriName;
 
-            if (current_node_ &&
-                current_node_->GetType() == FX_XMLNODE_Element) {
-              static_cast<CFX_XMLElement*>(current_node_)
-                  ->SetAttribute(current_attribute_name, GetTextData());
-            }
+            CFX_XMLElement* elem = ToXMLElement(current_node_);
+            if (elem)
+              elem->SetAttribute(current_attribute_name, GetTextData());
+
             current_attribute_name.clear();
           } else {
             ProcessTextChar(ch);
@@ -311,13 +310,13 @@
               node_type_stack.pop();
               current_parser_state = FDE_XmlSyntaxState::Text;
 
-              if (current_node_->GetType() != FX_XMLNODE_Element)
+              CFX_XMLElement* element = ToXMLElement(current_node_);
+              if (!element)
                 return false;
 
               WideString element_name = GetTextData();
               if (element_name.GetLength() > 0 &&
-                  element_name !=
-                      static_cast<CFX_XMLElement*>(current_node_)->GetName()) {
+                  element_name != element->GetName()) {
                 return false;
               }
 
diff --git a/fxjs/xfa/cjx_object.cpp b/fxjs/xfa/cjx_object.cpp
index 1c0454d..6984c3a 100644
--- a/fxjs/xfa/cjx_object.cpp
+++ b/fxjs/xfa/cjx_object.cpp
@@ -806,30 +806,31 @@
   if (ToNode(GetXFAObject())->IsModelNode() ||
       ToNode(GetXFAObject())->GetElementType() == XFA_Element::Packet) {
     CFX_XMLNode* pXMLNode = ToNode(GetXFAObject())->GetXMLMappingNode();
-    if (!pXMLNode || pXMLNode->GetType() != FX_XMLNODE_Element)
+    CFX_XMLElement* element = ToXMLElement(pXMLNode);
+    if (!element)
       return {};
 
-    return {static_cast<CFX_XMLElement*>(pXMLNode)->GetNamespaceURI()};
+    return {element->GetNamespaceURI()};
   }
 
   if (ToNode(GetXFAObject())->GetPacketType() != XFA_PacketType::Datasets)
     return ToNode(GetXFAObject())->GetModelNode()->JSObject()->TryNamespace();
 
   CFX_XMLNode* pXMLNode = ToNode(GetXFAObject())->GetXMLMappingNode();
-  if (!pXMLNode || pXMLNode->GetType() != FX_XMLNODE_Element)
+  CFX_XMLElement* element = ToXMLElement(pXMLNode);
+  if (!element)
     return {};
 
   if (ToNode(GetXFAObject())->GetElementType() == XFA_Element::DataValue &&
       GetEnum(XFA_Attribute::Contains) == XFA_AttributeEnum::MetaData) {
     WideString wsNamespace;
-    bool ret = XFA_FDEExtension_ResolveNamespaceQualifier(
-        static_cast<CFX_XMLElement*>(pXMLNode),
-        GetCData(XFA_Attribute::QualifiedName), &wsNamespace);
-    if (!ret)
+    if (!XFA_FDEExtension_ResolveNamespaceQualifier(
+            element, GetCData(XFA_Attribute::QualifiedName), &wsNamespace)) {
       return {};
+    }
     return {wsNamespace};
   }
-  return {static_cast<CFX_XMLElement*>(pXMLNode)->GetNamespaceURI()};
+  return {element->GetNamespaceURI()};
 }
 
 std::pair<CXFA_Node*, int32_t> CJX_Object::GetPropertyInternal(
diff --git a/fxjs/xfa/cjx_packet.cpp b/fxjs/xfa/cjx_packet.cpp
index b27cfba..2288de4 100644
--- a/fxjs/xfa/cjx_packet.cpp
+++ b/fxjs/xfa/cjx_packet.cpp
@@ -35,11 +35,10 @@
     return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
 
   WideString attributeValue;
-  CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode();
-  if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
-    attributeValue = static_cast<CFX_XMLElement*>(pXMLNode)->GetAttribute(
-        runtime->ToWideString(params[0]));
-  }
+  CFX_XMLElement* element = ToXMLElement(GetXFANode()->GetXMLMappingNode());
+  if (element)
+    attributeValue = element->GetAttribute(runtime->ToWideString(params[0]));
+
   return CJS_Return(
       runtime->NewString(attributeValue.UTF8Encode().AsStringView()));
 }
@@ -50,10 +49,10 @@
   if (params.size() != 2)
     return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
 
-  CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode();
-  if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
-    static_cast<CFX_XMLElement*>(pXMLNode)->SetAttribute(
-        runtime->ToWideString(params[1]), runtime->ToWideString(params[0]));
+  CFX_XMLElement* element = ToXMLElement(GetXFANode()->GetXMLMappingNode());
+  if (element) {
+    element->SetAttribute(runtime->ToWideString(params[1]),
+                          runtime->ToWideString(params[0]));
   }
   return CJS_Return(runtime->NewNull());
 }
@@ -64,12 +63,11 @@
   if (params.size() != 1)
     return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
 
-  CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode();
-  if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
+  CFX_XMLElement* pElement = ToXMLElement(GetXFANode()->GetXMLMappingNode());
+  if (pElement) {
     WideString name = runtime->ToWideString(params[0]);
-    CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode);
-    if (pXMLElement->HasAttribute(name))
-      pXMLElement->RemoveAttribute(name);
+    if (pElement->HasAttribute(name))
+      pElement->RemoveAttribute(name);
   }
   return CJS_Return(runtime->NewNull());
 }
@@ -77,25 +75,23 @@
 void CJX_Packet::content(CFXJSE_Value* pValue,
                          bool bSetting,
                          XFA_Attribute eAttribute) {
-  CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode();
+  CFX_XMLElement* element = ToXMLElement(GetXFANode()->GetXMLMappingNode());
   if (bSetting) {
-    if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
-      auto* text = GetXFANode()
-                       ->GetDocument()
-                       ->GetNotify()
-                       ->GetHDOC()
-                       ->GetXMLDocument()
-                       ->CreateNode<CFX_XMLText>(pValue->ToWideString());
-      pXMLNode->AppendChild(text);
+    if (element) {
+      element->AppendChild(
+          GetXFANode()
+              ->GetDocument()
+              ->GetNotify()
+              ->GetHDOC()
+              ->GetXMLDocument()
+              ->CreateNode<CFX_XMLText>(pValue->ToWideString()));
     }
     return;
   }
 
   WideString wsTextData;
-  if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
-    CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode);
-    wsTextData = pXMLElement->GetTextData();
-  }
+  if (element)
+    wsTextData = element->GetTextData();
 
   pValue->SetString(wsTextData.UTF8Encode().AsStringView());
 }
diff --git a/xfa/fxfa/cxfa_textlayout.cpp b/xfa/fxfa/cxfa_textlayout.cpp
index 32a1b39..8bf955b 100644
--- a/xfa/fxfa/cxfa_textlayout.cpp
+++ b/xfa/fxfa/cxfa_textlayout.cpp
@@ -77,19 +77,17 @@
   if (!pXMLRoot)
     return nullptr;
 
-  CFX_XMLNode* pXMLContainer = nullptr;
   for (CFX_XMLNode* pXMLChild = pXMLRoot->GetFirstChild(); pXMLChild;
        pXMLChild = pXMLChild->GetNextSibling()) {
-    if (pXMLChild->GetType() == FX_XMLNODE_Element) {
-      CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLChild);
-      WideString wsTag = pXMLElement->GetLocalTagName();
-      if (wsTag == L"body" || wsTag == L"html") {
-        pXMLContainer = pXMLChild;
-        break;
-      }
-    }
+    CFX_XMLElement* pXMLElement = ToXMLElement(pXMLChild);
+    if (!pXMLElement)
+      continue;
+    WideString wsTag = pXMLElement->GetLocalTagName();
+    if (wsTag == L"body" || wsTag == L"html")
+      return pXMLChild;
   }
-  return pXMLContainer;
+
+  return nullptr;
 }
 
 std::unique_ptr<CFX_RTFBreak> CXFA_TextLayout::CreateBreak(bool bDefault) {
diff --git a/xfa/fxfa/cxfa_textparser.cpp b/xfa/fxfa/cxfa_textparser.cpp
index f0a6dd2..c60b774 100644
--- a/xfa/fxfa/cxfa_textparser.cpp
+++ b/xfa/fxfa/cxfa_textparser.cpp
@@ -276,18 +276,18 @@
 std::unique_ptr<CXFA_TextParser::TagProvider> CXFA_TextParser::ParseTagInfo(
     CFX_XMLNode* pXMLNode) {
   auto tagProvider = pdfium::MakeUnique<TagProvider>();
-
-  WideString wsName;
-  if (pXMLNode->GetType() == FX_XMLNODE_Element) {
-    CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode);
-    wsName = pXMLElement->GetLocalTagName();
+  CFX_XMLElement* pXMLElement = ToXMLElement(pXMLNode);
+  if (pXMLElement) {
+    WideString wsName = pXMLElement->GetLocalTagName();
     tagProvider->SetTagName(wsName);
     tagProvider->m_bTagAvailable = TagValidate(wsName);
-
     WideString wsValue = pXMLElement->GetAttribute(L"style");
     if (!wsValue.IsEmpty())
       tagProvider->SetAttribute(L"style", wsValue);
-  } else if (pXMLNode->GetType() == FX_XMLNODE_Text) {
+
+    return tagProvider;
+  }
+  if (pXMLNode->GetType() == FX_XMLNODE_Text) {
     tagProvider->m_bTagAvailable = true;
     tagProvider->m_bContent = true;
   }
@@ -502,38 +502,38 @@
   if (!pXMLNode)
     return false;
 
-  bool bRet = false;
-  if (pXMLNode->GetType() == FX_XMLNODE_Element) {
-    CFX_XMLElement* pElement = static_cast<CFX_XMLElement*>(pXMLNode);
-    WideString wsAttr = pElement->GetAttribute(L"xfa:embed");
-    if (wsAttr.IsEmpty())
-      return false;
-    if (wsAttr[0] == L'#')
-      wsAttr.Delete(0);
+  CFX_XMLElement* pElement = ToXMLElement(pXMLNode);
+  if (!pElement)
+    return false;
 
-    WideString ws = pElement->GetAttribute(L"xfa:embedType");
-    if (ws.IsEmpty())
-      ws = L"som";
-    else
-      ws.MakeLower();
+  WideString wsAttr = pElement->GetAttribute(L"xfa:embed");
+  if (wsAttr.IsEmpty())
+    return false;
 
-    bool bURI = (ws == L"uri");
-    if (!bURI && ws != L"som")
-      return false;
+  if (wsAttr[0] == L'#')
+    wsAttr.Delete(0);
 
-    ws = pElement->GetAttribute(L"xfa:embedMode");
-    if (ws.IsEmpty())
-      ws = L"formatted";
-    else
-      ws.MakeLower();
+  WideString ws = pElement->GetAttribute(L"xfa:embedType");
+  if (ws.IsEmpty())
+    ws = L"som";
+  else
+    ws.MakeLower();
 
-    bool bRaw = (ws == L"raw");
-    if (!bRaw && ws != L"formatted")
-      return false;
+  bool bURI = (ws == L"uri");
+  if (!bURI && ws != L"som")
+    return false;
 
-    bRet = pTextProvider->GetEmbbedObj(bURI, bRaw, wsAttr, wsValue);
-  }
-  return bRet;
+  ws = pElement->GetAttribute(L"xfa:embedMode");
+  if (ws.IsEmpty())
+    ws = L"formatted";
+  else
+    ws.MakeLower();
+
+  bool bRaw = (ws == L"raw");
+  if (!bRaw && ws != L"formatted")
+    return false;
+
+  return pTextProvider->GetEmbbedObj(bURI, bRaw, wsAttr, wsValue);
 }
 
 CXFA_TextParseContext* CXFA_TextParser::GetParseContextFromMap(
diff --git a/xfa/fxfa/cxfa_textprovider.cpp b/xfa/fxfa/cxfa_textprovider.cpp
index 60c5294..ba48cd8 100644
--- a/xfa/fxfa/cxfa_textprovider.cpp
+++ b/xfa/fxfa/cxfa_textprovider.cpp
@@ -61,13 +61,12 @@
   if (m_eType == XFA_TEXTPROVIDERTYPE_Datasets) {
     CXFA_Node* pBind = m_pNode->GetBindData();
     CFX_XMLNode* pXMLNode = pBind->GetXMLMappingNode();
-    ASSERT(pXMLNode);
     for (CFX_XMLNode* pXMLChild = pXMLNode->GetFirstChild(); pXMLChild;
          pXMLChild = pXMLChild->GetNextSibling()) {
-      if (pXMLChild->GetType() == FX_XMLNODE_Element) {
-        CFX_XMLElement* pElement = static_cast<CFX_XMLElement*>(pXMLChild);
-        if (XFA_RecognizeRichText(pElement))
-          bRichText = true;
+      CFX_XMLElement* pElement = ToXMLElement(pXMLChild);
+      if (pElement && XFA_RecognizeRichText(pElement)) {
+        bRichText = true;
+        break;
       }
     }
     return pBind;
diff --git a/xfa/fxfa/parser/cxfa_dataexporter.cpp b/xfa/fxfa/parser/cxfa_dataexporter.cpp
index eb3e04a..820d354 100644
--- a/xfa/fxfa/parser/cxfa_dataexporter.cpp
+++ b/xfa/fxfa/parser/cxfa_dataexporter.cpp
@@ -38,9 +38,8 @@
         break;
       }
       case XFA_PacketType::Datasets: {
-        CFX_XMLElement* pElement =
-            static_cast<CFX_XMLElement*>(pNode->GetXMLMappingNode());
-        if (!pElement || pElement->GetType() != FX_XMLNODE_Element)
+        CFX_XMLElement* pElement = ToXMLElement(pNode->GetXMLMappingNode());
+        if (!pElement)
           return false;
 
         CXFA_Node* pDataNode = pNode->GetFirstChild();
@@ -54,9 +53,8 @@
         break;
       case XFA_PacketType::Template:
       default: {
-        CFX_XMLElement* pElement =
-            static_cast<CFX_XMLElement*>(pNode->GetXMLMappingNode());
-        if (!pElement || pElement->GetType() != FX_XMLNODE_Element)
+        CFX_XMLElement* pElement = ToXMLElement(pNode->GetXMLMappingNode());
+        if (!pElement)
           return false;
 
         pElement->Save(pStream);
@@ -75,9 +73,8 @@
       break;
     }
   }
-  CFX_XMLElement* pElement =
-      static_cast<CFX_XMLElement*>(pExportNode->GetXMLMappingNode());
-  if (!pElement || pElement->GetType() != FX_XMLNODE_Element)
+  CFX_XMLElement* pElement = ToXMLElement(pExportNode->GetXMLMappingNode());
+  if (!pElement)
     return false;
 
   XFA_DataExporter_DealWithDataGroupNode(pExportNode);
@@ -85,6 +82,5 @@
                          L"http://www.xfa.org/schema/xfa-data/1.0/");
   pElement->Save(pStream);
   pElement->RemoveAttribute(L"xmlns:xfa");
-
   return true;
 }
diff --git a/xfa/fxfa/parser/cxfa_document.cpp b/xfa/fxfa/parser/cxfa_document.cpp
index 3e80466..3b086fa 100644
--- a/xfa/fxfa/parser/cxfa_document.cpp
+++ b/xfa/fxfa/parser/cxfa_document.cpp
@@ -510,9 +510,8 @@
           wsHref = image->GetHref();
         }
         CFX_XMLElement* pXMLDataElement =
-            static_cast<CFX_XMLElement*>(pDataNode->GetXMLMappingNode());
+            ToXMLElement(pDataNode->GetXMLMappingNode());
         ASSERT(pXMLDataElement);
-
         pDataNode->JSObject()->SetAttributeValue(
             wsValue, pFormNode->GetFormatDataValue(wsValue), false, false);
         pDataNode->JSObject()->SetCData(XFA_Attribute::ContentType,
@@ -656,9 +655,7 @@
       CXFA_Image* image = defValue ? defValue->GetImageIfExists() : nullptr;
       if (image) {
         CFX_XMLElement* pXMLDataElement =
-            static_cast<CFX_XMLElement*>(pDataNode->GetXMLMappingNode());
-        ASSERT(pXMLDataElement);
-
+            ToXMLElement(pDataNode->GetXMLMappingNode());
         WideString wsContentType =
             pXMLDataElement->GetAttribute(L"xfa:contentType");
         if (!wsContentType.IsEmpty()) {
diff --git a/xfa/fxfa/parser/cxfa_document_parser.cpp b/xfa/fxfa/parser/cxfa_document_parser.cpp
index b126d82..3495582 100644
--- a/xfa/fxfa/parser/cxfa_document_parser.cpp
+++ b/xfa/fxfa/parser/cxfa_document_parser.cpp
@@ -115,10 +115,10 @@
                    const WideStringView& wsLocalTagName,
                    const WideStringView& wsNamespaceURIPrefix,
                    uint32_t eMatchFlags = XFA_XDPPACKET_FLAGS_NOMATCH) {
-  if (!pNode || pNode->GetType() != FX_XMLNODE_Element)
+  CFX_XMLElement* pElement = ToXMLElement(pNode);
+  if (!pElement)
     return false;
 
-  CFX_XMLElement* pElement = static_cast<CFX_XMLElement*>(pNode);
   WideString wsNodeStr = pElement->GetLocalTagName();
   if (wsNodeStr != wsLocalTagName)
     return false;
@@ -449,7 +449,7 @@
   m_pRootNode = pXFARootNode;
   pXFARootNode->JSObject()->SetCData(XFA_Attribute::Name, L"xfa", false, false);
 
-  CFX_XMLElement* pElement = static_cast<CFX_XMLElement*>(pXMLDocumentNode);
+  CFX_XMLElement* pElement = ToXMLElement(pXMLDocumentNode);
   for (auto it : pElement->GetAttributes()) {
     if (it.first == L"uuid")
       pXFARootNode->JSObject()->SetCData(XFA_Attribute::Uuid, it.second, false,
@@ -483,12 +483,10 @@
   CFX_XMLNode* pXMLTemplateDOMRoot = nullptr;
   for (CFX_XMLNode* pChildItem = pXMLDocumentNode->GetFirstChild(); pChildItem;
        pChildItem = pChildItem->GetNextSibling()) {
-    if (!pChildItem || pChildItem->GetType() != FX_XMLNODE_Element)
-      continue;
-    if (pChildItem == pXMLConfigDOMRoot)
+    CFX_XMLElement* pElement = ToXMLElement(pChildItem);
+    if (!pElement || pElement == pXMLConfigDOMRoot)
       continue;
 
-    CFX_XMLElement* pElement = static_cast<CFX_XMLElement*>(pChildItem);
     WideString wsPacketName = pElement->GetLocalTagName();
     const PacketInfo* pPacketInfo =
         GetPacketByName(wsPacketName.AsStringView());
@@ -591,8 +589,7 @@
 
   pNode->JSObject()->SetCData(XFA_Attribute::Name, packet->name, false, false);
 
-  CFX_XMLElement* pXMLDocumentElement =
-      static_cast<CFX_XMLElement*>(pXMLDocumentNode);
+  CFX_XMLElement* pXMLDocumentElement = ToXMLElement(pXMLDocumentNode);
   WideString wsNamespaceURI = pXMLDocumentElement->GetNamespaceURI();
   if (wsNamespaceURI.IsEmpty())
     wsNamespaceURI = pXMLDocumentElement->GetAttribute(L"xmlns:xfa");
@@ -661,8 +658,7 @@
 
   CFX_XMLNode* pDataXMLNode = nullptr;
   if (MatchNodeName(pXMLDocumentNode, L"data", packet->uri, packet->flags)) {
-    static_cast<CFX_XMLElement*>(pXMLDocumentNode)
-        ->RemoveAttribute(L"xmlns:xfa");
+    ToXMLElement(pXMLDocumentNode)->RemoveAttribute(L"xmlns:xfa");
     pDataXMLNode = pXMLDocumentNode;
   } else {
     auto* pDataElement = xml_doc_->CreateNode<CFX_XMLElement>(L"xfa:data");
@@ -686,8 +682,7 @@
   if (!pNode)
     return nullptr;
 
-  WideString wsLocalName =
-      static_cast<CFX_XMLElement*>(pDataXMLNode)->GetLocalTagName();
+  WideString wsLocalName = ToXMLElement(pDataXMLNode)->GetLocalTagName();
   pNode->JSObject()->SetCData(XFA_Attribute::Name, wsLocalName, false, false);
   if (!DataLoader(pNode, pDataXMLNode, true))
     return nullptr;
@@ -742,8 +737,7 @@
   if (!pNode)
     return nullptr;
 
-  WideString wsName =
-      static_cast<CFX_XMLElement*>(pXMLDocumentNode)->GetLocalTagName();
+  WideString wsName = ToXMLElement(pXMLDocumentNode)->GetLocalTagName();
   pNode->JSObject()->SetCData(XFA_Attribute::Name, wsName, false, false);
   if (!UserPacketLoader(pNode, pXMLDocumentNode))
     return nullptr;
@@ -875,20 +869,18 @@
     if (eNodeType == FX_XMLNODE_Instruction)
       continue;
 
+    CFX_XMLElement* pElement = ToXMLElement(pXMLChild);
     if (element == XFA_Element::SharpxHTML) {
-      if (eNodeType != FX_XMLNODE_Element)
+      if (!pElement)
         break;
-
-      if (XFA_RecognizeRichText(static_cast<CFX_XMLElement*>(pXMLChild)))
-        wsValue +=
-            GetPlainTextFromRichText(static_cast<CFX_XMLElement*>(pXMLChild));
+      if (XFA_RecognizeRichText(pElement))
+        wsValue += GetPlainTextFromRichText(pElement);
     } else if (element == XFA_Element::Sharpxml) {
-      if (eNodeType != FX_XMLNODE_Element)
+      if (!pElement)
         break;
-
-      ConvertXMLToPlainText(static_cast<CFX_XMLElement*>(pXMLChild), wsValue);
+      ConvertXMLToPlainText(pElement, wsValue);
     } else {
-      if (eNodeType == FX_XMLNODE_Element)
+      if (pElement)
         break;
       if (eNodeType == FX_XMLNODE_Text || eNodeType == FX_XMLNODE_CharData)
         wsValue = static_cast<CFX_XMLText*>(pXMLChild)->GetText();
@@ -951,12 +943,10 @@
         if (eNodeType == XFA_Element::DataModel) {
           for (CFX_XMLNode* pXMLDataChild = pXMLElement->GetFirstChild();
                pXMLDataChild; pXMLDataChild = pXMLDataChild->GetNextSibling()) {
-            if (pXMLDataChild->GetType() == FX_XMLNODE_Element) {
-              if (!XFA_RecognizeRichText(
-                      static_cast<CFX_XMLElement*>(pXMLDataChild))) {
-                eNodeType = XFA_Element::DataGroup;
-                break;
-              }
+            CFX_XMLElement* pElement = ToXMLElement(pXMLDataChild);
+            if (pElement && !XFA_RecognizeRichText(pElement)) {
+              eNodeType = XFA_Element::DataGroup;
+              break;
             }
           }
         }
@@ -1063,9 +1053,8 @@
         pXMLCurValueNode = pXMLChild;
 
       wsCurValueTextBuf << wsText;
-    } else if (XFA_RecognizeRichText(static_cast<CFX_XMLElement*>(pXMLChild))) {
-      WideString wsText =
-          GetPlainTextFromRichText(static_cast<CFX_XMLElement*>(pXMLChild));
+    } else if (XFA_RecognizeRichText(ToXMLElement(pXMLChild))) {
+      WideString wsText = GetPlainTextFromRichText(ToXMLElement(pXMLChild));
       if (!pXMLCurValueNode)
         pXMLCurValueNode = pXMLChild;
 
@@ -1097,8 +1086,7 @@
       if (!pXFAChild)
         return;
 
-      WideString wsNodeStr =
-          static_cast<CFX_XMLElement*>(pXMLChild)->GetLocalTagName();
+      WideString wsNodeStr = ToXMLElement(pXMLChild)->GetLocalTagName();
       pXFAChild->JSObject()->SetCData(XFA_Attribute::Name, wsNodeStr, false,
                                       false);
       ParseDataValue(pXFAChild, pXMLChild, ePacketID);
diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp
index 363af54..9fdea14 100644
--- a/xfa/fxfa/parser/cxfa_node.cpp
+++ b/xfa/fxfa/parser/cxfa_node.cpp
@@ -1259,11 +1259,9 @@
     return;
   }
 
-  ASSERT(pNode->xml_node_.Get() == xml_node_.Get() &&
-         xml_node_->GetType() == FX_XMLNODE_Element);
-  if (pNode->xml_node_->GetType() == FX_XMLNODE_Element) {
-    CFX_XMLElement* pXMLElement =
-        static_cast<CFX_XMLElement*>(pNode->xml_node_.Get());
+  ASSERT(pNode->xml_node_.Get() == xml_node_.Get());
+  CFX_XMLElement* pXMLElement = ToXMLElement(pNode->xml_node_.Get());
+  if (pXMLElement) {
     WideString wsAttributeName =
         pNode->JSObject()->GetCData(XFA_Attribute::QualifiedName);
     pXMLElement->RemoveAttribute(wsAttributeName);
@@ -4700,10 +4698,10 @@
 }
 
 void CXFA_Node::SetToXML(const WideString& value) {
-  auto* elem = static_cast<CFX_XMLElement*>(GetXMLMappingNode());
-  FX_XMLNODETYPE eXMLType = elem->GetType();
-  switch (eXMLType) {
+  auto* pNode = GetXMLMappingNode();
+  switch (pNode->GetType()) {
     case FX_XMLNODE_Element: {
+      auto* elem = static_cast<CFX_XMLElement*>(pNode);
       if (IsAttributeInXML()) {
         elem->SetAttribute(JSObject()->GetCData(XFA_Attribute::QualifiedName),
                            value);
diff --git a/xfa/fxfa/parser/cxfa_xmllocale.cpp b/xfa/fxfa/parser/cxfa_xmllocale.cpp
index 1922b31..e4858e9 100644
--- a/xfa/fxfa/parser/cxfa_xmllocale.cpp
+++ b/xfa/fxfa/parser/cxfa_xmllocale.cpp
@@ -41,17 +41,15 @@
   CFX_XMLElement* locale = nullptr;
   for (auto* child = doc->GetRoot()->GetFirstChild(); child;
        child = child->GetNextSibling()) {
-    if (child->GetType() != FX_XMLNODE_Element)
-      continue;
-
-    CFX_XMLElement* elem = static_cast<CFX_XMLElement*>(child);
-    if (elem->GetName() == L"locale") {
+    CFX_XMLElement* elem = ToXMLElement(child);
+    if (elem && elem->GetName() == L"locale") {
       locale = elem;
       break;
     }
   }
   if (!locale)
     return nullptr;
+
   return pdfium::MakeUnique<CXFA_XMLLocale>(std::move(doc), locale);
 }
 
@@ -129,11 +127,8 @@
   CFX_XMLElement* name_child = nullptr;
   for (auto* name = child->GetFirstChild(); name;
        name = name->GetNextSibling()) {
-    if (name->GetType() != FX_XMLNODE_Element)
-      continue;
-
-    auto* elem = static_cast<CFX_XMLElement*>(name);
-    if (elem->GetName() != pstrSymbolNames)
+    CFX_XMLElement* elem = ToXMLElement(name);
+    if (!elem || elem->GetName() != pstrSymbolNames)
       continue;
 
     WideString abbr = elem->GetAttribute(L"abbr");
@@ -213,16 +208,11 @@
                                       const WideStringView& wsName) const {
   for (auto* child = patterns->GetFirstChild(); child;
        child = child->GetNextSibling()) {
-    if (child->GetType() != FX_XMLNODE_Element)
-      continue;
-
-    CFX_XMLElement* pattern = static_cast<CFX_XMLElement*>(child);
-    if (pattern->GetName() != bsTag)
-      continue;
-    if (pattern->GetAttribute(L"name") != wsName)
-      continue;
-
-    return pattern->GetTextData();
+    CFX_XMLElement* pattern = ToXMLElement(child);
+    if (pattern && pattern->GetName() == bsTag &&
+        pattern->GetAttribute(L"name") == wsName) {
+      return pattern->GetTextData();
+    }
   }
   return L"";
 }
diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp
index 54cca88..1331a43 100644
--- a/xfa/fxfa/parser/xfa_utils.cpp
+++ b/xfa/fxfa/parser/xfa_utils.cpp
@@ -470,11 +470,8 @@
   }
   for (CFX_XMLNode* pParent = pNode; pParent != pFakeRoot;
        pParent = pParent->GetParent()) {
-    if (pParent->GetType() != FX_XMLNODE_Element)
-      continue;
-
-    auto* pElement = static_cast<CFX_XMLElement*>(pParent);
-    if (pElement->HasAttribute(wsNSAttribute)) {
+    CFX_XMLElement* pElement = ToXMLElement(pParent);
+    if (pElement && pElement->HasAttribute(wsNSAttribute)) {
       *wsNamespaceURI = pElement->GetAttribute(wsNSAttribute);
       return true;
     }