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_packet.h"
8
9 #include <utility>
10 #include <vector>
11
12 #include "core/fxcrt/xml/cfx_xmldocument.h"
13 #include "core/fxcrt/xml/cfx_xmlelement.h"
14 #include "core/fxcrt/xml/cfx_xmltext.h"
15 #include "fxjs/cfx_v8.h"
16 #include "fxjs/fxv8.h"
17 #include "fxjs/js_resources.h"
18 #include "v8/include/v8-primitive.h"
19 #include "xfa/fxfa/cxfa_ffdoc.h"
20 #include "xfa/fxfa/cxfa_ffnotify.h"
21 #include "xfa/fxfa/parser/cxfa_packet.h"
22
23 const CJX_MethodSpec CJX_Packet::MethodSpecs[] = {
24 {"getAttribute", getAttribute_static},
25 {"removeAttribute", removeAttribute_static},
26 {"setAttribute", setAttribute_static}};
27
CJX_Packet(CXFA_Packet * packet)28 CJX_Packet::CJX_Packet(CXFA_Packet* packet) : CJX_Node(packet) {
29 DefineMethods(MethodSpecs);
30 }
31
32 CJX_Packet::~CJX_Packet() = default;
33
DynamicTypeIs(TypeTag eType) const34 bool CJX_Packet::DynamicTypeIs(TypeTag eType) const {
35 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
36 }
37
getAttribute(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)38 CJS_Result CJX_Packet::getAttribute(
39 CFXJSE_Engine* runtime,
40 const std::vector<v8::Local<v8::Value>>& params) {
41 if (params.size() != 1)
42 return CJS_Result::Failure(JSMessage::kParamError);
43
44 WideString attributeValue;
45 CFX_XMLElement* element = ToXMLElement(GetXFANode()->GetXMLMappingNode());
46 if (element)
47 attributeValue = element->GetAttribute(runtime->ToWideString(params[0]));
48
49 return CJS_Result::Success(
50 runtime->NewString(attributeValue.ToUTF8().AsStringView()));
51 }
52
setAttribute(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)53 CJS_Result CJX_Packet::setAttribute(
54 CFXJSE_Engine* runtime,
55 const std::vector<v8::Local<v8::Value>>& params) {
56 if (params.size() != 2)
57 return CJS_Result::Failure(JSMessage::kParamError);
58
59 CFX_XMLElement* element = ToXMLElement(GetXFANode()->GetXMLMappingNode());
60 if (element) {
61 element->SetAttribute(runtime->ToWideString(params[1]),
62 runtime->ToWideString(params[0]));
63 }
64 return CJS_Result::Success(runtime->NewNull());
65 }
66
removeAttribute(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)67 CJS_Result CJX_Packet::removeAttribute(
68 CFXJSE_Engine* runtime,
69 const std::vector<v8::Local<v8::Value>>& params) {
70 if (params.size() != 1)
71 return CJS_Result::Failure(JSMessage::kParamError);
72
73 CFX_XMLElement* pElement = ToXMLElement(GetXFANode()->GetXMLMappingNode());
74 if (pElement)
75 pElement->RemoveAttribute(runtime->ToWideString(params[0]));
76
77 return CJS_Result::Success(runtime->NewNull());
78 }
79
content(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)80 void CJX_Packet::content(v8::Isolate* pIsolate,
81 v8::Local<v8::Value>* pValue,
82 bool bSetting,
83 XFA_Attribute eAttribute) {
84 CFX_XMLElement* element = ToXMLElement(GetXFANode()->GetXMLMappingNode());
85 if (bSetting) {
86 if (element) {
87 element->AppendLastChild(
88 GetXFANode()
89 ->GetDocument()
90 ->GetNotify()
91 ->GetFFDoc()
92 ->GetXMLDocument()
93 ->CreateNode<CFX_XMLText>(
94 fxv8::ReentrantToWideStringHelper(pIsolate, *pValue)));
95 }
96 return;
97 }
98
99 WideString wsTextData;
100 if (element)
101 wsTextData = element->GetTextData();
102
103 *pValue = fxv8::NewStringHelper(pIsolate, wsTextData.ToUTF8().AsStringView());
104 }
105