From fa3e3d8df96d7c4eacbe162fd14f7263d96a7d04 Mon Sep 17 00:00:00 2001 From: "heikki%netscape.com" Date: Sun, 19 Jan 2003 01:08:05 +0000 Subject: [PATCH] Bug 1882, get ref from usemap even when it contains full URL. r+sr=roc+moz. git-svn-id: svn://10.0.0.236/trunk@136614 18797224-902f-48f8-a5cc-f745e15eee43 --- .../html/content/src/nsImageMapUtils.cpp | 45 ++++++++++++++----- .../html/document/src/nsHTMLDocument.cpp | 2 +- .../html/document/src/nsHTMLDocument.h | 2 +- .../html/document/src/nsIHTMLDocument.h | 2 +- .../content/shared/src/nsImageMapUtils.cpp | 45 ++++++++++++++----- 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/mozilla/content/html/content/src/nsImageMapUtils.cpp b/mozilla/content/html/content/src/nsImageMapUtils.cpp index c73efc437a6..6d46747c5c5 100644 --- a/mozilla/content/html/content/src/nsImageMapUtils.cpp +++ b/mozilla/content/html/content/src/nsImageMapUtils.cpp @@ -55,24 +55,42 @@ nsresult nsImageMapUtils::FindImageMap(nsIDocument *aDocument, NS_ENSURE_ARG_POINTER(aMap); *aMap = nsnull; - nsresult rv = NS_OK; - - nsAutoString usemap(aUsemap); - /* we used to strip the whitespace from the usemap value as a Quirk, but it was too quirky and didn't really work correctly - see bug 87050 */ - if (!usemap.IsEmpty() && usemap.First() == '#') { - usemap.Cut(0, 1); + if (aUsemap.IsEmpty()) + return NS_OK; + + PRInt32 hash = aUsemap.FindChar('#'); + nsAString::const_iterator start, end; + if (hash > -1) { + aUsemap.BeginReading(start); + aUsemap.EndReading(end); + start.advance(hash + 1); + if (start == end) + return NS_OK; // aUsemap == "#" } - if (usemap.IsEmpty()) - return rv; + // At this point, if hash < 0, then there was no '#' + // so aUsemap IS the ref. Otherwise the ref starts + // from 'start' + + // NOTE! + // I'd really like to do this, but it won't compile, and casting + // Substring return to (const nsAString&) will cause a runtime crash. + // Therefore, to avoid doing needless work, this pattern is replicated + // below where we actually call functions with 'usemap' values. + //const nsAString &usemap = (hash < 0) ? aUsemap : Substring(start, end); nsCOMPtr htmlDoc(do_QueryInterface(aDocument)); if (htmlDoc) { - htmlDoc->GetImageMap(usemap,aMap); + // See above NOTE + if (hash < 0) { + htmlDoc->GetImageMap(aUsemap, aMap); + } else { + htmlDoc->GetImageMap(Substring(start, end), aMap); + } } else { // XHTML requires that where a name attribute was used in HTML 4.01, // the ID attribute must be used in XHTML. name is officially deprecated. @@ -81,12 +99,17 @@ nsresult nsImageMapUtils::FindImageMap(nsIDocument *aDocument, nsCOMPtr domDoc(do_QueryInterface(aDocument)); if (domDoc) { nsCOMPtr element; - domDoc->GetElementById(usemap,getter_AddRefs(element)); + // See above NOTE + if (hash < 0) { + domDoc->GetElementById(aUsemap, getter_AddRefs(element)); + } else { + domDoc->GetElementById(Substring(start, end), getter_AddRefs(element)); + } if (element) { CallQueryInterface(element, aMap); } } } - return rv; + return NS_OK; } diff --git a/mozilla/content/html/document/src/nsHTMLDocument.cpp b/mozilla/content/html/document/src/nsHTMLDocument.cpp index a01267dabdf..c07c1d797da 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/content/html/document/src/nsHTMLDocument.cpp @@ -1144,7 +1144,7 @@ nsHTMLDocument::RemoveImageMap(nsIDOMHTMLMapElement* aMap) } NS_IMETHODIMP -nsHTMLDocument::GetImageMap(const nsString& aMapName, +nsHTMLDocument::GetImageMap(const nsAString& aMapName, nsIDOMHTMLMapElement** aResult) { NS_PRECONDITION(nsnull != aResult, "null ptr"); diff --git a/mozilla/content/html/document/src/nsHTMLDocument.h b/mozilla/content/html/document/src/nsHTMLDocument.h index a43c988fae1..1e79e13eabd 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.h +++ b/mozilla/content/html/document/src/nsHTMLDocument.h @@ -109,7 +109,7 @@ public: NS_IMETHOD RemoveImageMap(nsIDOMHTMLMapElement* aMap); - NS_IMETHOD GetImageMap(const nsString& aMapName, + NS_IMETHOD GetImageMap(const nsAString& aMapName, nsIDOMHTMLMapElement** aResult); NS_IMETHOD GetAttributeStyleSheet(nsIHTMLStyleSheet** aStyleSheet); diff --git a/mozilla/content/html/document/src/nsIHTMLDocument.h b/mozilla/content/html/document/src/nsIHTMLDocument.h index 6f691901958..4591b33b989 100644 --- a/mozilla/content/html/document/src/nsIHTMLDocument.h +++ b/mozilla/content/html/document/src/nsIHTMLDocument.h @@ -67,7 +67,7 @@ public: NS_IMETHOD AddImageMap(nsIDOMHTMLMapElement* aMap) = 0; - NS_IMETHOD GetImageMap(const nsString& aMapName, + NS_IMETHOD GetImageMap(const nsAString& aMapName, nsIDOMHTMLMapElement** aResult) = 0; NS_IMETHOD RemoveImageMap(nsIDOMHTMLMapElement* aMap) = 0; diff --git a/mozilla/content/shared/src/nsImageMapUtils.cpp b/mozilla/content/shared/src/nsImageMapUtils.cpp index c73efc437a6..6d46747c5c5 100644 --- a/mozilla/content/shared/src/nsImageMapUtils.cpp +++ b/mozilla/content/shared/src/nsImageMapUtils.cpp @@ -55,24 +55,42 @@ nsresult nsImageMapUtils::FindImageMap(nsIDocument *aDocument, NS_ENSURE_ARG_POINTER(aMap); *aMap = nsnull; - nsresult rv = NS_OK; - - nsAutoString usemap(aUsemap); - /* we used to strip the whitespace from the usemap value as a Quirk, but it was too quirky and didn't really work correctly - see bug 87050 */ - if (!usemap.IsEmpty() && usemap.First() == '#') { - usemap.Cut(0, 1); + if (aUsemap.IsEmpty()) + return NS_OK; + + PRInt32 hash = aUsemap.FindChar('#'); + nsAString::const_iterator start, end; + if (hash > -1) { + aUsemap.BeginReading(start); + aUsemap.EndReading(end); + start.advance(hash + 1); + if (start == end) + return NS_OK; // aUsemap == "#" } - if (usemap.IsEmpty()) - return rv; + // At this point, if hash < 0, then there was no '#' + // so aUsemap IS the ref. Otherwise the ref starts + // from 'start' + + // NOTE! + // I'd really like to do this, but it won't compile, and casting + // Substring return to (const nsAString&) will cause a runtime crash. + // Therefore, to avoid doing needless work, this pattern is replicated + // below where we actually call functions with 'usemap' values. + //const nsAString &usemap = (hash < 0) ? aUsemap : Substring(start, end); nsCOMPtr htmlDoc(do_QueryInterface(aDocument)); if (htmlDoc) { - htmlDoc->GetImageMap(usemap,aMap); + // See above NOTE + if (hash < 0) { + htmlDoc->GetImageMap(aUsemap, aMap); + } else { + htmlDoc->GetImageMap(Substring(start, end), aMap); + } } else { // XHTML requires that where a name attribute was used in HTML 4.01, // the ID attribute must be used in XHTML. name is officially deprecated. @@ -81,12 +99,17 @@ nsresult nsImageMapUtils::FindImageMap(nsIDocument *aDocument, nsCOMPtr domDoc(do_QueryInterface(aDocument)); if (domDoc) { nsCOMPtr element; - domDoc->GetElementById(usemap,getter_AddRefs(element)); + // See above NOTE + if (hash < 0) { + domDoc->GetElementById(aUsemap, getter_AddRefs(element)); + } else { + domDoc->GetElementById(Substring(start, end), getter_AddRefs(element)); + } if (element) { CallQueryInterface(element, aMap); } } } - return rv; + return NS_OK; }