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_validate.h"
8
9 #include "fxjs/xfa/cjx_node.h"
10 #include "fxjs/xfa/cjx_object.h"
11 #include "third_party/base/ptr_util.h"
12 #include "xfa/fxfa/parser/cxfa_message.h"
13 #include "xfa/fxfa/parser/cxfa_picture.h"
14 #include "xfa/fxfa/parser/cxfa_script.h"
15 #include "xfa/fxfa/parser/xfa_basic_data.h"
16
17 namespace {
18
19 const CXFA_Node::PropertyData kValidatePropertyData[] = {
20 {XFA_Element::Message, 1, 0},
21 {XFA_Element::Picture, 1, 0},
22 {XFA_Element::Script, 1, 0},
23 {XFA_Element::Extras, 1, 0},
24 };
25 const CXFA_Node::AttributeData kValidateAttributeData[] = {
26 {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
27 {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
28 {XFA_Attribute::ScriptTest, XFA_AttributeType::Enum,
29 (void*)XFA_AttributeValue::Error},
30 {XFA_Attribute::NullTest, XFA_AttributeType::Enum,
31 (void*)XFA_AttributeValue::Disabled},
32 {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
33 {XFA_Attribute::Desc, XFA_AttributeType::CData, nullptr},
34 {XFA_Attribute::FormatTest, XFA_AttributeType::Enum,
35 (void*)XFA_AttributeValue::Warning},
36 {XFA_Attribute::Lock, XFA_AttributeType::Integer, (void*)0},
37 };
38
39 constexpr wchar_t kFormatTest[] = L"formatTest";
40 constexpr wchar_t kNullTest[] = L"nullTest";
41 constexpr wchar_t kScriptTest[] = L"scriptTest";
42
43 } // namespace
44
CXFA_Validate(CXFA_Document * doc,XFA_PacketType packet)45 CXFA_Validate::CXFA_Validate(CXFA_Document* doc, XFA_PacketType packet)
46 : CXFA_Node(
47 doc,
48 packet,
49 (XFA_XDPPACKET_Config | XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
50 XFA_ObjectType::ContentNode,
51 XFA_Element::Validate,
52 kValidatePropertyData,
53 kValidateAttributeData,
54 pdfium::MakeUnique<CJX_Node>(this)) {}
55
56 CXFA_Validate::~CXFA_Validate() = default;
57
GetFormatTest()58 XFA_AttributeValue CXFA_Validate::GetFormatTest() {
59 return JSObject()->GetEnum(XFA_Attribute::FormatTest);
60 }
61
SetNullTest(const WideString & wsValue)62 void CXFA_Validate::SetNullTest(const WideString& wsValue) {
63 Optional<XFA_AttributeValue> item =
64 XFA_GetAttributeValueByName(wsValue.AsStringView());
65 JSObject()->SetEnum(XFA_Attribute::NullTest,
66 item ? *item : XFA_AttributeValue::Disabled, false);
67 }
68
GetNullTest()69 XFA_AttributeValue CXFA_Validate::GetNullTest() {
70 return JSObject()->GetEnum(XFA_Attribute::NullTest);
71 }
72
GetScriptTest()73 XFA_AttributeValue CXFA_Validate::GetScriptTest() {
74 return JSObject()->GetEnum(XFA_Attribute::ScriptTest);
75 }
76
GetMessageText(const WideString & wsMessageType)77 WideString CXFA_Validate::GetMessageText(const WideString& wsMessageType) {
78 CXFA_Message* pNode =
79 JSObject()->GetProperty<CXFA_Message>(0, XFA_Element::Message);
80 if (!pNode)
81 return WideString();
82
83 for (CXFA_Node* pItemNode = pNode->GetFirstChild(); pItemNode;
84 pItemNode = pItemNode->GetNextSibling()) {
85 if (pItemNode->GetElementType() != XFA_Element::Text)
86 continue;
87
88 WideString wsName = pItemNode->JSObject()->GetCData(XFA_Attribute::Name);
89 if (wsName.IsEmpty() || wsName == wsMessageType)
90 return pItemNode->JSObject()->GetContent(false);
91 }
92 return WideString();
93 }
94
SetFormatMessageText(const WideString & wsMessage)95 void CXFA_Validate::SetFormatMessageText(const WideString& wsMessage) {
96 SetMessageText(kFormatTest, wsMessage);
97 }
98
GetFormatMessageText()99 WideString CXFA_Validate::GetFormatMessageText() {
100 return GetMessageText(kFormatTest);
101 }
102
SetNullMessageText(const WideString & wsMessage)103 void CXFA_Validate::SetNullMessageText(const WideString& wsMessage) {
104 SetMessageText(kNullTest, wsMessage);
105 }
106
GetNullMessageText()107 WideString CXFA_Validate::GetNullMessageText() {
108 return GetMessageText(kNullTest);
109 }
110
GetScriptMessageText()111 WideString CXFA_Validate::GetScriptMessageText() {
112 return GetMessageText(kScriptTest);
113 }
114
SetScriptMessageText(const WideString & wsMessage)115 void CXFA_Validate::SetScriptMessageText(const WideString& wsMessage) {
116 SetMessageText(kScriptTest, wsMessage);
117 }
118
SetMessageText(const WideString & wsMessageType,const WideString & wsMessage)119 void CXFA_Validate::SetMessageText(const WideString& wsMessageType,
120 const WideString& wsMessage) {
121 CXFA_Message* pNode =
122 JSObject()->GetOrCreateProperty<CXFA_Message>(0, XFA_Element::Message);
123 if (!pNode)
124 return;
125
126 for (CXFA_Node* pItemNode = pNode->GetFirstChild(); pItemNode;
127 pItemNode = pItemNode->GetNextSibling()) {
128 if (pItemNode->GetElementType() != XFA_Element::Text)
129 continue;
130
131 WideString wsName = pItemNode->JSObject()->GetCData(XFA_Attribute::Name);
132 if (wsName.IsEmpty() || wsName == wsMessageType) {
133 pItemNode->JSObject()->SetContent(wsMessage, wsMessage, false, false,
134 true);
135 return;
136 }
137 }
138
139 CXFA_Node* pTextNode = pNode->CreateSamePacketNode(XFA_Element::Text);
140 pNode->InsertChildAndNotify(pTextNode, nullptr);
141 pTextNode->JSObject()->SetCData(XFA_Attribute::Name, wsMessageType, false,
142 false);
143 pTextNode->JSObject()->SetContent(wsMessage, wsMessage, false, false, true);
144 }
145
GetPicture()146 WideString CXFA_Validate::GetPicture() {
147 CXFA_Picture* pNode = GetChild<CXFA_Picture>(0, XFA_Element::Picture, false);
148 return pNode ? pNode->JSObject()->GetContent(false) : WideString();
149 }
150
GetScriptIfExists()151 CXFA_Script* CXFA_Validate::GetScriptIfExists() {
152 return GetChild<CXFA_Script>(0, XFA_Element::Script, false);
153 }
154