Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(chat): set list direction so RTL gutters follow the list

koeria210cb05232e5
diff --git a/apps/desktop/src/components/Markdown.tsx b/apps/desktop/src/components/Markdown.tsx
index c4e1a997d..c56041d58 100644
--- a/apps/desktop/src/components/Markdown.tsx
+++ b/apps/desktop/src/components/Markdown.tsx
@@ -858,7 +858,9 @@ function textOfChildren(node: unknown): string {
// #2635: `text-align: start` follows the element's dir. A physical
// `text-align: left` inherited from an ancestor (or a platform default)
// would keep an Arabic-only reply left-aligned even when dir=rtl is set.
-// User message paragraphs already use this logical alignment.
+// User message paragraphs already use this logical alignment. Lists also
+// set dir on the ul/ol container so padding-inline-start follows the
+// list's majority script; item-only dir leaves the gutter on the LTR side.
function bidiDirAndStyle(children: ReactNode, style?: CSSProperties) {
return {
dir: directionAttr(textOfChildren(children)),
@@ -866,7 +868,7 @@ function bidiDirAndStyle(children: ReactNode, style?: CSSProperties) {
};
}
-type BidiTag = "p" | "li" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "blockquote";
+type BidiTag = "p" | "li" | "ul" | "ol" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "blockquote";
function makeBidi<T extends BidiTag>(tag: T) {
function BidiElement({
@@ -887,6 +889,8 @@ function makeBidi<T extends BidiTag>(tag: T) {
const BidiParagraph = makeBidi("p");
const BidiListItem = makeBidi("li");
+const BidiList = makeBidi("ul");
+const BidiOrderedList = makeBidi("ol");
const BidiHeading1 = makeBidi("h1");
const BidiHeading2 = makeBidi("h2");
const BidiHeading3 = makeBidi("h3");
@@ -905,6 +909,8 @@ const markdownComponents: Components = {
table: Table,
p: BidiParagraph,
li: BidiListItem,
+ ul: BidiList,
+ ol: BidiOrderedList,
h1: BidiHeading1,
h2: BidiHeading2,
h3: BidiHeading3,
diff --git a/apps/desktop/test/markdown-bidi-alignment.test.mjs b/apps/desktop/test/markdown-bidi-alignment.test.mjs
index 7152d95cf..c53a219ef 100644
--- a/apps/desktop/test/markdown-bidi-alignment.test.mjs
+++ b/apps/desktop/test/markdown-bidi-alignment.test.mjs
@@ -14,7 +14,13 @@ import { readFile } from "node:fs/promises";
* when the paragraph was Arabic-only. #2389 already set dir from majority
* script and used text-align:start on user bubbles, but Markdown blocks only
* set dir. This renders the production Markdown pipeline and checks that
- * Arabic blocks also get logical alignment, including headings.
+ * Arabic blocks also get logical alignment, including headings, and that
+ * list containers get the same majority-script dir so logical padding can
+ * follow. Inherited physical alignment and list gutter geometry are
+ * exercised in the Chromium probe (scripts/e2e/bidi-direction.tsx): a
+ * Windows-only default was not independently reproduced, so that probe
+ * uses an ancestor `text-align: left`, which is the inheritance path the
+ * reviewer confirmed.
*/
test("Arabic Markdown blocks get dir=rtl and text-align:start", async () => {
const styles = await readFile(
@@ -80,8 +86,14 @@ test("Arabic Markdown blocks get dir=rtl and text-align:start", async () => {
const quote = render("> اقتباس عربي");
assert.ok(hasDir(quote, "blockquote", "rtl") && start(quote), quote);
- const list = render("- عنصر قائمة عربي");
- assert.ok(hasDir(list, "li", "rtl") && start(list), list);
+ const list = render("- عنصر قائمة عربي\n- عنصر ثان");
+ assert.ok(hasDir(list, "ul", "rtl") && hasDir(list, "li", "rtl") && start(list), list);
+
+ const englishList = render("- First item\n- Second item");
+ assert.ok(hasDir(englishList, "ul", "ltr") && hasDir(englishList, "li", "ltr"), englishList);
+
+ const englishOrdered = render("1. First item\n2. Second item");
+ assert.ok(hasDir(englishOrdered, "ol", "ltr") && hasDir(englishOrdered, "li", "ltr"), englishOrdered);
const english = render("Just an English paragraph.");
assert.ok(hasDir(english, "p", "ltr") && start(english), english);
diff --git a/scripts/e2e/bidi-direction.tsx b/scripts/e2e/bidi-direction.tsx
index abd234a4e..84f1aad63 100644
--- a/scripts/e2e/bidi-direction.tsx
+++ b/scripts/e2e/bidi-direction.tsx
@@ -26,7 +26,7 @@ export async function bidiDirectionProbe(i18n: ReturnType<typeof createInstance>
const sessionId = "bidi-direction-probe";
const messages = [
message("bidi-user", "user", "hello English paragraph 123!\n\nپژگچ این پاراگراف فارسی است ؟"),
- message("bidi-assistant", "assistant", "English **paragraph** 123!\n\nپژگچ **پاسخ** فارسی ؟ ؛\n\n- English item\n- پژگچ فارسی\n\n# عنوان عربي"),
+ message("bidi-assistant", "assistant", "English **paragraph** 123!\n\nپژگچ **پاسخ** فارسی ؟ ؛\n\n- English item\n- پژگچ فارسی\n\n# عنوان عربي\n\n- عنصر واحد\n- عنصر اثنان\n\n1. First item\n2. Second item"),
];
try {
useAppStore.setState({ activeSessionId: sessionId, page: "chat" });
@@ -107,20 +107,40 @@ export async function bidiDirectionProbe(i18n: ReturnType<typeof createInstance>
const rtlBubble = bubble.querySelector<HTMLElement>('span[dir="rtl"]');
assert(rtlBubble && getComputedStyle(rtlBubble).direction === "rtl", "bubble RTL paragraph has no RTL layout");
assert(glyph(rtlBubble, "؟") < glyph(rtlBubble, "پ"), "bubble punctuation order is wrong");
- const listDirections = [...host.querySelectorAll<HTMLElement>(".prose-chat li")].map(item => item.dir);
- assert(listDirections.join(",") === "ltr,rtl", `Markdown list directions: ${listDirections}`);
+ const lists = [...host.querySelectorAll<HTMLElement>(".prose-chat ul, .prose-chat ol")];
+ const mixedList = lists.find((list) => /English item/.test(list.textContent ?? "") && /پژگچ/.test(list.textContent ?? ""));
+ const arabicList = lists.find((list) => list.tagName === "UL" && /عنصر واحد/.test(list.textContent ?? "") && !/First item/.test(list.textContent ?? ""));
+ const latinList = lists.find((list) => list.tagName === "OL" && /First item/.test(list.textContent ?? ""));
+ const mixedDirs = [...(mixedList?.querySelectorAll<HTMLElement>(":scope > li") ?? [])].map((item) => item.dir);
+ assert(mixedDirs.join(",") === "ltr,rtl", `Markdown list directions: ${mixedDirs.join(",")}`);
const emphasis = host.querySelector(".prose-chat strong");
assert(emphasis && getComputedStyle(emphasis).unicodeBidi === "isolate", "inline Markdown is not isolated");
assert(bubble.textContent === messages[0].content, "paragraph splitting changed user text");
const rtlMarkdown = markdownParagraphs[1];
assert(rtlMarkdown && glyph(rtlMarkdown, "؟") < glyph(rtlMarkdown, "پ"), "Markdown punctuation order is wrong");
+ const ltrMarkdown = markdownParagraphs[0];
+ // A Windows-only default was not independently reproduced. The confirmed
+ // inheritance path is an ancestor with a physical `text-align: left`.
+ const inheritHost = rtlMarkdown.closest<HTMLElement>(".prose-chat")?.parentElement ?? rtlMarkdown.parentElement;
+ assert(inheritHost, "Markdown ancestor for inherited alignment is missing");
+ inheritHost.style.textAlign = "left";
const rtlAlign = getComputedStyle(rtlMarkdown).textAlign;
assert(
rtlAlign === "start" || rtlAlign === "right",
- `RTL Markdown paragraph text-align was ${rtlAlign}`,
+ `RTL Markdown paragraph text-align was ${rtlAlign} under inherited left`,
+ );
+ const rtlBox = rtlMarkdown.getBoundingClientRect();
+ assert(
+ glyph(rtlMarkdown, "پ") > rtlBox.left + rtlBox.width / 2,
+ "Arabic paragraph glyphs stayed on the left under inherited text-align:left",
+ );
+ const ltrBox = ltrMarkdown.getBoundingClientRect();
+ assert(
+ ltrMarkdown && glyph(ltrMarkdown, "E") < ltrBox.left + ltrBox.width / 2,
+ "English paragraph glyphs did not stay on the left under inherited text-align:left",
);
const rtlItem = [...host.querySelectorAll<HTMLElement>(".prose-chat li")]
- .find((item) => item.dir === "rtl");
+ .find((item) => item.dir === "rtl" && /پژگچ/.test(item.textContent ?? ""));
const itemAlign = rtlItem && getComputedStyle(rtlItem).textAlign;
assert(
itemAlign === "start" || itemAlign === "right",
@@ -133,6 +153,30 @@ export async function bidiDirectionProbe(i18n: ReturnType<typeof createInstance>
headingAlign === "start" || headingAlign === "right",
`RTL Markdown heading text-align was ${headingAlign}`,
);
+ const padding = (element: HTMLElement) => {
+ const style = getComputedStyle(element);
+ return {
+ direction: style.direction,
+ paddingLeft: parseFloat(style.paddingLeft),
+ paddingRight: parseFloat(style.paddingRight),
+ };
+ };
+ assert(arabicList, "pure RTL list did not render");
+ assert(arabicList.dir === "rtl", `pure RTL list dir was ${arabicList.dir}`);
+ const rtlGutter = padding(arabicList);
+ assert(rtlGutter.direction === "rtl", `pure RTL list computed direction was ${rtlGutter.direction}`);
+ assert(
+ rtlGutter.paddingRight > 10 && rtlGutter.paddingRight > rtlGutter.paddingLeft + 8,
+ `pure RTL list gutter was padding-left ${rtlGutter.paddingLeft}px / padding-right ${rtlGutter.paddingRight}px`,
+ );
+ assert(latinList, "ordinary LTR list did not render");
+ assert(latinList.dir === "ltr", `ordinary LTR list dir was ${latinList.dir}`);
+ const ltrGutter = padding(latinList);
+ assert(ltrGutter.direction === "ltr", `ordinary LTR list computed direction was ${ltrGutter.direction}`);
+ assert(
+ ltrGutter.paddingLeft > 10 && ltrGutter.paddingLeft > ltrGutter.paddingRight + 8,
+ `ordinary LTR list gutter was padding-left ${ltrGutter.paddingLeft}px / padding-right ${ltrGutter.paddingRight}px`,
+ );
assert(
getComputedStyle(editor).unicodeBidi === "plaintext",
`composer unicode-bidi was ${getComputedStyle(editor).unicodeBidi}`,