@celestia-island/hikari 0.55.451 → 0.55.452

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,16 @@
1
+ import type { HighlightResult } from "../wasm/highlight/hikari_wasm_highlight.js";
2
+ /**
3
+ * Adapt the generated wasm record to this module's contract.
4
+ *
5
+ * `string` = highlighted HTML; `null` = "the wasm has no highlighting for
6
+ * this input", which every caller renders as plaintext. The wasm omits
7
+ * `html` (`{ html: undefined }`) whenever it cannot highlight — an
8
+ * unregistered language, or a tokenizer failure on a known one — and an
9
+ * empty string is a real answer, so this is `??` — not `||` — and never
10
+ * the record itself. Split out from `highlight()` so the seam can be
11
+ * pinned table-driven, without the lazy-loading mock in the way.
12
+ */
13
+ export declare function adaptHighlightResult(result: HighlightResult): string | null;
1
14
  /** Highlight `code` in `lang`; plain text until the wasm module loads. */
2
15
  export declare function highlight(code: string, lang?: string): string;
3
16
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celestia-island/hikari",
3
- "version": "0.55.451",
3
+ "version": "0.55.452",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Hikari UI library, Vue 3 runtime — one design system for Vue 3 and Rust/WASM applications",
@@ -3,9 +3,13 @@ import { createApp, defineComponent, h } from "vue";
3
3
 
4
4
  // Mock the wasm module at the top level: the composable's lazy import()
5
5
  // resolves to this factory, exactly like the real module landing after
6
- // the first plaintext render.
6
+ // the first plaintext render. The shape is the real ABI — the binding
7
+ // returns `HighlightResult = { html?: string }`, NOT a bare string
8
+ // (packages/vue/src/wasm/highlight/hikari_wasm_highlight.d.ts). A string
9
+ // mock here hid the seam that fed the record to `v-html`, so every row
10
+ // rendered "[object Object]"; the adapter test pins that separately.
7
11
  vi.mock("../wasm/highlight/hikari_wasm_highlight.js", () => ({
8
- highlight: (text: string, lang: string) => `<wasm>${lang}:${text}</wasm>`,
12
+ highlight: (text: string, lang: string) => ({ html: `<wasm>${lang}:${text}</wasm>` }),
9
13
  }));
10
14
 
11
15
  import HkDiffViewer from "./HkDiffViewer";
@@ -1,6 +1,7 @@
1
1
  import HkIconButton from "./HkIconButton";
2
2
 
3
3
  import { computed, defineComponent, onBeforeUnmount, ref, watch, type PropType } from "vue";
4
+ import { Search, X } from "lucide-vue-next";
4
5
 
5
6
  import { useI18n } from "../i18n/context";
6
7
 
@@ -221,19 +222,7 @@ export default defineComponent({
221
222
  >
222
223
  <div class="hk-kw-search">
223
224
  <div class="hk-kw-search-box">
224
- <svg
225
- class="hk-kw-search-icon"
226
- width="15"
227
- height="15"
228
- viewBox="0 0 24 24"
229
- fill="none"
230
- stroke="currentColor"
231
- stroke-width="2"
232
- stroke-linecap="round"
233
- >
234
- <circle cx="11" cy="11" r="8" />
235
- <line x1="21" y1="21" x2="16.65" y2="16.65" />
236
- </svg>
225
+ <Search class="hk-kw-search-icon" size={15} />
237
226
  <input
238
227
  class="hk-kw-search-input"
239
228
  type="text"
@@ -254,20 +243,7 @@ export default defineComponent({
254
243
  semanticResults.value = [];
255
244
  }}
256
245
  >
257
- {{ icon: () => (
258
- <svg
259
- width="14"
260
- height="14"
261
- viewBox="0 0 24 24"
262
- fill="none"
263
- stroke="currentColor"
264
- stroke-width="2"
265
- stroke-linecap="round"
266
- >
267
- <line x1="18" y1="6" x2="6" y2="18" />
268
- <line x1="6" y1="6" x2="18" y2="18" />
269
- </svg>
270
- ) }}
246
+ {{ icon: () => <X size={14} /> }}
271
247
  </HkIconButton>
272
248
  )}
273
249
  </div>
@@ -128,4 +128,47 @@ describe("HkPopupSelect", () => {
128
128
  expect(value.textContent).toBe("Agent One");
129
129
  expect(value.textContent).not.toContain("secret-meta");
130
130
  });
