Fixing bug 113498. Making .src on a new Image(); always be absolute. r=sicking@bigfoot.com, rs=rpotts@netscape.com.

This commit is contained in:
jst%netscape.com 2002-02-02 00:17:40 +00:00
Родитель 88244c2a75
Коммит d693e68e36
2 изменённых файлов: 69 добавлений и 90 удалений

Просмотреть файл

@ -2180,7 +2180,14 @@ nsGenericHTMLElement::GetBaseURL(nsIURI*& aBaseURL) const
if (mAttributes) { if (mAttributes) {
mAttributes->GetAttribute(nsHTMLAtoms::_baseHref, baseHref); mAttributes->GetAttribute(nsHTMLAtoms::_baseHref, baseHref);
} }
return GetBaseURL(baseHref, mDocument, &aBaseURL);
nsCOMPtr<nsIDocument> doc(mDocument);
if (!doc) {
mNodeInfo->GetDocument(*getter_AddRefs(doc));
}
return GetBaseURL(baseHref, doc, &aBaseURL);
} }
nsresult nsresult
@ -2191,9 +2198,11 @@ nsGenericHTMLElement::GetBaseURL(const nsHTMLValue& aBaseHref,
nsresult result = NS_OK; nsresult result = NS_OK;
nsIURI* docBaseURL = nsnull; nsIURI* docBaseURL = nsnull;
if (nsnull != aDocument) {
if (aDocument) {
result = aDocument->GetBaseURL(docBaseURL); result = aDocument->GetBaseURL(docBaseURL);
} }
*aBaseURL = docBaseURL; *aBaseURL = docBaseURL;
if (eHTMLUnit_String == aBaseHref.GetUnit()) { if (eHTMLUnit_String == aBaseHref.GetUnit()) {

Просмотреть файл

@ -118,8 +118,6 @@ public:
PRUint32 argc, jsval *argv); PRUint32 argc, jsval *argv);
// nsIContent // nsIContent
NS_IMETHOD SetDocument(nsIDocument* aDocument, PRBool aDeep,
PRBool aCompileEventHandlers);
NS_IMETHOD StringToAttribute(nsIAtom* aAttribute, NS_IMETHOD StringToAttribute(nsIAtom* aAttribute,
const nsAReadableString& aValue, const nsAReadableString& aValue,
nsHTMLValue& aResult); nsHTMLValue& aResult);
@ -145,9 +143,6 @@ protected:
nsresult GetXY(PRInt32* aX, PRInt32* aY); nsresult GetXY(PRInt32* aX, PRInt32* aY);
nsresult GetWidthHeight(PRInt32* aWidth, PRInt32* aHeight); nsresult GetWidthHeight(PRInt32* aWidth, PRInt32* aHeight);
// Only used if this is a script constructed image
nsIDocument* mOwnerDocument;
nsCOMPtr<imgIRequest> mRequest; nsCOMPtr<imgIRequest> mRequest;
}; };
@ -164,10 +159,34 @@ NS_NewHTMLImageElement(nsIHTMLContent** aInstancePtrResult,
*/ */
nsCOMPtr<nsINodeInfo> nodeInfo(aNodeInfo); nsCOMPtr<nsINodeInfo> nodeInfo(aNodeInfo);
if (!nodeInfo) { if (!nodeInfo) {
nsCOMPtr<nsIJSContextStack> stack =
do_GetService("@mozilla.org/js/xpc/ContextStack;1");
NS_ENSURE_TRUE(stack, NS_ERROR_NOT_AVAILABLE);
JSContext *cx = nsnull;
nsresult rv = stack->Peek(&cx);
if (NS_FAILED(rv) || !cx)
return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIScriptGlobalObject> globalObject;
nsContentUtils::GetStaticScriptGlobal(cx, ::JS_GetGlobalObject(cx),
getter_AddRefs(globalObject));;
nsCOMPtr<nsIDOMWindowInternal> win(do_QueryInterface(globalObject));
NS_ENSURE_TRUE(win, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIDOMDocument> dom_doc;
win->GetDocument(getter_AddRefs(dom_doc));
nsCOMPtr<nsIDocument> doc(do_QueryInterface(dom_doc));
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsINodeInfoManager> nodeInfoManager; nsCOMPtr<nsINodeInfoManager> nodeInfoManager;
nsresult rv; doc->GetNodeInfoManager(*getter_AddRefs(nodeInfoManager));
rv = nsNodeInfoManager::GetAnonymousManager(*getter_AddRefs(nodeInfoManager)); NS_ENSURE_TRUE(nodeInfoManager, NS_ERROR_UNEXPECTED);
NS_ENSURE_SUCCESS(rv, rv);
rv = nodeInfoManager->GetNodeInfo(nsHTMLAtoms::img, nsnull, rv = nodeInfoManager->GetNodeInfo(nsHTMLAtoms::img, nsnull,
kNameSpaceID_None, kNameSpaceID_None,
@ -198,17 +217,10 @@ NS_NewHTMLImageElement(nsIHTMLContent** aInstancePtrResult,
nsHTMLImageElement::nsHTMLImageElement() nsHTMLImageElement::nsHTMLImageElement()
{ {
mOwnerDocument = nsnull;
} }
nsHTMLImageElement::~nsHTMLImageElement() nsHTMLImageElement::~nsHTMLImageElement()
{ {
NS_IF_RELEASE(mOwnerDocument);
#ifndef USE_IMG2
if (mLoader)
mLoader->RemoveFrame(this);
#endif
} }
@ -721,73 +733,33 @@ NS_IMETHODIMP
nsHTMLImageElement::Initialize(JSContext* aContext, JSObject *aObj, nsHTMLImageElement::Initialize(JSContext* aContext, JSObject *aObj,
PRUint32 argc, jsval *argv) PRUint32 argc, jsval *argv)
{ {
nsresult result = NS_OK; if (argc <= 0) {
// Nothing to do here if we don't get any arguments.
// XXX This element is created unattached to any document. Later return NS_OK;
// on, it might be used to preload the image cache. For that, we
// need a document (actually a pres context). The only way to get
// one is to associate the image with one at creation time.
// This is safer than it used to be since we get the global object
// from the static scope chain rather than through the JSCOntext.
nsCOMPtr<nsIScriptGlobalObject> globalObject;
nsContentUtils::GetStaticScriptGlobal(aContext, aObj,
getter_AddRefs(globalObject));;
nsCOMPtr<nsIDOMWindowInternal> domWindow(do_QueryInterface(globalObject));
if (domWindow) {
nsCOMPtr<nsIDOMDocument> domDocument;
result = domWindow->GetDocument(getter_AddRefs(domDocument));
if (NS_SUCCEEDED(result)) {
// Maintain the reference
result = CallQueryInterface(domDocument, &mOwnerDocument);
}
} }
if (NS_SUCCEEDED(result) && (argc > 0)) { // The first (optional) argument is the width of the image
// The first (optional) argument is the width of the image int32 width;
int32 width; JSBool ret = JS_ValueToInt32(aContext, argv[0], &width);
JSBool ret = JS_ValueToInt32(aContext, argv[0], &width); NS_ENSURE_TRUE(ret, NS_ERROR_INVALID_ARG);
if (ret) { nsHTMLValue widthVal((PRInt32)width, eHTMLUnit_Integer);
nsHTMLValue widthVal((PRInt32)width, eHTMLUnit_Integer);
result = SetHTMLAttribute(nsHTMLAtoms::width, widthVal, PR_FALSE); nsresult rv = SetHTMLAttribute(nsHTMLAtoms::width, widthVal, PR_FALSE);
if (NS_SUCCEEDED(result) && (argc > 1)) { if (NS_SUCCEEDED(rv) && (argc > 1)) {
// The second (optional) argument is the height of the image // The second (optional) argument is the height of the image
int32 height; int32 height;
ret = JS_ValueToInt32(aContext, argv[1], &height); ret = JS_ValueToInt32(aContext, argv[1], &height);
NS_ENSURE_TRUE(ret, NS_ERROR_INVALID_ARG);
if (ret) { nsHTMLValue heightVal((PRInt32)height, eHTMLUnit_Integer);
nsHTMLValue heightVal((PRInt32)height, eHTMLUnit_Integer);
result = SetHTMLAttribute(nsHTMLAtoms::height, heightVal, PR_FALSE); rv = SetHTMLAttribute(nsHTMLAtoms::height, heightVal, PR_FALSE);
}
else {
result = NS_ERROR_INVALID_ARG;
}
}
}
else {
result = NS_ERROR_INVALID_ARG;
}
} }
return result; return rv;
}
NS_IMETHODIMP
nsHTMLImageElement::SetDocument(nsIDocument* aDocument,
PRBool aDeep, PRBool aCompileEventHandlers)
{
// If we've been added to the document, we can get rid of
// our owner document reference so as to avoid a circular
// reference.
NS_IF_RELEASE(mOwnerDocument);
return nsGenericHTMLLeafElement::SetDocument(aDocument, aDeep,
aCompileEventHandlers);
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -917,10 +889,13 @@ nsHTMLImageElement::SetSrcInner(nsIURI* aBaseURL,
nsresult result = SetAttr(kNameSpaceID_HTML, nsHTMLAtoms::src, aSrc, nsresult result = SetAttr(kNameSpaceID_HTML, nsHTMLAtoms::src, aSrc,
PR_TRUE); PR_TRUE);
if (NS_SUCCEEDED(result) && mOwnerDocument) { if (NS_SUCCEEDED(result) && !mDocument) {
nsCOMPtr<nsIDocument> doc;
mNodeInfo->GetDocument(*getter_AddRefs(doc));
nsCOMPtr<nsIPresShell> shell; nsCOMPtr<nsIPresShell> shell;
mOwnerDocument->GetShellAt(0, getter_AddRefs(shell)); doc->GetShellAt(0, getter_AddRefs(shell));
if (shell) { if (shell) {
nsCOMPtr<nsIPresContext> context; nsCOMPtr<nsIPresContext> context;
@ -1014,10 +989,6 @@ nsHTMLImageElement::SetSrcInner(nsIURI* aBaseURL,
nsnull, nsnull, getter_AddRefs(mRequest)); nsnull, nsnull, getter_AddRefs(mRequest));
} }
} }
// Only do this the first time since it's only there for
// backwards compatability
NS_RELEASE(mOwnerDocument);
} }
return result; return result;
@ -1027,23 +998,22 @@ NS_IMETHODIMP
nsHTMLImageElement::SetSrc(const nsAReadableString& aSrc) nsHTMLImageElement::SetSrc(const nsAReadableString& aSrc)
{ {
nsCOMPtr<nsIURI> baseURL; nsCOMPtr<nsIURI> baseURL;
nsresult result = NS_OK; nsresult rv = NS_OK;
(void) GetCallerSourceURL(getter_AddRefs(baseURL)); (void) GetCallerSourceURL(getter_AddRefs(baseURL));
if (mDocument && !baseURL) { nsCOMPtr<nsIDocument> doc;
result = mDocument->GetBaseURL(*getter_AddRefs(baseURL)); mNodeInfo->GetDocument(*getter_AddRefs(doc));
if (doc && !baseURL) {
rv = doc->GetBaseURL(*getter_AddRefs(baseURL));
} }
if (mOwnerDocument && !baseURL) { if (NS_SUCCEEDED(rv)) {
result = mOwnerDocument->GetBaseURL(*getter_AddRefs(baseURL)); rv = SetSrcInner(baseURL, aSrc);
} }
if (NS_SUCCEEDED(result)) { return rv;
result = SetSrcInner(baseURL, aSrc);
}
return result;
} }
#ifdef DEBUG #ifdef DEBUG