• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_validate.h"
8 
9 #include "fxjs/xfa/cjx_node.h"
10 #include "fxjs/xfa/cjx_object.h"
11 #include "xfa/fxfa/parser/cxfa_document.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, {}},
21     {XFA_Element::Picture, 1, {}},
22     {XFA_Element::Script, 1, {}},
23     {XFA_Element::Extras, 1, {}},
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(doc,
47                 packet,
48                 {XFA_XDPPACKET::kConfig, XFA_XDPPACKET::kTemplate,
49                  XFA_XDPPACKET::kForm},
50                 XFA_ObjectType::ContentNode,
51                 XFA_Element::Validate,
52                 kValidatePropertyData,
53                 kValidateAttributeData,
54                 cppgc::MakeGarbageCollected<CJX_Node>(
55                     doc->GetHeap()->GetAllocationHandle(),
56                     this)) {}
57 
58 CXFA_Validate::~CXFA_Validate() = default;
59 
GetFormatTest()60 XFA_AttributeValue CXFA_Validate::GetFormatTest() {
61   return JSObject()->GetEnum(XFA_Attribute::FormatTest);
62 }
63 
SetNullTest(const WideString & wsValue)64 void CXFA_Validate::SetNullTest(const WideString& wsValue) {
65   absl::optional<XFA_AttributeValue> item =
66       XFA_GetAttributeValueByName(wsValue.AsStringView());
67   JSObject()->SetEnum(
68       XFA_Attribute::NullTest,
69       item.has_value() ? item.value() : XFA_AttributeValue::Disabled, false);
70 }
71 
GetNullTest()72 XFA_AttributeValue CXFA_Validate::GetNullTest() {
73   return JSObject()->GetEnum(XFA_Attribute::NullTest);
74 }
75 
GetScriptTest()76 XFA_AttributeValue CXFA_Validate::GetScriptTest() {
77   return JSObject()->GetEnum(XFA_Attribute::ScriptTest);
78 }
79 
GetMessageText(const WideString & wsMessageType)80 WideString CXFA_Validate::GetMessageText(const WideString& wsMessageType) {
81   CXFA_Message* pNode =
82       JSObject()->GetProperty<CXFA_Message>(0, XFA_Element::Message);
83   if (!pNode)
84     return WideString();
85 
86   for (CXFA_Node* pItemNode = pNode->GetFirstChild(); pItemNode;
87        pItemNode = pItemNode->GetNextSibling()) {
88     if (pItemNode->GetElementType() != XFA_Element::Text)
89       continue;
90 
91     WideString wsName = pItemNode->JSObject()->GetCData(XFA_Attribute::Name);
92     if (wsName.IsEmpty() || wsName == wsMessageType)
93       return pItemNode->JSObject()->GetContent(false);
94   }
95   return WideString();
96 }
97 
SetFormatMessageText(const WideString & wsMessage)98 void CXFA_Validate::SetFormatMessageText(const WideString& wsMessage) {
99   SetMessageText(kFormatTest, wsMessage);
100 }
101 
GetFormatMessageText()102 WideString CXFA_Validate::GetFormatMessageText() {
103   return GetMessageText(kFormatTest);
104 }
105 
SetNullMessageText(const WideString & wsMessage)106 void CXFA_Validate::SetNullMessageText(const WideString& wsMessage) {
107   SetMessageText(kNullTest, wsMessage);
108 }
109 
GetNullMessageText()110 WideString CXFA_Validate::GetNullMessageText() {
111   return GetMessageText(kNullTest);
112 }
113 
GetScriptMessageText()114 WideString CXFA_Validate::GetScriptMessageText() {
115   return GetMessageText(kScriptTest);
116 }
117 
SetScriptMessageText(const WideString & wsMessage)118 void CXFA_Validate::SetScriptMessageText(const WideString& wsMessage) {
119   SetMessageText(kScriptTest, wsMessage);
120 }
121 
SetMessageText(const WideString & wsMessageType,const WideString & wsMessage)122 void CXFA_Validate::SetMessageText(const WideString& wsMessageType,
123                                    const WideString& wsMessage) {
124   CXFA_Message* pNode =
125       JSObject()->GetOrCreateProperty<CXFA_Message>(0, XFA_Element::Message);
126   if (!pNode)
127     return;
128 
129   for (CXFA_Node* pItemNode = pNode->GetFirstChild(); pItemNode;
130        pItemNode = pItemNode->GetNextSibling()) {
131     if (pItemNode->GetElementType() != XFA_Element::Text)
132       continue;
133 
134     WideString wsName = pItemNode->JSObject()->GetCData(XFA_Attribute::Name);
135     if (wsName.IsEmpty() || wsName == wsMessageType) {
136       pItemNode->JSObject()->SetContent(wsMessage, wsMessage, false, false,
137                                         true);
138       return;
139     }
140   }
141 
142   CXFA_Node* pTextNode = pNode->CreateSamePacketNode(XFA_Element::Text);
143   pNode->InsertChildAndNotify(pTextNode, nullptr);
144   pTextNode->JSObject()->SetCData(XFA_Attribute::Name, wsMessageType);
145   pTextNode->JSObject()->SetContent(wsMessage, wsMessage, false, false, true);
146 }
147 
GetPicture() const148 WideString CXFA_Validate::GetPicture() const {
149   const auto* pNode = GetChild<CXFA_Picture>(0, XFA_Element::Picture, false);
150   return pNode ? pNode->JSObject()->GetContent(false) : WideString();
151 }
152 
GetScriptIfExists()153 CXFA_Script* CXFA_Validate::GetScriptIfExists() {
154   return GetChild<CXFA_Script>(0, XFA_Element::Script, false);
155 }
156