Dan Sinclair | f473672 | 2017-11-27 18:10:47 +0000 | [diff] [blame] | 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 | |
Dan Sinclair | fe9d640 | 2017-12-14 19:45:53 +0000 | [diff] [blame] | 9 | #include "fxjs/xfa/cjx_occur.h" |
| 10 | #include "third_party/base/ptr_util.h" |
| 11 | |
Dan Sinclair | f473672 | 2017-11-27 18:10:47 +0000 | [diff] [blame] | 12 | namespace { |
| 13 | |
Lei Zhang | ec7d8e2 | 2018-02-07 19:57:25 +0000 | [diff] [blame] | 14 | const CXFA_Node::PropertyData kOccurPropertyData[] = { |
| 15 | {XFA_Element::Extras, 1, 0}, |
Tom Sepez | a8c86c2 | 2019-01-25 22:06:03 +0000 | [diff] [blame] | 16 | }; |
Tom Sepez | 7fc43dd | 2018-12-13 17:54:26 +0000 | [diff] [blame] | 17 | |
Lei Zhang | ec7d8e2 | 2018-02-07 19:57:25 +0000 | [diff] [blame] | 18 | const CXFA_Node::AttributeData kOccurAttributeData[] = { |
Dan Sinclair | 21b0827 | 2017-11-30 20:22:20 +0000 | [diff] [blame] | 19 | {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 Sepez | a8c86c2 | 2019-01-25 22:06:03 +0000 | [diff] [blame] | 25 | }; |
Dan Sinclair | f473672 | 2017-11-27 18:10:47 +0000 | [diff] [blame] | 26 | |
Dan Sinclair | f473672 | 2017-11-27 18:10:47 +0000 | [diff] [blame] | 27 | } // namespace |
| 28 | |
Dan Sinclair | c40c5aa | 2017-11-30 21:29:11 +0000 | [diff] [blame] | 29 | CXFA_Occur::CXFA_Occur(CXFA_Document* doc, XFA_PacketType packet) |
Dan Sinclair | f473672 | 2017-11-27 18:10:47 +0000 | [diff] [blame] | 30 | : CXFA_Node(doc, |
| 31 | packet, |
| 32 | (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form), |
| 33 | XFA_ObjectType::Node, |
| 34 | XFA_Element::Occur, |
Lei Zhang | ec7d8e2 | 2018-02-07 19:57:25 +0000 | [diff] [blame] | 35 | kOccurPropertyData, |
| 36 | kOccurAttributeData, |
Dan Sinclair | fe9d640 | 2017-12-14 19:45:53 +0000 | [diff] [blame] | 37 | pdfium::MakeUnique<CJX_Occur>(this)) {} |
Dan Sinclair | f473672 | 2017-11-27 18:10:47 +0000 | [diff] [blame] | 38 | |
Tom Sepez | 7fc43dd | 2018-12-13 17:54:26 +0000 | [diff] [blame] | 39 | CXFA_Occur::~CXFA_Occur() = default; |
Dan Sinclair | 9647614 | 2018-01-03 11:25:05 -0500 | [diff] [blame] | 40 | |
| 41 | int32_t CXFA_Occur::GetMax() { |
Ryan Harrison | c560a8c | 2018-01-04 14:43:27 -0500 | [diff] [blame] | 42 | Optional<int32_t> max = JSObject()->TryInteger(XFA_Attribute::Max, true); |
Dan Sinclair | 9647614 | 2018-01-03 11:25:05 -0500 | [diff] [blame] | 43 | return max ? *max : GetMin(); |
| 44 | } |
| 45 | |
| 46 | int32_t CXFA_Occur::GetMin() { |
Ryan Harrison | c560a8c | 2018-01-04 14:43:27 -0500 | [diff] [blame] | 47 | Optional<int32_t> min = JSObject()->TryInteger(XFA_Attribute::Min, true); |
Dan Sinclair | 9647614 | 2018-01-03 11:25:05 -0500 | [diff] [blame] | 48 | return min && *min >= 0 ? *min : 1; |
| 49 | } |
| 50 | |
| 51 | std::tuple<int32_t, int32_t, int32_t> CXFA_Occur::GetOccurInfo() { |
| 52 | int32_t iMin = GetMin(); |
| 53 | int32_t iMax = GetMax(); |
| 54 | |
Ryan Harrison | c560a8c | 2018-01-04 14:43:27 -0500 | [diff] [blame] | 55 | Optional<int32_t> init = |
Dan Sinclair | 9647614 | 2018-01-03 11:25:05 -0500 | [diff] [blame] | 56 | JSObject()->TryInteger(XFA_Attribute::Initial, false); |
| 57 | return {iMin, iMax, init && *init >= iMin ? *init : iMin}; |
| 58 | } |
| 59 | |
| 60 | void 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 | |
| 71 | void 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 | } |