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_occur.h"
8 
9 #include "fxjs/xfa/cjx_occur.h"
10 #include "third_party/base/ptr_util.h"
11 
12 namespace {
13 
14 const CXFA_Node::PropertyData kPropertyData[] = {{XFA_Element::Extras, 1, 0},
15                                                  {XFA_Element::Unknown, 0, 0}};
16 const CXFA_Node::AttributeData kAttributeData[] = {
17     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
18     {XFA_Attribute::Max, XFA_AttributeType::Integer, (void*)1},
19     {XFA_Attribute::Min, XFA_AttributeType::Integer, (void*)1},
20     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
21     {XFA_Attribute::Initial, XFA_AttributeType::Integer, (void*)1},
22     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
23     {XFA_Attribute::Unknown, XFA_AttributeType::Integer, nullptr}};
24 
25 constexpr wchar_t kName[] = L"occur";
26 
27 }  // namespace
28 
CXFA_Occur(CXFA_Document * doc,XFA_PacketType packet)29 CXFA_Occur::CXFA_Occur(CXFA_Document* doc, XFA_PacketType packet)
30     : CXFA_Node(doc,
31                 packet,
32                 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
33                 XFA_ObjectType::Node,
34                 XFA_Element::Occur,
35                 kPropertyData,
36                 kAttributeData,
37                 kName,
38                 pdfium::MakeUnique<CJX_Occur>(this)) {}
39 
~CXFA_Occur()40 CXFA_Occur::~CXFA_Occur() {}
41 
GetMax()42 int32_t CXFA_Occur::GetMax() {
43   Optional<int32_t> max = JSObject()->TryInteger(XFA_Attribute::Max, true);
44   return max ? *max : GetMin();
45 }
46 
GetMin()47 int32_t CXFA_Occur::GetMin() {
48   Optional<int32_t> min = JSObject()->TryInteger(XFA_Attribute::Min, true);
49   return min && *min >= 0 ? *min : 1;
50 }
51 
GetOccurInfo()52 std::tuple<int32_t, int32_t, int32_t> CXFA_Occur::GetOccurInfo() {
53   int32_t iMin = GetMin();
54   int32_t iMax = GetMax();
55 
56   Optional<int32_t> init =
57       JSObject()->TryInteger(XFA_Attribute::Initial, false);
58   return {iMin, iMax, init && *init >= iMin ? *init : iMin};
59 }
60 
SetMax(int32_t iMax)61 void CXFA_Occur::SetMax(int32_t iMax) {
62   iMax = (iMax != -1 && iMax < 1) ? 1 : iMax;
63   JSObject()->SetInteger(XFA_Attribute::Max, iMax, false);
64 
65   int32_t iMin = GetMin();
66   if (iMax != -1 && iMax < iMin) {
67     iMin = iMax;
68     JSObject()->SetInteger(XFA_Attribute::Min, iMin, false);
69   }
70 }
71 
SetMin(int32_t iMin)72 void CXFA_Occur::SetMin(int32_t iMin) {
73   iMin = (iMin < 0) ? 1 : iMin;
74   JSObject()->SetInteger(XFA_Attribute::Min, iMin, false);
75 
76   int32_t iMax = GetMax();
77   if (iMax > 0 && iMax < iMin) {
78     iMax = iMin;
79     JSObject()->SetInteger(XFA_Attribute::Max, iMax, false);
80   }
81 }
82