summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/fxcrt/xml/cfx_xmlchardata.cpp8
-rw-r--r--core/fxcrt/xml/cfx_xmlchardata.h1
-rw-r--r--core/fxcrt/xml/cfx_xmlelement.cpp28
-rw-r--r--core/fxcrt/xml/cfx_xmlelement.h1
-rw-r--r--core/fxcrt/xml/cfx_xmlinstruction.cpp35
-rw-r--r--core/fxcrt/xml/cfx_xmlinstruction.h1
-rw-r--r--core/fxcrt/xml/cfx_xmlnode.cpp128
-rw-r--r--core/fxcrt/xml/cfx_xmlnode.h5
-rw-r--r--core/fxcrt/xml/cfx_xmltext.cpp4
-rw-r--r--core/fxcrt/xml/cfx_xmltext.h1
-rw-r--r--fxjs/xfa/cjx_node.cpp2
-rw-r--r--xfa/fxfa/parser/cxfa_dataexporter.cpp6
-rw-r--r--xfa/fxfa/parser/xfa_utils.cpp2
13 files changed, 106 insertions, 116 deletions
diff --git a/core/fxcrt/xml/cfx_xmlchardata.cpp b/core/fxcrt/xml/cfx_xmlchardata.cpp
index 902d139c99..7712b30dfe 100644
--- a/core/fxcrt/xml/cfx_xmlchardata.cpp
+++ b/core/fxcrt/xml/cfx_xmlchardata.cpp
@@ -20,3 +20,11 @@ FX_XMLNODETYPE CFX_XMLCharData::GetType() const {
std::unique_ptr<CFX_XMLNode> CFX_XMLCharData::Clone() {
return pdfium::MakeUnique<CFX_XMLCharData>(GetText());
}
+
+void CFX_XMLCharData::Save(
+ const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
+ WideString ws = L"<![CDATA[";
+ ws += GetText();
+ ws += L"]]>";
+ pXMLStream->WriteString(ws.AsStringView());
+}
diff --git a/core/fxcrt/xml/cfx_xmlchardata.h b/core/fxcrt/xml/cfx_xmlchardata.h
index b7e691495f..c702d9636f 100644
--- a/core/fxcrt/xml/cfx_xmlchardata.h
+++ b/core/fxcrt/xml/cfx_xmlchardata.h
@@ -20,6 +20,7 @@ class CFX_XMLCharData : public CFX_XMLText {
// CFX_XMLNode
FX_XMLNODETYPE GetType() const override;
std::unique_ptr<CFX_XMLNode> Clone() override;
+ void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) override;
};
#endif // CORE_FXCRT_XML_CFX_XMLCHARDATA_H_
diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp
index 39233190b1..4eb3900c2f 100644
--- a/core/fxcrt/xml/cfx_xmlelement.cpp
+++ b/core/fxcrt/xml/cfx_xmlelement.cpp
@@ -101,3 +101,31 @@ void CFX_XMLElement::SetTextData(const WideString& wsText) {
return;
AppendChild(new CFX_XMLText(wsText));
}
+
+void CFX_XMLElement::Save(
+ const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
+ WideString ws(L"<");
+ ws += GetName();
+ pXMLStream->WriteString(ws.AsStringView());
+
+ for (auto it : GetAttributes()) {
+ pXMLStream->WriteString(
+ AttributeToString(it.first, it.second).AsStringView());
+ }
+
+ if (GetFirstChild()) {
+ ws = L"\n>";
+ pXMLStream->WriteString(ws.AsStringView());
+ CFX_XMLNode* pChild = GetFirstChild();
+ while (pChild) {
+ pChild->Save(pXMLStream);
+ pChild = pChild->GetNextSibling();
+ }
+ ws = L"</";
+ ws += GetName();
+ ws += L"\n>";
+ } else {
+ ws = L"\n/>";
+ }
+ pXMLStream->WriteString(ws.AsStringView());
+}
diff --git a/core/fxcrt/xml/cfx_xmlelement.h b/core/fxcrt/xml/cfx_xmlelement.h
index 59e3af6a44..e665e24a98 100644
--- a/core/fxcrt/xml/cfx_xmlelement.h
+++ b/core/fxcrt/xml/cfx_xmlelement.h
@@ -21,6 +21,7 @@ class CFX_XMLElement : public CFX_XMLAttributeNode {
// CFX_XMLNode
FX_XMLNODETYPE GetType() const override;
std::unique_ptr<CFX_XMLNode> Clone() override;
+ void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) override;
WideString GetLocalTagName() const;
WideString GetNamespacePrefix() const;
diff --git a/core/fxcrt/xml/cfx_xmlinstruction.cpp b/core/fxcrt/xml/cfx_xmlinstruction.cpp
index 75a4a2eec6..dabd538b4b 100644
--- a/core/fxcrt/xml/cfx_xmlinstruction.cpp
+++ b/core/fxcrt/xml/cfx_xmlinstruction.cpp
@@ -8,6 +8,7 @@
#include <utility>
+#include "core/fxcrt/fx_codepage.h"
#include "core/fxcrt/fx_extension.h"
#include "third_party/base/ptr_util.h"
#include "third_party/base/stl_util.h"
@@ -36,3 +37,37 @@ void CFX_XMLInstruction::RemoveData(int32_t index) {
if (pdfium::IndexInBounds(m_TargetData, index))
m_TargetData.erase(m_TargetData.begin() + index);
}
+
+void CFX_XMLInstruction::Save(
+ const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
+ if (GetName().CompareNoCase(L"xml") == 0) {
+ WideString ws = L"<?xml version=\"1.0\" encoding=\"";
+ uint16_t wCodePage = pXMLStream->GetCodePage();
+ if (wCodePage == FX_CODEPAGE_UTF16LE)
+ ws += L"UTF-16";
+ else if (wCodePage == FX_CODEPAGE_UTF16BE)
+ ws += L"UTF-16be";
+ else
+ ws += L"UTF-8";
+
+ ws += L"\"?>";
+ pXMLStream->WriteString(ws.AsStringView());
+ return;
+ }
+
+ pXMLStream->WriteString(
+ WideString::Format(L"<?%ls", GetName().c_str()).AsStringView());
+ for (auto it : GetAttributes()) {
+ pXMLStream->WriteString(
+ AttributeToString(it.first, it.second).AsStringView());
+ }
+
+ for (auto target : m_TargetData) {
+ WideString ws = L" \"";
+ ws += target;
+ ws += L"\"";
+ pXMLStream->WriteString(ws.AsStringView());
+ }
+
+ pXMLStream->WriteString(WideStringView(L"?>"));
+}
diff --git a/core/fxcrt/xml/cfx_xmlinstruction.h b/core/fxcrt/xml/cfx_xmlinstruction.h
index ff27dae3a4..415a86a379 100644
--- a/core/fxcrt/xml/cfx_xmlinstruction.h
+++ b/core/fxcrt/xml/cfx_xmlinstruction.h
@@ -21,6 +21,7 @@ class CFX_XMLInstruction : public CFX_XMLAttributeNode {
// CFX_XMLNode
FX_XMLNODETYPE GetType() const override;
std::unique_ptr<CFX_XMLNode> Clone() override;
+ void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) override;
const std::vector<WideString>& GetTargetData() const { return m_TargetData; }
void AppendData(const WideString& wsData);
diff --git a/core/fxcrt/xml/cfx_xmlnode.cpp b/core/fxcrt/xml/cfx_xmlnode.cpp
index 7cc506af9f..6135907557 100644
--- a/core/fxcrt/xml/cfx_xmlnode.cpp
+++ b/core/fxcrt/xml/cfx_xmlnode.cpp
@@ -8,7 +8,6 @@
#include <vector>
-#include "core/fxcrt/fx_codepage.h"
#include "core/fxcrt/xml/cfx_xmlchardata.h"
#include "core/fxcrt/xml/cfx_xmlelement.h"
#include "core/fxcrt/xml/cfx_xmlinstruction.h"
@@ -98,113 +97,22 @@ std::unique_ptr<CFX_XMLNode> CFX_XMLNode::Clone() {
return nullptr;
}
-void CFX_XMLNode::SaveXMLNode(
- const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
- CFX_XMLNode* pNode = (CFX_XMLNode*)this;
- switch (pNode->GetType()) {
- case FX_XMLNODE_Instruction: {
- WideString ws;
- CFX_XMLInstruction* pInstruction = (CFX_XMLInstruction*)pNode;
- if (pInstruction->GetName().CompareNoCase(L"xml") == 0) {
- ws = L"<?xml version=\"1.0\" encoding=\"";
- uint16_t wCodePage = pXMLStream->GetCodePage();
- if (wCodePage == FX_CODEPAGE_UTF16LE) {
- ws += L"UTF-16";
- } else if (wCodePage == FX_CODEPAGE_UTF16BE) {
- ws += L"UTF-16be";
- } else {
- ws += L"UTF-8";
- }
- ws += L"\"?>";
- pXMLStream->WriteString(ws.AsStringView());
- } else {
- ws = WideString::Format(L"<?%ls", pInstruction->GetName().c_str());
- pXMLStream->WriteString(ws.AsStringView());
-
- for (auto it : pInstruction->GetAttributes()) {
- WideString wsValue = it.second;
- wsValue.Replace(L"&", L"&amp;");
- wsValue.Replace(L"<", L"&lt;");
- wsValue.Replace(L">", L"&gt;");
- wsValue.Replace(L"\'", L"&apos;");
- wsValue.Replace(L"\"", L"&quot;");
-
- ws = L" ";
- ws += it.first;
- ws += L"=\"";
- ws += wsValue;
- ws += L"\"";
- pXMLStream->WriteString(ws.AsStringView());
- }
-
- for (auto target : pInstruction->GetTargetData()) {
- ws = L" \"";
- ws += target;
- ws += L"\"";
- pXMLStream->WriteString(ws.AsStringView());
- }
- ws = L"?>";
- pXMLStream->WriteString(ws.AsStringView());
- }
- break;
- }
- case FX_XMLNODE_Element: {
- WideString ws;
- ws = L"<";
- ws += static_cast<CFX_XMLElement*>(pNode)->GetName();
- pXMLStream->WriteString(ws.AsStringView());
-
- for (auto it : static_cast<CFX_XMLElement*>(pNode)->GetAttributes()) {
- WideString wsValue = it.second;
- wsValue.Replace(L"&", L"&amp;");
- wsValue.Replace(L"<", L"&lt;");
- wsValue.Replace(L">", L"&gt;");
- wsValue.Replace(L"\'", L"&apos;");
- wsValue.Replace(L"\"", L"&quot;");
-
- ws = L" ";
- ws += it.first;
- ws += L"=\"";
- ws += wsValue;
- ws += L"\"";
- pXMLStream->WriteString(ws.AsStringView());
- }
- if (pNode->first_child_) {
- ws = L"\n>";
- pXMLStream->WriteString(ws.AsStringView());
- CFX_XMLNode* pChild = pNode->first_child_;
- while (pChild) {
- pChild->SaveXMLNode(pXMLStream);
- pChild = pChild->next_sibling_;
- }
- ws = L"</";
- ws += static_cast<CFX_XMLElement*>(pNode)->GetName();
- ws += L"\n>";
- } else {
- ws = L"\n/>";
- }
- pXMLStream->WriteString(ws.AsStringView());
- break;
- }
- case FX_XMLNODE_Text: {
- WideString ws = static_cast<CFX_XMLText*>(pNode)->GetText();
- ws.Replace(L"&", L"&amp;");
- ws.Replace(L"<", L"&lt;");
- ws.Replace(L">", L"&gt;");
- ws.Replace(L"\'", L"&apos;");
- ws.Replace(L"\"", L"&quot;");
- pXMLStream->WriteString(ws.AsStringView());
- break;
- }
- case FX_XMLNODE_CharData: {
- WideString ws = L"<![CDATA[";
- ws += static_cast<CFX_XMLCharData*>(pNode)->GetText();
- ws += L"]]>";
- pXMLStream->WriteString(ws.AsStringView());
- break;
- }
- case FX_XMLNODE_Unknown:
- default:
- break;
- }
+void CFX_XMLNode::Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {}
+
+WideString CFX_XMLNode::EncodeEntities(WideString value) {
+ value.Replace(L"&", L"&amp;");
+ value.Replace(L"<", L"&lt;");
+ value.Replace(L">", L"&gt;");
+ value.Replace(L"\'", L"&apos;");
+ value.Replace(L"\"", L"&quot;");
+ return value;
+}
+
+WideString CFX_XMLNode::AttributeToString(WideString name, WideString value) {
+ WideString ret = L" ";
+ ret += name;
+ ret += L"=\"";
+ ret += EncodeEntities(value);
+ ret += L"\"";
+ return ret;
}
diff --git a/core/fxcrt/xml/cfx_xmlnode.h b/core/fxcrt/xml/cfx_xmlnode.h
index 55c79dcfe0..e2154c4f71 100644
--- a/core/fxcrt/xml/cfx_xmlnode.h
+++ b/core/fxcrt/xml/cfx_xmlnode.h
@@ -32,6 +32,7 @@ class CFX_XMLNode {
virtual FX_XMLNODETYPE GetType() const;
virtual std::unique_ptr<CFX_XMLNode> Clone();
+ virtual void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream);
CFX_XMLNode* GetRoot();
CFX_XMLNode* GetParent() const { return parent_; }
@@ -43,7 +44,9 @@ class CFX_XMLNode {
void RemoveChildNode(CFX_XMLNode* pNode);
void DeleteChildren();
- void SaveXMLNode(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream);
+ protected:
+ WideString AttributeToString(WideString name, WideString value);
+ WideString EncodeEntities(WideString value);
private:
CFX_XMLNode* parent_ = nullptr;
diff --git a/core/fxcrt/xml/cfx_xmltext.cpp b/core/fxcrt/xml/cfx_xmltext.cpp
index 83ad0434bf..05e2935440 100644
--- a/core/fxcrt/xml/cfx_xmltext.cpp
+++ b/core/fxcrt/xml/cfx_xmltext.cpp
@@ -20,3 +20,7 @@ FX_XMLNODETYPE CFX_XMLText::GetType() const {
std::unique_ptr<CFX_XMLNode> CFX_XMLText::Clone() {
return pdfium::MakeUnique<CFX_XMLText>(m_wsText);
}
+
+void CFX_XMLText::Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
+ pXMLStream->WriteString(EncodeEntities(GetText()).AsStringView());
+}
diff --git a/core/fxcrt/xml/cfx_xmltext.h b/core/fxcrt/xml/cfx_xmltext.h
index e9f35855f5..b26eef14ac 100644
--- a/core/fxcrt/xml/cfx_xmltext.h
+++ b/core/fxcrt/xml/cfx_xmltext.h
@@ -20,6 +20,7 @@ class CFX_XMLText : public CFX_XMLNode {
// CFX_XMLNode
FX_XMLNODETYPE GetType() const override;
std::unique_ptr<CFX_XMLNode> Clone() override;
+ void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) override;
WideString GetText() const { return m_wsText; }
void SetText(const WideString& wsText) { m_wsText = wsText; }
diff --git a/fxjs/xfa/cjx_node.cpp b/fxjs/xfa/cjx_node.cpp
index 8009208561..d3a9bf17c1 100644
--- a/fxjs/xfa/cjx_node.cpp
+++ b/fxjs/xfa/cjx_node.cpp
@@ -358,7 +358,7 @@ CJS_Return CJX_Node::saveXML(CFX_V8* runtime,
if (GetXFANode()->GetPacketType() == XFA_PacketType::Form)
XFA_DataExporter_RegenerateFormFile(GetXFANode(), pStream, nullptr, true);
else
- pElement->SaveXMLNode(pStream);
+ pElement->Save(pStream);
return CJS_Return(runtime->NewString(
ByteStringView(pMemoryStream->GetBuffer(), pMemoryStream->GetSize())));
diff --git a/xfa/fxfa/parser/cxfa_dataexporter.cpp b/xfa/fxfa/parser/cxfa_dataexporter.cpp
index b254ce6cad..4f10d0ddfe 100644
--- a/xfa/fxfa/parser/cxfa_dataexporter.cpp
+++ b/xfa/fxfa/parser/cxfa_dataexporter.cpp
@@ -65,7 +65,7 @@ bool CXFA_DataExporter::Export(
CXFA_Node* pDataNode = pNode->GetFirstChild();
ASSERT(pDataNode);
XFA_DataExporter_DealWithDataGroupNode(pDataNode);
- pElement->SaveXMLNode(pStream);
+ pElement->Save(pStream);
break;
}
case XFA_PacketType::Form: {
@@ -79,7 +79,7 @@ bool CXFA_DataExporter::Export(
if (!pElement || pElement->GetType() != FX_XMLNODE_Element)
return false;
- pElement->SaveXMLNode(pStream);
+ pElement->Save(pStream);
break;
}
}
@@ -102,7 +102,7 @@ bool CXFA_DataExporter::Export(
XFA_DataExporter_DealWithDataGroupNode(pExportNode);
pElement->SetString(L"xmlns:xfa", L"http://www.xfa.org/schema/xfa-data/1.0/");
- pElement->SaveXMLNode(pStream);
+ pElement->Save(pStream);
pElement->RemoveAttribute(L"xmlns:xfa");
return true;
diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp
index 66b4e9d561..65be9625ba 100644
--- a/xfa/fxfa/parser/xfa_utils.cpp
+++ b/xfa/fxfa/parser/xfa_utils.cpp
@@ -220,7 +220,7 @@ void RegenerateFormFile_Changed(CXFA_Node* pNode,
pdfium::MakeRetain<CFX_SeekableStreamProxy>(pMemStream, true);
pTempStream->SetCodePage(FX_CODEPAGE_UTF8);
- pRichTextXML->SaveXMLNode(pTempStream);
+ pRichTextXML->Save(pTempStream);
wsChildren += WideString::FromUTF8(
ByteStringView(pMemStream->GetBuffer(), pMemStream->GetSize()));
} else if (pRawValueNode->GetElementType() == XFA_Element::Sharpxml &&