summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/dom_types.ts70
-rw-r--r--js/globals.ts2
-rw-r--r--js/lib.web_assembly.d.ts5
-rw-r--r--js/location.ts52
-rw-r--r--js/location_test.ts8
-rw-r--r--js/main.ts12
-rw-r--r--js/unit_tests.ts1
7 files changed, 147 insertions, 3 deletions
diff --git a/js/dom_types.ts b/js/dom_types.ts
index 0c6814dae..651eece9a 100644
--- a/js/dom_types.ts
+++ b/js/dom_types.ts
@@ -536,3 +536,73 @@ export interface Response extends Body {
/** Creates a clone of a `Response` object. */
clone(): Response;
}
+
+export interface Location {
+ /**
+ * Returns a DOMStringList object listing the origins of the ancestor browsing
+ * contexts, from the parent browsing context to the top-level browsing
+ * context.
+ */
+ readonly ancestorOrigins: string[];
+ /**
+ * Returns the Location object's URL's fragment (includes leading "#" if
+ * non-empty).
+ * Can be set, to navigate to the same URL with a changed fragment (ignores
+ * leading "#").
+ */
+ hash: string;
+ /**
+ * Returns the Location object's URL's host and port (if different from the
+ * default port for the scheme). Can be set, to navigate to the same URL with
+ * a changed host and port.
+ */
+ host: string;
+ /**
+ * Returns the Location object's URL's host. Can be set, to navigate to the
+ * same URL with a changed host.
+ */
+ hostname: string;
+ /**
+ * Returns the Location object's URL. Can be set, to navigate to the given
+ * URL.
+ */
+ href: string;
+ /** Returns the Location object's URL's origin. */
+ readonly origin: string;
+ /**
+ * Returns the Location object's URL's path.
+ * Can be set, to navigate to the same URL with a changed path.
+ */
+ pathname: string;
+ /**
+ * Returns the Location object's URL's port.
+ * Can be set, to navigate to the same URL with a changed port.
+ */
+ port: string;
+ /**
+ * Returns the Location object's URL's scheme.
+ * Can be set, to navigate to the same URL with a changed scheme.
+ */
+ protocol: string;
+ /**
+ * Returns the Location object's URL's query (includes leading "?" if
+ * non-empty). Can be set, to navigate to the same URL with a changed query
+ * (ignores leading "?").
+ */
+ search: string;
+ /**
+ * Navigates to the given URL.
+ */
+ assign(url: string): void;
+ /**
+ * Reloads the current page.
+ */
+ reload(): void;
+ /** @deprecated */
+ reload(forcedReload: boolean): void;
+ /**
+ * Removes the current page from the session history and navigates to the
+ * given URL.
+ */
+ replace(url: string): void;
+}
diff --git a/js/globals.ts b/js/globals.ts
index e0fa6ef12..935890be4 100644
--- a/js/globals.ts
+++ b/js/globals.ts
@@ -58,6 +58,8 @@ window.clearInterval = timers.clearTimer;
window.console = new consoleTypes.Console(libdeno.print);
window.setTimeout = timers.setTimeout;
window.setInterval = timers.setInterval;
+// tslint:disable-next-line:no-any
+window.location = (undefined as unknown) as domTypes.Location;
// When creating the runtime type library, we use modifications to `window` to
// determine what is in the global namespace. When we put a class in the
diff --git a/js/lib.web_assembly.d.ts b/js/lib.web_assembly.d.ts
index a5747b30e..1ec6a7943 100644
--- a/js/lib.web_assembly.d.ts
+++ b/js/lib.web_assembly.d.ts
@@ -161,3 +161,8 @@ declare namespace WebAssembly {
constructor(message: string, fileName?: string, lineNumber?: string);
}
}
+
+// TODO Move ImportMeta intos its own lib.import_meta.d.ts file?
+interface ImportMeta {
+ url: string;
+}
diff --git a/js/location.ts b/js/location.ts
new file mode 100644
index 000000000..5e7cb07b4
--- /dev/null
+++ b/js/location.ts
@@ -0,0 +1,52 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { URL } from "./url";
+import { notImplemented } from "./util";
+import { Location } from "./dom_types";
+import { window } from "./globals";
+
+export function setLocation(url: string): void {
+ window.location = new LocationImpl(url);
+ Object.freeze(window.location);
+}
+
+export class LocationImpl implements Location {
+ constructor(url: string) {
+ const u = new URL(url);
+ this.url = u;
+ this.hash = u.hash;
+ this.host = u.host;
+ this.href = u.href;
+ this.hostname = u.hostname;
+ this.origin = u.protocol + "//" + u.host;
+ this.pathname = u.pathname;
+ this.protocol = u.protocol;
+ this.port = u.port;
+ this.search = u.search;
+ }
+
+ private url: URL;
+
+ toString(): string {
+ return this.url.toString();
+ }
+
+ readonly ancestorOrigins: string[] = [];
+ hash: string;
+ host: string;
+ hostname: string;
+ href: string;
+ readonly origin: string;
+ pathname: string;
+ port: string;
+ protocol: string;
+ search: string;
+ assign(url: string): void {
+ throw notImplemented();
+ }
+ reload(): void {
+ throw notImplemented();
+ }
+ replace(url: string): void {
+ throw notImplemented();
+ }
+}
diff --git a/js/location_test.ts b/js/location_test.ts
new file mode 100644
index 000000000..2302c32ed
--- /dev/null
+++ b/js/location_test.ts
@@ -0,0 +1,8 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { test, assert } from "./test_util.ts";
+
+test(function locationBasic() {
+ // location example: file:///Users/rld/src/deno/js/unit_tests.ts
+ console.log("location", window.location.toString());
+ assert(window.location.toString().endsWith("unit_tests.ts"));
+});
diff --git a/js/main.ts b/js/main.ts
index a5aae51a1..b67f188ac 100644
--- a/js/main.ts
+++ b/js/main.ts
@@ -4,12 +4,13 @@
import "./globals";
-import { log } from "./util";
+import { assert, log } from "./util";
import * as os from "./os";
import { libdeno } from "./libdeno";
import { args } from "./deno";
import { replLoop } from "./repl";
import { setVersions } from "./version";
+import { setLocation } from "./location";
// builtin modules
import * as deno from "./deno";
@@ -42,6 +43,12 @@ export default function denoMain() {
os.exit(0);
}
+ const mainModule = startResMsg.mainModule();
+ if (mainModule) {
+ assert(mainModule.length > 0);
+ setLocation(mainModule);
+ }
+
const cwd = startResMsg.cwd();
log("cwd", cwd);
@@ -51,8 +58,7 @@ export default function denoMain() {
log("args", args);
Object.freeze(args);
- const inputFn = args[0];
- if (!inputFn) {
+ if (!mainModule) {
replLoop();
}
}
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index c8479145b..91c1745b6 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -22,6 +22,7 @@ import "./files_test.ts";
import "./form_data_test.ts";
import "./globals_test.ts";
import "./headers_test.ts";
+import "./location_test.ts";
import "./make_temp_dir_test.ts";
import "./metrics_test.ts";
import "./mixins/dom_iterable_test.ts";