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 "xfa/fxfa/parser/cxfa_calculate.h"
8
9 #include "fxjs/xfa/cjx_node.h"
10 #include "third_party/base/ptr_util.h"
11 #include "xfa/fxfa/parser/cxfa_message.h"
12 #include "xfa/fxfa/parser/cxfa_script.h"
13 #include "xfa/fxfa/parser/cxfa_text.h"
14
15 namespace {
16
17 const CXFA_Node::PropertyData kCalculatePropertyData[] = {
18 {XFA_Element::Message, 1, 0},
19 {XFA_Element::Script, 1, 0},
20 {XFA_Element::Extras, 1, 0},
21 };
22
23 const CXFA_Node::AttributeData kCalculateAttributeData[] = {
24 {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
25 {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
26 {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
27 {XFA_Attribute::Override, XFA_AttributeType::Enum,
28 (void*)XFA_AttributeValue::Error},
29 };
30
31 } // namespace
32
CXFA_Calculate(CXFA_Document * doc,XFA_PacketType packet)33 CXFA_Calculate::CXFA_Calculate(CXFA_Document* doc, XFA_PacketType packet)
34 : CXFA_Node(doc,
35 packet,
36 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
37 XFA_ObjectType::Node,
38 XFA_Element::Calculate,
39 kCalculatePropertyData,
40 kCalculateAttributeData,
41 pdfium::MakeUnique<CJX_Node>(this)) {}
42
43 CXFA_Calculate::~CXFA_Calculate() = default;
44
GetOverride()45 XFA_AttributeValue CXFA_Calculate::GetOverride() {
46 return JSObject()
47 ->TryEnum(XFA_Attribute::Override, false)
48 .value_or(XFA_AttributeValue::Error);
49 }
50
GetScriptIfExists()51 CXFA_Script* CXFA_Calculate::GetScriptIfExists() {
52 return GetChild<CXFA_Script>(0, XFA_Element::Script, false);
53 }
54
GetMessageText()55 WideString CXFA_Calculate::GetMessageText() {
56 CXFA_Message* pNode = GetChild<CXFA_Message>(0, XFA_Element::Message, false);
57 if (!pNode)
58 return WideString();
59
60 CXFA_Text* text = pNode->GetChild<CXFA_Text>(0, XFA_Element::Text, false);
61 return text ? text->GetContent() : WideString();
62 }
63