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