How do I create a TouchEvent in Chrome?
the W3C specification declares initTouchEvent
as follows:
void initTouchEvent (in DOMString type,
in boolean canBubble,
in boolean cancelable,
in AbstractView view,
in long detail,
in boolean ctrlKey,
in boolean altKey,
in boolean shiftKey,
in boolean metaKey,
in TouchList touches,
in TouchList targetTouches,
in TouchList changedTouches);
However, when I try to use it in Chrome 44:
var e = document.createEvent('TouchEvent');
e.initTouchEvent("touchstart", true, true, window, 1,
false, false, false, false, touches, null, null);
where touches
is valid TouchList
, this is what Chrome created:
> TouchEvent {}
altKey: false
bubbles: true
cancelBubble: false
cancelable: true
changedTouches: null
charCode: 0
ctrlKey: true
currentTarget: null
defaultPrevented: false
detail: 0
eventPhase: 0
keyCode: 0
layerX: 0
layerY: 0
metaKey: false
pageX: 0
pageY: 0
path: Array[0]
returnValue: true
shiftKey: false
srcElement: null
target: null
targetTouches: null
timeStamp: 1435339572699
touches: null
type: "[object Window]"
view: null
which: 0
Look closely at the field type
. Looks like Chrome is not following the spec where the 4th parameter turned into a type instead of the 1st parameter.
The question is, how can I build TouchEvent
in Chrome since it is out of spec?
source to share
Looking at the source of Chromium and Qiita (in Japanese) , this is similar to how its parameters are located:
initTouchEvent (TouchList touches,
TouchList targetTouches,
TouchList changedTouches,
String type,
Window view,
number screenX,
number screenY,
number clientX,
number clientY,
boolean ctrlKey,
boolean altKey,
boolean shiftKey,
boolean metaKey);
Please note that Chrome is not W3C compliant.
Relevant part in chromium source:
TouchEvent.cpp
Line 63:
void TouchEvent::initTouchEvent(ScriptState* scriptState, TouchList* touches, TouchList* targetTouches,
TouchList* changedTouches, const AtomicString& type,
PassRefPtrWillBeRawPtr<AbstractView> view,
int, int, int, int,
bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
{
if (dispatched())
return;
if (scriptState->world().isIsolatedWorld())
UIEventWithKeyState::didCreateEventInIsolatedWorld(ctrlKey, altKey, shiftKey, metaKey);
bool cancelable = true;
if (type == EventTypeNames::touchcancel)
cancelable = false;
initUIEvent(type, true, cancelable, view, 0);
m_touches = touches;
m_targetTouches = targetTouches;
m_changedTouches = changedTouches;
m_ctrlKey = ctrlKey;
m_altKey = altKey;
m_shiftKey = shiftKey;
m_metaKey = metaKey;
}
source to share