diff --git a/.changeset/37664-cited-heading-drawn-text.md b/.changeset/37664-cited-heading-drawn-text.md new file mode 100644 index 00000000000..375e2a4a24d --- /dev/null +++ b/.changeset/37664-cited-heading-drawn-text.md @@ -0,0 +1,10 @@ +--- +'@object-ui/app-shell': patch +--- + +fix(app-shell): a cited section whose heading carries a link or an entity opens at that heading + +Clause-②: no +Metadata-protocol: none + +The record page finds a cited section (`?section=ANCHOR`) by reading the body outline's heading and the drawn heading the same way. The outline lists a markdown heading as its source line, so a heading with a link (`## [Terms](https://example.com) scope`) or an entity (`©`) read differently from the text the face draws, and the page opened the field instead of the section. The page now reads each listed heading as its face draws it: a markdown heading through the face's own parse, found by where it starts, and an html heading with every entity decoded. Bold, inline code and escaped punctuation already matched and still do. The anchor and the outline are unchanged. diff --git a/ui/packages/app-shell/src/views/recordSectionAnchor.test.tsx b/ui/packages/app-shell/src/views/recordSectionAnchor.test.tsx index a25b0ba7a01..d7e8674ac3e 100644 --- a/ui/packages/app-shell/src/views/recordSectionAnchor.test.tsx +++ b/ui/packages/app-shell/src/views/recordSectionAnchor.test.tsx @@ -392,3 +392,37 @@ describe('objectstack#37365 — focusRecordSection', () => { expect(a.getAttribute(RECORD_SECTION_TARGET_ATTR)).toBe('true'); }); }); + +describe('objectstack#37664 — a cited heading is matched by the text its face draws, not its source', () => { + // The outline lists a markdown heading as its source line and an html one + // with six entities decoded; the page reads the drawn heading's text. Each + // body below cites its second heading, drawn as the face draws it. + const cited = (field: 'content' | 'legal', value: string, drawn: string): HTMLElement | undefined => { + const anchor = headingsOf(value, field === 'legal')[1].anchor; + document.body.innerHTML = row(field, `

Intro

x

${drawn}

y

`); + return locateSection(document.body, sectionCandidates(FIELDS, { [field]: value }, anchor))?.heading; + }; + const markdown = (heading: string): string => ['# Intro', '', 'x', '', `## ${heading}`, '', 'y'].join('\n'); + + it.each([ + ['bold', '**Data** scope', 'Data scope'], + ['inline code', '`Data` scope', 'Data scope'], + ['a link', '[Data](https://example.test/a1) scope', 'Data scope'], + ['an entity', 'Data © 2026 scope', 'Data © 2026 scope'], + ['an escape', 'Data \\*1\\* scope', 'Data *1* scope'], + ['CONTROL: plain text', 'Data scope', 'Data scope'], + ])('a markdown heading with %s opens its section', (_, source, drawn) => { + expect(cited('content', markdown(source), drawn)?.id).toBe('cited'); + }); + + it('an html heading with an entity the outline leaves encoded opens its section; CONTROL: one the outline decodes still does', () => { + expect(cited('legal', '

Intro

x

Café © terms

y

', 'Café © terms')?.id).toBe('cited'); + expect(cited('legal', '

Intro

x

Data & terms

y

', 'Data & terms')?.id).toBe('cited'); + }); + + it('a link heading drawn as the cited title counts as listed, not as an unlisted same-titled heading; CONTROL: a setext one still counts', () => { + const value = ['# [Scope](https://example.test/s)', '', 'x', '', '## Scope', '', 'y'].join('\n'); + expect(sectionCandidates(FIELDS, { content: value }, 'scope')[0]?.unlisted).toBe(0); + expect(sectionCandidates(FIELDS, { content: `Scope\n=====\n\n${value}` }, 'scope')[0]?.unlisted).toBe(1); + }); +}); diff --git a/ui/packages/app-shell/src/views/recordSectionAnchor.ts b/ui/packages/app-shell/src/views/recordSectionAnchor.ts index c5fa5fb7134..6a1f031d463 100644 --- a/ui/packages/app-shell/src/views/recordSectionAnchor.ts +++ b/ui/packages/app-shell/src/views/recordSectionAnchor.ts @@ -19,7 +19,11 @@ * * The heading element is then found in that field's drawn face: among the * field's heading elements, the ones whose letters and digits read the same as - * the outline's heading, counted in document order. Counting by reading rather + * the outline's heading, counted in document order. The outline's heading is + * read as the face draws it, not as its source spells it: a markdown heading's + * text is the face's own parse of it (found by where it starts), so a link, + * an entity or inline markup reads as drawn, and an html heading's text has + * every entity decoded, as the browser draws it. Counting by reading rather * than by bare position keeps a heading of another title that the outline does * not list (a setext heading, an empty one, one inside a quote) from shifting * every later one onto the wrong section. The count is trusted only when the @@ -85,7 +89,7 @@ type SectionFace = 'markdown' | 'html' | 'text'; export interface SectionCandidate { field: string; face: SectionFace; - /** The heading's letters and digits, lowercased: how a drawn heading is matched. */ + /** The heading's letters and digits as its face draws them, lowercased: how a drawn heading is matched. */ reading: string; /** How many of the field's earlier headings read the same. */ ordinal: number; @@ -124,18 +128,27 @@ function isHeadingOpener(lower: string, at: number): boolean { return lower.startsWith('= 1 && level <= 6 && !/^[a-z0-9_]$/.test(lower[at + 3] ?? ''); } -/** The entities the spec's outline decodes, each one a character no reading keeps. */ -function blankEntities(text: string): string { - return text.replace(/&(?:nbsp|lt|gt|quot|amp|#39);/g, ' '); +/** + * `text` with its character references decoded as the browser decodes them + * where it draws html text. Without a document (no DOM), the text is answered + * as it is. + */ +function decodeDrawnEntities(text: string): string { + if (!text.includes('&') || typeof document === 'undefined') return text; + // A textarea's content is parsed as text with references: no element is made and nothing runs. + const area = document.createElement('textarea'); + area.innerHTML = text; + return area.value; } /** - * The texts of every heading tag in an html `value` that the outline - * (`headings`) does not list: one left unclosed, or opened inside another. + * Every heading tag of an html `value` with its drawn text and where it + * starts: the outline's headings among them, and those it does not list (one + * left unclosed, or opened inside another). A heading ends at the first + * heading closer or opener after it, as the browser ends it. */ -function unlistedHtmlHeadingTexts(value: string, headings: readonly BodyHeading[]): string[] { - const listed = new Set(headings.map((h) => h.start)); - const out: string[] = []; +function drawnHtmlHeadings(value: string): DrawnMarkdownHeading[] { + const out: DrawnMarkdownHeading[] = []; { // Every search remembers where it landed and starts no earlier than an // earlier one, so the scan stays linear however many openers are unclosed. @@ -143,7 +156,7 @@ function unlistedHtmlHeadingTexts(value: string, headings: readonly BodyHeading[ let gt = -2; let closer = -2; for (let at = lower.indexOf('= 0; at = lower.indexOf('', at); if (gt < 0) break; if (closer !== -1 && closer < gt) closer = lower.indexOf(' { } /** - * How many headings reading `reading` the markdown `value` draws that are not - * the outline's headings of that reading (`listedAt`, their starts), or - * `undefined` while the reader is not loaded. + * The candidate `field` is, for its heading `at` of `headings`: each heading + * read as `face` draws it, matched to the drawn heading that starts where it + * does (the source's reading when none does: a heading the face does not draw). + * A markdown body is read only once the reader is loaded; until then the + * source is read and the unlisted count is `undefined`. */ -function unlistedMarkdownCount(value: string, reading: string, listedAt: ReadonlySet): number | undefined { - if (!readDrawnMarkdown) return undefined; - return readDrawnMarkdown(value).filter( - (h) => headingReading(h.text) === reading && (h.start === undefined || !listedAt.has(h.start)), - ).length; +function candidateOf( + field: string, + face: SectionFace, + value: string, + headings: readonly BodyHeading[], + at: number, +): SectionCandidate { + const drawn = face === 'html' ? drawnHtmlHeadings(value) : face === 'markdown' ? readDrawnMarkdown?.(value) : []; + const drawnAt = new Map(); + for (const h of drawn ?? []) if (h.start !== undefined && !drawnAt.has(h.start)) drawnAt.set(h.start, h.text); + const readings = headings.map((h) => headingReading(drawnAt.get(h.start) ?? h.heading)); + const reading = readings[at]; + const ordinal = readings.slice(0, at).filter((r) => r === reading).length; + const count = readings.filter((r) => r === reading).length; + const listed = new Set(headings.map((h) => h.start)); + const unlisted = + face === 'text' || reading === '' + ? 0 + : drawn?.filter((h) => headingReading(h.text) === reading && (h.start === undefined || !listed.has(h.start))) + .length; + return { field, face, reading, ordinal, count, unlisted }; } /** A markdown candidate counted before the reader loaded: how to count it once it has. */ -const pendingUnlisted = new WeakMap number | undefined>(); +const pendingUnlisted = new WeakMap SectionCandidate>(); -/** `candidate`, with its unlisted count read now when it was pending. */ +/** `candidate`, counted again now when it was pending. */ function withUnlistedCount(candidate: SectionCandidate): SectionCandidate { if (candidate.unlisted !== undefined) return candidate; - const unlisted = pendingUnlisted.get(candidate)?.(); + const counted = pendingUnlisted.get(candidate)?.(); // Still unread (the reader did not load): the same candidate, so a later read finds its count. - return unlisted === undefined ? candidate : { ...candidate, unlisted }; + return counted === undefined || counted.unlisted === undefined ? candidate : counted; } /** @@ -224,20 +255,8 @@ export function sectionCandidates( const headings = headingsOf(value, face === 'html'); const at = headings.findIndex((h) => h.anchor === anchor); if (at < 0) continue; - const reading = headingReading(headings[at].heading); - const readings = headings.map((h) => headingReading(h.heading)); - const ordinal = readings.slice(0, at).filter((r) => r === reading).length; - const count = readings.filter((r) => r === reading).length; - const listedAt = new Set(headings.filter((_, i) => readings[i] === reading).map((h) => h.start)); - const countMarkdown = (): number | undefined => unlistedMarkdownCount(value, reading, listedAt); - const unlisted = - face === 'text' || reading === '' - ? 0 - : face === 'html' - ? unlistedHtmlHeadingTexts(value, headings).filter((t) => headingReading(t) === reading).length - : countMarkdown(); - const candidate: SectionCandidate = { field, face, reading, ordinal, count, unlisted }; - if (unlisted === undefined) pendingUnlisted.set(candidate, countMarkdown); + const candidate = candidateOf(field, face, value, headings, at); + if (candidate.unlisted === undefined) pendingUnlisted.set(candidate, () => candidateOf(field, face, value, headings, at)); out.push(candidate); } return out;