131
+
132
+ it("scroll-to-highlight resolves inside this popup's viewport, never the document", async () => {
133
+ // The issue #907 collision: a foreign surface's row carries the SAME
134
+ // data-select-index and sits EARLIER in document order than the
135
+ // teleported panel (the overlay root is appended after this test's
136
+ // mount containers), so a document-scoped query resolved to it first
137
+ // and scrolled the wrong row. The prototype spy records every
138
+ // scrollIntoView target, pinning where the query actually resolved.
139
+ const decoy = document.createElement("div");
140
+ decoy.setAttribute("data-select-index", "1");
141
+ document.body.appendChild(decoy);
142
+ const scrolled: Element[] = [];
143
+ const spy = vi
144
+ .spyOn(Element.prototype, "scrollIntoView")
145
+ .mockImplementation(function (this: Element) {
146
+ scrolled.push(this);
147
+ });
148
+ try {
149
+ // modelValue "a2" → the popup opens with index 1 highlighted.
150
+ const c = mount(h(HkPopupSelect, { modelValue: "a2", options: OPTIONS }));
151
+ c.querySelector<HTMLButtonElement>(".hk-popup-select-trigger")!
152
+ .dispatchEvent(new MouseEvent("click", { bubbles: true }));
153
+ await Promise.resolve();
154
+ await Promise.resolve();
155
+ const ownRow = document.querySelector<HTMLElement>(
156
+ '.hk-popup-select-item[data-select-index="1"]',
157
+ );
158
+ expect(ownRow).toBeTruthy();
159
+ // Sanity: the row really lives inside the popup's own viewport.
160
+ expect(ownRow!.closest(".hk-popup-select-viewport")).not.toBeNull();
161
+ // The highlighted row got scrolled…
162
+ expect(scrolled).toContain(ownRow);
163
+ // …and nothing outside this popup's viewport ever did — the decoy
164
+ // must not even be reached by the query.
165
+ expect(scrolled).not.toContain(decoy);
166
+ for (const el of scrolled) {
167
+ expect(el.closest(".hk-popup-select-viewport")).not.toBeNull();
168
+ }
169
+ } finally {
170
+ spy.mockRestore();
171
+ decoy.remove();
172
+ }
173
+ });
131
174
  });
@@ -31,11 +31,6 @@ export function closeAllPopups(): void {
31
31
  activePopupId.value = null;
32
32
  }
33
33
 
