Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cjx_eventpseudomodel.cpp
Go to the documentation of this file.
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 "fxjs/xfa/cjx_eventpseudomodel.h"
8
9#include <algorithm>
10#include <vector>
11
12#include "fxjs/fxv8.h"
13#include "fxjs/xfa/cfxjse_engine.h"
14#include "third_party/base/containers/span.h"
15#include "third_party/base/notreached.h"
16#include "third_party/base/numerics/safe_conversions.h"
17#include "v8/include/v8-primitive.h"
18#include "xfa/fxfa/cxfa_eventparam.h"
19#include "xfa/fxfa/cxfa_ffnotify.h"
20#include "xfa/fxfa/parser/cscript_eventpseudomodel.h"
21
22namespace {
23
24void StringProperty(v8::Isolate* pIsolate,
25 v8::Local<v8::Value>* pReturn,
26 WideString* wsValue,
27 bool bSetting) {
28 if (bSetting) {
29 *wsValue = fxv8::ReentrantToWideStringHelper(pIsolate, *pReturn);
30 return;
31 }
32 *pReturn = fxv8::NewStringHelper(pIsolate, wsValue->ToUTF8().AsStringView());
33}
34
35void IntegerProperty(v8::Isolate* pIsolate,
36 v8::Local<v8::Value>* pReturn,
37 int32_t* iValue,
38 bool bSetting) {
39 if (bSetting) {
40 *iValue = fxv8::ReentrantToInt32Helper(pIsolate, *pReturn);
41 return;
42 }
43 *pReturn = fxv8::NewNumberHelper(pIsolate, *iValue);
44}
45
46void BooleanProperty(v8::Isolate* pIsolate,
47 v8::Local<v8::Value>* pReturn,
48 bool* bValue,
49 bool bSetting) {
50 if (bSetting) {
51 *bValue = fxv8::ReentrantToBooleanHelper(pIsolate, *pReturn);
52 return;
53 }
54 *pReturn = fxv8::NewBooleanHelper(pIsolate, *bValue);
55}
56
57} // namespace
58
59const CJX_MethodSpec CJX_EventPseudoModel::MethodSpecs[] = {
60 {"emit", emit_static},
61 {"reset", reset_static}};
62
63CJX_EventPseudoModel::CJX_EventPseudoModel(CScript_EventPseudoModel* model)
64 : CJX_Object(model) {
65 DefineMethods(MethodSpecs);
66}
67
68CJX_EventPseudoModel::~CJX_EventPseudoModel() = default;
69
70bool CJX_EventPseudoModel::DynamicTypeIs(TypeTag eType) const {
71 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
72}
73
74void CJX_EventPseudoModel::cancelAction(v8::Isolate* pIsolate,
75 v8::Local<v8::Value>* pValue,
76 bool bSetting,
77 XFA_Attribute eAttribute) {
78 Property(pIsolate, pValue, XFA_Event::CancelAction, bSetting);
79}
80
81void CJX_EventPseudoModel::change(v8::Isolate* pIsolate,
82 v8::Local<v8::Value>* pValue,
83 bool bSetting,
84 XFA_Attribute eAttribute) {
85 Property(pIsolate, pValue, XFA_Event::Change, bSetting);
86}
87
88void CJX_EventPseudoModel::commitKey(v8::Isolate* pIsolate,
89 v8::Local<v8::Value>* pValue,
90 bool bSetting,
91 XFA_Attribute eAttribute) {
92 Property(pIsolate, pValue, XFA_Event::CommitKey, bSetting);
93}
94
95void CJX_EventPseudoModel::fullText(v8::Isolate* pIsolate,
96 v8::Local<v8::Value>* pValue,
97 bool bSetting,
98 XFA_Attribute eAttribute) {
99 Property(pIsolate, pValue, XFA_Event::FullText, bSetting);
100}
101
102void CJX_EventPseudoModel::keyDown(v8::Isolate* pIsolate,
103 v8::Local<v8::Value>* pValue,
104 bool bSetting,
105 XFA_Attribute eAttribute) {
106 Property(pIsolate, pValue, XFA_Event::Keydown, bSetting);
107}
108
109void CJX_EventPseudoModel::modifier(v8::Isolate* pIsolate,
110 v8::Local<v8::Value>* pValue,
111 bool bSetting,
112 XFA_Attribute eAttribute) {
113 Property(pIsolate, pValue, XFA_Event::Modifier, bSetting);
114}
115
116void CJX_EventPseudoModel::newContentType(v8::Isolate* pIsolate,
117 v8::Local<v8::Value>* pValue,
118 bool bSetting,
119 XFA_Attribute eAttribute) {
120 Property(pIsolate, pValue, XFA_Event::NewContentType, bSetting);
121}
122
123void CJX_EventPseudoModel::newText(v8::Isolate* pIsolate,
124 v8::Local<v8::Value>* pValue,
125 bool bSetting,
126 XFA_Attribute eAttribute) {
127 if (bSetting)
128 return;
129
130 CXFA_EventParam* pEventParam =
131 GetDocument()->GetScriptContext()->GetEventParam();
132 if (!pEventParam)
133 return;
134
135 *pValue = fxv8::NewStringHelper(
136 pIsolate, pEventParam->GetNewText().ToUTF8().AsStringView());
137}
138
139void CJX_EventPseudoModel::prevContentType(v8::Isolate* pIsolate,
140 v8::Local<v8::Value>* pValue,
141 bool bSetting,
142 XFA_Attribute eAttribute) {
143 Property(pIsolate, pValue, XFA_Event::PreviousContentType, bSetting);
144}
145
146void CJX_EventPseudoModel::prevText(v8::Isolate* pIsolate,
147 v8::Local<v8::Value>* pValue,
148 bool bSetting,
149 XFA_Attribute eAttribute) {
150 Property(pIsolate, pValue, XFA_Event::PreviousText, bSetting);
151}
152
153void CJX_EventPseudoModel::reenter(v8::Isolate* pIsolate,
154 v8::Local<v8::Value>* pValue,
155 bool bSetting,
156 XFA_Attribute eAttribute) {
157 Property(pIsolate, pValue, XFA_Event::Reenter, bSetting);
158}
159
160void CJX_EventPseudoModel::selEnd(v8::Isolate* pIsolate,
161 v8::Local<v8::Value>* pValue,
162 bool bSetting,
163 XFA_Attribute eAttribute) {
164 Property(pIsolate, pValue, XFA_Event::SelectionEnd, bSetting);
165}
166
167void CJX_EventPseudoModel::selStart(v8::Isolate* pIsolate,
168 v8::Local<v8::Value>* pValue,
169 bool bSetting,
170 XFA_Attribute eAttribute) {
171 Property(pIsolate, pValue, XFA_Event::SelectionStart, bSetting);
172}
173
174void CJX_EventPseudoModel::shift(v8::Isolate* pIsolate,
175 v8::Local<v8::Value>* pValue,
176 bool bSetting,
177 XFA_Attribute eAttribute) {
178 Property(pIsolate, pValue, XFA_Event::Shift, bSetting);
179}
180
181void CJX_EventPseudoModel::soapFaultCode(v8::Isolate* pIsolate,
182 v8::Local<v8::Value>* pValue,
183 bool bSetting,
184 XFA_Attribute eAttribute) {
185 Property(pIsolate, pValue, XFA_Event::SoapFaultCode, bSetting);
186}
187
188void CJX_EventPseudoModel::soapFaultString(v8::Isolate* pIsolate,
189 v8::Local<v8::Value>* pValue,
190 bool bSetting,
191 XFA_Attribute eAttribute) {
192 Property(pIsolate, pValue, XFA_Event::SoapFaultString, bSetting);
193}
194
195void CJX_EventPseudoModel::target(v8::Isolate* pIsolate,
196 v8::Local<v8::Value>* pValue,
197 bool bSetting,
198 XFA_Attribute eAttribute) {
199 Property(pIsolate, pValue, XFA_Event::Target, bSetting);
200}
201
202CJS_Result CJX_EventPseudoModel::emit(
203 CFXJSE_Engine* runtime,
204 pdfium::span<v8::Local<v8::Value>> params) {
205 CXFA_EventParam* pEventParam = runtime->GetEventParam();
206 if (!pEventParam)
208
209 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
210 if (!pNotify)
212
213 pNotify->HandleWidgetEvent(runtime->GetEventTarget(), pEventParam);
215}
216
217CJS_Result CJX_EventPseudoModel::reset(
218 CFXJSE_Engine* runtime,
219 pdfium::span<v8::Local<v8::Value>> params) {
220 CXFA_EventParam* pEventParam = runtime->GetEventParam();
221 if (pEventParam)
223
225}
226
227void CJX_EventPseudoModel::Property(v8::Isolate* pIsolate,
228 v8::Local<v8::Value>* pValue,
229 XFA_Event dwFlag,
230 bool bSetting) {
231 // Only the cancelAction, selStart, selEnd and change properties are writable.
232 if (bSetting && dwFlag != XFA_Event::CancelAction &&
233 dwFlag != XFA_Event::SelectionStart &&
234 dwFlag != XFA_Event::SelectionEnd && dwFlag != XFA_Event::Change) {
235 return;
236 }
237
238 CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext();
239 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();
240 if (!pEventParam)
241 return;
242
243 switch (dwFlag) {
245 BooleanProperty(pIsolate, pValue, &pEventParam->m_bCancelAction,
246 bSetting);
247 break;
249 StringProperty(pIsolate, pValue, &pEventParam->m_wsChange, bSetting);
250 break;
252 IntegerProperty(pIsolate, pValue, &pEventParam->m_iCommitKey, bSetting);
253 break;
255 StringProperty(pIsolate, pValue, &pEventParam->m_wsFullText, bSetting);
256 break;
258 BooleanProperty(pIsolate, pValue, &pEventParam->m_bKeyDown, bSetting);
259 break;
261 BooleanProperty(pIsolate, pValue, &pEventParam->m_bModifier, bSetting);
262 break;
264 StringProperty(pIsolate, pValue, &pEventParam->m_wsNewContentType,
265 bSetting);
266 break;
268 NOTREACHED_NORETURN();
270 StringProperty(pIsolate, pValue, &pEventParam->m_wsPrevContentType,
271 bSetting);
272 break;
274 StringProperty(pIsolate, pValue, &pEventParam->m_wsPrevText, bSetting);
275 break;
277 BooleanProperty(pIsolate, pValue, &pEventParam->m_bReenter, bSetting);
278 break;
280 IntegerProperty(pIsolate, pValue, &pEventParam->m_iSelEnd, bSetting);
281
282 pEventParam->m_iSelEnd = std::max(0, pEventParam->m_iSelEnd);
283 pEventParam->m_iSelEnd = std::min(
284 pEventParam->m_iSelEnd, pdfium::base::checked_cast<int32_t>(
285 pEventParam->m_wsPrevText.GetLength()));
286 pEventParam->m_iSelStart =
287 std::min(pEventParam->m_iSelStart, pEventParam->m_iSelEnd);
288 break;
290 IntegerProperty(pIsolate, pValue, &pEventParam->m_iSelStart, bSetting);
291 pEventParam->m_iSelStart = std::max(0, pEventParam->m_iSelStart);
292 pEventParam->m_iSelStart = std::min(
293 pEventParam->m_iSelStart, pdfium::base::checked_cast<int32_t>(
294 pEventParam->m_wsPrevText.GetLength()));
295 pEventParam->m_iSelEnd =
296 std::max(pEventParam->m_iSelStart, pEventParam->m_iSelEnd);
297 break;
298 case XFA_Event::Shift:
299 BooleanProperty(pIsolate, pValue, &pEventParam->m_bShift, bSetting);
300 break;
302 StringProperty(pIsolate, pValue, &pEventParam->m_wsSoapFaultCode,
303 bSetting);
304 break;
306 StringProperty(pIsolate, pValue, &pEventParam->m_wsSoapFaultString,
307 bSetting);
308 break;
310 break;
311 }
312}
@ PreviousContentType
CXFA_Node * GetEventTarget() const
CXFA_EventParam * GetEventParam() const
friend class EventParamScope
static CJS_Result Success()
Definition cjs_result.h:27
bool DynamicTypeIs(TypeTag eType) const override
~CJX_EventPseudoModel() override
void DefineMethods(pdfium::span< const CJX_MethodSpec > methods)
CXFA_Document * GetDocument() const
virtual bool DynamicTypeIs(TypeTag eType) const
WideString GetNewText() const
CXFA_EventParam & operator=(CXFA_EventParam &&other) noexcept
CXFA_EventParam(XFA_EVENTTYPE type)
void HandleWidgetEvent(CXFA_Node *pNode, CXFA_EventParam *pParam)
ByteString ToUTF8() const
@ XFA_EVENT_Unknown
XFA_Attribute
Definition fxfa_basic.h:67