Skip to content

Commit

Permalink
Release/0.2.2 (#281)
Browse files Browse the repository at this point in the history
* Fix clippy lints

* Vertical border and padding percentage values are multiplied by available_space.width (#273)

* fixed vertical padding + border calculation

* changes:
    added fix to release notes,
    added new tests in "border_and_padding.rs"

* fixed percentage border and padding heights being scaled by available_space.width

* fmt, add fix to release notes

* added test

(cherry picked from commit 996e70d)

* Revert "Resolve vertical percentage padding/border against node height"

All percentage padding/border values resolve against the node's inline size
(width in horizontal writing modes) as per the CSS spec.

See: https://developer.mozilla.org/en-US/docs/Web/CSS/padding#values

* Bump version to 0.2.2

Co-authored-by: ickshonpe <[email protected]>
  • Loading branch information
nicoburns and ickshonpe authored Dec 14, 2022
1 parent 5ce388d commit df97993
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "taffy"
version = "0.2.1"
version = "0.2.2"
authors = [
"Alice Cecile <[email protected]>",
"Johnathan Kelley <[email protected]>",
Expand Down
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## 0.2.2

### Fixes

- Border or padding on the horizontal axis could, in some cases, increase the height of nodes.

## 0.2.1

### Fixes
Expand Down
7 changes: 3 additions & 4 deletions benches/big_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ fn build_yoga_deep_hierarchy(taffy: &mut Taffy, node_count: u32, branching_facto
flex_grow: 1.0,
..Default::default()
};
let mut build_leaf_node = |taffy: &mut Taffy| taffy.new_leaf(style.clone()).unwrap();
let mut build_leaf_node = |taffy: &mut Taffy| taffy.new_leaf(style).unwrap();
let mut build_flex_node =
|taffy: &mut Taffy, children: Vec<Node>| taffy.new_with_children(style.clone(), &children).unwrap();
|taffy: &mut Taffy, children: Vec<Node>| taffy.new_with_children(style, &children).unwrap();

let tree = build_deep_tree(taffy, node_count, branching_factor, &mut build_leaf_node, &mut build_flex_node);
let root = taffy.new_with_children(Style::DEFAULT, &tree).unwrap();

root
taffy.new_with_children(Style::DEFAULT, &tree).unwrap()
}

fn taffy_benchmarks(c: &mut Criterion) {
Expand Down
6 changes: 3 additions & 3 deletions examples/flexbox-gap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ fn main() -> Result<(), taffy::error::TaffyError> {

let child_style =
Style { size: Size { width: Dimension::Points(20.0), height: Dimension::Points(20.0) }, ..Default::default() };
let child0 = taffy.new_leaf(child_style.clone())?;
let child1 = taffy.new_leaf(child_style.clone())?;
let child2 = taffy.new_leaf(child_style.clone())?;
let child0 = taffy.new_leaf(child_style)?;
let child1 = taffy.new_leaf(child_style)?;
let child2 = taffy.new_leaf(child_style)?;

let root = taffy.new_with_children(
Style { gap: Size { width: Dimension::Points(10.0), height: Dimension::Undefined }, ..Default::default() },
Expand Down
11 changes: 4 additions & 7 deletions scripts/gentest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn generate_assertions(ident: &str, node: &json::JsonValue) -> TokenStream {
let mut c = Vec::new();
if let json::JsonValue::Array(ref value) = node["children"] {
for (idx, child) in value.iter().enumerate() {
c.push(generate_assertions(&format!("{}{}", ident, idx), child));
c.push(generate_assertions(&format!("{ident}{idx}"), child));
}
};
c.into_iter().fold(quote!(), |a, b| quote!(#a #b))
Expand Down Expand Up @@ -430,15 +430,12 @@ fn generate_node(ident: &str, node: &json::JsonValue) -> TokenStream {
let (children_body, children) = match node["children"] {
json::JsonValue::Array(ref value) => {
if !value.is_empty() {
let body = value
.iter()
.enumerate()
.map(|(i, child)| generate_node(&format!("{}{}", ident, i), child))
.collect();
let body =
value.iter().enumerate().map(|(i, child)| generate_node(&format!("{ident}{i}"), child)).collect();
let idents = value
.iter()
.enumerate()
.map(|(i, _)| Ident::new(&format!("{}{}", ident, i), Span::call_site()))
.map(|(i, _)| Ident::new(&format!("{ident}{i}"), Span::call_site()))
.collect::<Vec<_>>();
(body, quote!(&[#(#idents),*]))
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/compute/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub(crate) fn compute(
return node_size.unwrap_or(measured_size).maybe_clamp(node_min_size, node_max_size);
}

// Note: both horizontal and vertical percentage padding/borders are resolved against the container's inline size (i.e. width).
// This is not a bug, but is how CSS is specified (see: https://developer.mozilla.org/en-US/docs/Web/CSS/padding#values)
let padding = style.padding.resolve_or_default(available_space.width.into_option());
let border = style.border.resolve_or_default(available_space.width.into_option());

Expand All @@ -77,8 +79,8 @@ pub(crate) fn compute(
.maybe_clamp(node_min_size.width, node_max_size.width),
height: node_size
.height
// .unwrap_or(0.0) + padding.horizontal_axis_sum() + border.horizontal_axis_sum(), // content-box
.unwrap_or(0.0 + padding.horizontal_axis_sum() + border.horizontal_axis_sum()) // border-box
// .unwrap_or(0.0) + padding.vertical_axis_sum() + border.vertical_axis_sum(), // content-box
.unwrap_or(0.0 + padding.vertical_axis_sum() + border.vertical_axis_sum()) // border-box
.maybe_clamp(node_min_size.height, node_max_size.height),
}
}
14 changes: 6 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ pub enum TaffyError {
impl Display for TaffyError {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count } => write!(
f,
"Index (is {}) should be < child_count ({}) for parent node {:?}",
child_index, child_count, parent
),
TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count } => {
write!(f, "Index (is {child_index}) should be < child_count ({child_count}) for parent node {parent:?}")
}
TaffyError::InvalidParentNode(parent) => {
write!(f, "Parent Node {:?} is not in the Taffy instance", parent)
write!(f, "Parent Node {parent:?} is not in the Taffy instance")
}
TaffyError::InvalidChildNode(child) => write!(f, "Child Node {:?} is not in the Taffy instance", child),
TaffyError::InvalidInputNode(node) => write!(f, "Supplied Node {:?} is not in the Taffy instance", node),
TaffyError::InvalidChildNode(child) => write!(f, "Child Node {child:?} is not in the Taffy instance"),
TaffyError::InvalidInputNode(node) => write!(f, "Supplied Node {node:?} is not in the Taffy instance"),
}
}
}
Expand Down
106 changes: 106 additions & 0 deletions tests/border_and_padding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use taffy::prelude::*;

fn arr_to_rect<T: Copy>(items: [T; 4]) -> Rect<T> {
Rect { left: items[0], right: items[1], top: items[2], bottom: items[3] }
}

#[test]
fn border_on_a_single_axis_doesnt_increase_size() {
for i in 0..4 {
let mut taffy = Taffy::new();
let node = taffy
.new_leaf(Style {
border: {
let mut lengths = [Dimension::Points(0.); 4];
lengths[i] = Dimension::Points(10.);
arr_to_rect(lengths)
},
..Default::default()
})
.unwrap();

taffy
.compute_layout(
node,
Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
)
.ok();

let layout = taffy.layout(node).unwrap();
assert_eq!(layout.size.width * layout.size.height, 0.);
}
}

#[test]
fn padding_on_a_single_axis_doesnt_increase_size() {
for i in 0..4 {
let mut taffy = Taffy::new();
let node = taffy
.new_leaf(Style {
border: {
let mut lengths = [Dimension::Points(0.); 4];
lengths[i] = Dimension::Points(10.);
arr_to_rect(lengths)
},
..Default::default()
})
.unwrap();

taffy
.compute_layout(
node,
Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
)
.ok();

let layout = taffy.layout(node).unwrap();
assert_eq!(layout.size.width * layout.size.height, 0.);
}
}

#[test]
fn border_and_padding_on_a_single_axis_doesnt_increase_size() {
for i in 0..4 {
let mut taffy = Taffy::new();
let rect = {
let mut lengths = [Dimension::Points(0.); 4];
lengths[i] = Dimension::Points(10.);
arr_to_rect(lengths)
};
let node = taffy.new_leaf(Style { border: rect, padding: rect, ..Default::default() }).unwrap();

taffy
.compute_layout(
node,
Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
)
.ok();
let layout = taffy.layout(node).unwrap();
assert_eq!(layout.size.width * layout.size.height, 0.);
}
}

#[test]
fn vertical_border_and_padding_percentage_values_use_available_space_correctly() {
let mut taffy = Taffy::new();

let node = taffy
.new_leaf(Style {
padding: Rect {
left: Dimension::Percent(1.0),
top: Dimension::Percent(1.0),
right: Dimension::Points(0.),
bottom: Dimension::Points(0.),
},
..Default::default()
})
.unwrap();

taffy
.compute_layout(node, Size { width: AvailableSpace::Definite(200.0), height: AvailableSpace::Definite(100.0) })
.ok();

let layout = taffy.layout(node).unwrap();
assert_eq!(layout.size.width, 200.0);
assert_eq!(layout.size.height, 200.0);
}

0 comments on commit df97993

Please sign in to comment.