diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index 0ff214207..06815e0bc 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -28932,3 +28932,16 @@ - 未覆盖:完全没写标题、把依据混在正文里的回答不折叠(按原样显示)。 - 关联记录:BUG-2137、BUG-1942、BUG-1920。 - 修复版本:`claude/chat-basis-fold-2142`。 + +## BUG-2140 | 报告里圈一段话提问:跨段、三击整段、超过 300 字、手机长按都没有「问这一段」,只能复制粘贴 + +- 状态:fixed-pending-verify +- 首次发现 / 最近更新:2026-11-22 / 2026-11-22。 +- 现象:产品负责人反馈,个人、年运、合盘三种报告里选中一部分正文后,没有「问这一段」,提问框上方也没有引用,只能自己复制粘贴进提问框。 +- 复现(测试环境 e8e35821,电脑 1280 宽和手机 390 宽,三种报告各一份,未发送问题):电脑上在一段之内选 ≤300 字可以正常出现并引用;以下情况都没有反应:(1) 选中跨过段落边界(拖到下一段、拖过行尾);(2) 三击选整段(选区的终点落在下一段开头的第 0 位,也算跨段);(3) 选中超过 300 字;(4) 手机/平板长按选字,连一段之内的短句也不出现。 +- 根因:(1)(2) `frontend/src/lib/report-ask-selection.ts` 的 `selectionAnchor` 要求选区起点和终点在同一个 `data-ask-text` 段落里,否则返回 null;(3) 同一函数超过 `REPORT_QUOTE_MAX_CHARS = 300` 返回 null,也不告诉用户;(4) `frontend/src/components/report-ask/report-ask-selection.tsx` 只在 `pointerup` 里认出触屏,长按选字只有 pointerdown + pointercancel,没有 pointerup,触屏标记一直是 false,`selectionchange` 又只处理「选区变空」,于是不显示。对比 BUG-2073 之后的提交(784a47bbe、c2c730234、1f427fd83、933ab1d96、2637aa68d),没有一处加全局监听或改 user-select;第十九轮只验过电脑上一段之内的短句。 +- 修复:(1) 新增 `selectedParts(range, root)`:取选区覆盖到的每个可提问段落里实际选中的文字(只碰到边缘的段落不算,标题和表格不算);`passageAnchor(parts)`:同一节的多段合成一处引用;跨到下一节时只留起始那一节;太短返回 null。(2) 上限从 300 提到 1000 字,请求上限 600 → 2400;更长的选区截取前 1000 字,并弹出提示「选中的内容较长,只引用了前 1000 字。」(toast,不占版面)。(3) 服务端 `verifiedReportQuote` 改为认可「某段的结尾 + 若干整段 + 某段的开头」,段落顺序不限(年运的段落表不一定按页面顺序);仍然只认这一节自己的文字。(4) 高亮 `findQuoteRange` / `markReportPassage` 可以跨段:有高亮 API 时精确到字,没有时给涉及的每一段上底色。(5) 触屏在 `pointerdown`(捕获阶段)就认出来,长按选字后出现「问这一段」。 +- 验证:`frontend/tests/report-questions-2073.test.ts`(多段合成、跨节只留起始节、超长截取、服务端多段认可与拒绝拼接的别处文字);把新函数注入测试环境的真实页面检查:三击、跨两段拖选、跨节拖选、整节(合盘 5 段、年运 3 段)都得到正确的节和引用,页面高亮成功,用页面文字校验通过。 +- 防复发:测试覆盖跨段、跨节、超长和服务端校验;测试注释写明原值与新值。 +- 关联记录:BUG-2073、BUG-2081、BUG-2085、BUG-2093。 +- 修复版本:`claude/report-ask-select-2140`。 diff --git a/frontend/src/components/annual/annual-report-page.tsx b/frontend/src/components/annual/annual-report-page.tsx index 67793f863..edf3627a4 100644 --- a/frontend/src/components/annual/annual-report-page.tsx +++ b/frontend/src/components/annual/annual-report-page.tsx @@ -902,7 +902,7 @@ export function AnnualReportWithQuestions({ reportId, snapshot, area, onHeader } ), }} /> - ask.startAsk(anchor)} /> + ask.startAsk(anchor)} /> {ask.sheet ? ( ask.setOpen(false)} /> ) : ask.open ? ( diff --git a/frontend/src/components/personal-report/personal-report-questions.tsx b/frontend/src/components/personal-report/personal-report-questions.tsx index 68d6c258d..6d9d97f94 100644 --- a/frontend/src/components/personal-report/personal-report-questions.tsx +++ b/frontend/src/components/personal-report/personal-report-questions.tsx @@ -148,7 +148,7 @@ export function PersonalReportQuestions({ {children({ view, headerEntry })} {enabled ? ( <> - startAsk(anchor)} /> + startAsk(anchor)} /> {ask.sheet ? ( ask.setOpen(false)} /> ) : ask.open ? ( diff --git a/frontend/src/components/report-ask/report-ask-selection.tsx b/frontend/src/components/report-ask/report-ask-selection.tsx index 197765bc3..ffd0c1839 100644 --- a/frontend/src/components/report-ask/report-ask-selection.tsx +++ b/frontend/src/components/report-ask/report-ask-selection.tsx @@ -5,9 +5,10 @@ import { useEffect, useState, type RefObject } from "react"; import { Button } from "@/components/ui/button"; import type { ReportAnchor } from "@/lib/report-ask-anchor"; -import { ASK_TEXT_ATTRIBUTE, placeSelectionBar, selectionAnchor, type LineBox } from "@/lib/report-ask-selection"; +import { notifyInfo } from "@/lib/notify"; +import { ASK_TEXT_ATTRIBUTE, passageAnchor, placeSelectionBar, selectedParts, type LineBox } from "@/lib/report-ask-selection"; -type Picked = { anchor: Required; left: number; top: number; side: "above" | "below" | "beside" }; +type Picked = { anchor: Required; trimmed: boolean; left: number; top: number; side: "above" | "below" | "beside" }; const GAP = 10; // The bar's own box: the button (36, or 44 on touch) plus its padding and border. @@ -51,9 +52,11 @@ function paragraphEnd(node: Node): { right: number; lastLine: LineBox } | null { * BUG-2081: on a pointer it goes beside the end of the selection when that line has room, * otherwise above the first selected line; see placeSelectionBar. */ -export function ReportAskSelection({ container, label, disabled, onAsk }: Readonly<{ +export function ReportAskSelection({ container, label, trimmedNotice, disabled, onAsk }: Readonly<{ container: RefObject; label: string; + /** BUG-2140: said (as a toast) when the selection was cut to fit a question. */ + trimmedNotice: string; disabled?: boolean; onAsk: (anchor: Required) => void; }>) { @@ -74,8 +77,9 @@ export function ReportAskSelection({ container, label, disabled, onAsk }: Readon setPicked(null); return; } - const anchor = selectionAnchor({ text: selection.toString(), startNode: range.startContainer, endNode: range.endContainer }); - if (!anchor) { + // BUG-2140: a selection may run over several paragraphs of a section, and may be long. + const passage = passageAnchor(selectedParts(range, root)); + if (!passage) { setPicked(null); return; } @@ -91,7 +95,12 @@ export function ReportAskSelection({ container, label, disabled, onAsk }: Readon gap: GAP, touch, }); - setPicked({ anchor, ...placed }); + setPicked({ anchor: passage.anchor, trimmed: passage.trimmed, ...placed }); + }; + // BUG-2140: a long press on a touch screen ends in pointercancel, never pointerup, so a + // touch is known from the pointer that went down. + const onPointerDown = (event: PointerEvent) => { + touch = event.pointerType === "touch"; }; const onPointerUp = (event: PointerEvent) => { touch = event.pointerType === "touch"; @@ -103,11 +112,13 @@ export function ReportAskSelection({ container, label, disabled, onAsk }: Readon if (touch || !window.getSelection()?.toString()) read(); }; const hide = () => setPicked(null); + document.addEventListener("pointerdown", onPointerDown, true); document.addEventListener("pointerup", onPointerUp); document.addEventListener("selectionchange", onSelection); document.addEventListener("scroll", hide, true); window.addEventListener("resize", hide); return () => { + document.removeEventListener("pointerdown", onPointerDown, true); document.removeEventListener("pointerup", onPointerUp); document.removeEventListener("selectionchange", onSelection); document.removeEventListener("scroll", hide, true); @@ -126,6 +137,7 @@ export function ReportAskSelection({ container, label, disabled, onAsk }: Readon onPointerDown={(event) => event.preventDefault()} onClick={() => { onAsk(picked.anchor); + if (picked.trimmed) notifyInfo(trimmedNotice); setPicked(null); window.getSelection()?.removeAllRanges(); }} diff --git a/frontend/src/components/synastry/synastry-report-view.tsx b/frontend/src/components/synastry/synastry-report-view.tsx index ea256fc56..8ebe271cf 100644 --- a/frontend/src/components/synastry/synastry-report-view.tsx +++ b/frontend/src/components/synastry/synastry-report-view.tsx @@ -563,6 +563,7 @@ export function SynastryReportView(props: { startAsk(anchor)} /> )} diff --git a/frontend/src/lib/report-ask-anchor.ts b/frontend/src/lib/report-ask-anchor.ts index 93b66dc80..c68ee424b 100644 --- a/frontend/src/lib/report-ask-anchor.ts +++ b/frontend/src/lib/report-ask-anchor.ts @@ -6,9 +6,13 @@ */ export const REPORT_QUOTE_MIN_CHARS = 2; -export const REPORT_QUOTE_MAX_CHARS = 300; +/** + * BUG-2140: a selection may run over several paragraphs of a section; a longer one is cut to + * its first REPORT_QUOTE_MAX_CHARS characters on the page, and the reader is told so. + */ +export const REPORT_QUOTE_MAX_CHARS = 1000; /** What a request may carry before normalising: a selection across lines brings whitespace with it. */ -export const REPORT_QUOTE_REQUEST_MAX_CHARS = 600; +export const REPORT_QUOTE_REQUEST_MAX_CHARS = 2400; export type ReportAnchor = Readonly<{ sectionId: string; quote?: string }>; @@ -31,6 +35,42 @@ function squeeze(value: string): string { return value.replace(/\s+/gu, ""); } +/** + * BUG-2140: whether the (whitespace-free) quote is the section's own text, possibly running over + * several paragraphs: the end of one paragraph, any whole paragraphs, the start of another. The + * paragraphs may come in any order (the stored lists do not always follow the page's order). + */ +function coveredByParagraphs(needle: string, paragraphs: readonly string[]): boolean { + if (paragraphs.some((paragraph) => paragraph.includes(needle))) return true; + const length = needle.length; + const reached = new Set(); + const queue: number[] = []; + const reach = (at: number) => { + if (at > 0 && at < length && !reached.has(at)) { + reached.add(at); + queue.push(at); + } + }; + // The first piece: the end of a paragraph that is the start of the quote. + for (const paragraph of paragraphs) { + for (let take = Math.min(paragraph.length, length - 1); take >= 1; take -= 1) { + if (paragraph[paragraph.length - take] === needle[0] && paragraph.endsWith(needle.slice(0, take))) reach(take); + } + } + while (queue.length > 0) { + const at = queue.shift()!; + const rest = needle.slice(at); + for (const paragraph of paragraphs) { + if (!paragraph) continue; + // The last piece: the start of a paragraph. + if (paragraph.startsWith(rest)) return true; + // A whole paragraph in between. + if (rest.startsWith(paragraph)) reach(at + paragraph.length); + } + } + return false; +} + /** * The quote as it will be stored, or null when it is not a passage of these paragraphs: * too short, too long, or text that is not in them (edited, or taken from another report). @@ -42,7 +82,7 @@ export function verifiedReportQuote(quote: unknown, paragraphs: readonly string[ if (length < REPORT_QUOTE_MIN_CHARS || length > REPORT_QUOTE_MAX_CHARS) return null; const needle = squeeze(normalized); if (!needle) return null; - return paragraphs.some((paragraph) => squeeze(paragraph).includes(needle)) ? normalized : null; + return coveredByParagraphs(needle, paragraphs.map(squeeze).filter(Boolean)) ? normalized : null; } /** A stored anchor read back from a message; anything of another shape reads as none. */ diff --git a/frontend/src/lib/report-ask-client.ts b/frontend/src/lib/report-ask-client.ts index 700c796dc..d84335fa6 100644 --- a/frontend/src/lib/report-ask-client.ts +++ b/frontend/src/lib/report-ask-client.ts @@ -1,4 +1,4 @@ -import type { ReportAnchor } from "@/lib/report-ask-anchor"; +import { REPORT_QUOTE_MAX_CHARS, type ReportAnchor } from "@/lib/report-ask-anchor"; import type { ReportQuestionMessage } from "@/lib/report-questions"; /** @@ -148,6 +148,8 @@ export type ReportAskText = { wholeReport: string; /** BUG-2081: the small close button beside the pending passage or section. */ clearDraft: string; + /** BUG-2140: a selection longer than a question can carry was cut to its start. */ + passageTrimmed: string; }; export const REPORT_ASK_TEXT: Readonly> = { @@ -173,6 +175,7 @@ export const REPORT_ASK_TEXT: Readonly> = { askReport: "提问", wholeReport: "整份报告", clearDraft: "不针对这一处", + passageTrimmed: `选中的内容较长,只引用了前 ${REPORT_QUOTE_MAX_CHARS} 字。`, }, en: { panelTitle: "Ask about this report", @@ -196,5 +199,6 @@ export const REPORT_ASK_TEXT: Readonly> = { askReport: "Ask", wholeReport: "The whole report", clearDraft: "Ask about the whole report", + passageTrimmed: `The selection is long; only its first ${REPORT_QUOTE_MAX_CHARS} characters are quoted.`, }, }; diff --git a/frontend/src/lib/report-ask-selection.ts b/frontend/src/lib/report-ask-selection.ts index ec9e7ffd5..0ada582b4 100644 --- a/frontend/src/lib/report-ask-selection.ts +++ b/frontend/src/lib/report-ask-selection.ts @@ -18,52 +18,102 @@ function askParagraph(node: unknown): ElementLike | null { return element && typeof element.closest === "function" ? element.closest(`[${ASK_TEXT_ATTRIBUTE}]`) : null; } +/** One reading paragraph's share of a selection. */ +export type SelectedPart = Readonly<{ sectionId: string; text: string }>; + +export type SelectedPassage = Readonly<{ + anchor: Required; + /** BUG-2140: the selection was longer than a question can carry; the quote is its start. */ + trimmed: boolean; +}>; + /** - * The anchor of a selection, or null when it is not something to ask about: empty, across - * two paragraphs, outside the reading text, too short or too long. + * BUG-2140: what a selection asks about, from the reading paragraphs it covers, in page order. + * A selection may run over several paragraphs of one section (a triple click, or a drag over a + * whole part); one that runs into the next section keeps the section it starts in. Too long: + * the first REPORT_QUOTE_MAX_CHARS characters, marked `trimmed`. Null when there is nothing to + * ask about: no reading text, or too short. */ -export function selectionAnchor(input: { text: string; startNode: unknown; endNode: unknown }): Required | null { - const start = askParagraph(input.startNode); - const end = askParagraph(input.endNode); - if (!start || start !== end) return null; - const sectionId = start.getAttribute?.(ASK_SECTION_ATTRIBUTE) ?? ""; +export function passageAnchor(parts: readonly SelectedPart[]): SelectedPassage | null { + const present = parts.filter((part) => part.sectionId && normalizeReportQuote(part.text)); + const sectionId = present[0]?.sectionId; if (!sectionId) return null; - const quote = normalizeReportQuote(input.text); - const length = [...quote].length; - if (length < REPORT_QUOTE_MIN_CHARS || length > REPORT_QUOTE_MAX_CHARS) return null; - return { sectionId, quote }; + const quote = normalizeReportQuote(present.filter((part) => part.sectionId === sectionId).map((part) => part.text).join(" ")); + const chars = [...quote]; + if (chars.length < REPORT_QUOTE_MIN_CHARS) return null; + if (chars.length <= REPORT_QUOTE_MAX_CHARS) return { anchor: { sectionId, quote }, trimmed: false }; + return { anchor: { sectionId, quote: chars.slice(0, REPORT_QUOTE_MAX_CHARS).join("").trim() }, trimmed: true }; } -function squeezeWithMap(text: string): { squeezed: string; map: number[] } { - const map: number[] = []; - let squeezed = ""; - for (let index = 0; index < text.length; index += 1) { - if (/\s/u.test(text[index]!)) continue; - squeezed += text[index]; - map.push(index); +/** + * The reading paragraphs a selection covers and the text of each it covers, in page order. + * A paragraph the selection only touches at an edge (a triple click ends at the start of the + * next one) gives no text. Text outside reading paragraphs (headings, tables) is left out. + */ +export function selectedParts(range: Range, root: ParentNode): SelectedPart[] { + const parts: SelectedPart[] = []; + const own = askParagraph(range.startContainer) as Element | null; + const candidates = [...root.querySelectorAll(`[${ASK_TEXT_ATTRIBUTE}]`)]; + if (own && !candidates.includes(own as HTMLElement)) candidates.unshift(own as HTMLElement); + for (const element of candidates) { + if (!range.intersectsNode(element)) continue; + const piece = document.createRange(); + piece.selectNodeContents(element); + if (piece.compareBoundaryPoints(Range.START_TO_START, range) < 0) piece.setStart(range.startContainer, range.startOffset); + if (piece.compareBoundaryPoints(Range.END_TO_END, range) > 0) piece.setEnd(range.endContainer, range.endOffset); + const text = piece.toString(); + if (text.trim()) parts.push({ sectionId: element.getAttribute(ASK_SECTION_ATTRIBUTE) ?? "", text }); } - return { squeezed, map }; + return parts; } /** Where a quote sits in a paragraph's text, ignoring whitespace; null when it is not there. */ export function quoteOffsets(paragraph: string, quote: string): { start: number; end: number } | null { const needle = quote.replace(/\s+/gu, ""); if (!needle) return null; - const { squeezed, map } = squeezeWithMap(paragraph); + let squeezed = ""; + const map: number[] = []; + for (let index = 0; index < paragraph.length; index += 1) { + if (/\s/u.test(paragraph[index]!)) continue; + squeezed += paragraph[index]; + map.push(index); + } const at = squeezed.indexOf(needle); if (at < 0) return null; return { start: map[at]!, end: map[at + needle.length - 1]! + 1 }; } -/** The paragraph of a section that holds the quote, in the page as it is shown now. */ -export function findQuoteParagraph(root: ParentNode, anchor: ReportAnchor): { element: HTMLElement; offsets: { start: number; end: number } } | null { - if (!anchor.quote) return null; +type TextPoint = { node: Text; offset: number; element: HTMLElement }; + +/** + * BUG-2140: the words of a quote on the page, which may run over several paragraphs of its + * section: the first and last character as text positions, and the paragraphs it covers. + */ +export function findQuoteRange(root: ParentNode, anchor: ReportAnchor): { start: TextPoint; end: TextPoint; elements: HTMLElement[] } | null { + const needle = (anchor.quote ?? "").replace(/\s+/gu, ""); + if (!needle) return null; const paragraphs = root.querySelectorAll(`[${ASK_TEXT_ATTRIBUTE}][${ASK_SECTION_ATTRIBUTE}="${anchor.sectionId}"]`); + // Every visible character of the section, in page order, with where it is. + let squeezed = ""; + const points: TextPoint[] = []; for (const element of paragraphs) { - const offsets = quoteOffsets(element.textContent ?? "", anchor.quote); - if (offsets) return { element, offsets }; + const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT); + for (let node = walker.nextNode() as Text | null; node; node = walker.nextNode() as Text | null) { + const value = node.data; + for (let offset = 0; offset < value.length; offset += 1) { + if (/\s/u.test(value[offset]!)) continue; + squeezed += value[offset]; + points.push({ node, offset, element }); + } + } } - return null; + const at = squeezed.indexOf(needle); + if (at < 0) return null; + const start = points[at]!; + const last = points[at + needle.length - 1]!; + const covered = new Set(points.slice(at, at + needle.length).map((point) => point.element)); + const elements = [...paragraphs].filter((element) => covered.has(element)); + return { start, end: { ...last, offset: last.offset + 1 }, elements }; } type HighlightRegistry = { set: (name: string, value: unknown) => void; delete: (name: string) => void }; @@ -76,28 +126,27 @@ function highlights(): { registry: HighlightRegistry; Highlight: new (range: Ran /** * Tints the quoted passage in the report: the exact words where the browser has the - * highlight API, the whole paragraph otherwise. Returns the paragraph, or null when the - * passage is not on the page (the other language is showing). + * highlight API, the whole paragraphs otherwise. Returns the first paragraph, or null when + * the passage is not on the page (the other language is showing). */ export function markReportPassage(root: ParentNode, anchor: ReportAnchor): HTMLElement | null { clearReportPassage(root); - const found = findQuoteParagraph(root, anchor); - if (!found) return null; + const found = findQuoteRange(root, anchor); + if (!found || found.elements.length === 0) return null; const api = highlights(); - const text = found.element.firstChild; - if (api && text && text.nodeType === 3 && found.element.childNodes.length === 1) { + if (api) { try { const range = document.createRange(); - range.setStart(text, found.offsets.start); - range.setEnd(text, found.offsets.end); + range.setStart(found.start.node, found.start.offset); + range.setEnd(found.end.node, found.end.offset); api.registry.set(ASK_HIGHLIGHT_NAME, new api.Highlight(range)); - return found.element; + return found.elements[0]!; } catch { // Fall through to the paragraph tint. } } - found.element.setAttribute("data-ask-marked", "true"); - return found.element; + for (const element of found.elements) element.setAttribute("data-ask-marked", "true"); + return found.elements[0]!; } export function clearReportPassage(root: ParentNode): void { diff --git a/frontend/tests/report-questions-2073.test.ts b/frontend/tests/report-questions-2073.test.ts index 5d3f9318b..151a1420b 100644 --- a/frontend/tests/report-questions-2073.test.ts +++ b/frontend/tests/report-questions-2073.test.ts @@ -13,7 +13,7 @@ import { REPORT_ASK_TEXT, synastryQuestionRequest, } from "../src/lib/report-ask-client.ts"; -import { quoteOffsets, selectionAnchor } from "../src/lib/report-ask-selection.ts"; +import { passageAnchor, quoteOffsets } from "../src/lib/report-ask-selection.ts"; import { pickReportSession, readAskParam, @@ -72,6 +72,15 @@ test("BUG-2073: a selected passage is kept only when it is text of that section const long = "克制".repeat(REPORT_QUOTE_MAX_CHARS); assert.equal(verifiedReportQuote(long, [long]), null); assert.equal(verifiedReportQuote(long.slice(0, REPORT_QUOTE_MAX_CHARS), [long])?.length, REPORT_QUOTE_MAX_CHARS); + // BUG-2140: a passage over several paragraphs of the section, whatever order they are stored in (only the + // section's own sentences pass; their order is not checked). + const parts = ["第一段先说性格,", "第二段讲相处。", "第三段讲时间。"]; + assert.equal(verifiedReportQuote("先说性格, 第二段讲相处。 第三段", parts), "先说性格, 第二段讲相处。 第三段"); + assert.equal(verifiedReportQuote("讲时间。 第一段先说", [...parts].reverse()), "讲时间。 第一段先说"); + assert.equal(verifiedReportQuote("先说性格, 第三段讲时间。 第二段讲相处。 第三", parts), "先说性格, 第三段讲时间。 第二段讲相处。 第三"); + assert.equal(verifiedReportQuote("先说性格,第二段讲", ["第一段先说性格,", "第二段讲别的。"]), "先说性格,第二段讲"); + assert.equal(verifiedReportQuote("先说性格,第四段讲", parts), null); + assert.equal(verifiedReportQuote("性格,第二段", ["第一段先说性格,多一句", "第二段讲相处。"]), null); // The timeline's periods are selectable too; the structure is read from the Chinese body. assert.deepEqual(synastryReaderParagraphs(payload, "timeline"), ["先把日常说清楚。", "Sort out the everyday first."]); assert.equal(synastryPayloadStructure(payload), "skill_v3"); @@ -231,20 +240,21 @@ test("BUG-2073: the answer stream is read to its end; a stream without a complet assert.equal(readReportQuestionsPayload({ price: 10 }), null); }); -test("BUG-2073: only reading paragraphs of one section can be selected for a question", () => { - const paragraph = (section: string) => { - const element = { getAttribute: (name: string) => (name === "data-ask-section" ? section : ""), closest: () => element as unknown }; - return element; - }; - const text = (parent: unknown) => ({ parentElement: parent }); - const a = paragraph("climate"); - const b = paragraph("climate"); - const outside = { closest: () => null }; - assert.deepEqual(selectionAnchor({ text: " 容易把话\n咽回去 ", startNode: text(a), endNode: text(a) }), { sectionId: "climate", quote: "容易把话 咽回去" }); - assert.equal(selectionAnchor({ text: "跨两段", startNode: text(a), endNode: text(b) }), null); - assert.equal(selectionAnchor({ text: "表格里的字", startNode: text(outside), endNode: text(outside) }), null); - assert.equal(selectionAnchor({ text: "字", startNode: text(a), endNode: text(a) }), null); - assert.equal(selectionAnchor({ text: "长".repeat(301), startNode: text(a), endNode: text(a) }), null); +test("BUG-2073 / BUG-2140: a selection asks about the reading text of the section it starts in", () => { + // 原值: 跨两段 → null,超过 300 字 → null;新值: 同一节的多段合成一处引用,跨到下一节只留起始一节, + // 超长截取前 REPORT_QUOTE_MAX_CHARS 字并标 trimmed;原因: BUG-2140(三击、整段拖选、长段落都没反应)。 + assert.deepEqual(passageAnchor([{ sectionId: "climate", text: " 容易把话\n咽回去 " }]), { anchor: { sectionId: "climate", quote: "容易把话 咽回去" }, trimmed: false }); + assert.deepEqual(passageAnchor([{ sectionId: "climate", text: "第一段的后半。" }, { sectionId: "climate", text: "第二段的前半" }]), { anchor: { sectionId: "climate", quote: "第一段的后半。 第二段的前半" }, trimmed: false }); + assert.deepEqual(passageAnchor([{ sectionId: "climate", text: "这一节的末尾" }, { sectionId: "attraction", text: "下一节的开头" }])?.anchor, { sectionId: "climate", quote: "这一节的末尾" }); + assert.equal(passageAnchor([]), null); + assert.equal(passageAnchor([{ sectionId: "climate", text: "字" }]), null); + const long = passageAnchor([{ sectionId: "climate", text: "长".repeat(REPORT_QUOTE_MAX_CHARS + 40) }]); + assert.equal(long?.trimmed, true); + assert.equal([...(long?.anchor.quote ?? "")].length, REPORT_QUOTE_MAX_CHARS); + // BUG-2140: a long press on a touch screen ends in pointercancel, so the bar learns it is a touch on pointerdown. + const bar = read("../src/components/report-ask/report-ask-selection.tsx"); + assert.match(bar, /addEventListener\("pointerdown", onPointerDown, true\)/); + assert.match(bar, /passageAnchor\(selectedParts\(range, root\)\)/); assert.deepEqual(quoteOffsets("阿宁会更克制, 容易把话咽回去。", "克制,容易"), { start: 4, end: 10 }); assert.equal(quoteOffsets("阿宁会更克制", "放松"), null); // The report marks exactly its reading paragraphs: prose sections and the timeline's periods.