summaryrefslogtreecommitdiff
path: root/core/fxcrt/xml
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2018-07-28 00:00:24 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-07-28 00:00:24 +0000
commitf19ae5dcd0b618cdeb80d6a1df5b13610d0ff7da (patch)
tree6e834ca7ebaff809e6e8d352c41bb1f8d4ec4203 /core/fxcrt/xml
parent0415e02db77da26495dc0fbd80de76ce1d777585 (diff)
downloadpdfium-f19ae5dcd0b618cdeb80d6a1df5b13610d0ff7da.tar.xz
Add ToXML{Instruction,Text,CharData}() checked conversion functions
All usages were previously checked correctly, but this consolidates some code as well. Change-Id: I63711748b31b698a3f21f98fdb536db1e9e0b1cf Reviewed-on: https://pdfium-review.googlesource.com/39010 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'core/fxcrt/xml')
-rw-r--r--core/fxcrt/xml/cfx_xmlchardata.h6
-rw-r--r--core/fxcrt/xml/cfx_xmlchardata_unittest.cpp2
-rw-r--r--core/fxcrt/xml/cfx_xmlelement.cpp8
-rw-r--r--core/fxcrt/xml/cfx_xmlelement_unittest.cpp2
-rw-r--r--core/fxcrt/xml/cfx_xmlinstruction.h6
-rw-r--r--core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp13
-rw-r--r--core/fxcrt/xml/cfx_xmlparser.cpp8
-rw-r--r--core/fxcrt/xml/cfx_xmlparser_unittest.cpp3
-rw-r--r--core/fxcrt/xml/cfx_xmltext.h9
-rw-r--r--core/fxcrt/xml/cfx_xmltext_unittest.cpp2
10 files changed, 35 insertions, 24 deletions
diff --git a/core/fxcrt/xml/cfx_xmlchardata.h b/core/fxcrt/xml/cfx_xmlchardata.h
index 013414bc82..9e5669a464 100644
--- a/core/fxcrt/xml/cfx_xmlchardata.h
+++ b/core/fxcrt/xml/cfx_xmlchardata.h
@@ -25,4 +25,10 @@ class CFX_XMLCharData : public CFX_XMLText {
void Save(const RetainPtr<IFX_SeekableWriteStream>& pXMLStream) override;
};
+inline CFX_XMLCharData* ToXMLCharData(CFX_XMLNode* pNode) {
+ return pNode && pNode->GetType() == FX_XMLNODE_CharData
+ ? static_cast<CFX_XMLCharData*>(pNode)
+ : nullptr;
+}
+
#endif // CORE_FXCRT_XML_CFX_XMLCHARDATA_H_
diff --git a/core/fxcrt/xml/cfx_xmlchardata_unittest.cpp b/core/fxcrt/xml/cfx_xmlchardata_unittest.cpp
index 4f0c49eaac..41906edbfd 100644
--- a/core/fxcrt/xml/cfx_xmlchardata_unittest.cpp
+++ b/core/fxcrt/xml/cfx_xmlchardata_unittest.cpp
@@ -26,7 +26,7 @@ TEST(CFX_XMLCharDataTest, Clone) {
EXPECT_TRUE(clone != nullptr);
EXPECT_NE(&data, clone);
ASSERT_EQ(FX_XMLNODE_CharData, clone->GetType());
- EXPECT_EQ(L"My Data", static_cast<CFX_XMLCharData*>(clone)->GetText());
+ EXPECT_EQ(L"My Data", ToXMLCharData(clone)->GetText());
}
TEST(CFX_XMLCharDataTest, Save) {
diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp
index 4bb4eae1bd..bd24091205 100644
--- a/core/fxcrt/xml/cfx_xmlelement.cpp
+++ b/core/fxcrt/xml/cfx_xmlelement.cpp
@@ -76,13 +76,11 @@ WideString CFX_XMLElement::GetNamespaceURI() const {
WideString CFX_XMLElement::GetTextData() const {
CFX_WideTextBuf buffer;
-
for (CFX_XMLNode* pChild = GetFirstChild(); pChild;
pChild = pChild->GetNextSibling()) {
- if (pChild->GetType() == FX_XMLNODE_Text ||
- pChild->GetType() == FX_XMLNODE_CharData) {
- buffer << static_cast<CFX_XMLText*>(pChild)->GetText();
- }
+ CFX_XMLText* pText = ToXMLText(pChild);
+ if (pText)
+ buffer << pText->GetText();
}
return buffer.MakeString();
}
diff --git a/core/fxcrt/xml/cfx_xmlelement_unittest.cpp b/core/fxcrt/xml/cfx_xmlelement_unittest.cpp
index dfc60a10c3..a1c63589ec 100644
--- a/core/fxcrt/xml/cfx_xmlelement_unittest.cpp
+++ b/core/fxcrt/xml/cfx_xmlelement_unittest.cpp
@@ -104,7 +104,7 @@ TEST(CFX_XMLElementTest, Clone) {
EXPECT_TRUE(inst->GetFirstChild()->GetNextSibling() == nullptr);
ASSERT_EQ(FX_XMLNODE_Text, inst->GetFirstChild()->GetType());
- auto* text = static_cast<CFX_XMLText*>(inst->GetFirstChild());
+ auto* text = ToXMLText(inst->GetFirstChild());
EXPECT_EQ(L"Text Child", text->GetText());
}
diff --git a/core/fxcrt/xml/cfx_xmlinstruction.h b/core/fxcrt/xml/cfx_xmlinstruction.h
index 153ef2b625..8962d73ac1 100644
--- a/core/fxcrt/xml/cfx_xmlinstruction.h
+++ b/core/fxcrt/xml/cfx_xmlinstruction.h
@@ -36,4 +36,10 @@ class CFX_XMLInstruction : public CFX_XMLNode {
std::vector<WideString> m_TargetData;
};
+inline CFX_XMLInstruction* ToXMLInstruction(CFX_XMLNode* pNode) {
+ return pNode && pNode->GetType() == FX_XMLNODE_Instruction
+ ? static_cast<CFX_XMLInstruction*>(pNode)
+ : nullptr;
+}
+
#endif // CORE_FXCRT_XML_CFX_XMLINSTRUCTION_H_
diff --git a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
index b25a18be9c..0109ab2985 100644
--- a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
+++ b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
@@ -53,8 +53,7 @@ TEST(CFX_XMLInstructionTest, Clone) {
EXPECT_TRUE(clone != nullptr);
ASSERT_EQ(FX_XMLNODE_Instruction, clone->GetType());
- CFX_XMLInstruction* inst = static_cast<CFX_XMLInstruction*>(clone);
-
+ CFX_XMLInstruction* inst = ToXMLInstruction(clone);
EXPECT_TRUE(inst->IsAcrobat());
auto& data = inst->GetTargetData();
@@ -98,8 +97,7 @@ TEST(CFX_XMLInstructionTest, ParseAndReSave) {
ASSERT_TRUE(root->GetFirstChild() != nullptr);
ASSERT_EQ(FX_XMLNODE_Instruction, root->GetFirstChild()->GetType());
- CFX_XMLInstruction* node =
- static_cast<CFX_XMLInstruction*>(root->GetFirstChild());
+ CFX_XMLInstruction* node = ToXMLInstruction(root->GetFirstChild());
ASSERT_TRUE(node != nullptr);
EXPECT_TRUE(node->IsAcrobat());
@@ -136,12 +134,9 @@ TEST(CFX_XMLInstructionTest, ParseAndReSaveInnerInstruction) {
EXPECT_EQ(L"node", node->GetName());
CFX_XMLInstruction* instruction = nullptr;
- for (auto* elem = node->GetFirstChild(); elem;
+ for (auto* elem = node->GetFirstChild(); elem && !instruction;
elem = elem->GetNextSibling()) {
- if (elem->GetType() == FX_XMLNODE_Instruction) {
- instruction = static_cast<CFX_XMLInstruction*>(elem);
- break;
- }
+ instruction = ToXMLInstruction(elem);
}
ASSERT_TRUE(instruction != nullptr);
EXPECT_TRUE(instruction->IsAcrobat());
diff --git a/core/fxcrt/xml/cfx_xmlparser.cpp b/core/fxcrt/xml/cfx_xmlparser.cpp
index dd28cf8adc..094daac889 100644
--- a/core/fxcrt/xml/cfx_xmlparser.cpp
+++ b/core/fxcrt/xml/cfx_xmlparser.cpp
@@ -518,12 +518,10 @@ void CFX_XMLParser::ProcessTargetData() {
WideString target_data = GetTextData();
if (target_data.IsEmpty())
return;
- if (!current_node_)
- return;
- if (current_node_->GetType() != FX_XMLNODE_Instruction)
- return;
- static_cast<CFX_XMLInstruction*>(current_node_)->AppendData(target_data);
+ CFX_XMLInstruction* instruction = ToXMLInstruction(current_node_);
+ if (instruction)
+ instruction->AppendData(target_data);
}
WideString CFX_XMLParser::GetTextData() {
diff --git a/core/fxcrt/xml/cfx_xmlparser_unittest.cpp b/core/fxcrt/xml/cfx_xmlparser_unittest.cpp
index 300131db95..c1236ce182 100644
--- a/core/fxcrt/xml/cfx_xmlparser_unittest.cpp
+++ b/core/fxcrt/xml/cfx_xmlparser_unittest.cpp
@@ -325,8 +325,7 @@ TEST_F(CFX_XMLParserTest, ParseInstruction) {
ASSERT_TRUE(root->GetFirstChild() != nullptr);
ASSERT_EQ(FX_XMLNODE_Instruction, root->GetFirstChild()->GetType());
- CFX_XMLInstruction* instruction =
- static_cast<CFX_XMLInstruction*>(root->GetFirstChild());
+ CFX_XMLInstruction* instruction = ToXMLInstruction(root->GetFirstChild());
EXPECT_TRUE(instruction->IsOriginalXFAVersion());
}
diff --git a/core/fxcrt/xml/cfx_xmltext.h b/core/fxcrt/xml/cfx_xmltext.h
index c5c168059d..f7feae669e 100644
--- a/core/fxcrt/xml/cfx_xmltext.h
+++ b/core/fxcrt/xml/cfx_xmltext.h
@@ -31,4 +31,13 @@ class CFX_XMLText : public CFX_XMLNode {
WideString m_wsText;
};
+inline bool IsXMLText(CFX_XMLNode* pNode) {
+ FX_XMLNODETYPE type = pNode->GetType();
+ return type == FX_XMLNODE_Text || type == FX_XMLNODE_CharData;
+}
+
+inline CFX_XMLText* ToXMLText(CFX_XMLNode* pNode) {
+ return pNode && IsXMLText(pNode) ? static_cast<CFX_XMLText*>(pNode) : nullptr;
+}
+
#endif // CORE_FXCRT_XML_CFX_XMLTEXT_H_
diff --git a/core/fxcrt/xml/cfx_xmltext_unittest.cpp b/core/fxcrt/xml/cfx_xmltext_unittest.cpp
index 6e53f66612..04b1ee8571 100644
--- a/core/fxcrt/xml/cfx_xmltext_unittest.cpp
+++ b/core/fxcrt/xml/cfx_xmltext_unittest.cpp
@@ -25,7 +25,7 @@ TEST(CFX_XMLTextTest, Clone) {
CFX_XMLNode* clone = data.Clone(&doc);
EXPECT_TRUE(clone != nullptr);
ASSERT_EQ(FX_XMLNODE_Text, clone->GetType());
- EXPECT_EQ(L"My Data", static_cast<CFX_XMLText*>(clone)->GetText());
+ EXPECT_EQ(L"My Data", ToXMLText(clone)->GetText());
}
TEST(CFX_XMLTextTest, Save) {