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