Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent infinite loop while splitting pages #2822

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 13 additions & 7 deletions packages/layout/src/steps/resolvePagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,19 @@ const splitNodes = (height, contentArea, nodes) => {

// All children are moved to the next page, it doesn't make sense to show the parent on the current page
if (child.children.length > 0 && currentChild.children.length === 0) {
const box = Object.assign({}, child.box, {
top: child.box.top - height,
});
const next = Object.assign({}, child, { box });

currentChildren.push(...futureFixedNodes);
nextChildren.push(next, ...futureNodes);
// But if the current page is empty then we can just include the parent on the current page
if (currentChildren.length === 0) {
currentChildren.push(child, ...futureFixedNodes);
nextChildren.push(...futureNodes);
} else {
const box = Object.assign({}, child.box, {
top: child.box.top - height,
});
const next = Object.assign({}, child, { box });

currentChildren.push(...futureFixedNodes);
nextChildren.push(next, ...futureNodes);
}
break;
}

Expand Down
40 changes: 40 additions & 0 deletions packages/layout/tests/steps/resolvePagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,44 @@ describe('pagination step', () => {
expect(page2.children.length).toBe(1);
expect(page2.children[0].box.height).toBe(40);
});

test('should not infinitely loop when splitting pages', async () => {
const yoga = await loadYoga();

const root = {
type: 'DOCUMENT',
yoga,
children: [
{
type: 'PAGE',
box: {},
style: {
height: 400,
},
children: [
{
type: 'VIEW',
box: {},
style: { height: 401 },
children: [
{
type: 'VIEW',
box: {},
style: {
height: 400,
},
props: { wrap: false, break: true },
},
],
},
],
},
],
};

calcLayout(root);

// If calcLayout returns then we did not hit an infinite loop
expect(true).toBe(true);
});
});