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