Skip to content

Commit ec4ee32

Browse files
committed
avoid memory leaking (ish) the dom tree
this was happening in tests, where generate in a bench() was causing the dom tree to explode
1 parent a399d78 commit ec4ee32

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/layout-flow.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ function mapTree(
15921592
if (!bail) path.pop();
15931593
const end = text.value.length;
15941594
const box = new Inline(start, end, el.style, children, 0);
1595-
el.boxes.push(box);
1595+
el.boxes.splice(0, el.boxes.length, box);
15961596

15971597
return [bail, box];
15981598
}
@@ -1627,7 +1627,7 @@ function wrapInBlockContainer(parentEl: HTMLElement, inlines: InlineLevel[], tex
16271627
function generateBlockBox(el: HTMLElement): BlockLevel {
16281628
if (el.tagName === 'img') {
16291629
const box = new ReplacedBox(el.style, el.attrs.src ?? "");
1630-
el.boxes.push(box);
1630+
el.boxes.splice(0, el.boxes.length, box);
16311631
return box;
16321632
} else {
16331633
return generateBlockContainer(el);
@@ -1732,6 +1732,6 @@ export function generateBlockContainer(el: HTMLElement): BlockContainer {
17321732
box = new BlockContainerOfBlocks(el.style, blocks, attrs);
17331733
}
17341734

1735-
el.boxes.push(box);
1735+
el.boxes.splice(0, el.boxes.length, box);
17361736
return box;
17371737
}

test/api.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ describe('Hyperscript API', function () {
148148
expect(ifc.lineboxes).to.have.lengthOf(4);
149149
unregisterFontAsset('Arimo/Arimo-Regular.ttf');
150150
});
151+
152+
it('doesn\'t accumulate 2 boxes after generating twice', function () {
153+
const el = flow.dom([flow.h('div'), flow.h('span'), flow.h('img')]);
154+
flow.generate(el);
155+
expect(el.query('div').boxes).to.have.lengthOf(1);
156+
expect(el.query('span').boxes).to.have.lengthOf(1);
157+
expect(el.query('img').boxes).to.have.lengthOf(1);
158+
flow.generate(el);
159+
expect(el.query('div').boxes).to.have.lengthOf(1);
160+
expect(el.query('span').boxes).to.have.lengthOf(1);
161+
expect(el.query('img').boxes).to.have.lengthOf(1);
162+
});
151163
});
152164

153165
describe('Parse API', function () {

0 commit comments

Comments
 (0)