Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLImageElement.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsIDOMHTMLImageElement.h"
00038 #include "nsIDOMNSHTMLImageElement.h"
00039 #include "nsIDOMEventReceiver.h"
00040 #include "nsGenericHTMLElement.h"
00041 #include "nsImageLoadingContent.h"
00042 #include "nsHTMLAtoms.h"
00043 #include "nsStyleConsts.h"
00044 #include "nsPresContext.h"
00045 #include "nsIPresShell.h"
00046 #include "nsMappedAttributes.h"
00047 #include "nsIJSNativeInitializer.h"
00048 #include "nsSize.h"
00049 #include "nsIDocument.h"
00050 #include "nsIDOMWindowInternal.h"
00051 #include "nsIDOMDocument.h"
00052 #include "nsIScriptContext.h"
00053 #include "nsIScriptGlobalObject.h"
00054 #include "nsIURL.h"
00055 #include "nsIIOService.h"
00056 #include "nsIURL.h"
00057 #include "nsIServiceManager.h"
00058 #include "nsNetUtil.h"
00059 #include "nsContentUtils.h"
00060 #include "nsIFrame.h"
00061 #include "nsIImageFrame.h"
00062 #include "nsLayoutAtoms.h"
00063 #include "nsNodeInfoManager.h"
00064 #include "nsGUIEvent.h"
00065 #include "nsContentPolicyUtils.h"
00066 #include "nsIDOMWindow.h"
00067 
00068 #include "imgIContainer.h"
00069 #include "imgILoader.h"
00070 #include "imgIRequest.h"
00071 #include "imgIDecoderObserver.h"
00072 
00073 #include "nsILoadGroup.h"
00074 
00075 #include "nsRuleData.h"
00076 
00077 #include "nsIJSContextStack.h"
00078 #include "nsIView.h"
00079 #include "nsImageMapUtils.h"
00080 #include "nsIDOMHTMLMapElement.h"
00081 
00082 // XXX nav attrs: suppress
00083 
00084 class nsHTMLImageElement : public nsGenericHTMLElement,
00085                            public nsImageLoadingContent,
00086                            public nsIDOMHTMLImageElement,
00087                            public nsIDOMNSHTMLImageElement,
00088                            public nsIJSNativeInitializer
00089 {
00090 public:
00091   nsHTMLImageElement(nsINodeInfo *aNodeInfo);
00092   virtual ~nsHTMLImageElement();
00093 
00094   // nsISupports
00095   NS_DECL_ISUPPORTS_INHERITED
00096 
00097   // nsIDOMNode
00098   NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLElement::)
00099 
00100   // nsIDOMElement
00101   NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLElement::)
00102 
00103   // nsIDOMHTMLElement
00104   NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLElement::)
00105 
00106   // nsIDOMHTMLImageElement
00107   NS_DECL_NSIDOMHTMLIMAGEELEMENT
00108 
00109   // nsIDOMNSHTMLImageElement
00110   NS_DECL_NSIDOMNSHTMLIMAGEELEMENT
00111 
00112   // nsIJSNativeInitializer
00113   NS_IMETHOD Initialize(JSContext* aContext, JSObject *aObj,
00114                         PRUint32 argc, jsval *argv);
00115 
00116   // nsIContent
00117   virtual PRBool ParseAttribute(nsIAtom* aAttribute,
00118                                 const nsAString& aValue,
00119                                 nsAttrValue& aResult);
00120   virtual nsChangeHint GetAttributeChangeHint(const nsIAtom* aAttribute,
00121                                               PRInt32 aModType) const;
00122   NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
00123   virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
00124   virtual nsresult HandleDOMEvent(nsPresContext* aPresContext,
00125                                   nsEvent* aEvent, nsIDOMEvent** aDOMEvent,
00126                                   PRUint32 aFlags,
00127                                   nsEventStatus* aEventStatus);
00128   PRBool IsFocusable(PRInt32 *aTabIndex = nsnull);
00129 
00130   // SetAttr override.  C++ is stupid, so have to override both
00131   // overloaded methods.
00132   nsresult SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00133                    const nsAString& aValue, PRBool aNotify)
00134   {
00135     return SetAttr(aNameSpaceID, aName, nsnull, aValue, aNotify);
00136   }
00137   virtual nsresult SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00138                            nsIAtom* aPrefix, const nsAString& aValue,
00139                            PRBool aNotify);
00140 
00141   // XXXbz What about UnsetAttr?  We don't seem to unload images when
00142   // that happens...
00143 
00144   virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
00145                               nsIContent* aBindingParent,
00146                               PRBool aCompileEventHandlers);
00147 
00148 protected:
00149   void GetImageFrame(nsIImageFrame** aImageFrame);
00150   nsPoint GetXY();
00151   nsSize GetWidthHeight();
00152 };
00153 
00154 nsGenericHTMLElement*
00155 NS_NewHTMLImageElement(nsINodeInfo *aNodeInfo, PRBool aFromParser)
00156 {
00157   /*
00158    * nsHTMLImageElement's will be created without a nsINodeInfo passed in
00159    * if someone says "var img = new Image();" in JavaScript, in a case like
00160    * that we request the nsINodeInfo from the document's nodeinfo list.
00161    */
00162   nsresult rv;
00163   nsCOMPtr<nsINodeInfo> nodeInfo(aNodeInfo);
00164   if (!nodeInfo) {
00165     nsCOMPtr<nsIDocument> doc =
00166       do_QueryInterface(nsContentUtils::GetDocumentFromCaller());
00167     NS_ENSURE_TRUE(doc, nsnull);
00168 
00169     rv = doc->NodeInfoManager()->GetNodeInfo(nsHTMLAtoms::img, nsnull,
00170                                              kNameSpaceID_None,
00171                                              getter_AddRefs(nodeInfo));
00172     NS_ENSURE_SUCCESS(rv, nsnull);
00173   }
00174 
00175   return new nsHTMLImageElement(nodeInfo);
00176 }
00177 
00178 nsHTMLImageElement::nsHTMLImageElement(nsINodeInfo *aNodeInfo)
00179   : nsGenericHTMLElement(aNodeInfo)
00180 {
00181 }
00182 
00183 nsHTMLImageElement::~nsHTMLImageElement()
00184 {
00185   DestroyImageLoadingContent();
00186 }
00187 
00188 
00189 NS_IMPL_ADDREF_INHERITED(nsHTMLImageElement, nsGenericElement)
00190 NS_IMPL_RELEASE_INHERITED(nsHTMLImageElement, nsGenericElement)
00191 
00192 
00193 // QueryInterface implementation for nsHTMLImageElement
00194 NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLImageElement, nsGenericHTMLElement)
00195   NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLImageElement)
00196   NS_INTERFACE_MAP_ENTRY(nsIDOMNSHTMLImageElement)
00197   NS_INTERFACE_MAP_ENTRY(nsIJSNativeInitializer)
00198   NS_INTERFACE_MAP_ENTRY(imgIDecoderObserver)
00199   NS_INTERFACE_MAP_ENTRY(imgIDecoderObserver_MOZILLA_1_8_BRANCH)
00200   NS_INTERFACE_MAP_ENTRY(nsIImageLoadingContent)
00201   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLImageElement)
00202 NS_HTML_CONTENT_INTERFACE_MAP_END
00203 
00204 
00205 NS_IMPL_DOM_CLONENODE(nsHTMLImageElement)
00206 
00207 
00208 NS_IMPL_STRING_ATTR(nsHTMLImageElement, Name, name)
00209 NS_IMPL_STRING_ATTR(nsHTMLImageElement, Align, align)
00210 NS_IMPL_STRING_ATTR(nsHTMLImageElement, Alt, alt)
00211 NS_IMPL_STRING_ATTR(nsHTMLImageElement, Border, border)
00212 NS_IMPL_INT_ATTR(nsHTMLImageElement, Hspace, hspace)
00213 NS_IMPL_BOOL_ATTR(nsHTMLImageElement, IsMap, ismap)
00214 NS_IMPL_URI_ATTR(nsHTMLImageElement, LongDesc, longdesc)
00215 NS_IMPL_STRING_ATTR(nsHTMLImageElement, Lowsrc, lowsrc)
00216 NS_IMPL_URI_ATTR(nsHTMLImageElement, Src, src)
00217 NS_IMPL_STRING_ATTR(nsHTMLImageElement, UseMap, usemap)
00218 NS_IMPL_INT_ATTR(nsHTMLImageElement, Vspace, vspace)
00219 
00220 void
00221 nsHTMLImageElement::GetImageFrame(nsIImageFrame** aImageFrame)
00222 {
00223   *aImageFrame = nsnull;
00224   // If we have no parent, then we won't have a frame yet
00225   if (!GetParent())
00226     return;
00227 
00228   nsIFrame* frame = GetPrimaryFrame(PR_TRUE);
00229 
00230   if (frame) {
00231     CallQueryInterface(frame, aImageFrame);
00232   }
00233 }
00234 
00235 NS_IMETHODIMP
00236 nsHTMLImageElement::GetComplete(PRBool* aComplete)
00237 {
00238   NS_PRECONDITION(aComplete, "Null out param!");
00239   *aComplete = PR_TRUE;
00240 
00241   if (!mCurrentRequest) {
00242     return NS_OK;
00243   }
00244 
00245   PRUint32 status;
00246   mCurrentRequest->GetImageStatus(&status);
00247   *aComplete =
00248     (status &
00249      (imgIRequest::STATUS_LOAD_COMPLETE | imgIRequest::STATUS_ERROR)) != 0;
00250 
00251   return NS_OK;
00252 }
00253 
00254 nsPoint
00255 nsHTMLImageElement::GetXY()
00256 {
00257   nsPoint point(0, 0);
00258 
00259   nsIDocument *document = GetCurrentDoc();
00260 
00261   if (!document) {
00262     return point;
00263   }
00264 
00265   // Get Presentation shell 0
00266   nsIPresShell *presShell = document->GetShellAt(0);
00267   if (!presShell) {
00268     return point;
00269   }
00270 
00271   // Get the Presentation Context from the Shell
00272   nsPresContext *context = presShell->GetPresContext();
00273   if (!context) {
00274     return point;
00275   }
00276 
00277   // Flush all pending notifications so that our frames are laid out correctly
00278   document->FlushPendingNotifications(Flush_Layout);
00279 
00280   // Get the Frame for this image
00281   nsIFrame* frame = nsnull;
00282   presShell->GetPrimaryFrameFor(this, &frame);
00283 
00284   if (!frame) {
00285     return point;
00286   }
00287 
00288   nsPoint origin(0, 0);
00289   nsIView* parentView;
00290   nsresult rv = frame->GetOffsetFromView(origin, &parentView);
00291   if (NS_FAILED(rv)) {
00292     return point;
00293   }
00294 
00295   // Get the scale from that Presentation Context
00296   float scale;
00297   scale = context->TwipsToPixels();
00298 
00299   // Convert to pixels using that scale
00300   point.x = NSTwipsToIntPixels(origin.x, scale);
00301   point.y = NSTwipsToIntPixels(origin.y, scale);
00302 
00303   return point;
00304 }
00305 
00306 NS_IMETHODIMP
00307 nsHTMLImageElement::GetX(PRInt32* aX)
00308 {
00309   *aX = GetXY().x;
00310 
00311   return NS_OK;
00312 }
00313 
00314 NS_IMETHODIMP
00315 nsHTMLImageElement::GetY(PRInt32* aY)
00316 {
00317   *aY = GetXY().y;
00318 
00319   return NS_OK;
00320 }
00321 
00322 nsSize
00323 nsHTMLImageElement::GetWidthHeight()
00324 {
00325   nsSize size(0,0);
00326 
00327   nsIDocument* doc = GetCurrentDoc();
00328   if (doc) {
00329     // Flush all pending notifications so that our frames are up to date.
00330     // If we're not in a document, we don't have a frame anyway, so we
00331     // don't care.
00332     doc->FlushPendingNotifications(Flush_Layout);
00333   }
00334 
00335   nsIImageFrame* imageFrame;
00336   GetImageFrame(&imageFrame);
00337 
00338   nsIFrame* frame = nsnull;
00339 
00340   if (imageFrame) {
00341     CallQueryInterface(imageFrame, &frame);
00342     NS_WARN_IF_FALSE(frame,"Should not happen - image frame is not frame");
00343   }
00344 
00345   if (frame) {
00346     // XXX we could put an accessor on nsIImageFrame to return its
00347     // mComputedSize.....
00348     size = frame->GetSize();
00349 
00350     nsMargin margin;
00351     frame->CalcBorderPadding(margin);
00352 
00353     size.height -= margin.top + margin.bottom;
00354     size.width -= margin.left + margin.right;
00355 
00356     nsPresContext *context = GetPresContext();
00357     if (context) {
00358       float t2p;
00359       t2p = context->TwipsToPixels();
00360 
00361       size.width = NSTwipsToIntPixels(size.width, t2p);
00362       size.height = NSTwipsToIntPixels(size.height, t2p);
00363     }
00364   } else {
00365     const nsAttrValue* value;
00366     nsCOMPtr<imgIContainer> image;
00367     if (mCurrentRequest) {
00368       mCurrentRequest->GetImage(getter_AddRefs(image));
00369     }
00370 
00371     if ((value = GetParsedAttr(nsHTMLAtoms::width)) &&
00372         value->Type() == nsAttrValue::eInteger) {
00373       size.width = value->GetIntegerValue();
00374     } else if (image) {
00375       image->GetWidth(&size.width);
00376     }
00377 
00378     if ((value = GetParsedAttr(nsHTMLAtoms::height)) &&
00379         value->Type() == nsAttrValue::eInteger) {
00380       size.height = value->GetIntegerValue();
00381     } else if (image) {
00382       image->GetHeight(&size.height);
00383     }
00384   }
00385 
00386   return size;
00387 }
00388 
00389 NS_IMETHODIMP
00390 nsHTMLImageElement::GetHeight(PRInt32* aHeight)
00391 {
00392   *aHeight = GetWidthHeight().height;
00393 
00394   return NS_OK;
00395 }
00396 
00397 NS_IMETHODIMP
00398 nsHTMLImageElement::SetHeight(PRInt32 aHeight)
00399 {
00400   nsAutoString val;
00401   val.AppendInt(aHeight);
00402 
00403   return nsGenericHTMLElement::SetAttr(kNameSpaceID_None, nsHTMLAtoms::height,
00404                                        val, PR_TRUE);
00405 }
00406 
00407 NS_IMETHODIMP
00408 nsHTMLImageElement::GetWidth(PRInt32* aWidth)
00409 {
00410   *aWidth = GetWidthHeight().width;
00411 
00412   return NS_OK;
00413 }
00414 
00415 NS_IMETHODIMP
00416 nsHTMLImageElement::SetWidth(PRInt32 aWidth)
00417 {
00418   nsAutoString val;
00419   val.AppendInt(aWidth);
00420 
00421   return nsGenericHTMLElement::SetAttr(kNameSpaceID_None, nsHTMLAtoms::width,
00422                                        val, PR_TRUE);
00423 }
00424 
00425 PRBool
00426 nsHTMLImageElement::ParseAttribute(nsIAtom* aAttribute,
00427                                    const nsAString& aValue,
00428                                    nsAttrValue& aResult)
00429 {
00430   if (aAttribute == nsHTMLAtoms::align) {
00431     return ParseAlignValue(aValue, aResult);
00432   }
00433   if (aAttribute == nsHTMLAtoms::src) {
00434     static const char* kWhitespace = " \n\r\t\b";
00435     aResult.SetTo(nsContentUtils::TrimCharsInSet(kWhitespace, aValue));
00436     return PR_TRUE;
00437   }
00438   if (ParseImageAttribute(aAttribute, aValue, aResult)) {
00439     return PR_TRUE;
00440   }
00441 
00442   return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
00443 }
00444 
00445 static void
00446 MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
00447                       nsRuleData* aData)
00448 {
00449   nsGenericHTMLElement::MapImageAlignAttributeInto(aAttributes, aData);
00450   nsGenericHTMLElement::MapImageBorderAttributeInto(aAttributes, aData);
00451   nsGenericHTMLElement::MapImageMarginAttributeInto(aAttributes, aData);
00452   nsGenericHTMLElement::MapImageSizeAttributesInto(aAttributes, aData);
00453   nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aData);
00454 }
00455 
00456 nsChangeHint
00457 nsHTMLImageElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
00458                                            PRInt32 aModType) const
00459 {
00460   nsChangeHint retval =
00461     nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
00462   if (aAttribute == nsHTMLAtoms::usemap ||
00463       aAttribute == nsHTMLAtoms::ismap) {
00464     NS_UpdateHint(retval, NS_STYLE_HINT_FRAMECHANGE);
00465   }
00466   return retval;
00467 }
00468 
00469 NS_IMETHODIMP_(PRBool)
00470 nsHTMLImageElement::IsAttributeMapped(const nsIAtom* aAttribute) const
00471 {
00472   static const MappedAttributeEntry* const map[] = {
00473     sCommonAttributeMap,
00474     sImageMarginSizeAttributeMap,
00475     sImageBorderAttributeMap,
00476     sImageAlignAttributeMap
00477   };
00478 
00479   return FindAttributeDependence(aAttribute, map, NS_ARRAY_LENGTH(map));
00480 }
00481 
00482 
00483 nsMapRuleToAttributesFunc
00484 nsHTMLImageElement::GetAttributeMappingFunction() const
00485 {
00486   return &MapAttributesIntoRule;
00487 }
00488 
00489 
00490 nsresult
00491 nsHTMLImageElement::HandleDOMEvent(nsPresContext* aPresContext,
00492                                    nsEvent* aEvent, nsIDOMEvent** aDOMEvent,
00493                                    PRUint32 aFlags,
00494                                    nsEventStatus* aEventStatus)
00495 {
00496   // If we are a map and get a mouse click, don't let it be handled by
00497   // the Generic Element as this could cause a click event to fire
00498   // twice, once by the image frame for the map and once by the Anchor
00499   // element. (bug 39723)
00500   if (NS_MOUSE_LEFT_CLICK == aEvent->message) {
00501     PRBool isMap = PR_FALSE;
00502     GetIsMap(&isMap);
00503     if (isMap) {
00504       *aEventStatus = nsEventStatus_eConsumeNoDefault;
00505     }
00506   }
00507 
00508   return nsGenericHTMLElement::HandleDOMEvent(aPresContext, aEvent, aDOMEvent,
00509                                               aFlags, aEventStatus);
00510 }
00511 
00512 PRBool
00513 nsHTMLImageElement::IsFocusable(PRInt32 *aTabIndex)
00514 {
00515   PRInt32 tabIndex;
00516   GetTabIndex(&tabIndex);
00517 
00518   if (IsInDoc()) {
00519     nsAutoString usemap;
00520     GetUseMap(usemap);
00521     // XXXbz which document should this be using?  sXBL/XBL2 issue!  I
00522     // think that GetOwnerDoc() is right, since we don't want to
00523     // assume stuff about the document we're bound to.
00524     nsCOMPtr<nsIDOMHTMLMapElement> imageMap =
00525       nsImageMapUtils::FindImageMap(GetOwnerDoc(), usemap);
00526     if (imageMap) {
00527       if (aTabIndex) {
00528         // Use tab index on individual map areas
00529         *aTabIndex = (sTabFocusModel & eTabFocus_linksMask)? 0 : -1;
00530       }
00531       // Image map is not focusable itself, but flag as tabbable
00532       // so that image map areas get walked into.
00533       return PR_FALSE;
00534     }
00535   }
00536 
00537   if (aTabIndex) {
00538     // Can be in tab order if tabindex >=0 and form controls are tabbable.
00539     *aTabIndex = (sTabFocusModel & eTabFocus_formElementsMask)? tabIndex : -1;
00540   }
00541 
00542   return tabIndex >= 0;
00543 }
00544 
00545 nsresult
00546 nsHTMLImageElement::SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00547                             nsIAtom* aPrefix, const nsAString& aValue,
00548                             PRBool aNotify)
00549 {
00550   // If we plan to call ImageURIChanged, we want to do it first so that the
00551   // image load kicks off _before_ the reflow triggered by the SetAttr.  But if
00552   // aNotify is false, we are coming from the parser or some such place; we'll
00553   // get bound after all the attributes have been set, so we'll do the
00554   // image load from BindToTree.  Skip the ImageURIChanged call in that case.
00555   if (aNotify &&
00556       aNameSpaceID == kNameSpaceID_None && aName == nsHTMLAtoms::src) {
00557 
00558     // If caller is not chrome and dom.disable_image_src_set is true,
00559     // prevent setting image.src by exiting early
00560     if (nsContentUtils::GetBoolPref("dom.disable_image_src_set") &&
00561         !nsContentUtils::IsCallerChrome()) {
00562       return NS_OK;
00563     }
00564 
00565     nsCOMPtr<imgIRequest> oldCurrentRequest = mCurrentRequest;
00566 
00567     // Force image loading here, so that we'll try to load the image from
00568     // network if it's set to be not cacheable...
00569     ImageURIChanged(aValue, PR_TRUE);
00570 
00571     if (mCurrentRequest && !mPendingRequest &&
00572         oldCurrentRequest != mCurrentRequest) {
00573       // We have a current request, and it's not the same one as we used
00574       // to have, and we have no pending request.  So imglib already had
00575       // that image.  Reset the animation on it -- see bug 210001
00576       nsCOMPtr<imgIContainer> container;
00577       mCurrentRequest->GetImage(getter_AddRefs(container));
00578       if (container) {
00579         container->ResetAnimation();
00580       }
00581     }
00582   }
00583     
00584   return nsGenericHTMLElement::SetAttr(aNameSpaceID, aName, aPrefix, aValue,
00585                                        aNotify);
00586 }
00587 
00588 nsresult
00589 nsHTMLImageElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
00590                                nsIContent* aBindingParent,
00591                                PRBool aCompileEventHandlers)
00592 {
00593   nsresult rv = nsGenericHTMLElement::BindToTree(aDocument, aParent,
00594                                                  aBindingParent,
00595                                                  aCompileEventHandlers);
00596   NS_ENSURE_SUCCESS(rv, rv);
00597 
00598   // Our base URI may have changed; claim that our URI changed, and the
00599   // nsImageLoadingContent will decide whether a new image load is warranted.
00600   nsAutoString uri;
00601   nsresult result = GetAttr(kNameSpaceID_None, nsHTMLAtoms::src, uri);
00602   if (result == NS_CONTENT_ATTR_HAS_VALUE) {
00603     ImageURIChanged(uri, PR_FALSE);
00604   }
00605 
00606   return rv;
00607 }
00608 
00609 NS_IMETHODIMP
00610 nsHTMLImageElement::Initialize(JSContext* aContext, JSObject *aObj,
00611                                PRUint32 argc, jsval *argv)
00612 {
00613   if (argc <= 0) {
00614     // Nothing to do here if we don't get any arguments.
00615 
00616     return NS_OK;
00617   }
00618 
00619   // The first (optional) argument is the width of the image
00620   int32 width;
00621   JSBool ret = JS_ValueToInt32(aContext, argv[0], &width);
00622   NS_ENSURE_TRUE(ret, NS_ERROR_INVALID_ARG);
00623 
00624   nsresult rv = SetIntAttr(nsHTMLAtoms::width, NS_STATIC_CAST(PRInt32, width));
00625 
00626   if (NS_SUCCEEDED(rv) && (argc > 1)) {
00627     // The second (optional) argument is the height of the image
00628     int32 height;
00629     ret = JS_ValueToInt32(aContext, argv[1], &height);
00630     NS_ENSURE_TRUE(ret, NS_ERROR_INVALID_ARG);
00631 
00632     rv = SetIntAttr(nsHTMLAtoms::height, NS_STATIC_CAST(PRInt32, height));
00633   }
00634 
00635   return rv;
00636 }
00637 
00638 NS_IMETHODIMP
00639 nsHTMLImageElement::GetNaturalHeight(PRInt32* aNaturalHeight)
00640 {
00641   NS_ENSURE_ARG_POINTER(aNaturalHeight);
00642 
00643   *aNaturalHeight = 0;
00644 
00645   if (!mCurrentRequest) {
00646     return NS_OK;
00647   }
00648   
00649   nsCOMPtr<imgIContainer> image;
00650   mCurrentRequest->GetImage(getter_AddRefs(image));
00651   if (!image) {
00652     return NS_OK;
00653   }
00654 
00655   image->GetHeight(aNaturalHeight);
00656   return NS_OK;
00657 }
00658 
00659 NS_IMETHODIMP
00660 nsHTMLImageElement::GetNaturalWidth(PRInt32* aNaturalWidth)
00661 {
00662   NS_ENSURE_ARG_POINTER(aNaturalWidth);
00663 
00664   *aNaturalWidth = 0;
00665 
00666   if (!mCurrentRequest) {
00667     return NS_OK;
00668   }
00669   
00670   nsCOMPtr<imgIContainer> image;
00671   mCurrentRequest->GetImage(getter_AddRefs(image));
00672   if (!image) {
00673     return NS_OK;
00674   }
00675 
00676   image->GetWidth(aNaturalWidth);
00677   return NS_OK;
00678 }
00679 
00680