34
- function scrollToElement(selector: string): void {
35
- const el = document.querySelector(selector) as HTMLElement | null;
36
- el?.scrollIntoView({ block: "nearest" });
37
- }
38
-
39
34
  export default defineComponent({
40
35
  name: "HkPopupSelect",
41
36
  props: {
@@ -68,6 +63,9 @@ export default defineComponent({
68
63
  const instanceId = `hk-popup-select-${++popupCounter}`;
69
64
  const isOpen = ref(false);
70
65
  const triggerRef = ref<HTMLElement>();
66
+ /** The popup's own scroll viewport (HkScrollContainer expose) — row
67
+ * queries stay scoped to it. */
68
+ const viewportExpose = ref<{ getScrollElement: () => HTMLElement | undefined }>();
71
69
  const highlightedIndex = ref(-1);
72
70
 
73
71
  // Registered with the overlay registry so closeAll()/isOverlayOpen()
@@ -180,7 +178,16 @@ export default defineComponent({
180
178
  });
181
179
 
182
180
  function scrollToHighlighted() {
183
- scrollToElement(`[data-select-index="${highlightedIndex.value}"]`);
181
+ // Scoped to OUR viewport — a document-scoped query could hit another
182
+ // concurrently open surface's rows (HkSelect/HkCurrencySelect/
183
+ // HkHeaderPicker panels carry the same data attribute), and the
184
+ // teleported panel is not even inside this component's own subtree
185
+ // (HkSelect's rule). Null viewport (panel not mounted yet) → no-op.
186
+ const root = viewportExpose.value?.getScrollElement() ?? null;
187
+ const el = root?.querySelector(
188
+ `[data-select-index="${highlightedIndex.value}"]`,
189
+ );
190
+ el?.scrollIntoView({ block: "nearest" });
184
191
  }
185
192
 
186
193
  watch(highlightedIndex, () => {
@@ -228,7 +235,10 @@ export default defineComponent({
228
235
  title={props.label || props.placeholder || undefined}
229
236
  class="hk-popup-select-content"
230
237
  >
231
- <HkScrollContainer class="hk-popup-select-viewport">
238
+ <HkScrollContainer
239
+ ref={viewportExpose}
240
+ class="hk-popup-select-viewport"
241
+ >
232
242
  {slots.default
233
243
  ? slots.default()
234
244
  : normalizedOptions.value.map((opt, i) => (
@@ -0,0 +1,61 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { adaptHighlightResult } from "./useHighlight";
4
+ import type { HighlightResult } from "../wasm/highlight/hikari_wasm_highlight.js";
5
+
6
+ /**
7
+ * The wasm seam: the binding returns `HighlightResult = { html?: string }`
8
+ * (generated — `wasm/highlight/hikari_wasm_highlight.d.ts` +
9
+ * `interfaces/hikari-highlight-types.d.ts`), not a string. Handing that
10
+ * record straight back made `HkDiffViewer`'s `v-html` render every row as
11
+ * "[object Object]"; the old string-returning mock could not see it.
12
+ *
13
+ * These cases are the whole contract of `adaptHighlightResult`, pinned
14
+ * table-driven and independent of the lazy-load/mock path: an `html`
15
+ * string is highlighted HTML, and anything the wasm declines to answer
16
+ * (`html: undefined`, or the field absent entirely) is `null`, which
17
+ * callers render as plaintext.
18
+ */
19
+ const CASES: Array<{ name: string; result: HighlightResult; expected: string | null }> = [
20
+ {
21
+ name: "html present → highlighted HTML",
22
+ result: { html: `<span class="hl">fn</span> main` },
23
+ expected: `<span class="hl">fn</span> main`,
24
+ },
25
+ {
26
+ name: "html explicitly undefined → null (plaintext fallback)",
27
+ result: { html: undefined },
28
+ expected: null,
29
+ },
30
+ {
31
+ name: "html field absent → null (plaintext fallback)",
32
+ result: {},
33
+ expected: null,
34
+ },
35
+ {
36
+ // `option<string>` allows `""`, so the guard must be `??` and not `||`:
37
+ // an empty-but-present html is highlighted-and-empty, not "no answer".
38
+ // (Today's Rust component cannot emit it — `tokens = { token+ }` needs at
39
+ // least one token — so this row pins the WIT contract, not a live path.)
40
+ name: "html empty string is kept, not coerced to null",
41
+ result: { html: "" },
42
+ expected: "",
43
+ },
44
+ ];
45
+
46
+ describe("adaptHighlightResult (wasm record → string | null)", () => {
47
+ for (const { name, result, expected } of CASES) {
48
+ it(name, () => {
49
+ expect(adaptHighlightResult(result)).toBe(expected);
50
+ });
51
+ }
52
+
53
+ it("never returns the record itself", () => {
54
+ // The original defect in one line: whatever the wasm says, the value
55
+ // that reaches `v-html` must be a string or null, never an object.
56
+ for (const { result } of CASES) {
57
+ const adapted = adaptHighlightResult(result);
58
+ expect(typeof adapted === "string" || adapted === null).toBe(true);
59
+ }
60
+ });
61
+ });
@@ -8,8 +8,14 @@
8
8
  // module instead of 85 JS grammar chunks).
9
9
 
10
10
  import { ref } from "vue";
11
+ import type { HighlightResult } from "../wasm/highlight/hikari_wasm_highlight.js";
11
12
 
12
- type WasmHighlightFn = (code: string, language: string) => string | null;
13
+ // The binding trades in a record, not a string: `HighlightResult` is
14
+ // `{ html?: string }` (generated, see hikari_wasm_highlight.d.ts and
15
+ // interfaces/hikari-highlight-types.d.ts). Keeping the generated type
16
+ // here is deliberate — a `(code, lang) => string | null` alias let the
17
+ // record reach `v-html` and every diff row rendered "[object Object]".
18
+ type WasmHighlightFn = (code: string, language: string) => HighlightResult;
13
19
 
14
20
  // Module-level state: the loaded wasm function (null until loaded).
15
21
  let wasmFn: WasmHighlightFn | null = null;
@@ -25,7 +31,7 @@ function ensureLoaded(): Promise<WasmHighlightFn | null> {
25
31
  if (!loadPromise) {
26
32
  loadPromise = import("../wasm/highlight/hikari_wasm_highlight.js")
27
33
  .then((mod) => {
28
- wasmFn = mod.highlight as WasmHighlightFn;
34
+ wasmFn = mod.highlight;
29
35
  version.value += 1;
30
36
  return wasmFn;
31
37
  })
@@ -64,6 +70,21 @@ function escapeHtml(s: string): string {
64
70
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
65
71
  }
66
72
 
73
+ /**
74
+ * Adapt the generated wasm record to this module's contract.
75
+ *
76
+ * `string` = highlighted HTML; `null` = "the wasm has no highlighting for
77
+ * this input", which every caller renders as plaintext. The wasm omits
78
+ * `html` (`{ html: undefined }`) whenever it cannot highlight — an
79
+ * unregistered language, or a tokenizer failure on a known one — and an
80
+ * empty string is a real answer, so this is `??` — not `||` — and never
81
+ * the record itself. Split out from `highlight()` so the seam can be
82
+ * pinned table-driven, without the lazy-loading mock in the way.
83
+ */
84
+ export function adaptHighlightResult(result: HighlightResult): string | null {
85
+ return result.html ?? null;
86
+ }
87
+
67
88
  /** Highlight `code` in `lang`; plain text until the wasm module loads. */
68
89
  export function highlight(code: string, lang?: string): string {
69
90
  // Establish reactivity — re-runs when the module finishes loading.
@@ -71,8 +92,8 @@ export function highlight(code: string, lang?: string): string {
71
92
 
72
93
  const name = lang ? normalize(lang) : "";
73
94
  if (name && wasmFn) {
74
- const result = wasmFn(code, name);
75
- if (result !== null) return result;
95
+ const html = adaptHighlightResult(wasmFn(code, name));
96
+ if (html !== null) return html;
76
97
  }
77
98
  // Trigger the lazy load (first call only); render plain text for now.
78
99
  if (!wasmFn && !loadPromise) {