1 // Copyright 2014 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "fxjs/xfa/cfxjse_nodehelper.h"
8
9 #include "core/fxcrt/fx_extension.h"
10 #include "fxjs/xfa/cfxjse_engine.h"
11 #include "fxjs/xfa/cjx_object.h"
12 #include "xfa/fxfa/parser/cxfa_document.h"
13 #include "xfa/fxfa/parser/cxfa_localemgr.h"
14 #include "xfa/fxfa/parser/cxfa_node.h"
15 #include "xfa/fxfa/parser/xfa_basic_data.h"
16 #include "xfa/fxfa/parser/xfa_utils.h"
17
18 CFXJSE_NodeHelper::CFXJSE_NodeHelper() = default;
19
20 CFXJSE_NodeHelper::~CFXJSE_NodeHelper() = default;
21
CreateNodeForCondition(const WideString & wsCondition)22 bool CFXJSE_NodeHelper::CreateNodeForCondition(const WideString& wsCondition) {
23 size_t szLen = wsCondition.GetLength();
24 WideString wsIndex(L"0");
25 bool bAll = false;
26 if (szLen == 0) {
27 m_iCreateFlag = CFXJSE_Engine::ResolveResult::Type::kCreateNodeOne;
28 return false;
29 }
30 if (wsCondition[0] != '[')
31 return false;
32
33 size_t i = 1;
34 for (; i < szLen; ++i) {
35 wchar_t ch = wsCondition[i];
36 if (ch == ' ')
37 continue;
38
39 if (ch == '*')
40 bAll = true;
41 break;
42 }
43 if (bAll) {
44 wsIndex = L"1";
45 m_iCreateFlag = CFXJSE_Engine::ResolveResult::Type::kCreateNodeAll;
46 } else {
47 m_iCreateFlag = CFXJSE_Engine::ResolveResult::Type::kCreateNodeOne;
48 wsIndex = wsCondition.Substr(i, szLen - 1 - i);
49 }
50 int32_t iCount = wsIndex.GetInteger();
51 if (iCount < 0)
52 return false;
53
54 m_iCreateCount = iCount;
55 return true;
56 }
57
CreateNode(const WideString & wsName,const WideString & wsCondition,bool bLastNode,CFXJSE_Engine * pScriptContext)58 bool CFXJSE_NodeHelper::CreateNode(const WideString& wsName,
59 const WideString& wsCondition,
60 bool bLastNode,
61 CFXJSE_Engine* pScriptContext) {
62 if (!m_pCreateParent)
63 return false;
64
65 WideStringView wsNameView = wsName.AsStringView();
66 bool bIsClassName = false;
67 bool bResult = false;
68 if (!wsNameView.IsEmpty() && wsNameView[0] == '!') {
69 wsNameView = wsNameView.Last(wsNameView.GetLength() - 1);
70 m_pCreateParent = ToNode(
71 pScriptContext->GetDocument()->GetXFAObject(XFA_HASHCODE_Datasets));
72 }
73 if (!wsNameView.IsEmpty() && wsNameView[0] == '#') {
74 bIsClassName = true;
75 wsNameView = wsNameView.Last(wsNameView.GetLength() - 1);
76 }
77 if (wsNameView.IsEmpty())
78 return false;
79
80 if (m_iCreateCount == 0)
81 CreateNodeForCondition(wsCondition);
82
83 if (bIsClassName) {
84 XFA_Element eType = XFA_GetElementByName(wsNameView);
85 if (eType == XFA_Element::Unknown)
86 return false;
87
88 for (size_t i = 0; i < m_iCreateCount; ++i) {
89 CXFA_Node* pNewNode = m_pCreateParent->CreateSamePacketNode(eType);
90 if (pNewNode) {
91 m_pCreateParent->InsertChildAndNotify(pNewNode, nullptr);
92 if (i == m_iCreateCount - 1) {
93 m_pCreateParent = pNewNode;
94 }
95 bResult = true;
96 }
97 }
98 } else {
99 XFA_Element eClassType = XFA_Element::DataGroup;
100 if (bLastNode) {
101 eClassType = m_eLastCreateType;
102 }
103 for (size_t i = 0; i < m_iCreateCount; ++i) {
104 CXFA_Node* pNewNode = m_pCreateParent->CreateSamePacketNode(eClassType);
105 if (pNewNode) {
106 pNewNode->JSObject()->SetAttributeByEnum(XFA_Attribute::Name,
107 WideString(wsNameView), false);
108 pNewNode->CreateXMLMappingNode();
109 m_pCreateParent->InsertChildAndNotify(pNewNode, nullptr);
110 if (i == m_iCreateCount - 1) {
111 m_pCreateParent = pNewNode;
112 }
113 bResult = true;
114 }
115 }
116 }
117 if (!bResult)
118 m_pCreateParent = nullptr;
119
120 return bResult;
121 }
122
SetCreateNodeType(CXFA_Node * refNode)123 void CFXJSE_NodeHelper::SetCreateNodeType(CXFA_Node* refNode) {
124 if (!refNode)
125 return;
126
127 if (refNode->GetElementType() == XFA_Element::Subform) {
128 m_eLastCreateType = XFA_Element::DataGroup;
129 } else if (refNode->GetElementType() == XFA_Element::Field) {
130 m_eLastCreateType = XFA_FieldIsMultiListBox(refNode)
131 ? XFA_Element::DataGroup
132 : XFA_Element::DataValue;
133 } else if (refNode->GetElementType() == XFA_Element::ExclGroup) {
134 m_eLastCreateType = XFA_Element::DataValue;
135 }
136 }
137