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_list.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 "third_party/base/numerics/safe_conversions.h"
15 #include "xfa/fxfa/parser/cxfa_document.h"
16 #include "xfa/fxfa/parser/cxfa_list.h"
17 #include "xfa/fxfa/parser/cxfa_node.h"
18
19 const CJX_MethodSpec CJX_List::MethodSpecs[] = {{"append", append_static},
20 {"insert", insert_static},
21 {"item", item_static},
22 {"remove", remove_static}};
23
CJX_List(CXFA_List * list)24 CJX_List::CJX_List(CXFA_List* list) : CJX_Object(list) {
25 DefineMethods(MethodSpecs, FX_ArraySize(MethodSpecs));
26 }
27
~CJX_List()28 CJX_List::~CJX_List() {}
29
GetXFAList()30 CXFA_List* CJX_List::GetXFAList() {
31 return static_cast<CXFA_List*>(GetXFAObject());
32 }
33
append(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)34 CJS_Return CJX_List::append(CJS_V8* runtime,
35 const std::vector<v8::Local<v8::Value>>& params) {
36 if (params.size() != 1)
37 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
38
39 auto* pNode = ToNode(runtime->ToXFAObject(params[0]));
40 if (!pNode)
41 return CJS_Return(JSGetStringFromID(JSMessage::kValueError));
42
43 GetXFAList()->Append(pNode);
44 return CJS_Return(true);
45 }
46
insert(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)47 CJS_Return CJX_List::insert(CJS_V8* runtime,
48 const std::vector<v8::Local<v8::Value>>& params) {
49 if (params.size() != 2)
50 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
51
52 auto* pNewNode = ToNode(runtime->ToXFAObject(params[0]));
53 if (!pNewNode)
54 return CJS_Return(JSGetStringFromID(JSMessage::kValueError));
55
56 auto* pBeforeNode = ToNode(runtime->ToXFAObject(params[1]));
57 GetXFAList()->Insert(pNewNode, pBeforeNode);
58 return CJS_Return(true);
59 }
60
remove(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)61 CJS_Return CJX_List::remove(CJS_V8* runtime,
62 const std::vector<v8::Local<v8::Value>>& params) {
63 if (params.size() != 1)
64 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
65
66 auto* pNode = ToNode(runtime->ToXFAObject(params[0]));
67 if (!pNode)
68 return CJS_Return(JSGetStringFromID(JSMessage::kValueError));
69
70 GetXFAList()->Remove(pNode);
71 return CJS_Return(true);
72 }
73
item(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)74 CJS_Return CJX_List::item(CJS_V8* runtime,
75 const std::vector<v8::Local<v8::Value>>& params) {
76 if (params.size() != 1)
77 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
78
79 int32_t index = runtime->ToInt32(params[0]);
80 size_t cast_index = static_cast<size_t>(index);
81 if (index < 0 || cast_index >= GetXFAList()->GetLength())
82 return CJS_Return(JSGetStringFromID(JSMessage::kInvalidInputError));
83
84 return CJS_Return(runtime->NewXFAObject(
85 GetXFAList()->Item(cast_index),
86 GetDocument()->GetScriptContext()->GetJseNormalClass()->GetTemplate()));
87 }
88
length(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)89 void CJX_List::length(CFXJSE_Value* pValue,
90 bool bSetting,
91 XFA_Attribute eAttribute) {
92 if (bSetting) {
93 ThrowInvalidPropertyException();
94 return;
95 }
96 pValue->SetInteger(
97 pdfium::base::checked_cast<int32_t>(GetXFAList()->GetLength()));
98 }
99