Skip to content

Commit 4074150

Browse files
handle undefined touches[0]
1 parent 10467b9 commit 4074150

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

tests/pointerevent.html

+22-27
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@
5858
<td>
5959
<div class="reset" id="reset"><div class="flex=item" id="resettext">1: START</div></div>
6060
<div class="pointer" id="target">
61-
<div class="flex-item"><br>2: CLICK<br>
62-
<br><span class='s3'>MOUSE</span><br>middle or<br>context<br>
63-
<br><span class='s3'>TOUCH | PEN</span><br>tap + drag a bit<br>tap again</div>
61+
<div class="flex-item"><br>2: CLICK<br><br>context click<br>(if you can)<br><br>otherwise<br><br>keep stabbing<br>and moving</div>
6462
</div>
6563
<div class="reset"><div class="flex=item">3: FINISH</div></div>
6664
</td>
@@ -126,11 +124,16 @@
126124
altitudeAngle: "number",
127125
azimuthAngle: "number",
128126
}
129-
// events we want to catch data on
127+
// events
130128
let oDataEvents = {
131129
'pointer': ['down','enter','leave','move','over','out','up'],
132130
'touch': ['cancel','end','move','start'],
133131
}
132+
let oNonDataEvents = {
133+
'mouse': ['down','enter','leave','move','out','over','up'],
134+
'pointer': ['cancel'],
135+
//'touch': ['cancel'],
136+
}
134137

135138
function finish() {
136139
// check all events have been recorded
@@ -172,6 +175,10 @@
172175

173176
try {
174177
let touch = event.touches[0]
178+
// touchcancel + touchend don't have touch data
179+
if (undefined == touch) {
180+
touch = event.changedTouches[0]
181+
}
175182
aList.forEach(function(k){
176183
let value
177184
try {
@@ -183,14 +190,7 @@
183190
}
184191
oTemp[input][type][k] = value
185192
} catch(e) {
186-
// touchcancel and touchend don't have values except for force
187-
// so don't record them if we catch a TypeError
188-
// this means we can now properly compare valid matches in screen/client X/Y
189-
if (e.name == 'TypeError' && k !== 'force') {
190-
// record nothing
191-
} else {
192-
oTemp[input][type][k] = e.name
193-
}
193+
oTemp[input][type][k] = e.name
194194
}
195195
})
196196

@@ -205,13 +205,11 @@
205205
// if all tests are the same value just display one value
206206
let aSet = new Set(), aArray = []
207207
for (const p of Object.keys(oTemp[input]).sort()) { // sort so array is in order when needed
208-
if (undefined !== oTemp[input][p][k]) {
209-
let x = oTemp[input][p][k]
210-
// force is variable
211-
if ('force' == k) {if (!aStable.includes(x)) {x = 'float'}}
212-
aSet.add(x)
213-
aArray.push(x)
214-
}
208+
let x = oTemp[input][p][k]
209+
// force is variable
210+
if ('force' == k) {if ('number' == typeof x && !aStable.includes(x)) {x = 'float'}}
211+
aSet.add(x)
212+
aArray.push(x)
215213
}
216214
let fp = aArray
217215
if (1 == aSet.size) {aArray = Array.from(aSet); fp = aArray[0]}
@@ -299,7 +297,7 @@
299297
let x = oTemp[input][p][k]
300298
// active pen pressure is variable between 0-1
301299
if ('pressure' == k || 'mozPressure' == k) {
302-
if (!aStable.includes(x)) {x = 'float'}
300+
if ('number' == typeof x && !aStable.includes(x)) {x = 'float'}
303301
}
304302
aSet.add(x)
305303
aArray.push(x)
@@ -346,17 +344,14 @@
346344

347345
function start() {
348346
let target = dom.target
349-
// just record the event happening
350-
let oEvents = {
351-
'mouse': ['down','enter','leave','move','out','over','up'],
352-
'pointer': ['cancel']
353-
}
354-
for (const k of Object.keys(oEvents)) {
355-
let list = oEvents[k]
347+
// record the event happening
348+
for (const k of Object.keys(oNonDataEvents)) {
349+
let list = oNonDataEvents[k]
356350
list.forEach(function(type){
357351
target.addEventListener(k + type, (event) => {addEvent(k + type)})
358352
})
359353
}
354+
// record data on these events
360355
for (const k of Object.keys(oDataEvents)) {
361356
let list = oDataEvents[k]
362357
dom[k +'events'].innerHTML = ' [' + list.join('|') +'] '

0 commit comments

Comments
 (0)