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_list.h"
8
9 #include <vector>
10
11 #include "fxjs/fxv8.h"
12 #include "fxjs/js_resources.h"
13 #include "fxjs/xfa/cfxjse_class.h"
14 #include "fxjs/xfa/cfxjse_engine.h"
15 #include "third_party/base/numerics/safe_conversions.h"
16 #include "v8/include/v8-object.h"
17 #include "v8/include/v8-primitive.h"
18 #include "xfa/fxfa/parser/cxfa_document.h"
19 #include "xfa/fxfa/parser/cxfa_list.h"
20 #include "xfa/fxfa/parser/cxfa_node.h"
21
22 const CJX_MethodSpec CJX_List::MethodSpecs[] = {{"append", append_static},
23 {"insert", insert_static},
24 {"item", item_static},
25 {"remove", remove_static}};
26
CJX_List(CXFA_List * list)27 CJX_List::CJX_List(CXFA_List* list) : CJX_Object(list) {
28 DefineMethods(MethodSpecs);
29 }
30
31 CJX_List::~CJX_List() = default;
32
DynamicTypeIs(TypeTag eType) const33 bool CJX_List::DynamicTypeIs(TypeTag eType) const {
34 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
35 }
36
GetXFAList()37 CXFA_List* CJX_List::GetXFAList() {
38 return ToList(GetXFAObject());
39 }
40
append(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)41 CJS_Result CJX_List::append(CFXJSE_Engine* runtime,
42 const std::vector<v8::Local<v8::Value>>& params) {
43 if (params.size() != 1)
44 return CJS_Result::Failure(JSMessage::kParamError);
45
46 auto* pNode = ToNode(runtime->ToXFAObject(params[0]));
47 if (!pNode)
48 return CJS_Result::Failure(JSMessage::kValueError);
49
50 if (!GetXFAList()->Append(pNode))
51 return CJS_Result::Failure(JSMessage::kWouldBeCyclic);
52
53 return CJS_Result::Success();
54 }
55
insert(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)56 CJS_Result CJX_List::insert(CFXJSE_Engine* runtime,
57 const std::vector<v8::Local<v8::Value>>& params) {
58 if (params.size() != 2)
59 return CJS_Result::Failure(JSMessage::kParamError);
60
61 auto* pNewNode = ToNode(runtime->ToXFAObject(params[0]));
62 if (!pNewNode)
63 return CJS_Result::Failure(JSMessage::kValueError);
64
65 auto* pBeforeNode = ToNode(runtime->ToXFAObject(params[1]));
66 if (!GetXFAList()->Insert(pNewNode, pBeforeNode))
67 return CJS_Result::Failure(JSMessage::kValueError);
68
69 return CJS_Result::Success();
70 }
71
remove(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)72 CJS_Result CJX_List::remove(CFXJSE_Engine* runtime,
73 const std::vector<v8::Local<v8::Value>>& params) {
74 if (params.size() != 1)
75 return CJS_Result::Failure(JSMessage::kParamError);
76
77 auto* pNode = ToNode(runtime->ToXFAObject(params[0]));
78 if (!pNode)
79 return CJS_Result::Failure(JSMessage::kValueError);
80
81 GetXFAList()->Remove(pNode);
82 return CJS_Result::Success();
83 }
84
item(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)85 CJS_Result CJX_List::item(CFXJSE_Engine* runtime,
86 const std::vector<v8::Local<v8::Value>>& params) {
87 if (params.size() != 1)
88 return CJS_Result::Failure(JSMessage::kParamError);
89
90 int32_t index = runtime->ToInt32(params[0]);
91 size_t cast_index = static_cast<size_t>(index);
92 if (index < 0 || cast_index >= GetXFAList()->GetLength())
93 return CJS_Result::Failure(JSMessage::kInvalidInputError);
94
95 return CJS_Result::Success(
96 runtime->NewNormalXFAObject(GetXFAList()->Item(cast_index)));
97 }
98
length(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)99 void CJX_List::length(v8::Isolate* pIsolate,
100 v8::Local<v8::Value>* pValue,
101 bool bSetting,
102 XFA_Attribute eAttribute) {
103 if (bSetting) {
104 ThrowInvalidPropertyException(pIsolate);
105 return;
106 }
107 *pValue = fxv8::NewNumberHelper(
108 pIsolate, pdfium::base::checked_cast<int32_t>(GetXFAList()->GetLength()));
109 }
110