Bug 828261: Implement 'window.location.origin'. r=bholley, sr=sicking

This commit is contained in:
Mike West 2013-01-09 17:37:25 +01:00
Родитель 8d0e59d7f2
Коммит e4b50cc11c
7 изменённых файлов: 131 добавлений и 1 удалений

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

@ -580,6 +580,26 @@ nsLocation::SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
return result;
}
NS_IMETHODIMP
nsLocation::GetOrigin(nsAString& aOrigin)
{
if (!CallerSubsumes())
return NS_ERROR_DOM_SECURITY_ERR;
aOrigin.Truncate();
nsCOMPtr<nsIURI> uri;
nsresult rv = GetURI(getter_AddRefs(uri), true);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString origin;
rv = nsContentUtils::GetUTFOrigin(uri, origin);
NS_ENSURE_SUCCESS(rv, rv);
aOrigin = origin;
return NS_OK;
}
NS_IMETHODIMP
nsLocation::GetPathname(nsAString& aPathname)
{

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

@ -5,7 +5,7 @@
#include "domstubs.idl"
[scriptable, uuid(9472bf0f-2d1c-415c-90fd-f4260678b73b)]
[scriptable, uuid(79de76e5-994e-4f6b-81aa-42d9adb6e67e)]
interface nsIDOMLocation : nsISupports
{
/**
@ -22,6 +22,8 @@ interface nsIDOMLocation : nsISupports
attribute DOMString protocol;
attribute DOMString search;
readonly attribute DOMString origin;
void reload([optional] in boolean forceget);
void replace(in DOMString url);
void assign(in DOMString url);

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

@ -23,6 +23,10 @@ MOCHITEST_FILES = \
test_innerWidthHeight_script.html \
innerWidthHeight_script.html \
test_location_setters.html \
test_location_getters.html \
test_location_framed.html \
test_location_sandboxed.html \
framed_location.html \
$(NULL)
include $(topsrcdir)/config/rules.mk

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

@ -0,0 +1,11 @@
<script>
window.parent.postMessage({
"hash": window.location.hash,
"host": window.location.host,
"hostname": window.location.hostname,
"origin": window.location.origin,
"pathname": window.location.pathname,
"port": window.location.port,
"protocol": window.location.protocol,
}, "*");
</script>

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

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>MessageEvent tests</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<pre id="test">
<script>
SimpleTest.waitForExplicitFinish();
window.addEventListener('message', function (e) {
console.log(e);
console.dir(e);
var loc = e.data;
is(loc.hash, '', 'Unexpected hash.');
is(loc.host, 'mochi.test:8888', 'Unexpected host.');
is(loc.hostname, 'mochi.test', 'Unexpected hostname.');
is(loc.origin, 'http://mochi.test:8888', 'Unexpected origin.');
is(loc.pathname, '/tests/dom/tests/mochitest/dom-level0/framed_location.html', 'Unexpected pathname.');
is(loc.port, '8888', 'Unexpected port.');
is(loc.protocol, 'http:', 'Unexpected protocol.');
SimpleTest.finish();
});
</script>
<iframe src="framed_location.html"></iframe>
</pre>
</body>
</html>

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

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>MessageEvent tests</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="test()">
<p id="display"></p>
<pre id="test">
<script>
SimpleTest.waitForExplicitFinish();
function test()
{
var loc = window.location;
is(loc.hash, '', 'Unexpected hash.');
is(loc.host, 'mochi.test:8888', 'Unexpected host.');
is(loc.hostname, 'mochi.test', 'Unexpected hostname.');
is(loc.origin, 'http://mochi.test:8888', 'Unexpected origin.');
is(loc.pathname, '/tests/dom/tests/mochitest/dom-level0/test_location_getters.html', 'Unexpected pathname.');
is(loc.port, '8888', 'Unexpected port.');
is(loc.protocol, 'http:', 'Unexpected protocol.');
SimpleTest.finish();
}
</script>
</pre>
</body>
</html>

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

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>MessageEvent tests</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<pre id="test">
<script>
SimpleTest.waitForExplicitFinish();
window.addEventListener('message', function (e) {
console.log(e);
console.dir(e);
var loc = e.data;
is(loc.hash, '', 'Unexpected hash.');
is(loc.host, 'mochi.test:8888', 'Unexpected host.');
is(loc.hostname, 'mochi.test', 'Unexpected hostname.');
// Is this correct? It matches WebKit, but it seems wrong:
// https://bugs.webkit.org/show_bug.cgi?id=106488
is(loc.origin, 'http://mochi.test:8888', 'Unexpected origin.');
is(loc.pathname, '/tests/dom/tests/mochitest/dom-level0/framed_location.html', 'Unexpected pathname.');
is(loc.port, '8888', 'Unexpected port.');
is(loc.protocol, 'http:', 'Unexpected protocol.');
SimpleTest.finish();
});
</script>
<iframe sandbox="allow-scripts" src="framed_location.html"></iframe>
</pre>
</body>
</html>