Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/37664-cited-heading-drawn-text.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 34 additions & 0 deletions ui/packages/app-shell/src/views/recordSectionAnchor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, `<h1>Intro</h1><p>x</p><h2 id="cited">${drawn}</h2><p>y</p>`);
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', '<strong>Data</strong> scope'],
['inline code', '`Data` scope', '<code>Data</code> scope'],
['a link', '[Data](https://example.test/a1) scope', '<a href="https://example.test/a1">Data</a> scope'],
['an entity', 'Data &copy; 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', '<h1>Intro</h1><p>x</p><h2>Caf&eacute; &copy; terms</h2><p>y</p>', 'Caf&eacute; &copy; terms')?.id).toBe('cited');
expect(cited('legal', '<h1>Intro</h1><p>x</p><h2>Data &amp; terms</h2><p>y</p>', 'Data &amp; 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);
});
});
95 changes: 57 additions & 38 deletions ui/packages/app-shell/src/views/recordSectionAnchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -124,26 +128,35 @@ function isHeadingOpener(lower: string, at: number): boolean {
return lower.startsWith('<h', at) && level >= 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.
const lower = value.toLowerCase();
let gt = -2;
let closer = -2;
for (let at = lower.indexOf('<h'); at >= 0; at = lower.indexOf('<h', at + 1)) {
if (listed.has(at) || !isHeadingOpener(lower, at)) continue;
if (!isHeadingOpener(lower, at)) continue;
if (gt !== -1 && gt < at) gt = lower.indexOf('>', at);
if (gt < 0) break;
if (closer !== -1 && closer < gt) closer = lower.indexOf('</h', gt);
Expand All @@ -154,7 +167,7 @@ function unlistedHtmlHeadingTexts(value: string, headings: readonly BodyHeading[
break;
}
}
out.push(blankEntities(stripBodyTags(value.slice(gt + 1, end))));
out.push({ text: decodeDrawnEntities(stripBodyTags(value.slice(gt + 1, end))), start: at });
}
return out;
}
Expand Down Expand Up @@ -184,26 +197,44 @@ export function loadSectionHeadingReader(): Promise<void> {
}

/**
* 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>): 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<number, string>();
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<SectionCandidate, () => number | undefined>();
const pendingUnlisted = new WeakMap<SectionCandidate, () => 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;
}

/**
Expand All @@ -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;
Expand Down
Loading