Bug 1018682 - URL API - SetProtocol should serialize and reparse the URL, r=bz

This commit is contained in:
Andrea Marchesini 2014-06-09 17:20:19 +01:00
parent 0c3504655d
commit 2befea780d
4 changed files with 70 additions and 1 deletions

View File

@ -546,6 +546,9 @@ Link::SetHrefAttribute(nsIURI *aURI)
{
NS_ASSERTION(aURI, "Null URI is illegal!");
// if we change this code to not reserialize we need to do something smarter
// in SetProtocol because changing the protocol of an URI can change the
// "nature" of the nsIURL/nsIURI implementation.
nsAutoCString href;
(void)aURI->GetSpec(href);
(void)mElement->SetAttr(kNameSpaceID_None, nsGkAtoms::href,

View File

@ -15,6 +15,7 @@
#include "nsIIOService.h"
#include "nsEscape.h"
#include "nsNetCID.h"
#include "nsNetUtil.h"
#include "nsIURL.h"
namespace mozilla {
@ -262,7 +263,34 @@ URL::SetProtocol(const nsAString& aProtocol)
nsAString::const_iterator iter(start);
FindCharInReadable(':', iter, end);
mURI->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
// Changing the protocol of a URL, changes the "nature" of the URI
// implementation. In order to do this properly, we have to serialize the
// existing URL and reparse it in a new object.
nsCOMPtr<nsIURI> clone;
nsresult rv = mURI->Clone(getter_AddRefs(clone));
if (NS_WARN_IF(NS_FAILED(rv)) || !clone) {
return;
}
rv = clone->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
nsAutoCString href;
rv = clone->GetSpec(href);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), href);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
mURI = uri;
}
#define URL_GETTER( value, func ) \

View File

@ -60,6 +60,7 @@ skip-if = (buildapp == 'b2g' && toolkit != 'gonk') # Fails on b2g-desktop, track
[test_setting_opener.html]
skip-if = (buildapp == 'b2g' && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage
[test_url.html]
[test_url_data.html]
[test_url_empty_port.html]
[test_urlExceptions.html]
[test_urlSearchParams.html]

View File

@ -0,0 +1,37 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test URL API - data:plain</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1018682">Mozilla Bug 1018682</a>
<script type="application/javascript">
var base = new URL("data:text/plain,");
base.protocol = "chrome:";
is(base.protocol, 'data:', "The protocol should not change from data to chrome.");
try {
var relative = new URL("a", base);
ok(false, "Relative URL from a data:text/plain should not work.");
} catch(e) {
ok(true, "Relative URL from a data:text/plain should not work.");
}
base.protocol = "http:";
ok(true, "Protocol: http changed");
is(base.href, "http://text/plain,", "Base URL is correct");
var relative = new URL("a", base);
ok(relative, "This works.");
is(relative.href, "http://text/a", "Relative URL is correct");
</script>
</body>
</html>