Bug 98461

Summary: Image object onerr event not working, triggers onload instead
Product: [Applications] konqueror Reporter: Pedro Fayolle <pfayolle>
Component: khtml ecmaAssignee: Konqueror Developers <konq-bugs>
Status: RESOLVED FIXED    
Severity: normal CC: maksim
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: Gentoo Packages   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: naive patch
fixes event type, too

Description Pedro Fayolle 2005-02-03 01:43:50 UTC
Version:            (using KDE KDE 3.3.2)
Installed from:    Gentoo Packages
OS:                Linux

When loading an image from pure JS the onload event (meaning a successful load) is triggered with no error checking, so the onerror event is never triggered no matter how mangled the src attribute is.

The following example displays "Image loaded" even though the supplied src attribute is not valid:

<script type="text/javascript">
var img = new Image;

img.onload = function()
{
 alert('Image loaded');
}

img.onerror = function()
{
 alert('Error');
}

img.src = 'http://fake/url/fake_image.jpg';
</script>

Changing the attribute to something uglier, even with no protocol at all, produces the same results.

This only applies to pure JavaScript image loading. When specifying the src attribute directly in an HTML img tag events are correctly handled.
Comment 1 Maksim Orlovich 2005-06-18 23:27:39 UTC
Created attachment 11505 [details]
naive patch

naive patch. Some code consolidation might be in order, and I don't understand
why the cast from ATTR_ONLOAD is there, seems like it's an internal detail?
Comment 2 Maksim Orlovich 2005-06-18 23:51:57 UTC
Yeah, the event types are bogus, as can be seen by testing event.type in the handlers. Revised patch for that next comment..
Comment 3 Maksim Orlovich 2005-06-18 23:55:26 UTC
Created attachment 11506 [details]
fixes event type, too

revised
Comment 4 Maksim Orlovich 2005-09-27 03:17:51 UTC
To update you: I've fixed up a lot of stuff with Image, and it now does distinguish onload vs. onerror, but there is a confusion inside KHTML about what onerror means (there are basically 2 types!), and so the handler doesn't get invoked, but that should be easy to fix once I can talk to person who put that stuff in and ask for some rationale.
Comment 5 Maksim Orlovich 2006-01-26 16:01:55 UTC
SVN commit 502559 by orlovich:

Make onerror work for images by actually using the same type of events 
for events and listeners.
BUG:98461


 M  +2 -2      ecma/kjs_dom.cpp  
 M  +2 -2      ecma/kjs_window.cpp  
 M  +0 -2      xml/dom2_eventsimpl.cpp  
 M  +0 -1      xml/dom2_eventsimpl.h  


--- branches/KDE/3.5/kdelibs/khtml/ecma/kjs_dom.cpp #502558:502559
@@ -210,7 +210,7 @@
   case OnDragDrop:
     return getListener(DOM::EventImpl::KHTML_DRAGDROP_EVENT);
   case OnError:
-    return getListener(DOM::EventImpl::KHTML_ERROR_EVENT);
+    return getListener(DOM::EventImpl::ERROR_EVENT);
   case OnFocus:
     return getListener(DOM::EventImpl::FOCUS_EVENT);
   case OnKeyDown:
@@ -351,7 +351,7 @@
     setListener(exec,DOM::EventImpl::KHTML_DRAGDROP_EVENT,value);
     break;
   case OnError:
-    setListener(exec,DOM::EventImpl::KHTML_ERROR_EVENT,value);
+    setListener(exec,DOM::EventImpl::ERROR_EVENT,value);
     break;
   case OnFocus:
     setListener(exec,DOM::EventImpl::FOCUS_EVENT,value);
--- branches/KDE/3.5/kdelibs/khtml/ecma/kjs_window.cpp #502558:502559
@@ -752,7 +752,7 @@
     case Ondragdrop:
       return getListener(exec,DOM::EventImpl::KHTML_DRAGDROP_EVENT);
     case Onerror:
-      return getListener(exec,DOM::EventImpl::KHTML_ERROR_EVENT);
+      return getListener(exec,DOM::EventImpl::ERROR_EVENT);
     case Onfocus:
       return getListener(exec,DOM::EventImpl::FOCUS_EVENT);
     case Onkeydown:
@@ -928,7 +928,7 @@
       return;
     case Onerror:
       if (isSafeScript(exec))
-        setListener(exec,DOM::EventImpl::KHTML_ERROR_EVENT,value);
+        setListener(exec,DOM::EventImpl::ERROR_EVENT,value);
       return;
     case Onfocus:
       if (isSafeScript(exec))
--- branches/KDE/3.5/kdelibs/khtml/xml/dom2_eventsimpl.cpp #502558:502559
@@ -260,8 +260,6 @@
         return "click";
     case KHTML_DRAGDROP_EVENT:
         return "khtml_dragdrop";
-    case KHTML_ERROR_EVENT:
-        return "khtml_error";
     case KHTML_MOVE_EVENT:
         return "khtml_move";
     case KHTML_READYSTATECHANGE_EVENT:
--- branches/KDE/3.5/kdelibs/khtml/xml/dom2_eventsimpl.h #502558:502559
@@ -84,7 +84,6 @@
 	KHTML_ECMA_DBLCLICK_EVENT, // for html ondblclick
 	KHTML_ECMA_CLICK_EVENT, // for html onclick
 	KHTML_DRAGDROP_EVENT,
-	KHTML_ERROR_EVENT,
 	KHTML_MOVE_EVENT,
         // XMLHttpRequest events
         KHTML_READYSTATECHANGE_EVENT
Comment 6 Maksim Orlovich 2006-01-26 16:08:53 UTC
SVN commit 502570 by orlovich:

Regression test for #98461
CCBUG:98461


 A             baseline/events/img_on_error.html-dom  
 A             baseline/events/img_on_error.html-render  
 M  +1 -0      baseline/events/svnignore  
 A             tests/events/img_on_error.html  


--- trunk/tests/khtmltests/regression/baseline/events/svnignore #502569:502570
@@ -12,3 +12,4 @@
 iframe_display.html-dom
 window_addevent.html-render
 wrong-type-1.html-dump.png
+img_on_error.html-dump.png