Skip to content

Commit 59a15a9

Browse files
committed
feat: text testing framework support font-size
1 parent 29ba47c commit 59a15a9

File tree

4 files changed

+102
-8
lines changed

4 files changed

+102
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod inline;
2+
mod text;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::*;
2+
3+
#[test]
4+
fn text_with_font_size() {
5+
assert_xml!(
6+
r#"
7+
<div style="font-size: 30px;" expect_height="30">
8+
XX
9+
</div>
10+
"#,
11+
true
12+
)
13+
}
14+
15+
#[test]
16+
fn text_with_font_size_2() {
17+
assert_xml!(
18+
r#"
19+
<div style="font-size: 30px;" expect_height="30">
20+
<div style="display: inline" expect_width="60">XX</div>
21+
</div>
22+
"#,
23+
true
24+
)
25+
}

float-pigment-forest/tests/custom/css_position.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,17 @@ fn inline_as_relative() {
320320
"#
321321
)
322322
}
323+
324+
#[test]
325+
fn absolute_with_max_width() {
326+
assert_xml!(
327+
r#"
328+
<div style="position: absolute; max-width: 20px; left: 0; top: 100px;">
329+
<div style="height: 10px; width: 30px;"></div>
330+
<div style="background: blue; opacity: 0.5" expect_width="20" expect_height="32">
331+
<div style="display: inline" expect_width="16">XX</div>
332+
</div>
333+
</div>
334+
"#
335+
)
336+
}

float-pigment-forest/tests/mod.rs

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use euclid::num::Zero;
66
pub use float_pigment_css::length_num::LengthNum;
77
use float_pigment_css::{
88
parser::parse_inline_style,
9-
property::NodeProperties,
9+
property::{NodeProperties, Property, PropertyValueWithGlobal},
10+
sheet::PropertyMeta,
1011
typing::{AspectRatio, Display, Gap},
1112
};
1213
pub use float_pigment_forest::Len;
@@ -169,10 +170,6 @@ impl TextInfoBuilder {
169170
text_len: 0,
170171
})
171172
}
172-
// fn with_text(mut self, text: String) -> Self {
173-
// self.0.raw_text = text;
174-
// self
175-
// }
176173
fn with_text_len(mut self, text_len: usize) -> Self {
177174
self.0.text_len = text_len;
178175
self
@@ -181,11 +178,22 @@ impl TextInfoBuilder {
181178
self.0.font_size = font_size;
182179
self
183180
}
181+
fn set_font_size(&mut self, font_size: f32) {
182+
self.0.font_size = font_size;
183+
}
184184
fn build(self) -> TextInfo {
185185
self.0
186186
}
187187
}
188188

189+
#[inline(always)]
190+
fn convert_font_size_to_px(font_size: float_pigment_css::typing::Length) -> f32 {
191+
match font_size {
192+
float_pigment_css::typing::Length::Px(x) => x,
193+
_ => 16.,
194+
}
195+
}
196+
189197
#[inline(always)]
190198
fn prepare_measure_node(node: *mut Node, text_info: TextInfo) {
191199
let node = unsafe { &mut *node };
@@ -361,8 +369,14 @@ impl TestCtx {
361369
match cur {
362370
NodeType::Text(t) => {
363371
let node = Node::new_ptr();
364-
let text_info = TextInfoBuilder::new().with_text_len(t.text().len()).build();
365-
prepare_measure_node(node, text_info);
372+
let mut text_info_builder = TextInfoBuilder::new().with_text_len(t.text().len());
373+
if let Some(parent_node_props) = parent_node_props {
374+
let font_size = parent_node_props.font_size();
375+
if let float_pigment_css::typing::Length::Px(x) = font_size {
376+
text_info_builder.set_font_size(x);
377+
}
378+
}
379+
prepare_measure_node(node, text_info_builder.build());
366380
node
367381
}
368382
NodeType::Element(e) => {
@@ -443,8 +457,48 @@ impl TestCtx {
443457
style,
444458
float_pigment_css::parser::StyleParsingDebugMode::None,
445459
);
460+
let mut font_size_p = float_pigment_css::typing::Length::Px(16.);
461+
for pm in props.iter() {
462+
match pm {
463+
PropertyMeta::Normal { property: p } | PropertyMeta::Important { property: p } => {
464+
if let Property::FontSize(x) = p {
465+
font_size_p = x
466+
.to_inner(
467+
parent_node_props.map(|p| p.font_size()).as_ref(),
468+
float_pigment_css::typing::Length::Px(16.),
469+
true,
470+
)
471+
.unwrap();
472+
}
473+
}
474+
PropertyMeta::DebugGroup {
475+
properties,
476+
disabled,
477+
..
478+
} => {
479+
if !disabled {
480+
for p in &**properties {
481+
if let Property::FontSize(x) = p {
482+
font_size_p = x
483+
.clone()
484+
.to_inner(
485+
parent_node_props.map(|p| p.font_size()).as_ref(),
486+
float_pigment_css::typing::Length::Px(16.),
487+
true,
488+
)
489+
.unwrap();
490+
}
491+
}
492+
}
493+
}
494+
};
495+
}
446496
props.iter().for_each(|p| {
447-
p.merge_to_node_properties(node_props, parent_node_props, 16.);
497+
p.merge_to_node_properties(
498+
node_props,
499+
parent_node_props,
500+
convert_font_size_to_px(font_size_p.clone()),
501+
);
448502
match p.get_property_name().to_string().as_str() {
449503
"display" => node.set_display(node_props.display()),
450504
"box-sizing" => node.set_box_sizing(node_props.box_sizing()),

0 commit comments

Comments
 (0)