Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#15821 closed defect (fixed)

Dojo/touch may throw "Type Error: 'null' is not an object" and fail to call touch.move handlers

Reported by: Eric Durocher Owned by: bill
Priority: high Milestone: 1.7.7
Component: Events Version: 1.8.0rc1
Keywords: Cc:
Blocked By: Blocking:

Description

Using dojo/touch, when listening to touch.move events, the handler may not be called because of a "Type Error: 'null' is not an object" error that occurs in dojo/touch.

This happens on an IOS 5 mobile device (iPhone), when the page is higher than the screen size, and the user has scrolled the page (using the native browser scroll). See the attached sample that reproduces the problem (place the sample in dojo/tests and run it on an iPhone with IOS 5).

The origin seems to be a change in the behavior of the document.elementFromPoint function between IOS 4 and 5, which now expects coordinates relative to the viewport, not the whole page.

The fix would be to substract window.pageX/YOffset from the coordinates used to get the hovered object in the touchmove handler, but this must be done on IOS 5 only since the code is correct for IOS 4.

Attachments (1)

touch_bug.html (1.5 KB) - added by Eric Durocher 7 years ago.
Test case that reproduces the problem.

Download all attachments as: .zip

Change History (17)

Changed 7 years ago by Eric Durocher

Attachment: touch_bug.html added

Test case that reproduces the problem.

comment:1 Changed 7 years ago by Eric Durocher

Priority: undecidedhigh
Summary: On IOS 5, dojo/touch may throw "Type Error: 'null' is not an object" and fail to call touch.move handlersDojo/touch may throw "Type Error: 'null' is not an object" and fail to call touch.move handlers

This does not seem to be specific to IOS 5 actually, it can also be reproduced on Android (all versions that we tested). So in fact the code should be fixed in all cases except IOS 4.

comment:2 Changed 7 years ago by bill

Judging from https://developer.mozilla.org/en-US/docs/DOM/document.elementFromPoint (which keeps mentioning "window"), sounds like the standard behavior is for coordinates relative to the viewport.

Although I don't yet understand why test_dnd.html isn't failing (firefox or chrome) even when the browser window is scrolled. Do you know?

comment:3 in reply to:  description Changed 7 years ago by bill

Replying to edurocher:

The fix would be to substract window.pageX/YOffset from the coordinates used to get the hovered object in the touchmove handler, but this must be done on IOS 5 only since the code is correct for IOS 4.

Actually, the code already does essentially the same thing:

hoveredNode = win.doc.elementFromPoint(
	evt.pageX - win.body().parentNode.scrollLeft,
	evt.pageY - win.body().parentNode.scrollTop
);

Perhaps that doesn't work on iOS?

comment:4 Changed 7 years ago by bill

Component: GeneralEvents
Milestone: tbd1.8
Owner: set to bill
Status: newassigned

Oh, I remember now, there's something weird about scrolling on mobile devices where it isn't really scrolling, so the above code doesn't work. I'll check in the general fix. Since I don't have iOS4 I won't add code for that. If you want to, then you can add branching code for iOS4.

comment:5 Changed 7 years ago by bill

Resolution: fixed
Status: assignedclosed

In [29445]:

fix dojo/touch.move on scrolled pages on mobile, except for iOS4 which I can't test, fixes #15821, refs #15185 !strict.

comment:6 Changed 7 years ago by Adrian Vasiliu

Unfortunately, with this fix I still get the error in edurocher's test case. Besides the failure on iOS 4 (expected, because the old code was correct for iOS 4), it also fails on iOS 5 with the same errors as before, that is win.doc.elementFromPoint still returns null. Reproduced on iPhone 4S iOS 5.0.1 and iPad 2 iOS 5.1.1 (and iPhone iOS 4.3.3).

comment:7 Changed 7 years ago by Adrian Vasiliu

BTW, the source of info we found about the changes with this respect in iOS 4 vs. 5: http://www.icab.de/blog/2011/10/17/elementfrompoint-under-ios-5/

comment:8 Changed 7 years ago by bill

Hmm, that's odd. Is the DnD at least fixed for you (test_dnd.html)?

comment:9 Changed 7 years ago by Adrian Vasiliu

I do get in the console the NPE (null return from elementFromPoint) after playing a while with test_dnd.html, although I can't say I noticed an impact on the DND behavior. Tested on iPhone 4S iOS 5.0.1.

BTW, there's also demos/touch, where the slider doesn't work well on the same iPhone, but this doesn't work well with the previous version either.

comment:10 Changed 7 years ago by Eric Durocher

I do reproduce the bug too after the fix, although with more difficulty, by moving my finger completely out of viewport while dragging (for example keep your finger down an drag completely up the iphone/ipad). Indeed, elementFromPoint seems to return null when the point is outside the viewport (although you can apparently get touch events out of the viewport).

So I think just testing for null return from elementFromPoint and doing nothing in that case should fix that.

comment:11 Changed 7 years ago by Adrian Vasiliu

I've tried with the following code (doing nothing if elementFromPoint returns null):

on(win.doc, "touchmove", function(evt){
  var elem = win.doc.elementFromPoint(
    evt.pageX - win.global.pageXOffset,
    evt.pageY - win.global.pageYOffset
  );
  if (elem){
    var oldNode = hoveredNode;
    hoveredNode = elem;
    if(oldNode !== hoveredNode){ ...remaining code... }
  }
}

The results on iPhone 4S iOS 5.0.1.:

  • edurocher's touch_bug.html: OK
  • tests/dnd/test_dnd.html: OK (DND works even when the page is scrolled)
  • demos/touch: the slider is still KO (but as I said it wasn't working with the previous version either).
Last edited 7 years ago by Adrian Vasiliu (previous) (diff)

comment:12 Changed 7 years ago by bill

OK, I'll check that in.

comment:13 Changed 7 years ago by bill

In [29451]:

ignore null result from elementFromPoint(), refs #15821 !strict

comment:14 Changed 7 years ago by bill

Filed #15831 to track the iOS4 issue. The slider in demos/touch is working for me (well, dragging the handle is working, clicking the bar is not). What problem were you seeing? You should file a separate ticket if there's a problem and it doesn't have a ticket yet.

comment:15 Changed 7 years ago by Adrian Vasiliu

(about the slider trouble)

bill: done, I filed #15834 (together with a patch).

comment:16 Changed 5 years ago by bill

Milestone: 1.81.7.7

Backported to 1.7 in e7403082d02faba2e47cb645d2532dd2920f60b8.

Note: See TracTickets for help on using tickets.