Skip to content

Commit 0c407a7

Browse files
committed
fix: containment fix for overlapping children
1 parent 9bb7799 commit 0c407a7

1 file changed

Lines changed: 10 additions & 18 deletions

File tree

src/render.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
* @module
99
*/
1010

11-
// ---------------------------------------------------------------------------
12-
// Public types
13-
// ---------------------------------------------------------------------------
14-
1511
/**
1612
* An entity together with the rendered text of its children.
1713
*
@@ -72,10 +68,6 @@ export interface RenderMapEntity<E, R> {
7268
*/
7369
export type RenderMapFn<E, R> = (ctx: RenderMapEntity<E, R>) => R;
7470

75-
// ---------------------------------------------------------------------------
76-
// Internal types
77-
// ---------------------------------------------------------------------------
78-
7971
/** @internal Node in the entity span tree used by `render`. */
8072
export type SpanNode = {
8173
start: number;
@@ -84,10 +76,6 @@ export type SpanNode = {
8476
children: SpanNode[];
8577
};
8678

87-
// ---------------------------------------------------------------------------
88-
// Tree builder
89-
// ---------------------------------------------------------------------------
90-
9179
/**
9280
* Build a tree of non-overlapping entity spans.
9381
*
@@ -121,10 +109,18 @@ export function buildSpanTree(
121109
stack.pop();
122110
}
123111

112+
// Pop parents that only partially contain this entity (one-sided overlap).
113+
// Full containment (start >= parent.start AND end <= parent.end) → child.
114+
// Partial overlap (extends past parent) → independent, try a higher parent.
115+
while (
116+
stack.length > 1 &&
117+
e.end > stack[stack.length - 1].end
118+
) {
119+
stack.pop();
120+
}
121+
124122
const parent = stack[stack.length - 1];
125123

126-
// Skip entities that overlap but aren't fully contained
127-
if (e.end > parent.end) continue;
128124
// Skip if this entity starts before the parent
129125
if (e.start < parent.start) continue;
130126

@@ -141,10 +137,6 @@ export function buildSpanTree(
141137
return root;
142138
}
143139

144-
// ---------------------------------------------------------------------------
145-
// Renderers
146-
// ---------------------------------------------------------------------------
147-
148140
/**
149141
* Recursively render a span tree node into a single string.
150142
*

0 commit comments

Comments
 (0)