diff --git a/packages/transient-render-engine/src/flow/__tests__/collapse.test.ts b/packages/transient-render-engine/src/flow/__tests__/collapse.test.ts
index 21bf4329..944cf9f2 100644
--- a/packages/transient-render-engine/src/flow/__tests__/collapse.test.ts
+++ b/packages/transient-render-engine/src/flow/__tests__/collapse.test.ts
@@ -37,6 +37,19 @@ describe('collapse function', () => {
const ttree = makeTTree('foo bar');
expect(ttree).toMatchSnapshot();
});
+ it('should preserve boundary spaces wrapped in nested inline phrasing tags', () => {
+ const ttree = makeTTree(
+ 'foo bar'
+ );
+ const [firstSpan, secondSpan] = ttree.children;
+ expect(firstSpan.tagName).toBe('span');
+ expect(firstSpan.children).toHaveLength(2);
+ expect((firstSpan.children[0] as TTextImpl).data).toBe('foo');
+ expect((firstSpan.children[1] as TTextImpl).data).toBe(' ');
+ expect(secondSpan.tagName).toBe('span');
+ expect(secondSpan.children).toHaveLength(1);
+ expect((secondSpan.children[0] as TTextImpl).data).toBe('bar');
+ });
it('should handle nested anchors', () => {
const ttree = makeTTree(nestedHyperlinksSource);
expect(ttree).toMatchSnapshot();
diff --git a/packages/transient-render-engine/src/tree/TPhrasingCtor.ts b/packages/transient-render-engine/src/tree/TPhrasingCtor.ts
index bcf61313..5ad6bacb 100644
--- a/packages/transient-render-engine/src/tree/TPhrasingCtor.ts
+++ b/packages/transient-render-engine/src/tree/TPhrasingCtor.ts
@@ -48,8 +48,12 @@ TPhrasingCtor.prototype.collapseChildren = function collapseChildren() {
}
previous = childK;
});
- this.trimLeft();
- this.trimRight();
+ // Preserve boundary spaces for named inline wrappers (e.g. styled spans) so
+ // their parent phrasing container can collapse sibling boundaries correctly.
+ if (this.tagName === null) {
+ this.trimLeft();
+ this.trimRight();
+ }
return null;
};