xref: /aosp_15_r20/external/pdfium/xfa/fxfa/parser/cxfa_calculate.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 "xfa/fxfa/parser/cxfa_calculate.h"
8 
9 #include "fxjs/xfa/cjx_node.h"
10 #include "xfa/fxfa/parser/cxfa_document.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, {}},
19     {XFA_Element::Script, 1, {}},
20     {XFA_Element::Extras, 1, {}},
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::kTemplate, XFA_XDPPACKET::kForm},
37                 XFA_ObjectType::Node,
38                 XFA_Element::Calculate,
39                 kCalculatePropertyData,
40                 kCalculateAttributeData,
41                 cppgc::MakeGarbageCollected<CJX_Node>(
42                     doc->GetHeap()->GetAllocationHandle(),
43                     this)) {}
44 
45 CXFA_Calculate::~CXFA_Calculate() = default;
46 
GetOverride()47 XFA_AttributeValue CXFA_Calculate::GetOverride() {
48   return JSObject()
49       ->TryEnum(XFA_Attribute::Override, false)
50       .value_or(XFA_AttributeValue::Error);
51 }
52 
GetScriptIfExists()53 CXFA_Script* CXFA_Calculate::GetScriptIfExists() {
54   return GetChild<CXFA_Script>(0, XFA_Element::Script, false);
55 }
56 
GetMessageText() const57 WideString CXFA_Calculate::GetMessageText() const {
58   const auto* pNode = GetChild<CXFA_Message>(0, XFA_Element::Message, false);
59   if (!pNode)
60     return WideString();
61 
62   const auto* text = pNode->GetChild<CXFA_Text>(0, XFA_Element::Text, false);
63   return text ? text->GetContent() : WideString();
64 }
65