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_treelist.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_document.h"
15 #include "xfa/fxfa/parser/cxfa_node.h"
16 #include "xfa/fxfa/parser/cxfa_treelist.h"
17 
18 const CJX_MethodSpec CJX_TreeList::MethodSpecs[] = {
19     {"namedItem", namedItem_static}};
20 
CJX_TreeList(CXFA_TreeList * list)21 CJX_TreeList::CJX_TreeList(CXFA_TreeList* list) : CJX_List(list) {
22   DefineMethods(MethodSpecs);
23 }
24 
~CJX_TreeList()25 CJX_TreeList::~CJX_TreeList() {}
26 
DynamicTypeIs(TypeTag eType) const27 bool CJX_TreeList::DynamicTypeIs(TypeTag eType) const {
28   return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
29 }
30 
GetXFATreeList()31 CXFA_TreeList* CJX_TreeList::GetXFATreeList() {
32   return ToTreeList(GetXFAObject());
33 }
34 
namedItem(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)35 CJS_Result CJX_TreeList::namedItem(
36     CFX_V8* runtime,
37     const std::vector<v8::Local<v8::Value>>& params) {
38   if (params.size() != 1)
39     return CJS_Result::Failure(JSMessage::kParamError);
40 
41   CXFA_Node* pNode = GetXFATreeList()->NamedItem(
42       runtime->ToWideString(params[0]).AsStringView());
43   if (!pNode)
44     return CJS_Result::Success();
45 
46   CFXJSE_Value* value =
47       GetDocument()->GetScriptContext()->GetOrCreateJSBindingFromMap(pNode);
48 
49   return CJS_Result::Success(
50       value->DirectGetValue().Get(runtime->GetIsolate()));
51 }
52