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
12 changes: 7 additions & 5 deletions apps/server/src/preview/ServerBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2705,12 +2705,12 @@ it.effect.each([false, true])(
).pipe(Effect.provide(layer)),
);

it.effect("a long emoji title is cut without splitting an emoji and watched as reported", () =>
it.effect("a long title is cut without splitting an emoji or flag and watched as reported", () =>
Effect.scoped(
Effect.gen(function* () {
const { tabId } = yield* ready;
const { page, sessions } = contexts[0]!;
const documentTitle = `a${"😀".repeat(300)}`;
const documentTitle = `a${"😀".repeat(254)}🇯🇵END`;
page.title.mockImplementation(async () => documentTitle);
const send = sessions[0]!.send;
const respond = send.getMockImplementation()!;
Expand All @@ -2734,12 +2734,14 @@ it.effect("a long emoji title is cut without splitting an emoji and watched as r
event = yield* PubSub.take(events);
}
const { title } = event.snapshot.navStatus;
expect(title).toBe(`a${"😀".repeat(255)}`);
expect(title).toBe(`a${"😀".repeat(254)}`);
expect(title.isWellFormed()).toBe(true);

const watch = yield* Effect.promise(() => watched.promise);
const changed = /const changed = \(\) => (.*);\n/.exec(watch)![1]!;
const titleChanged = new Function("document", `return ${changed};`) as (document: {
const prelude = /^new Promise\(\(resolve\) => \{\n([\s\S]*?)\n if \(changed\(\)\)/.exec(
watch,
)![1]!;
const titleChanged = new Function("document", `${prelude}\nreturn changed();`) as (document: {
title: string;
}) => boolean;
expect(titleChanged({ title: documentTitle })).toBe(false);
Expand Down
7 changes: 6 additions & 1 deletion apps/server/src/preview/ServerBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,12 @@ const editingCommand = (key: string, modifiers: number) => {
const TITLE_WORLD = "t3-preview-title";
// Observe root replacement without subscribing to body subtree mutations.
const titleChange = (reported: string) => `new Promise((resolve) => {
const changed = () => document.title.slice(0, ${PREVIEW_TITLE_MAX_LENGTH}).replace(/[\\uD800-\\uDBFF]$/u, '') !== ${JSON.stringify(reported)};
const truncate = (title) => {
const cut = title.slice(0, ${PREVIEW_TITLE_MAX_LENGTH}).replace(/[\\uD800-\\uDBFF]$/u, '');
const indicators = /\\p{RI}+$/u.exec(cut)?.[0].length ?? 0;
return indicators % 4 === 2 && /^\\p{RI}/u.test(title.slice(cut.length)) ? cut.slice(0, -2) : cut;
};
const changed = () => truncate(document.title) !== ${JSON.stringify(reported)};
if (changed()) return resolve();
let head;
let root;
Expand Down
10 changes: 10 additions & 0 deletions packages/contracts/src/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Schema } from "effect";
import { describe, expect, it } from "vite-plus/test";

import { EnvironmentId, ThreadId } from "./baseSchemas.ts";
import { truncatePreviewTitle } from "./preview.ts";
import {
PreviewAutomationHost,
PreviewAutomationInvalidSelectorError,
Expand Down Expand Up @@ -94,3 +95,12 @@ describe("PreviewAutomationInvalidSelectorError", () => {
expect(message).toContain("aria-ref=");
});
});

describe("truncatePreviewTitle", () => {
const flag = "\uD83C\uDDEF\uD83C\uDDF5";

it("does not leave the first half of a flag", () => {
expect(truncatePreviewTitle(`${"a".repeat(510)}${flag}END`)).toBe("a".repeat(510));
expect(truncatePreviewTitle(`${"a".repeat(508)}${flag}END`)).toBe(`${"a".repeat(508)}${flag}`);
});
});
8 changes: 6 additions & 2 deletions packages/contracts/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ export const ConfiguredLocalServerUrls = Schema.Array(Url).check(
Schema.isMaxLength(CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS),
);
export const PREVIEW_TITLE_MAX_LENGTH = 512;
export const truncatePreviewTitle = (title: string) =>
title.slice(0, PREVIEW_TITLE_MAX_LENGTH).replace(/[\uD800-\uDBFF]$/u, "");
export const truncatePreviewTitle = (title: string) => {
const cut = title.slice(0, PREVIEW_TITLE_MAX_LENGTH).replace(/[\uD800-\uDBFF]$/u, "");
// Regional indicators pair from the start of a run, so an odd run before the cut splits a flag.
const indicators = /\p{RI}+$/u.exec(cut)?.[0].length ?? 0;
return indicators % 4 === 2 && /^\p{RI}/u.test(title.slice(cut.length)) ? cut.slice(0, -2) : cut;
};

const Title = Schema.String.check(Schema.isMaxLength(PREVIEW_TITLE_MAX_LENGTH));

Expand Down
Loading