layout: grid template getComputedStyle resolved value (#34885)

* Store taffy detailed info into fragment

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>

* Fix info propagation and resolved grid track query

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>

* Fix import

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>

* Fix tracklist matching logic and type optimization

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>

* Run fmt

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>

* Update wpt expectations

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>

* Optimizing info propagation and minor qol

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>

* Run fmt

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>

---------

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>
This commit is contained in:
Steven Novaryo 2025-01-09 18:49:27 +08:00 committed by GitHub
parent 040e29415b
commit 76fa456a9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 297 additions and 778 deletions

View file

@ -8,6 +8,7 @@ use std::sync::Arc;
use app_units::Au;
use euclid::default::{Point2D, Rect};
use euclid::{SideOffsets2D, Size2D, Vector2D};
use itertools::Itertools;
use log::warn;
use script_layout_interface::wrapper_traits::{
LayoutNode, ThreadSafeLayoutElement, ThreadSafeLayoutNode,
@ -33,11 +34,16 @@ use style::stylist::RuleInclusion;
use style::traversal::resolve_style;
use style::values::computed::Float;
use style::values::generics::font::LineHeight;
use style::values::specified::box_::DisplayInside;
use style::values::specified::GenericGridTemplateComponent;
use style_traits::{ParsingMode, ToCss};
use crate::flow::inline::construct::{TextTransformation, WhitespaceCollapse};
use crate::fragment_tree::{BoxFragment, Fragment, FragmentFlags, FragmentTree, Tag};
use crate::fragment_tree::{
BoxFragment, DetailedLayoutInfo, Fragment, FragmentFlags, FragmentTree, Tag,
};
use crate::geom::{PhysicalRect, PhysicalVec};
use crate::taffy::DetailedTaffyGridInfo;
pub fn process_content_box_request(
requested_node: OpaqueNode,
@ -184,7 +190,7 @@ pub fn process_resolved_style_request<'dom>(
return None;
}
let (content_rect, margins, padding) = match fragment {
let (content_rect, margins, padding, detailed_layout_info) = match fragment {
Fragment::Box(ref box_fragment) | Fragment::Float(ref box_fragment) => {
if style.get_box().position != Position::Static {
let resolved_insets = || {
@ -207,15 +213,34 @@ pub fn process_resolved_style_request<'dom>(
let content_rect = box_fragment.content_rect;
let margins = box_fragment.margin;
let padding = box_fragment.padding;
(content_rect, margins, padding)
let detailed_layout_info = &box_fragment.detailed_layout_info;
(content_rect, margins, padding, detailed_layout_info)
},
Fragment::Positioning(positioning_fragment) => {
let content_rect = positioning_fragment.rect;
(content_rect, SideOffsets2D::zero(), SideOffsets2D::zero())
(
content_rect,
SideOffsets2D::zero(),
SideOffsets2D::zero(),
&None,
)
},
_ => return None,
};
// https://drafts.csswg.org/css-grid/#resolved-track-list
// > The grid-template-rows and grid-template-columns properties are
// > resolved value special case properties.
//
// > When an element generates a grid container box...
if display.inside() == DisplayInside::Grid {
if let Some(DetailedLayoutInfo::Grid(info)) = detailed_layout_info {
if let Some(value) = resolve_grid_template(info, style, longhand_id) {
return Some(value);
}
}
}
// https://drafts.csswg.org/cssom/#resolved-value-special-case-property-like-height
// > If the property applies to the element or pseudo-element and the resolved value of the
// > display property is not none or contents, then the resolved value is the used value.
@ -259,6 +284,63 @@ fn resolved_size_should_be_used_value(fragment: &Fragment) -> bool {
}
}
fn resolve_grid_template(
grid_info: &DetailedTaffyGridInfo,
style: &ComputedValues,
longhand_id: LonghandId,
) -> Option<String> {
// https://drafts.csswg.org/css-grid/#resolved-track-list-standalone
fn serialize_standalone_non_subgrid_track_list(track_sizes: &[Au]) -> Option<String> {
match track_sizes.is_empty() {
// Standalone non subgrid grids with empty track lists should compute to `none`.
// As of current standard, this behaviour should only invoked by `none` computed value,
// therefore we can fallback into computed value resolving.
true => None,
// <https://drafts.csswg.org/css-grid/#resolved-track-list-standalone>
// > - Every track listed individually, whether implicitly or explicitly created,
// without using the repeat() notation.
// > - Every track size given as a length in pixels, regardless of sizing function.
// > - Adjacent line names collapsed into a single bracketed set.
// TODO: implement line names
false => Some(
track_sizes
.iter()
.map(|size| size.to_css_string())
.join(" "),
),
}
}
let (track_info, computed_value) = match longhand_id {
LonghandId::GridTemplateRows => (&grid_info.rows, &style.get_position().grid_template_rows),
LonghandId::GridTemplateColumns => (
&grid_info.columns,
&style.get_position().grid_template_columns,
),
_ => return None,
};
match computed_value {
// <https://drafts.csswg.org/css-grid/#resolved-track-list-standalone>
// > When an element generates a grid container box, the resolved value of its grid-template-rows or
// > grid-template-columns property in a standalone axis is the used value, serialized with:
GenericGridTemplateComponent::None |
GenericGridTemplateComponent::TrackList(_) |
GenericGridTemplateComponent::Masonry => {
serialize_standalone_non_subgrid_track_list(&track_info.sizes)
},
// <https://drafts.csswg.org/css-grid/#resolved-track-list-subgrid>
// > When an element generates a grid container box that is a subgrid, the resolved value of the
// > grid-template-rows and grid-template-columns properties represents the used number of columns,
// > serialized as the subgrid keyword followed by a list representing each of its lines as a
// > line name set of all the lines names explicitly defined on the subgrid (not including those
// > adopted from the parent grid), without using the repeat() notation.
// TODO: implement subgrid
GenericGridTemplateComponent::Subgrid(_) => None,
}
}
pub fn process_resolved_style_request_for_unstyled_node<'dom>(
context: &SharedStyleContext,
node: impl LayoutNode<'dom>,