Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion sdk/ui/src/dom/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ function put(k: string, v: unknown) {

const UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/152.0.0.0 Safari/537.36";

/** The navigator's words for the system raea's window runs on (globalThis.__raea_window.os, std's name, set before the
* page's bundle: raea engine/page/window chrome.rs); a Mac's when there's no window (a render). Until 0.1.8 a Windows
* PC's page said MacIntel, and a library that picks ⌘ or Ctrl by navigator.platform (CodeMirror's and ProseMirror's
* Mod-) took Ctrl+Z, Ctrl+C and Ctrl+V as a Mac's. Read when asked, so the window's word, set later than this, counts. */
export function navigatorSystem(os = (globalThis as { __raea_window?: { os?: string } }).__raea_window?.os): { userAgent: string; platform: string; data: string } {
if (os === "windows") return { userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/152.0.0.0 Safari/537.36", platform: "Win32", data: "Windows" }
if (os === "linux") return { userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/152.0.0.0 Safari/537.36", platform: "Linux x86_64", data: "Linux" }
return { userAgent: UA, platform: "MacIntel", data: "macOS" }
}

/** window and document as globals (window is the global object, as in a browser), the element and event
* classes, the observers, matchMedia, rAF and getComputedStyle. Must run before a library's modules evaluate
* (they check `typeof document` once): import "@raea/ui/dom-globals" first. With globalThis.__UI_DOM_SCAN set,
Expand Down Expand Up @@ -378,7 +388,21 @@ export function install() {
const vv: any = new EventTarget_();
Object.defineProperties(vv, { width: { get: () => S.vw }, height: { get: () => S.vh }, offsetLeft: { value: 0 }, offsetTop: { value: 0 }, pageLeft: { value: 0 }, pageTop: { value: 0 }, scale: { value: 1 } });
put("visualViewport", vv);
put("navigator", { userAgent: UA, platform: "MacIntel", vendor: "Google Inc.", language: "en-US", languages: ["en-US", "en"], maxTouchPoints: 0, onLine: true, cookieEnabled: true, hardwareConcurrency: 8, userAgentData: { brands: [{ brand: "HeadlessChrome", version: "152" }], mobile: false, platform: "macOS" }, clipboard: { writeText: () => Promise.resolve(), readText: () => Promise.resolve("") } });
put("navigator", {
get userAgent() {
return navigatorSystem().userAgent;
},
get platform() {
return navigatorSystem().platform;
},
// the web's own fields, as a browser has them, on one line (taxonomy.sh S3b reads a line that starts with a
// processor's name as a raea prop)
vendor: "Google Inc.", language: "en-US", languages: ["en-US", "en"], maxTouchPoints: 0, onLine: true, cookieEnabled: true, hardwareConcurrency: 8,
get userAgentData() {
return { brands: [{ brand: "HeadlessChrome", version: "152" }], mobile: false, platform: navigatorSystem().data };
},
clipboard: { writeText: () => Promise.resolve(), readText: () => Promise.resolve("") },
});
g.location ??= { href: "http://localhost/", origin: "http://localhost", protocol: "http:", host: "localhost", hostname: "localhost", port: "", pathname: "/", search: "", hash: "", assign() {}, replace() {}, reload() {} };
put("CSS", { supports: (p: string, v?: string) => (v === undefined ? false : !/^-webkit-backdrop-filter$/.test(p) && /^[a-z-]+$/.test(p)), escape: cssEscape });
g.AbortController ??= class {
Expand Down
40 changes: 40 additions & 0 deletions sdk/ui/test/navigator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// The page's navigator says the system raea's window runs on (globalThis.__raea_window.os, set by the window before the
// page's bundle), so a library choosing ⌘ or Ctrl by navigator.platform picks Ctrl on Windows and Linux. Until 0.1.8
// a Windows PC's page said MacIntel. With no window (a render) it's a Mac's, as it always was.
import { afterEach, beforeAll, expect, test } from "bun:test";
import { install } from "../src/dom";
import { navigatorSystem } from "../src/dom/core";

const g = globalThis as any;
beforeAll(() => install());
afterEach(() => delete g.__raea_window);

test("on Windows the navigator says Windows, read when asked (the window's word comes after the DOM is installed)", () => {
g.__raea_window = { chrome: "native", os: "windows" };
expect(g.navigator.platform).toBe("Win32");
expect(g.navigator.userAgent).toContain("Windows NT 10.0; Win64; x64");
expect(g.navigator.userAgent).not.toContain("Mac");
expect(g.navigator.userAgentData.platform).toBe("Windows");
});

test("on Linux it says Linux", () => {
g.__raea_window = { chrome: "native", os: "linux" };
expect(g.navigator.platform).toBe("Linux x86_64");
expect(g.navigator.userAgent).toContain("X11; Linux x86_64");
expect(g.navigator.userAgentData.platform).toBe("Linux");
});

test("on a Mac, and with no window at all, it's a Mac's as before", () => {
g.__raea_window = { chrome: "inset", os: "macos" };
expect(g.navigator.platform).toBe("MacIntel");
delete g.__raea_window;
expect(g.navigator.platform).toBe("MacIntel");
expect(g.navigator.userAgent).toContain("Macintosh");
expect(navigatorSystem().data).toBe("macOS");
});

test("a library's Mac test, as CodeMirror and ProseMirror write it, is false on Windows", () => {
g.__raea_window = { chrome: "native", os: "windows" };
expect(/Mac/.test(g.navigator.platform)).toBe(false);
expect(/Mac|iP(hone|[oa]d)/.test(g.navigator.platform)).toBe(false);
});
Loading