Skip to content

Commit 93e3b62

Browse files
authored
fix(tui): don't hang on a slot path with an empty segment (#66863)
1 parent 999950b commit 93e3b62

2 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎packages/tui/src/plugin/structure.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function resolveSlots<Render>(input: {
7474
const winners = new Map<string, Claim<Render>>()
7575
for (const claim of input.claims) {
7676
if (claim.placement.kind !== "replace") continue
77-
if (!input.paths.has(claim.placement.target)) {
77+
if (!validPath(claim.placement.target) || !input.paths.has(claim.placement.target)) {
7878
suppressed.push({ claim })
7979
continue
8080
}
@@ -115,6 +115,10 @@ export function resolveSlots<Render>(input: {
115115
const kind = claim.placement.kind
116116
if (kind === "replace") continue
117117
const target = claim.placement.target
118+
if (!validPath(target)) {
119+
suppressed.push({ claim })
120+
continue
121+
}
118122
// Inside placements targeting a replaced boundary are part of its
119123
// contents; sibling placements on the boundary itself stay outside it.
120124
const inside = kind === "prepend" || kind === "append" ? boundaries.get(target) : undefined
@@ -150,8 +154,14 @@ function depth(path: string) {
150154
return path.split(".").length
151155
}
152156

157+
function validPath(path: string) {
158+
return path.length > 0 && !path.startsWith(".") && !path.endsWith(".") && !path.includes("..")
159+
}
160+
153161
function survivingAncestor(path: string, paths: ReadonlySet<string>) {
154-
for (let index = path.lastIndexOf("."); index !== -1; index = path.lastIndexOf(".", index - 1)) {
162+
// lastIndexOf clamps a negative start to 0, so stop before index 0 to avoid
163+
// looping on a leading dot.
164+
for (let index = path.lastIndexOf("."); index > 0; index = path.lastIndexOf(".", index - 1)) {
155165
const ancestor = path.slice(0, index)
156166
if (paths.has(ancestor)) return ancestor
157167
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { expect, test } from "bun:test"
2+
import type { Claim, Degraded, PlacementKind, Slotted, Suppressed } from "../src/plugin/structure"
3+
4+
function claim(kind: PlacementKind, target: string, id: string): Claim<string> {
5+
return { key: `p/${id}`, plugin: "p", placement: { kind, target }, render: id }
6+
}
7+
8+
type SerializedResolution = {
9+
readonly slotted: ReadonlyArray<[string, Slotted<string>]>
10+
readonly suppressed: ReadonlyArray<Suppressed<string>>
11+
readonly degraded: ReadonlyArray<Degraded<string>>
12+
}
13+
14+
// Run resolveSlots off the main thread so a synchronous infinite loop fails
15+
// via the timeout instead of blocking the test runner.
16+
async function resolveWithTimeout(
17+
paths: ReadonlyArray<string>,
18+
claims: ReadonlyArray<Claim<string>>,
19+
timeoutMs = 5_000,
20+
): Promise<SerializedResolution> {
21+
const moduleUrl = new URL("../src/plugin/structure.ts", import.meta.url).href
22+
const source = `
23+
import { resolveSlots } from ${JSON.stringify(moduleUrl)}
24+
self.onmessage = (event) => {
25+
const result = resolveSlots({ paths: new Set(event.data.paths), claims: event.data.claims })
26+
self.postMessage({
27+
slotted: [...result.slotted.entries()],
28+
suppressed: result.suppressed,
29+
degraded: result.degraded,
30+
})
31+
}
32+
`
33+
const blobUrl = URL.createObjectURL(new Blob([source], { type: "text/javascript" }))
34+
const worker = new Worker(blobUrl)
35+
let timer: ReturnType<typeof setTimeout> | undefined
36+
try {
37+
return await new Promise<SerializedResolution>((resolve, reject) => {
38+
timer = setTimeout(() => reject(new Error(`resolveSlots did not return within ${timeoutMs}ms`)), timeoutMs)
39+
worker.onmessage = (event) => resolve(event.data as SerializedResolution)
40+
worker.onerror = (event) => reject(event.error ?? new Error(event.message))
41+
worker.postMessage({ paths, claims })
42+
})
43+
} finally {
44+
clearTimeout(timer)
45+
worker.terminate()
46+
URL.revokeObjectURL(blobUrl)
47+
}
48+
}
49+
50+
test("suppresses slot claims targeting paths with empty segments without hanging", async () => {
51+
const invalid = [
52+
claim("append", ".x", "leading"),
53+
claim("after", ".", "dot"),
54+
claim("before", ".side.child", "leading-nested"),
55+
claim("prepend", "", "empty"),
56+
claim("append", "side.", "trailing"),
57+
claim("append", "side..child", "double-dot"),
58+
claim("replace", "side..child", "replace-double-dot"),
59+
claim("replace", ".x", "replace-leading"),
60+
]
61+
const direct = claim("append", "side", "direct")
62+
const degraded = claim("after", "side.missing", "degraded")
63+
64+
const result = await resolveWithTimeout(["side", "side.child"], [...invalid, direct, degraded])
65+
66+
expect(result.slotted).toEqual([
67+
[
68+
"side",
69+
{
70+
before: [],
71+
prepend: [],
72+
append: [direct, degraded],
73+
after: [],
74+
},
75+
],
76+
])
77+
expect(result.degraded).toEqual([{ claim: degraded, to: "side" }])
78+
expect(result.suppressed).toEqual([...invalid.slice(6), ...invalid.slice(0, 6)].map((item) => ({ claim: item })))
79+
})

0 commit comments

Comments
 (0)