1 // Copyright 2017 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/cjx_model.h"
8
9 #include <vector>
10
11 #include "fxjs/js_resources.h"
12 #include "fxjs/xfa/cfxjse_engine.h"
13 #include "fxjs/xfa/cfxjse_value.h"
14 #include "v8/include/v8-object.h"
15 #include "xfa/fxfa/parser/cxfa_delta.h"
16 #include "xfa/fxfa/parser/cxfa_document.h"
17 #include "xfa/fxfa/parser/xfa_basic_data.h"
18
19 const CJX_MethodSpec CJX_Model::MethodSpecs[] = {
20 {"clearErrorList", clearErrorList_static},
21 {"createNode", createNode_static},
22 {"isCompatibleNS", isCompatibleNS_static}};
23
CJX_Model(CXFA_Node * node)24 CJX_Model::CJX_Model(CXFA_Node* node) : CJX_Node(node) {
25 DefineMethods(MethodSpecs);
26 }
27
28 CJX_Model::~CJX_Model() = default;
29
DynamicTypeIs(TypeTag eType) const30 bool CJX_Model::DynamicTypeIs(TypeTag eType) const {
31 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
32 }
33
clearErrorList(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)34 CJS_Result CJX_Model::clearErrorList(
35 CFXJSE_Engine* runtime,
36 const std::vector<v8::Local<v8::Value>>& params) {
37 return CJS_Result::Success();
38 }
39
createNode(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)40 CJS_Result CJX_Model::createNode(
41 CFXJSE_Engine* runtime,
42 const std::vector<v8::Local<v8::Value>>& params) {
43 if (params.empty() || params.size() > 3)
44 return CJS_Result::Failure(JSMessage::kParamError);
45
46 WideString name;
47 if (params.size() > 1)
48 name = runtime->ToWideString(params[1]);
49
50 WideString nameSpace;
51 if (params.size() == 3)
52 nameSpace = runtime->ToWideString(params[2]);
53
54 WideString tagName = runtime->ToWideString(params[0]);
55 XFA_Element eType = XFA_GetElementByName(tagName.AsStringView());
56 CXFA_Node* pNewNode = GetXFANode()->CreateSamePacketNode(eType);
57 if (!pNewNode)
58 return CJS_Result::Success(runtime->NewNull());
59
60 if (!name.IsEmpty()) {
61 if (!pNewNode->HasAttribute(XFA_Attribute::Name))
62 return CJS_Result::Failure(JSMessage::kParamError);
63
64 pNewNode->JSObject()->SetAttributeByEnum(XFA_Attribute::Name, name, true);
65 if (pNewNode->GetPacketType() == XFA_PacketType::Datasets)
66 pNewNode->CreateXMLMappingNode();
67 }
68
69 v8::Local<v8::Value> value =
70 GetDocument()->GetScriptContext()->GetOrCreateJSBindingFromMap(pNewNode);
71
72 return CJS_Result::Success(value);
73 }
74
isCompatibleNS(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)75 CJS_Result CJX_Model::isCompatibleNS(
76 CFXJSE_Engine* runtime,
77 const std::vector<v8::Local<v8::Value>>& params) {
78 if (params.empty())
79 return CJS_Result::Failure(JSMessage::kParamError);
80
81 WideString nameSpace;
82 if (params.size() >= 1)
83 nameSpace = runtime->ToWideString(params[0]);
84
85 return CJS_Result::Success(
86 runtime->NewBoolean(TryNamespace().value_or(WideString()) == nameSpace));
87 }
88
context(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)89 void CJX_Model::context(v8::Isolate* pIsolate,
90 v8::Local<v8::Value>* pValue,
91 bool bSetting,
92 XFA_Attribute eAttribute) {}
93
aliasNode(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)94 void CJX_Model::aliasNode(v8::Isolate* pIsolate,
95 v8::Local<v8::Value>* pValue,
96 bool bSetting,
97 XFA_Attribute eAttribute) {}
98