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

Ensure that root flexbox node sizes are floored by their padding border #655

Merged
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
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- Content alignment (`align-content`/`justify-content`) behaviour was updated to match the latest spec (and Chrome 123+) (#635)
- Ensure that root Flexbox nodes are floored by their padding-border (#651, #655)

## 0.4.3

Expand Down
8 changes: 7 additions & 1 deletion src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ pub fn compute_flexbox_layout(tree: &mut impl LayoutPartialTree, node: NodeId, i
let aspect_ratio = style.aspect_ratio;
let min_size = style.min_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
let max_size = style.max_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
let padding = style.padding.resolve_or_zero(parent_size.width);
let border = style.border.resolve_or_zero(parent_size.width);
let padding_border_sum = padding.sum_axes() + border.sum_axes();
let clamped_style_size = if inputs.sizing_mode == SizingMode::InherentSize {
style.size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio).maybe_clamp(min_size, max_size)
} else {
Expand All @@ -172,7 +175,10 @@ pub fn compute_flexbox_layout(tree: &mut impl LayoutPartialTree, node: NodeId, i
(Some(min), Some(max)) if max <= min => Some(min),
_ => None,
});
let styled_based_known_dimensions = known_dimensions.or(min_max_definite_size).or(clamped_style_size);

// The size of the container should be floored by the padding and border
let styled_based_known_dimensions =
known_dimensions.or(min_max_definite_size.or(clamped_style_size).maybe_max(padding_border_sum));

// Short-circuit layout if the container's size is fully determined by the container's size and the run mode
// is ComputeSize (and thus the container's size is all that we're interested in)
Expand Down
17 changes: 17 additions & 0 deletions test_fixtures/flex/padding_border_overrides_size_root.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="../../scripts/gentest/test_helper.js"></script>
<link rel="stylesheet" type="text/css" href="../../scripts/gentest/test_base_style.css">
<title>
Test description
</title>
</head>
<body>

<div id="test-root" style="width: 12px; height: 12px; padding: 2px 4px 6px 8px; border-width: 1px 3px 5px 7px; border-style: solid; border-color: red;">
<div></div>
</div>

</body>
</html>
1 change: 1 addition & 0 deletions tests/generated/flex/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions tests/generated/flex/padding_border_overrides_size_root.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions tests/root_constraints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod root_constraints {
use taffy::style::AvailableSpace;
use taffy::TaffyTree;
use taffy::style_helpers::{length, TaffyMaxContent};
use taffy::{AvailableSpace, Rect, Size, Style, TaffyTree};

#[test]
fn root_with_percentage_size() {
Expand Down Expand Up @@ -78,4 +78,31 @@ mod root_constraints {
assert_eq!(layout.size.width, 200.0);
assert_eq!(layout.size.height, 200.0);
}

#[test]
fn root_padding_and_border_larger_than_definite_size() {
let mut tree: TaffyTree<()> = TaffyTree::with_capacity(16);

let child = tree.new_leaf(Style::default()).unwrap();

let root = tree
.new_with_children(
Style {
size: Size { width: length(10.0), height: length(10.0) },
padding: Rect { left: length(10.0), right: length(10.0), top: length(10.0), bottom: length(10.0) },

border: Rect { left: length(10.0), right: length(10.0), top: length(10.0), bottom: length(10.0) },
..Default::default()
},
&[child],
)
.unwrap();

tree.compute_layout(root, Size::MAX_CONTENT).unwrap();

let layout = tree.layout(root).unwrap();

assert_eq!(layout.size.width, 40.0);
assert_eq!(layout.size.height, 40.0);
}
}