Refactor inline event handlers in ClippedImageImpl for CSP compat.

The onload handler is used to detect the situation when an image has
loaded before having been "attached" (added to the page). This is not
really possible to do another way. Although the DOM APIs define a
'complete' property which can be used to determine if the image has
finished loading, there is no similar property that will tell you if an
error occurred along the way.

This CL removes the onload property from the SafeHtmlTemplate. This
should be safe to do because the template is supposed to be treated
opaquely (according to AbstractImagePrototype.getSafeHtml() docs) and
therefore should not have any listeners attached.

The .createStructure() method is refactored to programmatically add the
'onload' handler after the image element is instantiated from the
SafeHtml template.

Change-Id: Iefdf49204b83ef836d37492144b27e636d513f17
Review-Link: https://gwt-review.googlesource.com/#/c/18500/
diff --git a/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImpl.java b/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImpl.java
index a772f1d..b009637 100644
--- a/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImpl.java
+++ b/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImpl.java
@@ -16,6 +16,7 @@
 package com.google.gwt.user.client.ui.impl;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.Style.Unit;
@@ -38,13 +39,13 @@
 public class ClippedImageImpl {
 
   interface DraggableTemplate extends SafeHtmlTemplates {
-    @SafeHtmlTemplates.Template("<img onload='this.__gwtLastUnhandledEvent=\"load\";' src='{0}' "
+    @SafeHtmlTemplates.Template("<img src='{0}' "
         + "style='{1}' border='0' draggable='true'>")
     SafeHtml image(SafeUri clearImage, SafeStyles style);
   }
 
   interface Template extends SafeHtmlTemplates {
-    @SafeHtmlTemplates.Template("<img onload='this.__gwtLastUnhandledEvent=\"load\";' src='{0}' "
+    @SafeHtmlTemplates.Template("<img src='{0}' "
         + "style='{1}' border='0'>")
     SafeHtml image(SafeUri clearImage, SafeStyles style);
   }
@@ -64,9 +65,18 @@
   public Element createStructure(SafeUri url, int left, int top, int width, int height) {
     Element tmp = Document.get().createSpanElement();
     tmp.setInnerSafeHtml(getSafeHtml(url, left, top, width, height));
-    return tmp.getFirstChildElement();
+
+    Element elem = tmp.getFirstChildElement();
+    elem.setPropertyJSO("onload", createOnLoadHandlerFunction());
+    return elem;
   }
 
+  public static native JavaScriptObject createOnLoadHandlerFunction() /*-{
+    return function() {
+      this.__gwtLastUnhandledEvent = 'load';
+    }
+  }-*/;
+
   public Element getImgElement(Image image) {
     return image.getElement();
   }