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_calculate.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 kPropertyData[] = {{XFA_Element::Message, 1, 0},
18                                                  {XFA_Element::Script, 1, 0},
19                                                  {XFA_Element::Extras, 1, 0},
20                                                  {XFA_Element::Unknown, 0, 0}};
21 const CXFA_Node::AttributeData kAttributeData[] = {
22     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
23     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
24     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
25     {XFA_Attribute::Override, XFA_AttributeType::Enum,
26      (void*)XFA_AttributeEnum::Error},
27     {XFA_Attribute::Unknown, XFA_AttributeType::Integer, nullptr}};
28 
29 constexpr wchar_t kName[] = L"calculate";
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                 kPropertyData,
40                 kAttributeData,
41                 kName,
42                 pdfium::MakeUnique<CJX_Calculate>(this)) {}
43 
~CXFA_Calculate()44 CXFA_Calculate::~CXFA_Calculate() {}
45 
GetOverride()46 XFA_AttributeEnum CXFA_Calculate::GetOverride() {
47   return JSObject()
48       ->TryEnum(XFA_Attribute::Override, false)
49       .value_or(XFA_AttributeEnum::Error);
50 }
51 
GetScriptIfExists()52 CXFA_Script* CXFA_Calculate::GetScriptIfExists() {
53   return GetChild<CXFA_Script>(0, XFA_Element::Script, false);
54 }
55 
GetMessageText()56 WideString CXFA_Calculate::GetMessageText() {
57   CXFA_Message* pNode = GetChild<CXFA_Message>(0, XFA_Element::Message, false);
58   if (!pNode)
59     return L"";
60 
61   CXFA_Text* text = pNode->GetChild<CXFA_Text>(0, XFA_Element::Text, false);
62   return text ? text->GetContent() : L"";
63 }
64