Skip to content

Commit

Permalink
fix: define window.name (denoland#20804)
Browse files Browse the repository at this point in the history
Closes denoland#20750

This matches what browsers do:
https://developer.mozilla.org/en-US/docs/Web/API/Window/name

In the future we might want to change the behavior to actually update
the process name, but that needs a bit of discussion regarding if
it needs a permission flag (that would make polyfiling `process.title`
setter really easy too).
  • Loading branch information
bartlomieju committed Oct 8, 2023
1 parent d41d3b8 commit dfc254c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/tests/node_compat/test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ let knownGlobals = [
global.setTimeout,
localStorage,
location,
name,
navigator,
onload,
onunload,
Expand Down
14 changes: 13 additions & 1 deletion cli/tests/unit/globals_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file no-window-prefix
import { assert } from "./test_util.ts";
import { assert, assertEquals } from "./test_util.ts";

Deno.test(function globalThisExists() {
assert(globalThis != null);
Expand Down Expand Up @@ -128,3 +128,15 @@ Deno.test(function webApiGlobalThis() {
assert(globalThis.CountQueuingStrategy !== null);
assert(globalThis.ByteLengthQueuingStrategy !== null);
});

Deno.test(function windowNameIsDefined() {
assertEquals(typeof globalThis.name, "string");
assertEquals(name, "");
assertEquals(window.name, name);
name = "foobar";
assertEquals(window.name, "foobar");
assertEquals(name, "foobar");
name = "";
assertEquals(window.name, "");
assertEquals(name, "");
});
4 changes: 4 additions & 0 deletions cli/tsc/dts/lib.deno.window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare interface Window extends EventTarget {
localStorage: Storage;
sessionStorage: Storage;
caches: CacheStorage;
name: string;

addEventListener<K extends keyof WindowEventMap>(
type: K,
Expand Down Expand Up @@ -292,3 +293,6 @@ declare var Location: {
// The types there must first be split into window, worker and global types.
/** @category Web APIs */
declare var location: Location;

/** @category Web APIs */
declare var name: string;
4 changes: 4 additions & 0 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ function bootstrapMainRuntime(runtimeOptions) {
}
ObjectDefineProperties(globalThis, mainRuntimeGlobalProperties);
ObjectDefineProperties(globalThis, {
// TODO(bartlomieju): in the future we might want to change the
// behavior of setting `name` to actually update the process name.
// Empty string matches what browsers do.
name: util.writable(""),
close: util.writable(windowClose),
closed: util.getterOnly(() => windowIsClosing),
});
Expand Down

0 comments on commit dfc254c

Please sign in to comment.