KHtml

kjs_events.h
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2001 Peter Kelly ([email protected])
4  * Copyright (C) 2003 Apple Computer, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef _KJS_EVENTS_H_
22 #define _KJS_EVENTS_H_
23 
24 #include "ecma/kjs_dom.h"
25 #include "dom/dom2_events.h"
26 #include "dom/dom_misc.h"
27 #include "xml/dom2_eventsimpl.h"
28 
29 namespace KJS
30 {
31 
32 class Window;
33 
34 class JSEventListener : public DOM::EventListener
35 {
36 public:
37  /**
38  * @param _listener the function object, that will be called when the event is emitted
39  * @param _compareListenerImp Compare Listener implementation.
40  * @param _win Window object, for memory management and caching.
41  * @param _html \c true if it is HTML.
42  * Never create a JSEventListener directly, use Window::getJSEventListener.
43  */
44  JSEventListener(JSObject *_listener, JSObject *_compareListenerImp, JSObject *_win, bool _html = false);
45  virtual ~JSEventListener();
46  void handleEvent(DOM::Event &evt) override;
47  DOM::DOMString eventListenerType() override;
48  // Return the KJS function object executed when this event is emitted
49  virtual JSObject *listenerObj() const;
50  // for Window::clear(). This is a bad hack though. The JSEventListener might not get deleted
51  // if it was added to a DOM node in another frame (#61467). But calling removeEventListener on
52  // all nodes we're listening to is quite difficult.
53  void clear()
54  {
55  listener = nullptr;
56  compareListenerImp = nullptr;
57  }
58  bool isHTMLEventListener() const
59  {
60  return html;
61  }
62 
63 protected:
64  mutable ProtectedPtr<JSObject> listener;
65  // Storing a different JSObject ptr is needed to support addEventListener(.. [Object] ..) calls
66  // In the real-life case (where a 'function' is passed to addEventListener) we can directly call
67  // the 'listener' object and can cache the 'listener.imp()'. If the event listener should be removed
68  // the implementation will call removeEventListener(.. [Function] ..), and we can lookup the event
69  // listener by the passed function's imp() ptr.
70  // In the only dom-approved way (passing an Object to add/removeEventListener), the 'listener'
71  // variable stores the function object 'passedListener.handleEvent'. But we need to cache
72  // the imp() ptr of the 'passedListener' function _object_, as the implementation will
73  // call removeEventListener(.. [Object ..] on removal, and now we can successfully lookup
74  // the correct event listener, as well as the 'listener.handleEvent' function, we need to call.
75  mutable ProtectedPtr<JSObject> compareListenerImp;
76  bool html;
77  mutable ProtectedPtr<JSObject> win;
78 };
79 
80 class JSLazyEventListener : public JSEventListener
81 {
82 public:
83  JSLazyEventListener(const QString &_code, const QString &_url, int _lineNum,
84  const QString &_name, JSObject *_win, DOM::NodeImpl *node, bool _svg = false);
85  ~JSLazyEventListener();
86  void handleEvent(DOM::Event &evt) override;
87  JSObject *listenerObj() const override;
88 private:
89  void parseCode() const;
90 
91  mutable QString code;
92  mutable QString url;
93  int lineNum;
94 
95  mutable QString name;
96  mutable bool parsed;
97  DOM::NodeImpl *originalNode;
98  bool svg;
99 };
100 
101 // Constructor for Event - currently only used for some global vars
102 DEFINE_PSEUDO_CONSTRUCTOR(EventConstructor)
103 
104 class DOMEvent : public DOMObject
105 {
106 public:
107  // Build a DOMEvent
108  DOMEvent(ExecState *exec, DOM::EventImpl *e);
109  DOMEvent(JSObject *proto, DOM::EventImpl *e);
110  ~DOMEvent();
111 
112  using KJS::JSObject::getOwnPropertySlot;
113  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
114  JSValue *getValueProperty(ExecState *, int token) const;
115  using KJS::JSObject::put;
116  virtual void put(ExecState *exec, const Identifier &propertyName,
117  JSValue *value, int attr = None) override;
118  JSValue *defaultValue(ExecState *exec, KJS::JSType hint) const override;
119  void putValueProperty(ExecState *exec, int token, JSValue *value, int);
120  const ClassInfo *classInfo() const override
121  {
122  return &info;
123  }
124  static const ClassInfo info;
125  enum { Type, Target, CurrentTarget, EventPhase, Bubbles,
126  Cancelable, TimeStamp, StopPropagation, PreventDefault, InitEvent,
127  // MS IE equivalents
128  SrcElement, ReturnValue, CancelBubble
129  };
130  DOM::EventImpl *impl() const
131  {
132  return m_impl.get();
133  }
134 protected:
135  SharedPtr<DOM::EventImpl> m_impl;
136 };
137 
138 JSValue *getDOMEvent(ExecState *exec, DOM::EventImpl *e);
139 
140 /**
141  * Convert an object to an Event. Returns a null Event if not possible.
142  */
143 DOM::EventImpl *toEvent(JSValue *);
144 
145 // Constructor object EventException
146 class EventExceptionConstructor : public DOMObject
147 {
148 public:
149  EventExceptionConstructor(ExecState *);
150  using KJS::JSObject::getOwnPropertySlot;
151  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
152  JSValue *getValueProperty(ExecState *, int token) const;
153  // no put - all read-only
154  const ClassInfo *classInfo() const override
155  {
156  return &info;
157  }
158  static const ClassInfo info;
159 };
160 
161 JSValue *getEventExceptionConstructor(ExecState *exec);
162 
163 class DOMUIEvent : public DOMEvent
164 {
165 public:
166  // Build a DOMUIEvent
167  DOMUIEvent(ExecState *exec, DOM::UIEventImpl *ue);
168  DOMUIEvent(JSObject *proto, DOM::UIEventImpl *ue);
169  ~DOMUIEvent();
170  using KJS::JSObject::getOwnPropertySlot;
171  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
172  JSValue *getValueProperty(ExecState *, int token) const;
173  // no put - all read-only
174  const ClassInfo *classInfo() const override
175  {
176  return &info;
177  }
178  static const ClassInfo info;
179  enum { View, Detail, KeyCode, CharCode, LayerX, LayerY, PageX, PageY, Which, InitUIEvent };
180  DOM::UIEventImpl *impl() const
181  {
182  return static_cast<DOM::UIEventImpl *>(m_impl.get());
183  }
184 };
185 
186 class DOMMouseEvent : public DOMUIEvent
187 {
188 public:
189  DOMMouseEvent(ExecState *exec, DOM::MouseEventImpl *me);
190  ~DOMMouseEvent();
191  using KJS::JSObject::getOwnPropertySlot;
192  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
193  JSValue *getValueProperty(ExecState *, int token) const;
194  // no put - all read-only
195  const ClassInfo *classInfo() const override
196  {
197  return &info;
198  }
199  static const ClassInfo info;
200  enum { ScreenX, ScreenY, ClientX, X, ClientY, Y, OffsetX, OffsetY,
201  CtrlKey, ShiftKey, AltKey,
202  MetaKey, Button, RelatedTarget, FromElement, ToElement,
203  InitMouseEvent
204  };
205  DOM::MouseEventImpl *impl() const
206  {
207  return static_cast<DOM::MouseEventImpl *>(m_impl.get());
208  }
209 };
210 
211 class DOMKeyEventBase : public DOMUIEvent
212 {
213 public:
214  DOMKeyEventBase(JSObject *proto, DOM::KeyEventBaseImpl *ke);
215  ~DOMKeyEventBase();
216 
217  using KJS::JSObject::getOwnPropertySlot;
218  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
219  JSValue *getValueProperty(ExecState *, int token) const;
220  // no put - all read-only
221  const ClassInfo *classInfo() const override
222  {
223  return &info;
224  }
225  static const ClassInfo info;
226  enum { Key, VirtKey, CtrlKey, ShiftKey, AltKey, MetaKey };
227  DOM::KeyEventBaseImpl *impl() const
228  {
229  return static_cast<DOM::KeyEventBaseImpl *>(m_impl.get());
230  }
231 };
232 
233 class DOMTextEvent : public DOMKeyEventBase
234 {
235 public:
236  DOMTextEvent(ExecState *exec, DOM::TextEventImpl *ke);
237  ~DOMTextEvent();
238  using KJS::JSObject::getOwnPropertySlot;
239  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
240  JSValue *getValueProperty(ExecState *, int token) const;
241  // no put - all read-only
242  const ClassInfo *classInfo() const override
243  {
244  return &info;
245  }
246  static const ClassInfo info;
247  enum {Data, InitTextEvent};
248  DOM::TextEventImpl *impl() const
249  {
250  return static_cast<DOM::TextEventImpl *>(m_impl.get());
251  }
252 };
253 
254 class DOMKeyboardEvent : public DOMKeyEventBase
255 {
256 public:
257  DOMKeyboardEvent(ExecState *exec, DOM::KeyboardEventImpl *ke);
258  ~DOMKeyboardEvent();
259  using KJS::JSObject::getOwnPropertySlot;
260  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
261  JSValue *getValueProperty(ExecState *, int token) const;
262  // no put - all read-only
263  const ClassInfo *classInfo() const override
264  {
265  return &info;
266  }
267  static const ClassInfo info;
268  enum {KeyIdentifier, KeyLocation, GetModifierState, InitKeyboardEvent};
269  DOM::KeyboardEventImpl *impl() const
270  {
271  return static_cast<DOM::KeyboardEventImpl *>(m_impl.get());
272  }
273 };
274 
275 // Constructor object KeyboardEvent
276 class KeyboardEventConstructor : public DOMObject
277 {
278 public:
279  KeyboardEventConstructor(ExecState *);
280  using KJS::JSObject::getOwnPropertySlot;
281  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
282  JSValue *getValueProperty(ExecState *, int token) const;
283  // no put - all read-only
284  const ClassInfo *classInfo() const override
285  {
286  return &info;
287  }
288  static const ClassInfo info;
289 };
290 
291 JSValue *getKeyboardEventConstructor(ExecState *exec);
292 
293 // Constructor object MutationEvent
294 class MutationEventConstructor : public DOMObject
295 {
296 public:
297  MutationEventConstructor(ExecState *);
298  using KJS::JSObject::getOwnPropertySlot;
299  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
300  JSValue *getValueProperty(ExecState *, int token) const;
301  // no put - all read-only
302  const ClassInfo *classInfo() const override
303  {
304  return &info;
305  }
306  static const ClassInfo info;
307 };
308 
309 JSValue *getMutationEventConstructor(ExecState *exec);
310 
311 class DOMMutationEvent : public DOMEvent
312 {
313 public:
314  DOMMutationEvent(ExecState *exec, DOM::MutationEventImpl *me);
315  ~DOMMutationEvent();
316  using KJS::JSObject::getOwnPropertySlot;
317  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
318  JSValue *getValueProperty(ExecState *, int token) const;
319  // no put - all read-only
320  const ClassInfo *classInfo() const override
321  {
322  return &info;
323  }
324  static const ClassInfo info;
325  enum { AttrChange, RelatedNode, AttrName, PrevValue, NewValue,
326  InitMutationEvent
327  };
328  DOM::MutationEventImpl *impl() const
329  {
330  return static_cast<DOM::MutationEventImpl *>(m_impl.get());
331  }
332 };
333 
334 class DOMMessageEvent: public DOMEvent
335 {
336 public:
337  DOMMessageEvent(ExecState *exec, DOM::MessageEventImpl *me);
338 
339  using KJS::JSObject::getOwnPropertySlot;
340  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
341  JSValue *getValueProperty(ExecState *, int token) const;
342  // no put - all read-only
343  const ClassInfo *classInfo() const override
344  {
345  return &info;
346  }
347  static const ClassInfo info;
348  enum { Data, Origin, Source, LastEventId, InitMessageEvent };
349  DOM::MessageEventImpl *impl() const
350  {
351  return static_cast<DOM::MessageEventImpl *>(m_impl.get());
352  }
353 };
354 
355 DEFINE_PSEUDO_CONSTRUCTOR(MessageEventPseudoCtor)
356 
357 class DOMHashChangeEvent : public DOMEvent
358 {
359 public:
360  DOMHashChangeEvent(ExecState *exec, DOM::HashChangeEventImpl *me);
361 
362  using KJS::JSObject::getOwnPropertySlot;
363  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
364  JSValue *getValueProperty(ExecState *, int token) const;
365  // no put - all read-only
366  const ClassInfo *classInfo() const override
367  {
368  return &info;
369  }
370  static const ClassInfo info;
371  enum { NewUrl, OldUrl, InitHashChangeEvent };
372  DOM::HashChangeEventImpl *impl() const
373  {
374  return static_cast<DOM::HashChangeEventImpl *>(m_impl.get());
375  }
376 };
377 DEFINE_PSEUDO_CONSTRUCTOR(HashChangeEventPseudoCtor)
378 
379 } // namespace
380 
381 #endif
QString name(const QVariant &location)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
KIOCORE_EXPORT TransferJob * put(const QUrl &url, int permissions, JobFlags flags=DefaultFlags)
Introduced in DOM Level 2.
Definition: dom2_events.h:69
Introduced in DOM Level 2.
Definition: dom2_events.h:116
Window
Definition: kjs_window.h:393
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.