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

fix: insert before fixed element #930

Merged
merged 5 commits into from
Nov 30, 2021
Merged
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions integration_tests/specs/dom/nodes/insert-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,71 @@ describe('Insert before', () => {
await snapshot();
});

it('insert before position fixed element', async () => {
let child1 = createElement('div', {
style: {
position: 'fixed',
top: '100px',
width: '100px',
height: '100px',
background: 'red'
}
});
let child2;
const container = createElement('div', {
style: {
width: '200px',
height: '200px',
background: 'yellow'
}
}, [
(child2 = createElement('div', {
style: {
position: 'fixed',
width: '100px',
height: '100px',
background: 'green'
}
}))
]);

document.body.appendChild(container);

container.insertBefore(child1, child2);

await snapshot();
});

it('insert before position absolute element', async () => {
let child1 = createElement('div', {
style: {
width: '100px',
height: '100px',
background: 'red'
}
});
let child2;
const container = createElement('div', {
style: {
width: '200px',
height: '200px',
background: 'yellow'
}
}, [
(child2 = createElement('div', {
style: {
position: 'absolute',
width: '100px',
height: '100px',
background: 'green'
}
}))
]);

document.body.appendChild(container);

container.insertBefore(child1, child2);

await snapshot();
});
});
12 changes: 11 additions & 1 deletion kraken/lib/src/dom/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,17 @@ class Element extends Node
RenderBox? afterRenderObject;
// `referenceNode` should not be null, or `referenceIndex` can only be -1.
if (referenceIndex != -1 && referenceNode.isRendererAttached) {
afterRenderObject = (referenceNode.renderer!.parentData as ContainerParentDataMixin<RenderBox>).previousSibling;
RenderBox renderer = referenceNode.renderer!;
// Renderer of referenceNode may not moved to a difference place compared to its original place
// in the dom tree due to position absolute/fixed.
// Use the renderPositionPlaceholder to get the same place as dom tree in this case.
if (renderer is RenderBoxModel) {
RenderBox? renderPositionPlaceholder = renderer.renderPositionPlaceholder;
if (renderPositionPlaceholder != null) {
renderer = renderPositionPlaceholder;
}
}
afterRenderObject = (renderer.parentData as ContainerParentDataMixin<RenderBox>).previousSibling;
}
child.attachTo(this, after: afterRenderObject);
}
Expand Down