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