Add a method on the fixup object to strip off wyciwyg from nsIURIs

Fixes bug 164006, wyciwyg: should not appear in tab title
r=jag sr=bzbarsky
This commit is contained in:
caillon%returnzero.com 2002-09-11 01:44:13 +00:00
parent b394f6d78c
commit a5cd43f732
9 changed files with 147 additions and 57 deletions

View File

@ -46,6 +46,52 @@ nsDefaultURIFixup::~nsDefaultURIFixup()
/* destructor code */
}
/* nsIURI createExposableURI (in nsIRUI aURI); */
NS_IMETHODIMP
nsDefaultURIFixup::CreateExposableURI(nsIURI *aURI, nsIURI **aReturn)
{
NS_ENSURE_ARG_POINTER(aURI);
NS_ENSURE_ARG_POINTER(aReturn);
PRBool isWyciwyg = PR_FALSE;
aURI->SchemeIs("wyciwyg", &isWyciwyg);
if (!isWyciwyg)
{
*aReturn = aURI;
NS_ADDREF(*aReturn);
return NS_OK;
}
nsCAutoString path;
nsresult rv = aURI->GetPath(path);
NS_ENSURE_SUCCESS(rv, rv);
PRUint32 pathLength = path.Length();
if (pathLength <= 2)
{
return NS_ERROR_FAILURE;
}
// Path is of the form "//123/http://foo/bar", with a variable number of digits.
// To figure out where the "real" URL starts, search path for a '/', starting at
// the third character.
PRInt32 slashIndex = path.FindChar('/', 2);
if (slashIndex == kNotFound)
{
return NS_ERROR_FAILURE;
}
// Get the charset of the original URI so we can pass it to our fixed up URI.
nsCAutoString charset;
aURI->GetOriginCharset(charset);
rv = NS_NewURI(aReturn,
Substring(path, slashIndex + 1, pathLength - slashIndex - 1),
charset.get());
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
/* nsIURI createFixupURI (in wstring aURIText, in unsigned long aFixupFlags); */
NS_IMETHODIMP
@ -69,6 +115,8 @@ nsDefaultURIFixup::CreateFixupURI(const nsAString& aStringURI, PRUint32 aFixupFl
// Eliminate embedded newlines, which single-line text fields now allow:
uriString.StripChars("\r\n");
NS_ENSURE_TRUE(!uriString.IsEmpty(), NS_ERROR_FAILURE);
// View-source is a pseudo scheme. We're interested in fixing up the stuff
// after it. The easiest way to do that is to call this method again with the
// "view-source:" lopped off and then prepend it again afterwards.

View File

@ -2344,8 +2344,28 @@ nsDocShell::LoadURI(const PRUnichar * aURI,
nsIInputStream * aHeaderStream)
{
nsCOMPtr<nsIURI> uri;
nsresult rv;
// Create the fixup object if necessary
if (!mURIFixup) {
mURIFixup = do_GetService(NS_URIFIXUP_CONTRACTID);
if (!mURIFixup) {
// No fixup service so try and create a URI and see what happens
nsAutoString uriString(aURI);
// Cleanup the empty spaces that might be on each end.
uriString.Trim(" ");
// Eliminate embedded newlines, which single-line text fields now allow:
uriString.StripChars("\r\n");
NS_ENSURE_TRUE(!uriString.IsEmpty(), NS_ERROR_FAILURE);
nsresult rv = CreateFixupURI(nsDependentString(aURI), getter_AddRefs(uri));
rv = NS_NewURI(getter_AddRefs(uri), uriString);
}
}
if (mURIFixup) {
// Call the fixup object
rv = mURIFixup->CreateFixupURI(nsDependentString(aURI),
nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP,
getter_AddRefs(uri));
}
if (NS_ERROR_UNKNOWN_PROTOCOL == rv ||
NS_ERROR_MALFORMED_URI == rv) {
@ -4914,32 +4934,6 @@ nsDocShell::InternalLoad(nsIURI * aURI,
return rv;
}
NS_IMETHODIMP
nsDocShell::CreateFixupURI(const nsAString& aStringURI, nsIURI ** aURI)
{
*aURI = nsnull;
nsAutoString uriString(aStringURI);
uriString.Trim(" "); // Cleanup the empty spaces that might be on each end.
// Eliminate embedded newlines, which single-line text fields now allow:
uriString.StripChars("\r\n");
NS_ENSURE_TRUE(uriString.Length() > 0, NS_ERROR_FAILURE);
// Create the fixup object if necessary
if (!mURIFixup) {
mURIFixup = do_GetService(NS_URIFIXUP_CONTRACTID);
if (!mURIFixup) {
// No fixup service so try and create a URI and see what happens
return NS_NewURI(aURI, uriString);
}
}
// Call the fixup object
return mURIFixup->CreateFixupURI(aStringURI,
nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP, aURI);
}
NS_IMETHODIMP
nsDocShell::GetCurrentDocumentOwner(nsISupports ** aOwner)
{

View File

@ -200,7 +200,6 @@ protected:
NS_IMETHOD SetupNewViewer(nsIContentViewer * aNewViewer);
NS_IMETHOD GetEldestPresContext(nsIPresContext** aPresContext);
NS_IMETHOD CreateFixupURI(const nsAString& aStringURI, nsIURI ** aURI);
NS_IMETHOD GetCurrentDocumentOwner(nsISupports ** aOwner);
virtual nsresult DoURILoad(nsIURI * aURI,
nsIURI * aReferrer,

View File

@ -46,6 +46,18 @@ interface nsIURIFixup : nsISupports
*/
const unsigned long FIXUP_FLAGS_MAKE_ALTERNATE_URI = 2;
/**
* Converts an internal URI (e.g. a wyciwyg URI) into one which we can
* expose to the user, for example on the URL bar.
*
* @param aURI The URI to be converted
* @return nsIURI The converted, exposable URI
* @throws NS_ERROR_MALFORMED_URI when the exposable portion of aURI is malformed
* @throws NS_ERROR_UNKNOWN_PROTOCOL when we can't get a protocol handler service
* for the URI scheme.
*/
nsIURI createExposableURI(in nsIURI aURI);
/**
* Converts the specified string into a URI, first attempting
* to correct any errors in the syntax or other vagaries. Returns

View File

@ -2485,12 +2485,16 @@ nsHttpChannel::SetReferrer(nsIURI *referrerIn, PRUint32 referrerType)
// To figure out where the "real" URL starts, search path for a '/', starting at
// the third character.
PRInt32 slashIndex = path.FindChar('/', 2);
if (slashIndex < 0) return NS_ERROR_FAILURE;
if (slashIndex == kNotFound) return NS_ERROR_FAILURE;
// Get the charset of the original URI so we can pass it to our fixed up URI.
nsCAutoString charset;
referrer->GetOriginCharset(charset);
// Replace |referrer| with a URI without wyciwyg://123/.
nsCAutoString newReferrer;
path.Right(newReferrer, pathLength - slashIndex - 1);
rv = NS_NewURI(getter_AddRefs(referrer), newReferrer.get());
rv = NS_NewURI(getter_AddRefs(referrer),
Substring(path, slashIndex + 1, pathLength - slashIndex - 1),
charset.get());
if (NS_FAILED(rv)) return rv;
}

View File

@ -55,6 +55,7 @@ var gHaveUpdatedToolbarState = false;
var gClickSelectsAll = false;
var gIgnoreFocus = false;
var gIgnoreClick = false;
var gURIFixup = null;
var pref = null;
@ -1163,12 +1164,19 @@ function BrowserLoadURL(aTriggeringEvent)
}
}
else if (saveModifier) {
// Firstly, fixup the url so that (e.g.) "www.foo.com" works
const nsIURIFixup = Components.interfaces.nsIURIFixup;
var uriFixup = Components.classes["@mozilla.org/docshell/urifixup;1"].getService(nsIURIFixup);
url = uriFixup.createFixupURI(url, nsIURIFixup.FIXUP_FLAGS_MAKE_ALTERNATE_URI).spec;
// Open filepicker to save the url
saveURL(url, "");
try {
// Firstly, fixup the url so that (e.g.) "www.foo.com" works
if (!gURIFixup)
gURIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
.getService(Components.interfaces.nsIURIFixup);
url = gURIFixup.createFixupURI(url, nsIURIFixup.FIXUP_FLAGS_MAKE_ALTERNATE_URI).spec;
// Open filepicker to save the url
saveURL(url, "");
}
catch(ex) {
// XXX Do nothing for now.
// Do we want to put up an alert in the future? Mmm, l10n...
}
}
else {
// No modifier was pressed, load the URL normally and

View File

@ -311,7 +311,22 @@ nsBrowserStatusHandler.prototype =
{
this.setOverLink("", null);
var location = aLocation.spec;
var location = "";
if (aLocation) {
try {
if (!gURIFixup)
gURIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
.getService(Components.interfaces.nsIURIFixup);
// If the url has "wyciwyg://" as the protocol, strip it off.
// Nobody wants to see it on the urlbar for dynamically generated
// pages.
location = gURIFixup.createExposableURI(aLocation).spec;
}
catch(ex) {
location = aLocation.spec;
}
}
if (this.hideAboutBlank) {
this.hideAboutBlank = false;
@ -332,11 +347,6 @@ nsBrowserStatusHandler.prototype =
if (aWebProgress.DOMWindow == content) {
if (!this.userTyped.value) {
// If the url has "wyciwyg://" as the protocol, strip it off.
// Nobody wants to see it on the urlbar for dynamically generated
// pages.
if (/^\s*wyciwyg:\/\/\d+\//.test(location))
location = RegExp.rightContext;
this.urlBar.value = location;
// the above causes userTyped.value to become true, reset it
this.userTyped.value = false;

View File

@ -228,6 +228,10 @@
onget="return this.webNavigation.document;"
readonly="true"/>
<property name="contentTitle"
onget="return Components.lookupMethod(this.contentDocument, 'title').call(this.contentDocument);"
readonly="true"/>
<field name="mPrefs" readonly="true">
Components.classes['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefService)

View File

@ -109,6 +109,10 @@
.getService(Components.interfaces.nsIPrefService)
.getBranch(null);
</field>
<field name="mURIFixup" readonly="true">
Components.classes["@mozilla.org/docshell/urifixup;1"]
.getService(Components.interfaces.nsIURIFixup);
</field>
<field name="mTabBox">
document.getAnonymousNodes(this)[1]
</field>
@ -351,7 +355,7 @@
var newTitle = "";
var docTitle;
if (this.docShell.contentViewer)
docTitle = this.contentDocument.title;
docTitle = this.contentTitle;
if (docTitle) {
newTitle += this.ownerDocument.documentElement.getAttribute("titlepreface");
@ -390,9 +394,6 @@
// Update the URL bar.
var loc = this.mCurrentBrowser.currentURI;
if (!loc)
loc = ({ spec: "" });
var webProgress = this.mCurrentBrowser.webProgress;
var securityUI = this.mCurrentBrowser.securityUI;
var i, p;
@ -545,18 +546,24 @@
<body>
<![CDATA[
var browser = this.getBrowserForTab(aTab);
var title = browser.contentTitle;
var crop = "end";
var titleViaGetter = browser.contentDocument.__proto__.__lookupGetter__('title').call(browser.contentDocument);
var title;
if (titleViaGetter)
title = titleViaGetter
else if (browser.currentURI.spec && browser.currentURI.spec != "about:blank") {
title = browser.currentURI.spec;
crop = "center";
if (!title) {
if (browser.currentURI.spec) {
try {
title = this.mURIFixup.createExposableURI(browser.currentURI).spec;
}
catch(ex) {
title = browser.currentURI.spec;
}
}
if (title && title != "about:blank")
crop = "center";
else // Still no title? Fall back to our untitled string.
title = this.mStringBundle.getString("tabs.untitled");
}
else
title = this.mStringBundle.getString("tabs.untitled");
aTab.label = title;
aTab.setAttribute("crop", crop);
@ -1131,6 +1138,10 @@
onget="return this.mCurrentBrowser.contentDocument;"
readonly="true"/>
<property name="contentTitle"
onget="return this.mCurrentBrowser.contentTitle;"
readonly="true"/>
<property name="securityUI"
onget="return this.mCurrentBrowser.securityUI;"
readonly="true"/>