fix: contextual spacing reaches across a list's nesting

"No space between paragraphs of the same style" stopped at the item a nested
list opens with: its own list-item has no previous sibling, so the walk gave up
and the space stayed. Every step of a nested procedure was ~2.5mm lower than
LibreOffice draws it, accumulating down the page. The walk climbs out of the
item and the list now, taking the nearest paragraph from whichever end it
arrives at. Fixture 85 → 68; what is left there is a line pitch of its own,
which without the extra space now costs the file a page (47 against 48).
This commit is contained in:
Steffen Becker committed 2026-09-04 19:30:09 +02:00
1 parent 5059e971ed
commit 586ff300aa
1 file changed
+22 -7
+22 -7
View File
@@ -1912,16 +1912,31 @@ function applyContextualSpacing(el: Element, styleName: string | null, attrs: Re
}
// The paragraph before or after this one in the flow: its own sibling, or — in a list,
// where each item wraps its paragraphs — the neighbouring item's nearest one.
// where each item wraps its paragraphs and a deeper list wraps them again — the
// neighbouring item's nearest one, climbing out of the item a nested list opens with.
function flowSibling(el: Element, dir: 'previous' | 'next'): Element | null {
const isPara = (e: Element) => e.namespaceURI === NS.text && (e.localName === 'p' || e.localName === 'h');
const step = (e: Element) => (dir === 'previous' ? e.previousElementSibling : e.nextElementSibling);
for (let s = step(el); s; s = step(s)) if (isPara(s)) return s;
const item = el.parentElement;
if (!item || item.namespaceURI !== NS.text || item.localName !== 'list-item') return null;
for (let s = step(item); s; s = step(s)) {
const kids = Array.from(s.children).filter(isPara);
if (kids.length) return dir === 'previous' ? kids[kids.length - 1] : kids[0];
// The nearest paragraph inside a list or item, from whichever end the walk arrives at.
const inside = (e: Element): Element | null => {
if (isPara(e)) return e;
const kids = Array.from(e.children);
for (const k of dir === 'previous' ? kids.reverse() : kids) {
const found = inside(k);
if (found) return found;
}
return null;
};
for (let node: Element | null = el; node; node = node.parentElement) {
for (let s = step(node); s; s = step(s)) {
const found = inside(s);
if (found) return found;
}
const parent = node.parentElement;
const wrapper = parent && parent.namespaceURI === NS.text
&& (parent.localName === 'list-item' || parent.localName === 'list'
|| parent.localName === 'list-header');
if (!wrapper) return null;
}
return null;
}