blob: ee689813d0a868aaa0eda3db35412f66fb3b0826 [file] [log] [blame]
Dan Sinclairf4736722017-11-27 18:10:47 +00001// 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
Dan Sinclairfe9d6402017-12-14 19:45:53 +00009#include "fxjs/xfa/cjx_occur.h"
10#include "third_party/base/ptr_util.h"
11
Dan Sinclairf4736722017-11-27 18:10:47 +000012namespace {
13
Lei Zhangec7d8e22018-02-07 19:57:25 +000014const CXFA_Node::PropertyData kOccurPropertyData[] = {
15 {XFA_Element::Extras, 1, 0},
Tom Sepeza8c86c22019-01-25 22:06:03 +000016};
Tom Sepez7fc43dd2018-12-13 17:54:26 +000017
Lei Zhangec7d8e22018-02-07 19:57:25 +000018const CXFA_Node::AttributeData kOccurAttributeData[] = {
Dan Sinclair21b08272017-11-30 20:22:20 +000019 {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
20 {XFA_Attribute::Max, XFA_AttributeType::Integer, (void*)1},
21 {XFA_Attribute::Min, XFA_AttributeType::Integer, (void*)1},
22 {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
23 {XFA_Attribute::Initial, XFA_AttributeType::Integer, (void*)1},
24 {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
Tom Sepeza8c86c22019-01-25 22:06:03 +000025};
Dan Sinclairf4736722017-11-27 18:10:47 +000026
Dan Sinclairf4736722017-11-27 18:10:47 +000027} // namespace
28
Dan Sinclairc40c5aa2017-11-30 21:29:11 +000029CXFA_Occur::CXFA_Occur(CXFA_Document* doc, XFA_PacketType packet)
Dan Sinclairf4736722017-11-27 18:10:47 +000030 : CXFA_Node(doc,
31 packet,
32 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
33 XFA_ObjectType::Node,
34 XFA_Element::Occur,
Lei Zhangec7d8e22018-02-07 19:57:25 +000035 kOccurPropertyData,
36 kOccurAttributeData,
Dan Sinclairfe9d6402017-12-14 19:45:53 +000037 pdfium::MakeUnique<CJX_Occur>(this)) {}
Dan Sinclairf4736722017-11-27 18:10:47 +000038
Tom Sepez7fc43dd2018-12-13 17:54:26 +000039CXFA_Occur::~CXFA_Occur() = default;
Dan Sinclair96476142018-01-03 11:25:05 -050040
41int32_t CXFA_Occur::GetMax() {
Ryan Harrisonc560a8c2018-01-04 14:43:27 -050042 Optional<int32_t> max = JSObject()->TryInteger(XFA_Attribute::Max, true);
Dan Sinclair96476142018-01-03 11:25:05 -050043 return max ? *max : GetMin();
44}
45
46int32_t CXFA_Occur::GetMin() {
Ryan Harrisonc560a8c2018-01-04 14:43:27 -050047 Optional<int32_t> min = JSObject()->TryInteger(XFA_Attribute::Min, true);
Dan Sinclair96476142018-01-03 11:25:05 -050048 return min && *min >= 0 ? *min : 1;
49}
50
51std::tuple<int32_t, int32_t, int32_t> CXFA_Occur::GetOccurInfo() {
52 int32_t iMin = GetMin();
53 int32_t iMax = GetMax();
54
Ryan Harrisonc560a8c2018-01-04 14:43:27 -050055 Optional<int32_t> init =
Dan Sinclair96476142018-01-03 11:25:05 -050056 JSObject()->TryInteger(XFA_Attribute::Initial, false);
57 return {iMin, iMax, init && *init >= iMin ? *init : iMin};
58}
59
60void CXFA_Occur::SetMax(int32_t iMax) {
61 iMax = (iMax != -1 && iMax < 1) ? 1 : iMax;
62 JSObject()->SetInteger(XFA_Attribute::Max, iMax, false);
63
64 int32_t iMin = GetMin();
65 if (iMax != -1 && iMax < iMin) {
66 iMin = iMax;
67 JSObject()->SetInteger(XFA_Attribute::Min, iMin, false);
68 }
69}
70
71void CXFA_Occur::SetMin(int32_t iMin) {
72 iMin = (iMin < 0) ? 1 : iMin;
73 JSObject()->SetInteger(XFA_Attribute::Min, iMin, false);
74
75 int32_t iMax = GetMax();
76 if (iMax > 0 && iMax < iMin) {
77 iMax = iMin;
78 JSObject()->SetInteger(XFA_Attribute::Max, iMax, false);
79 }
80}