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_packet.h"
8
9 #include <vector>
10
11 #include "fxjs/cfxjse_value.h"
12 #include "fxjs/js_resources.h"
13 #include "xfa/fxfa/parser/cxfa_packet.h"
14
15 const CJX_MethodSpec CJX_Packet::MethodSpecs[] = {
16 {"getAttribute", getAttribute_static},
17 {"removeAttribute", removeAttribute_static},
18 {"setAttribute", setAttribute_static}};
19
CJX_Packet(CXFA_Packet * packet)20 CJX_Packet::CJX_Packet(CXFA_Packet* packet) : CJX_Node(packet) {
21 DefineMethods(MethodSpecs, FX_ArraySize(MethodSpecs));
22 }
23
~CJX_Packet()24 CJX_Packet::~CJX_Packet() {}
25
getAttribute(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)26 CJS_Return CJX_Packet::getAttribute(
27 CJS_V8* runtime,
28 const std::vector<v8::Local<v8::Value>>& params) {
29 if (params.size() != 1)
30 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
31
32 WideString attributeValue;
33 CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode();
34 if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
35 attributeValue = static_cast<CFX_XMLElement*>(pXMLNode)->GetString(
36 runtime->ToWideString(params[0]).c_str());
37 }
38 return CJS_Return(
39 runtime->NewString(attributeValue.UTF8Encode().AsStringView()));
40 }
41
setAttribute(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)42 CJS_Return CJX_Packet::setAttribute(
43 CJS_V8* runtime,
44 const std::vector<v8::Local<v8::Value>>& params) {
45 if (params.size() != 2)
46 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
47
48 CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode();
49 if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
50 static_cast<CFX_XMLElement*>(pXMLNode)->SetString(
51 runtime->ToWideString(params[1]), runtime->ToWideString(params[0]));
52 }
53 return CJS_Return(runtime->NewNull());
54 }
55
removeAttribute(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)56 CJS_Return CJX_Packet::removeAttribute(
57 CJS_V8* runtime,
58 const std::vector<v8::Local<v8::Value>>& params) {
59 if (params.size() != 1)
60 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
61
62 CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode();
63 if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
64 WideString name = runtime->ToWideString(params[0]);
65 CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode);
66 if (pXMLElement->HasAttribute(name.c_str()))
67 pXMLElement->RemoveAttribute(name.c_str());
68 }
69 return CJS_Return(runtime->NewNull());
70 }
71
content(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)72 void CJX_Packet::content(CFXJSE_Value* pValue,
73 bool bSetting,
74 XFA_Attribute eAttribute) {
75 CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode();
76 if (bSetting) {
77 if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
78 CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode);
79 pXMLElement->SetTextData(pValue->ToWideString());
80 }
81 return;
82 }
83
84 WideString wsTextData;
85 if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) {
86 CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode);
87 wsTextData = pXMLElement->GetTextData();
88 }
89
90 pValue->SetString(wsTextData.UTF8Encode().AsStringView());
91 }
92