Skip to content

Truly independent web browser

LibWeb: Size monospace text against its own 13px medium size

sideshowbarkerbf2eb3267557
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp
index 9e7881426c5..98216b644a2 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -533,6 +533,7 @@ NonnullOwnPtr<StyleComputer::AnimationSample> StyleComputer::begin_animation_sam
},
.is_th_element = false,
.has_new_font_size = false,
+ .new_font_size_includes_cascaded_value = false,
.has_tree_counting_context = false,
.sibling_count = 0,
.sibling_index = 0,
diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp
index d523ab91798..2623e66790b 100644
--- a/Libraries/LibWeb/Editing/Commands.cpp
+++ b/Libraries/LibWeb/Editing/Commands.cpp
@@ -820,13 +820,13 @@ Utf16String command_font_size_value(DOM::Document const& document)
// the active range, or if there is no such node, the effective command value of the active range's start node,
// in either case interpreted as a number of pixels.
auto first_formattable_node = first_formattable_node_effectively_contained(range);
- auto value = effective_command_value(
- first_formattable_node ? first_formattable_node : *range->start_container(),
- CommandNames::fontSize);
- auto pixel_size = font_size_to_pixel_size(value.value_or({}));
+ GC::Ref<DOM::Node> node = first_formattable_node ? *first_formattable_node : *range->start_container();
+ auto value = effective_command_value(node, CommandNames::fontSize);
+ auto medium_font_size = medium_font_size_at(node);
+ auto pixel_size = font_size_to_pixel_size(value.value_or({}), medium_font_size);
// 3. Return the legacy font size for pixel size.
- return legacy_font_size(pixel_size.to_int());
+ return legacy_font_size(pixel_size.to_int(), medium_font_size);
}
// https://w3c.github.io/editing/docs/execCommand/#the-forecolor-command
diff --git a/Libraries/LibWeb/Editing/ExecCommand.cpp b/Libraries/LibWeb/Editing/ExecCommand.cpp
index 9ec30d227ca..d0509432dc5 100644
--- a/Libraries/LibWeb/Editing/ExecCommand.cpp
+++ b/Libraries/LibWeb/Editing/ExecCommand.cpp
@@ -7,6 +7,7 @@
#include <AK/AnyOf.h>
#include <AK/ScopeGuard.h>
#include <AK/TemporaryChange.h>
+#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/EditingHostManager.h>
#include <LibWeb/DOM/ElementFactory.h>
@@ -774,8 +775,11 @@ WebIDL::ExceptionOr<Utf16String> Document::query_command_value(Utf16FlyString co
// 2. If command is "fontSize" and its value override is set, convert the value override to an
// integer number of pixels and return the legacy font size for the result.
if (command_definition.command == Editing::CommandNames::fontSize && value_override.has_value()) {
- auto pixel_size = Editing::font_size_to_pixel_size(value_override.release_value());
- return Editing::legacy_font_size(pixel_size.to_int());
+ // NB: The sizes are taken where the active range starts, as the effective command value is read there.
+ auto range = Editing::active_range(*this);
+ auto medium_font_size = range ? Editing::medium_font_size_at(*range->start_container()) : CSS::StyleComputer::default_user_font_size();
+ auto pixel_size = Editing::font_size_to_pixel_size(value_override.release_value(), medium_font_size);
+ return Editing::legacy_font_size(pixel_size.to_int(), medium_font_size);
}
// 3. If the value override for command is set, return it.
diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
index 04180f54d31..6d65414d9d5 100644
--- a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
+++ b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp
@@ -2096,7 +2096,7 @@ void force_the_value(GC::Ref<DOM::Node> node, Utf16FlyString const& command, Opt
[&](GC::Ref<DOM::Node> sibling) {
return is_simple_modifiable_element(sibling)
&& values_are_equivalent(command, optional_utf16_view(specified_command_value(as<DOM::Element>(*sibling), command)), new_value)
- && values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(sibling, command)), new_value);
+ && values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(sibling, command)), new_value, *sibling);
},
[] -> GC::Ptr<DOM::Node> { return {}; });
}
@@ -2106,7 +2106,7 @@ void force_the_value(GC::Ref<DOM::Node> node, Utf16FlyString const& command, Opt
return;
// 6. If the effective command value of command is loosely equivalent to new value on node, abort this algorithm.
- if (values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(node, command)), new_value))
+ if (values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(node, command)), new_value, *node))
return;
// 7. If node is not an allowed child of "span":
@@ -2135,7 +2135,7 @@ void force_the_value(GC::Ref<DOM::Node> node, Utf16FlyString const& command, Opt
}
// 8. If the effective command value of command is loosely equivalent to new value on node, abort this algorithm.
- if (values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(node, command)), new_value))
+ if (values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(node, command)), new_value, *node))
return;
// 9. Let new parent be null.
@@ -2249,7 +2249,7 @@ void force_the_value(GC::Ref<DOM::Node> node, Utf16FlyString const& command, Opt
// 17. If the effective command value of command for new parent is not loosely equivalent to new value, and the
// relevant CSS property for command is not null, set that CSS property of new parent to new value (if the new
// value would be valid).
- if (!values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(new_parent, command)), new_value)) {
+ if (!values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(new_parent, command)), new_value, *new_parent)) {
auto const& command_definition = find_command_definition(command);
if (command_definition->relevant_css_property.has_value()) {
auto inline_style = new_parent->style();
@@ -2280,7 +2280,7 @@ void force_the_value(GC::Ref<DOM::Node> node, Utf16FlyString const& command, Opt
// 21. If node is an Element and the effective command value of command for node is not loosely equivalent to new
// value:
if (is<DOM::Element>(*node)
- && !values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(node, command)), new_value)) {
+ && !values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(node, command)), new_value, *node)) {
// 1. Insert node into the parent of new parent before new parent, preserving ranges.
move_node_preserving_ranges(node, *new_parent->parent(), new_parent->index());
@@ -3538,7 +3538,7 @@ DOM::BoundaryPoint last_equivalent_point(DOM::BoundaryPoint boundary_point)
}
// https://w3c.github.io/editing/docs/execCommand/#legacy-font-size-for
-Utf16String legacy_font_size(int pixel_size)
+Utf16String legacy_font_size(int pixel_size, CSSPixels medium_font_size)
{
// 1. Let returned size be 1.
int returned_size = 1;
@@ -3547,11 +3547,11 @@ Utf16String legacy_font_size(int pixel_size)
while (returned_size < 7) {
// 1. Let lower bound be the resolved value of "font-size" in pixels of a font element whose size attribute is
// set to returned size.
- auto lower_bound = font_size_to_pixel_size(Utf16String::number(returned_size)).to_float();
+ auto lower_bound = font_size_to_pixel_size(Utf16String::number(returned_size), medium_font_size).to_float();
// 2. Let upper bound be the resolved value of "font-size" in pixels of a font element whose size attribute is
// set to one plus returned size.
- auto upper_bound = font_size_to_pixel_size(Utf16String::number(returned_size + 1)).to_float();
+ auto upper_bound = font_size_to_pixel_size(Utf16String::number(returned_size + 1), medium_font_size).to_float();
// 3. Let average be the average of upper bound and lower bound.
auto average = (lower_bound + upper_bound) / 2;
@@ -3967,7 +3967,7 @@ void push_down_values(Utf16FlyString const& command, GC::Ref<DOM::Node> node, Op
return;
// 3. If the effective command value of command is loosely equivalent to new value on node, abort this algorithm.
- if (values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(node, command)), new_value))
+ if (values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(node, command)), new_value, *node))
return;
// 4. Let current ancestor be node's parent.
@@ -3980,7 +3980,7 @@ void push_down_values(Utf16FlyString const& command, GC::Ref<DOM::Node> node, Op
// equivalent to new value on it, append current ancestor to ancestor list, then set current ancestor to its
// parent.
while (is<DOM::Element>(current_ancestor.ptr()) && current_ancestor->is_editable()
- && !values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(current_ancestor, command)), new_value)) {
+ && !values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(current_ancestor, command)), new_value, *current_ancestor)) {
ancestor_list.append(*current_ancestor);
current_ancestor = current_ancestor->parent();
}
@@ -4008,7 +4008,7 @@ void push_down_values(Utf16FlyString const& command, GC::Ref<DOM::Node> node, Op
return display.has_value() && display->is_inline_outside() && display->is_flow_inside();
});
if (!can_replace_inline_font_size && new_value.has_value() && ancestor_list.last()->parent()
- && !values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(ancestor_list.last()->parent(), command)), new_value))
+ && !values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(ancestor_list.last()->parent(), command)), new_value, *ancestor_list.last()->parent()))
return;
// 11. While ancestor list is not empty:
@@ -4260,7 +4260,7 @@ void reorder_modifiable_descendants(GC::Ref<DOM::Node> node, Utf16FlyString cons
if (candidate == node
|| !is_simple_modifiable_element(*candidate)
|| !values_are_equivalent(command, optional_utf16_view(specified_command_value(static_cast<DOM::Element&>(*candidate), command)), new_value)
- || !values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(candidate, command)), new_value))
+ || !values_are_loosely_equivalent(command, optional_utf16_view(effective_command_value(candidate, command)), new_value, *candidate))
return;
// 4. While candidate has children, insert the first child of candidate into candidate's parent immediately before
@@ -4336,11 +4336,12 @@ void restore_states_and_values(DOM::Document& document, Vector<RecordedOverride>
&& !values_are_loosely_equivalent(
CommandNames::fontSize,
optional_utf16_view(effective_command_value(node, CommandNames::fontSize)),
- override.value.get<Utf16String>().utf16_view())))) {
+ override.value.get<Utf16String>().utf16_view(), *node)))) {
// 1. Convert override to an integer number of pixels, and set override to the legacy font size for the
// result.
- auto override_pixel_size = font_size_to_pixel_size(override.value.get<Utf16String>());
- override.value = legacy_font_size(override_pixel_size.to_int());
+ auto medium_font_size = medium_font_size_at(*node);
+ auto override_pixel_size = font_size_to_pixel_size(override.value.get<Utf16String>(), medium_font_size);
+ override.value = legacy_font_size(override_pixel_size.to_int(), medium_font_size);
// 2. Take the action for "fontSize", with value equal to override.
take_the_action_for_command(document, CommandNames::fontSize, override.value.get<Utf16String>());
@@ -5262,7 +5263,7 @@ bool values_are_equivalent(Utf16FlyString const& command, Optional<Utf16View> a,
}
// https://w3c.github.io/editing/docs/execCommand/#loosely-equivalent-values
-bool values_are_loosely_equivalent(Utf16FlyString const& command, Optional<Utf16View> a, Optional<Utf16View> b)
+bool values_are_loosely_equivalent(Utf16FlyString const& command, Optional<Utf16View> a, Optional<Utf16View> b, GC::Ref<DOM::Node> context)
{
// Two quantities are loosely equivalent values for a command if either they are equivalent values for the command,
if (values_are_equivalent(command, a, b))
@@ -5285,12 +5286,14 @@ bool values_are_loosely_equivalent(Utf16FlyString const& command, Optional<Utf16
return true;
// NB: Compare the resolved value of "font-size" with the font sizes of font elements with each size attribute.
- auto resolved_value_index = [](Utf16View quantity) -> Optional<size_t> {
+ // NB: The font elements are taken to sit where context does, so they share its font family.
+ auto medium_font_size = medium_font_size_at(context);
+ auto resolved_value_index = [&](Utf16View quantity) -> Optional<size_t> {
if (!quantity.ends_with(u"px"sv))
return {};
- auto pixel_size = font_size_to_pixel_size(quantity).to_float();
+ auto pixel_size = font_size_to_pixel_size(quantity, medium_font_size).to_float();
for (size_t index = 0; index < 7; ++index) {
- if (fabsf(font_size_to_pixel_size(Utf16String::number(index + 1)).to_float() - pixel_size) < 0.05f)
+ if (fabsf(font_size_to_pixel_size(Utf16String::number(index + 1), medium_font_size).to_float() - pixel_size) < 0.05f)
return index;
}
return {};
@@ -5511,7 +5514,19 @@ GC::Ptr<DOM::Node> first_formattable_node_effectively_contained(GC::Ptr<DOM::Ran
return node;
}
-CSSPixels font_size_to_pixel_size(Utf16View const& font_size)
+// The size of medium at node, which absolute-size keywords and legacy font sizes scale: 13px where node's font-family is
+// just monospace, as it is for every font size there, and the default font size otherwise.
+CSSPixels medium_font_size_at(GC::Ref<DOM::Node> node)
+{
+ if (auto font_family = resolved_value(node, CSS::PropertyID::FontFamily); font_family && font_family->is_value_list()) {
+ auto const& values = font_family->as_value_list().values();
+ if (values.size() == 1 && values[0]->is_keyword() && values[0]->to_keyword() == CSS::Keyword::Monospace)
+ return 13;
+ }
+ return CSS::StyleComputer::default_user_font_size();
+}
+
+CSSPixels font_size_to_pixel_size(Utf16View const& font_size, CSSPixels medium_font_size)
{
// If the font size ends in 'px', interpret the preceding as a number and return it.
if (font_size.ends_with(u"px"sv)) {
@@ -5528,7 +5543,7 @@ CSSPixels font_size_to_pixel_size(Utf16View const& font_size)
keyword = HTML::HTMLFontElement::parse_legacy_font_size(font_size);
// If that also failed, give up
- auto pixel_size = CSS::StyleComputer::default_user_font_size();
+ auto pixel_size = medium_font_size;
if (!keyword.has_value())
return pixel_size;
diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.h b/Libraries/LibWeb/Editing/Internal/Algorithms.h
index 6c66412e4cb..287ec95b0ef 100644
--- a/Libraries/LibWeb/Editing/Internal/Algorithms.h
+++ b/Libraries/LibWeb/Editing/Internal/Algorithms.h
@@ -125,7 +125,7 @@ bool is_visible_node(GC::Ref<DOM::Node>);
bool is_whitespace_node(GC::Ref<DOM::Node>);
void justify_the_selection(DOM::Document&, JustifyAlignment);
DOM::BoundaryPoint last_equivalent_point(DOM::BoundaryPoint);
-Utf16String legacy_font_size(int);
+Utf16String legacy_font_size(int, CSSPixels medium_font_size);
void move_node_preserving_ranges(GC::Ref<DOM::Node>, GC::Ref<DOM::Node> new_parent, u32 new_index);
void unwrap_node_preserving_ranges(GC::Ref<DOM::Node>);
Optional<DOM::BoundaryPoint> next_equivalent_point(DOM::BoundaryPoint);
@@ -153,7 +153,7 @@ bool standard_inline_indeterminate(DOM::Document const&, Utf16FlyString const& c
Utf16String standard_inline_value(DOM::Document const&, Utf16FlyString const& command);
void toggle_lists(DOM::Document&, Utf16FlyString const&);
bool values_are_equivalent(Utf16FlyString const&, Optional<Utf16View>, Optional<Utf16View>);
-bool values_are_loosely_equivalent(Utf16FlyString const&, Optional<Utf16View>, Optional<Utf16View>);
+bool values_are_loosely_equivalent(Utf16FlyString const&, Optional<Utf16View>, Optional<Utf16View>, GC::Ref<DOM::Node> context);
enum class PreservePrecedingLineBreak {
No,
Yes,
@@ -163,7 +163,8 @@ GC::Ptr<DOM::Node> wrap(GC::RootVector<GC::Ref<DOM::Node>>, Function<bool(GC::Re
// Utility methods:
GC::Ptr<DOM::Node> first_formattable_node_effectively_contained(GC::Ptr<DOM::Range>);
-CSSPixels font_size_to_pixel_size(Utf16View const&);
+CSSPixels font_size_to_pixel_size(Utf16View const&, CSSPixels medium_font_size);
+CSSPixels medium_font_size_at(GC::Ref<DOM::Node>);
void for_each_node_effectively_contained_in_range(GC::Ptr<DOM::Range>, Function<TraversalDecision(GC::Ref<DOM::Node>)>);
bool has_visible_children(GC::Ref<DOM::Node>);
bool is_heading(Utf16FlyString const&);
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
index 917fbd04014..059da0adc82 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
@@ -597,6 +597,7 @@ impl super::StyleEngine {
},
is_th_element: false,
has_new_font_size: false,
+ new_font_size_includes_cascaded_value: false,
has_tree_counting_context: false,
sibling_count: 0,
sibling_index: 0,
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index 57bc54dc8e5..7c793f923eb 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -1636,15 +1636,6 @@ impl StyleEngine {
document_media: Default::default(),
document_functions: Default::default(),
custom_property_registry: None,
- monospace_font_family: RetainedStyleValueData::from_owned(
- crate::css::parser::value_parser::value_list(
- vec![StyleValueData::Keyword {
- keyword: crate::css::style_compute::keyword::MONOSPACE,
- }],
- 1,
- true,
- ),
- ),
font_resolution: None,
pass_fonts: None,
computed_group_sets: Default::default(),
@@ -2544,7 +2535,6 @@ impl RetainedState {
custom_property_registry: _,
font_resolution: _,
pass_fonts: _,
- monospace_font_family: _,
computed_group_sets,
style_record_view_epoch_depth: _,
custom_property_environments: _,
diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rs
index 55d6f5ff94f..6748572db6d 100644
--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs
@@ -241,9 +241,6 @@ pub struct RetainedState {
font_resolution: Option<font_resolution::FontResolutionCache>,
/// The fonts the threads of the style pass under way resolve, where they do.
pass_fonts: Option<std::sync::Arc<font_resolution::PassFonts>>,
- /// `font-family: monospace`, the family the monospace font-size recascade resolves an
- /// ancestor's font-relative lengths against.
- monospace_font_family: RetainedStyleValueData,
/// The shared computed-group payload tuple last published for each live element. This is the
/// computed half of the eventual base style record; custom properties and metadata remain
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
index b9ad8535e37..a9054a3d7e7 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
@@ -682,7 +682,35 @@ impl RetainedState {
& (bridge::element_adjustment_fact::CHECK_INPUT_LINE_HEIGHT
| bridge::element_adjustment_fact::FORCE_LINE_HEIGHT_NORMAL)
== 0;
- let runs_drive = drives_everything || !hard.is_empty() || (builds_font && !resolves_font);
+ // NB: A font size inherited across a change from `font-family: monospace` to another family,
+ // and a keyword font size in that family, are recascaded, which only a drive does.
+ let parent_font_family_is_monospace = parent_view.as_ref().is_some_and(|view| {
+ crate::css::computed_value_views::ComputedValuesView::of(view.payloads)
+ .font()
+ .font_family
+ .data()
+ .is_some_and(crate::css::style_compute::font_family_is_monospace)
+ });
+ let declared_font_value = |property: u16| {
+ native_font
+ .iter()
+ .map(|(declared, value)| (*declared, value.optional_data()))
+ .chain(
+ easy.iter()
+ .map(|(declared, value, ..)| (*declared, value.optional_data())),
+ )
+ .find(|(declared, _)| *declared == property)
+ .and_then(|(_, value)| value)
+ };
+ let recascades_font_size = parent_font_family_is_monospace
+ && (declared_font_value(prop::FONT_FAMILY)
+ .is_some_and(|value| !crate::css::style_compute::font_family_is_monospace(value))
+ || matches!(
+ declared_font_value(prop::FONT_SIZE),
+ Some(StyleValueData::Keyword { .. })
+ ));
+ let runs_drive =
+ drives_everything || !hard.is_empty() || (builds_font && !resolves_font) || recascades_font_size;
// A drive maps a logical alias by the writing mode and direction it computes, and marks
// the pair itself.
if runs_drive {
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
index 5f53ad3ee69..3b39ad42c4b 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
@@ -45,8 +45,8 @@ impl DriveRecords for RetainedState {
}
}
-/// What the monospace font-size recascade answers for a drive target.
-enum MonospaceRecascade {
+/// What the font-size recascade answers for a drive target.
+enum FontSizeRecascade {
/// The font size the recascade reaches, in pixels, and whether reaching it read the viewport.
Size(f64, bool),
/// A length in the ancestor chain resolves against the monospace font at the size reached so
@@ -61,12 +61,15 @@ pub(in crate::css::style) struct DrivenValues {
}
impl RetainedState {
- fn monospace_recascaded_font_size_with(
+ /// The font size a drive target inherits when its ancestors' font sizes are recascaded against
+ /// `default_size_raw`, the medium size of the generic family the target's own family falls in.
+ fn recascaded_font_size_with(
&self,
records: &impl DriveRecords,
target: computed::ComputedStyleTarget,
inputs: &bridge::FfiDocumentStyleComputationInputs,
- ) -> MonospaceRecascade {
+ default_size_raw: i32,
+ ) -> FontSizeRecascade {
use crate::css::css_pixels::CssPixels;
use crate::css::style_compute::{
FfiFontMetrics, FfiLengthResolutionContext, FontSizeRecascadeDocumentInputs, FontSizeRecascadeStatus,
@@ -103,20 +106,27 @@ impl RetainedState {
.as_ref()
.and_then(|view| view.longhands().raw_cascaded_font_size())
};
- let default_size = CssPixels::from_integer(13).raw_value();
+ let default_size = default_size_raw;
let document_inputs = FontSizeRecascadeDocumentInputs::from_document(inputs);
- // NB: The size an ancestor's animations give it wins over what the cascade of its ancestors decides, so the
+ // NB: The size an ancestor's animations give it wins over what the cascade of its ancestors decides, and so
+ // does a calc() size other than a percentage alone, which carries no keyword through to recompute. So the
// recascade goes on from that ancestor's computed size.
- let animated = views.iter().enumerate().rev().find_map(|(index, (_, view))| {
+ let restart = views.iter().enumerate().rev().find_map(|(index, (_, view))| {
let view = view.as_ref()?;
- view.longhands()
+ let animated = view
+ .longhands()
.overlay_value(
view.animated_overlay,
crate::css::property_metadata::property_id::FONT_SIZE,
)
- .map(|_| (index, view))
+ .is_some();
+ let calculated = view.longhands().raw_cascaded_font_size().is_some_and(|value| {
+ matches!(value, StyleValueData::Calculated { .. })
+ && !crate::css::style_compute::calculated_font_size_is_percentage(value)
+ });
+ (animated || calculated).then_some((index, view))
});
- let (start_index, start_size, start_depends_on_viewport) = animated.map_or(
+ let (start_index, start_size, start_depends_on_viewport) = restart.map_or(
(0, CssPixels::from_raw(default_size).to_double(), false),
|(index, view)| {
(
@@ -154,18 +164,56 @@ impl RetainedState {
} else {
zoom
};
- // AD-HOC: C++ measures the platform's monospace font directly, where this resolves the
- // generic `monospace` family like any other request. The two differ only where
- // an @font-face takes the platform monospace font's name.
+ // The ancestor's font-relative size resolves against its parent's font, or the initial one for the document
+ // element, at the size the recascade reached for that parent: its family, weight, width and style.
+ let parent_font = index
+ .checked_sub(1)
+ .and_then(|parent| views[parent].1.as_ref())
+ .map(|view| {
+ // SAFETY: The view's font group is live for the drive.
+ unsafe {
+ view.payloads[group_index::FONT]
+ .cast::<crate::css::computed_value_types::FontValues>()
+ .deref()
+ }
+ });
+ let parent_font_family = parent_font.map_or_else(
+ || {
+ crate::css::style_compute::initial_value_handle(
+ crate::css::property_metadata::property_id::FONT_FAMILY,
+ )
+ .pointer()
+ .cast::<std::ffi::c_void>()
+ },
+ |font| font.font_family.pointer,
+ );
+ let (parent_font_slope, parent_font_oblique_angle) =
+ match parent_font.and_then(|font| font.font_style.style_value()) {
+ Some(StyleValueData::FontStyle {
+ font_style,
+ angle_value,
+ }) => match *font_style {
+ crate::css::css_enums::font_style_keyword::ITALIC => (1, 14.0),
+ crate::css::css_enums::font_style_keyword::OBLIQUE => (
+ 2,
+ angle_value
+ .optional_data()
+ .and_then(crate::css::table_group_builder::angle_degrees)
+ .unwrap_or(14.0),
+ ),
+ _ => (0, 14.0),
+ },
+ _ => (0, 14.0),
+ };
let request = bridge::FfiFontResolutionRequest {
- font_family: bridge::FfiHostHandle::from_pointer(self.monospace_font_family.pointer().cast()),
+ font_family: bridge::FfiHostHandle::from_pointer(parent_font_family),
font_feature_values: [bridge::FfiHostHandle::default(); bridge::FONT_RESOLUTION_FEATURE_INPUT_COUNT],
font_size_raw: CssPixels::nearest_value_for(inherited_size).raw_value(),
- font_slope: 0,
- font_weight: 400.0,
- font_width: 100.0,
+ font_slope: parent_font_slope,
+ font_weight: parent_font.map_or(400.0, |font| font.font_weight),
+ font_width: parent_font.map_or(100.0, |font| font.font_width),
font_optical_sizing: 0,
- font_oblique_angle: 14.0,
+ font_oblique_angle: parent_font_oblique_angle,
font_synthesis: 0,
font_size_adjust: bridge::FontSizeAdjustKind::None,
font_size_adjust_metric: 0,
@@ -174,7 +222,7 @@ impl RetainedState {
font_environment_generation: inputs.font_environment_generation,
};
let Some(resolved) = self.resolved_font(request) else {
- return MonospaceRecascade::AwaitsFont(request);
+ return FontSizeRecascade::AwaitsFont(request);
};
// NB: The ancestor's own font is the one being recascaded; its line height is the one it
// inherits, or the initial font's normal line height when there is no parent.
@@ -246,7 +294,7 @@ impl RetainedState {
"the context resolves every length a cascaded font-size holds"
);
}
- MonospaceRecascade::Size(batch.current_size, batch.depends_on_viewport_metrics)
+ FontSizeRecascade::Size(batch.current_size, batch.depends_on_viewport_metrics)
}
/// The tree scope whose `@font-feature-values` an element's `font-variant-alternates` names
@@ -364,6 +412,7 @@ impl RetainedState {
.then(|| self.container_unit_bases_with(records, subject.target.node(), subject.target.is_pseudo()));
let document_base_url = &self.document_resource_contexts.document_base_url;
let store = store.view(self);
+ use crate::css::css_pixels::CssPixels;
use crate::css::property_metadata::property_id as prop;
use crate::css::style_compute::{
FfiEffectiveColorSchemeInput, FfiFontMetrics, FfiInputLineHeightMetrics, FfiLengthResolutionContext,
@@ -377,20 +426,52 @@ impl RetainedState {
let DriveSubject { target, parent, facts } = subject;
let has = |bit: u32| facts & bit != 0;
let is_document_element = has(fact::IS_DOCUMENT_ELEMENT);
- // HACK: A cascade that ends in `font-family: monospace` re-runs the font-size cascade over
- // the whole ancestor chain against a 13px default instead of the 16px one, which
- // changes what a keyword size an ancestor declared means.
+ // HACK: Where an element's font-family is just `monospace`, a keyword font size means 13px
+ // at medium rather than the default size. So the size an element inherits across a
+ // change between that family and any other is recascaded over the ancestor chain,
+ // against the medium size of the element's own family: Each keyword size an ancestor
+ // declared, and each size relative to one, is computed again. And the element's own
+ // size, where it's relative to the one it inherits, is computed from that result.
+ let parent_font_family_is_monospace = parent
+ .and_then(|parent| records.assigned_record_view(parent))
+ .is_some_and(|view| {
+ let font = unsafe {
+ view.payloads[group_index::FONT]
+ .cast::<crate::css::computed_value_types::FontValues>()
+ .deref()
+ };
+ font.font_family.data().is_some_and(font_family_is_monospace)
+ });
+ let font_family_is_monospace = match store.winning_declaration(prop::FONT_FAMILY) {
+ Some((value, ..)) => match value.data() {
+ StyleValueData::Keyword { keyword }
+ if *keyword == crate::css::style_compute::keyword::INHERIT
+ || *keyword == crate::css::style_compute::keyword::UNSET =>
+ {
+ parent_font_family_is_monospace
+ }
+ value => font_family_is_monospace(value),
+ },
+ None => parent_font_family_is_monospace,
+ };
+ let medium_font_size_raw = if font_family_is_monospace {
+ CssPixels::from_integer(13).raw_value()
+ } else {
+ inputs.default_font_size_raw
+ };
let mut recascaded_font_size_reads_viewport = false;
- let recascaded_font_size = if store
- .winning_declaration(prop::FONT_FAMILY)
- .is_some_and(|(value, ..)| font_family_is_monospace(value.data()))
- {
- match self.monospace_recascaded_font_size_with(records, target, inputs) {
- MonospaceRecascade::Size(recascaded, reads_viewport) => {
+ let mut recascaded_font_size_includes_cascaded_value = false;
+ let recascaded_font_size = if font_family_is_monospace != parent_font_family_is_monospace {
+ match self.recascaded_font_size_with(records, target, inputs, medium_font_size_raw) {
+ FontSizeRecascade::Size(recascaded, reads_viewport) => {
recascaded_font_size_reads_viewport = reads_viewport;
- Some(recascaded)
+ let own_size = store.winning_declaration(prop::FONT_SIZE).and_then(|(value, ..)| {
+ crate::css::style_compute::font_size_relative_to_inherited_size(value.data(), recascaded)
+ });
+ recascaded_font_size_includes_cascaded_value = own_size.is_some();
+ Some(own_size.unwrap_or(recascaded))
}
- MonospaceRecascade::AwaitsFont(request) => {
+ FontSizeRecascade::AwaitsFont(request) => {
*suspended_font = Some(font_resolution::FontRequest::new(request));
return None;
}
@@ -502,6 +583,7 @@ impl RetainedState {
},
is_th_element: has(fact::IS_TH),
has_new_font_size: recascaded_font_size.is_some(),
+ new_font_size_includes_cascaded_value: recascaded_font_size_includes_cascaded_value,
has_tree_counting_context: sibling_position.is_some(),
sibling_count: sibling_position.map_or(0, |position| u64::from(position.count)),
sibling_index: sibling_position.map_or(0, |position| u64::from(position.index)),
@@ -513,7 +595,7 @@ impl RetainedState {
style_sheet_resource_context_count: resource_contexts.len(),
device_pixels_per_css_pixel: inputs.device_pixels_per_css_pixel,
initial_font_size_raw: inputs.initial_font_size_raw,
- default_font_size_raw: inputs.default_font_size_raw,
+ default_font_size_raw: medium_font_size_raw,
root_effective_zoom: inputs.root_effective_zoom,
};
// The root metrics a phase resolves against: the document's, except for the document
@@ -588,9 +670,9 @@ impl RetainedState {
)
};
// The recascaded size stands in for the element's cascaded `font-size`, which the drive
- // then leaves alone, exactly as C++ writes it into the working set before driving. An
- // element that declares its own `font-size` still has that declaration win, because the
- // drive reads a winning declaration before it consults this.
+ // then leaves alone. An element that declares a `font-size` not relative to the one it
+ // inherits still has that declaration win, because the drive reads it before it consults
+ // this.
if let Some(recascaded) = recascaded_font_size {
writer.set_computed(
prop::FONT_SIZE,
diff --git a/Libraries/LibWeb/Rust/src/css/style/position_try.rs b/Libraries/LibWeb/Rust/src/css/style/position_try.rs
index 558a06fd628..4f069bf485a 100644
--- a/Libraries/LibWeb/Rust/src/css/style/position_try.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/position_try.rs
@@ -985,6 +985,7 @@ impl RetainedState {
},
is_th_element: false,
has_new_font_size: false,
+ new_font_size_includes_cascaded_value: false,
has_tree_counting_context: false,
sibling_count: 0,
sibling_index: 0,
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 28e6e1313fd..45933ce1293 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -909,12 +909,13 @@ pub(crate) fn recascade_font_size_batch<'a>(
};
match value {
StyleValueData::Keyword { keyword } => {
- if *keyword == keyword::INITIAL || *keyword == keyword::UNSET {
+ if *keyword == keyword::INITIAL {
current_size = zoomed_default_size;
depends_on_viewport_metrics = false;
continue;
}
- if *keyword == keyword::INHERIT {
+ // NB: font-size is an inherited property, so unset inherits it.
+ if *keyword == keyword::INHERIT || *keyword == keyword::UNSET {
continue;
}
if let Some(px) = absolute_size_font_mapping(*keyword, default_size) {
@@ -995,6 +996,13 @@ pub(crate) fn recascade_font_size_batch<'a>(
depends_on_viewport_metrics = result.resolved_viewport_relative_length;
continue;
}
+ calculated @ StyleValueData::Calculated { .. } if calculated_font_size_is_percentage(calculated) => {
+ if let Some(px) = crate::css::calc::resolve_calculated_length_without_context(calculated, current_size)
+ {
+ current_size = px;
+ }
+ continue;
+ }
StyleValueData::Calculated { .. } => {
continue;
}
@@ -1557,6 +1565,62 @@ fn compute_corner_shape_parameter(value: &StyleValueData) -> Option<f64> {
}
}
+/// Whether a calc() font size is a percentage alone, which scales the inherited size as a percentage
+/// does, and so carries a keyword size through to the element.
+pub(crate) fn calculated_font_size_is_percentage(value: &StyleValueData) -> bool {
+ use crate::css::calc::{BASE_TYPE_PERCENT, CalcNumericType};
+ let StyleValueData::Calculated(payload) = value else {
+ return false;
+ };
+ // NB: The type the calculation has with its percentages kept as percentages, rather than as the lengths they
+ // resolve to.
+ let mut percentage = CalcNumericType::default();
+ percentage.exponents[BASE_TYPE_PERCENT] = Some(1);
+ payload
+ .rust_calculation
+ .node()
+ .numeric_type(&percentage)
+ .is_some_and(|numeric_type| {
+ numeric_type.percent_hint.is_none()
+ && numeric_type
+ .exponents
+ .iter()
+ .enumerate()
+ .all(|(base, exponent)| match base {
+ BASE_TYPE_PERCENT => *exponent == Some(1),
+ _ => matches!(exponent, None | Some(0)),
+ })
+ })
+}
+
+/// The font size a cascaded font-size computes to from `inherited_font_size`, where it's relative
+/// to it: a percentage, alone or in a calc(), an `em` length, `larger` or `smaller`, or the size
+/// itself through `inherit` or `unset`.
+pub(crate) fn font_size_relative_to_inherited_size(value: &StyleValueData, inherited_font_size: f64) -> Option<f64> {
+ let size = match value {
+ StyleValueData::Percentage { value } => Some(inherited_font_size * (value / 100.0)),
+ calculated @ StyleValueData::Calculated { .. } if calculated_font_size_is_percentage(calculated) => {
+ crate::css::calc::resolve_calculated_length_without_context(calculated, inherited_font_size)
+ }
+ StyleValueData::Keyword { keyword } if matches!(*keyword, keyword::INHERIT | keyword::UNSET) => {
+ Some(inherited_font_size)
+ }
+ StyleValueData::Keyword { keyword } => {
+ relative_size_font_ratio(*keyword).map(|ratio| inherited_font_size * ratio)
+ }
+ StyleValueData::Length { value, unit } => matches!(
+ length_unit_kinds().get(usize::from(*unit)),
+ Some(LengthUnitKind::FontRelative {
+ metric: FontMetricSelector::FontSize,
+ root: false,
+ })
+ )
+ .then(|| value * inherited_font_size),
+ _ => None,
+ };
+ size.map(single_precision_font_size)
+}
+
/// Whether a font-family value is a single monospace keyword, which triggers
/// the monospace font-size recascade. The list entry's keyword is read through
/// the nested value's shared Rust data handle.
@@ -2327,6 +2391,9 @@ pub struct FfiStyleComputationEnvironment {
pub color_scheme_input: FfiEffectiveColorSchemeInput,
pub is_th_element: bool,
pub has_new_font_size: bool,
+ /// Whether the new font size already accounts for the element's own cascaded `font-size`, which
+ /// the drive then leaves alone.
+ pub new_font_size_includes_cascaded_value: bool,
pub has_tree_counting_context: bool,
pub sibling_count: u64,
pub sibling_index: u64,
@@ -3145,6 +3212,7 @@ pub(crate) unsafe fn drive_property_computation(
let color_scheme_input = &environment.color_scheme_input;
let is_th_element = environment.is_th_element;
let has_new_font_size = environment.has_new_font_size;
+ let new_font_size_includes_cascaded_value = has_new_font_size && environment.new_font_size_includes_cascaded_value;
let device_pixels_per_css_pixel = environment.device_pixels_per_css_pixel;
let initial_font_size_raw = environment.initial_font_size_raw;
let default_font_size_raw = environment.default_font_size_raw;
@@ -3280,13 +3348,15 @@ pub(crate) unsafe fn drive_property_computation(
let mut source_slot = -1;
let mut has_style_sheet_context = false;
let mut external_dependencies = None;
+ let winning_declaration = store.winning_declaration(cascaded_property_id);
+ let has_winning_declaration = winning_declaration.is_some();
if let Some((
value_data,
important,
cascaded_source_slot,
cascaded_has_style_sheet_context,
cascaded_external_dependencies,
- )) = store.winning_declaration(cascaded_property_id)
+ )) = winning_declaration
{
// An empty winning value cascades nothing, as no declaration would.
value = Some(value_data).filter(|value| value.optional_data().is_some());
@@ -3299,7 +3369,11 @@ pub(crate) unsafe fn drive_property_computation(
if property_id == crate::css::property_metadata::property_id::FONT_SIZE {
writer.flags_mut().set_raw_cascaded_font_size(Some(value_data.clone()));
}
- } else if property_id == crate::css::property_metadata::property_id::FONT_SIZE && has_new_font_size {
+ }
+ if property_id == crate::css::property_metadata::property_id::FONT_SIZE
+ && has_new_font_size
+ && (!has_winning_declaration || new_font_size_includes_cascaded_value)
+ {
// NB: The recascade walks the ancestors. The inherited result still takes this element's own zoom.
if zoom.length_zoom() != 1.0 {
let recascaded = writer.get(property_id).and_then(|value| {
@@ -3314,7 +3388,7 @@ pub(crate) unsafe fn drive_property_computation(
}
}
continue;
- } else if property_id == crate::css::property_metadata::property_id::FONT_SIZE {
+ } else if property_id == crate::css::property_metadata::property_id::FONT_SIZE && !has_winning_declaration {
writer.flags_mut().set_raw_cascaded_font_size(None);
}
@@ -6151,6 +6225,7 @@ unsafe fn create_document_style_record(
color_scheme_input: input.color_scheme_input,
is_th_element: false,
has_new_font_size: false,
+ new_font_size_includes_cascaded_value: false,
has_tree_counting_context: false,
sibling_count: 0,
sibling_index: 0,
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute/tests.rs b/Libraries/LibWeb/Rust/src/css/style_compute/tests.rs
index 231b36d17c2..860939603de 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute/tests.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute/tests.rs
@@ -818,6 +818,7 @@ fn svg_axis_case(svg: bool, vertical: bool) -> [f64; 3] {
},
is_th_element: false,
has_new_font_size: false,
+ new_font_size_includes_cascaded_value: false,
has_tree_counting_context: false,
sibling_count: 0,
sibling_index: 0,
@@ -950,6 +951,7 @@ fn font_phase_axis_case(
},
is_th_element: false,
has_new_font_size: false,
+ new_font_size_includes_cascaded_value: false,
has_tree_counting_context: false,
sibling_count: 0,
sibling_index: 0,
diff --git a/Tests/LibWeb/Layout/expected/letter-spacing-end-of-line-001.txt b/Tests/LibWeb/Layout/expected/letter-spacing-end-of-line-001.txt
index aee99b5d9c6..ad3892883ba 100644
--- a/Tests/LibWeb/Layout/expected/letter-spacing-end-of-line-001.txt
+++ b/Tests/LibWeb/Layout/expected/letter-spacing-end-of-line-001.txt
@@ -1,18 +1,18 @@
Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
- BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 112 0+0+0] [BFC] children: not-inline
- BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 96 0+0+8] children: not-inline
- BlockContainer <div> at [8,8] [0+0+0 784 0+0+0] [0+0+0 48 0+0+0] children: inline
- frag 0 from TextNode start: 0, length: 5, rect: [8,8 121.359375x48] baseline: 38
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 94 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 78 0+0+8] children: not-inline
+ BlockContainer <div> at [8,8] [0+0+0 784 0+0+0] [0+0+0 39 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [8,8 98.625x39] baseline: 31
"12345"
TextNode <#text> (not painted)
- BlockContainer <div> at [8,56] [0+0+0 784 0+0+0] [0+0+0 48 0+0+0] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [121.625,56 28.03125x48] baseline: 38
+ BlockContainer <div> at [8,47] [0+0+0 784 0+0+0] [0+0+0 39 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [100.34375,47 22.78125x39] baseline: 31
"a"
- BlockContainer <span> at [8,56] floating [0+0+0 113.625 0+0+0] [0+0+0 48 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 2, rect: [8,56 113.625x48] baseline: 38
+ BlockContainer <span> at [8,47] floating [0+0+0 92.34375 0+0+0] [0+0+0 39 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,47 92.34375x39] baseline: 31
"aa"
TextNode <#text> (not painted)
TextNode <#text> (not painted)
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
- SC for BlockContainer<HTML> [0,0 800x112] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x94] (z-index: auto)
diff --git a/Tests/LibWeb/Text/expected/css/font-size-monospace-medium-size.txt b/Tests/LibWeb/Text/expected/css/font-size-monospace-medium-size.txt
new file mode 100644
index 00000000000..726bb27b34f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/font-size-monospace-medium-size.txt
@@ -0,0 +1,48 @@
+mono: 13px
+mono 110%: 14.3px
+mono 1.1em: 14.3px
+mono medium: 13px
+mono large: 16px
+mono larger: 15.6px
+mono smaller: 10.833333px
+mono 16px: 16px
+mono 1rem: 16px
+mono calc(1em + 2px): 18px
+pre > span serif: 16px
+pre > span serif 110%: 17.6px
+pre > span inherit-family: 13px
+pre > span 2em: 26px
+pre(150%) > span serif: 24px
+pre(20px) > span serif: 20px
+div 20px > mono: 20px
+div 150% > mono: 19.5px
+div 1.5em > mono: 19.5px
+div large > mono: 16px
+div calc(1em+2px) > mono: 18px
+div 150% > div 110% > mono: 21.450001px
+mono,serif: 16px
+Courier New,mono: 16px
+NoSuch,mono: 16px
+mono > serif > mono 110%: 14.3px
+ui-monospace: 16px
+code in p: 13px
+th mono 110% (ffmpeg): 14.3px
+mono > serif child of mono-110%: 17.6px
+pre > span medium: 13px
+pre > span large: 16px
+pre > span initial: 13px
+pre > span font-size unset: 13px
+pre > span serif inherit-size: 16px
+mono initial: 13px
+mono font-family inherit under pre: 14.3px
+div unset > mono: 13px
+pre > b (weight only): 13px
+pre > span serif > span mono: 13px
+zoom 2 > mono 110%: 14.3px
+mono calc(110%): 14.3px
+mono calc(50% + 60%): 14.3px
+mono calc(110% + 0px): 17.6px
+mono calc(1.1em): 17.6px
+div calc(150%) > mono: 19.5px
+div calc(150% + 0px) > mono: 24px
+pre calc(150%) > span serif: 24px
diff --git a/Tests/LibWeb/Text/expected/css/font-size-monospace-recascade-reads-parent-font.txt b/Tests/LibWeb/Text/expected/css/font-size-monospace-recascade-reads-parent-font.txt
new file mode 100644
index 00000000000..aa4cbe03bac
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/font-size-monospace-recascade-reads-parent-font.txt
@@ -0,0 +1,2 @@
+weight 400: 66.09375px
+weight 700: 66.71875px
diff --git a/Tests/LibWeb/Text/expected/css/font-size-monospace-relative-size-restyle.txt b/Tests/LibWeb/Text/expected/css/font-size-monospace-relative-size-restyle.txt
new file mode 100644
index 00000000000..7c44036dab9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/font-size-monospace-relative-size-restyle.txt
@@ -0,0 +1,6 @@
+200% of 20px: 40px
+150% of 20px: 30px
+150% of 22px: 33px
+150% of medium: 19.5px
+inherit of medium: 13px
+inherit of large: 16px
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/monospace-recascade-over-viewport-and-calc-sizes.txt b/Tests/LibWeb/Text/expected/css/style-engine/monospace-recascade-over-viewport-and-calc-sizes.txt
index 319b03f77d0..51d48e0a789 100644
--- a/Tests/LibWeb/Text/expected/css/style-engine/monospace-recascade-over-viewport-and-calc-sizes.txt
+++ b/Tests/LibWeb/Text/expected/css/style-engine/monospace-recascade-over-viewport-and-calc-sizes.txt
@@ -1,2 +1,2 @@
under-viewport: 16px
-under-calc: 13px
+under-calc: 26px
diff --git a/Tests/LibWeb/Text/input/css/font-size-monospace-medium-size.html b/Tests/LibWeb/Text/input/css/font-size-monospace-medium-size.html
new file mode 100644
index 00000000000..290c5f89d9e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/font-size-monospace-medium-size.html
@@ -0,0 +1,65 @@
+<!DOCTYPE html>
+<div id="root"></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ // Where the font-family is just monospace, a keyword font size means 13px at medium, and a size relative to one
+ // follows: the cases cover inheriting across a change into and out of that family, and an own size relative to
+ // the inherited one.
+ const cases = [
+ ["mono", `<div id=t style="font-family: monospace">x</div>`],
+ ["mono 110%", `<div id=t style="font-family: monospace; font-size: 110%">x</div>`],
+ ["mono 1.1em", `<div id=t style="font-family: monospace; font-size: 1.1em">x</div>`],
+ ["mono medium", `<div id=t style="font-family: monospace; font-size: medium">x</div>`],
+ ["mono large", `<div id=t style="font-family: monospace; font-size: large">x</div>`],
+ ["mono larger", `<div id=t style="font-family: monospace; font-size: larger">x</div>`],
+ ["mono smaller", `<div id=t style="font-family: monospace; font-size: smaller">x</div>`],
+ ["mono 16px", `<div id=t style="font-family: monospace; font-size: 16px">x</div>`],
+ ["mono 1rem", `<div id=t style="font-family: monospace; font-size: 1rem">x</div>`],
+ ["mono calc(1em + 2px)", `<div id=t style="font-family: monospace; font-size: calc(1em + 2px)">x</div>`],
+ ["pre > span serif", `<pre><span id=t style="font-family: serif">x</span></pre>`],
+ ["pre > span serif 110%", `<pre><span id=t style="font-family: serif; font-size: 110%">x</span></pre>`],
+ ["pre > span inherit-family", `<pre><span id=t>x</span></pre>`],
+ ["pre > span 2em", `<pre><span id=t style="font-size: 2em">x</span></pre>`],
+ ["pre(150%) > span serif", `<pre style="font-size: 150%"><span id=t style="font-family: serif">x</span></pre>`],
+ ["pre(20px) > span serif", `<pre style="font-size: 20px"><span id=t style="font-family: serif">x</span></pre>`],
+ ["div 20px > mono", `<div style="font-size: 20px"><div id=t style="font-family: monospace">x</div></div>`],
+ ["div 150% > mono", `<div style="font-size: 150%"><div id=t style="font-family: monospace">x</div></div>`],
+ ["div 1.5em > mono", `<div style="font-size: 1.5em"><div id=t style="font-family: monospace">x</div></div>`],
+ ["div large > mono", `<div style="font-size: large"><div id=t style="font-family: monospace">x</div></div>`],
+ ["div calc(1em+2px) > mono", `<div style="font-size: calc(1em + 2px)"><div id=t style="font-family: monospace">x</div></div>`],
+ ["div 150% > div 110% > mono", `<div style="font-size: 150%"><div style="font-size: 110%"><div id=t style="font-family: monospace">x</div></div></div>`],
+ ["mono,serif", `<div id=t style="font-family: monospace, serif">x</div>`],
+ ["Courier New,mono", `<div id=t style="font-family: 'Courier New', monospace">x</div>`],
+ ["NoSuch,mono", `<div id=t style="font-family: NoSuchFont, monospace">x</div>`],
+ ["mono > serif > mono 110%", `<div style="font-family: monospace"><div style="font-family: serif"><div id=t style="font-family: monospace; font-size: 110%">x</div></div></div>`],
+ ["ui-monospace", `<div id=t style="font-family: ui-monospace">x</div>`],
+ ["code in p", `<p><code id=t>x</code></p>`],
+ ["th mono 110% (ffmpeg)", `<table><tr><th id=t style="font-family: monospace; font-size: 110%">x</th></tr></table>`],
+ ["mono > serif child of mono-110%", `<div style="font-family: monospace; font-size: 110%"><span id=t style="font-family: serif">x</span></div>`],
+ ["pre > span medium", `<pre><span id=t style="font-size: medium">x</span></pre>`],
+ ["pre > span large", `<pre><span id=t style="font-size: large">x</span></pre>`],
+ ["pre > span initial", `<pre><span id=t style="font-size: initial">x</span></pre>`],
+ ["pre > span font-size unset", `<pre><span id=t style="font-size: unset">x</span></pre>`],
+ ["pre > span serif inherit-size", `<pre><span id=t style="font-family: serif; font-size: inherit">x</span></pre>`],
+ ["mono initial", `<div id=t style="font-family: monospace; font-size: initial">x</div>`],
+ ["mono font-family inherit under pre", `<pre><span id=t style="font-family: inherit; font-size: 110%">x</span></pre>`],
+ ["div unset > mono", `<div style="font-size: unset"><div id=t style="font-family: monospace">x</div></div>`],
+ ["pre > b (weight only)", `<pre><b id=t>x</b></pre>`],
+ ["pre > span serif > span mono", `<pre><span style="font-family: serif"><span id=t style="font-family: monospace">x</span></span></pre>`],
+ ["zoom 2 > mono 110%", `<div style="zoom: 2"><div id=t style="font-family: monospace; font-size: 110%">x</div></div>`],
+ ["mono calc(110%)", `<div id=t style="font-family: monospace; font-size: calc(110%)">x</div>`],
+ ["mono calc(50% + 60%)", `<div id=t style="font-family: monospace; font-size: calc(50% + 60%)">x</div>`],
+ ["mono calc(110% + 0px)", `<div id=t style="font-family: monospace; font-size: calc(110% + 0px)">x</div>`],
+ ["mono calc(1.1em)", `<div id=t style="font-family: monospace; font-size: calc(1.1em)">x</div>`],
+ ["div calc(150%) > mono", `<div style="font-size: calc(150%)"><div id=t style="font-family: monospace">x</div></div>`],
+ ["div calc(150% + 0px) > mono", `<div style="font-size: calc(150% + 0px)"><div id=t style="font-family: monospace">x</div></div>`],
+ ["pre calc(150%) > span serif", `<pre style="font-size: calc(150%)"><span id=t style="font-family: serif">x</span></pre>`],
+ ];
+ for (const [name, html] of cases) {
+ root.innerHTML = html;
+ println(`${name}: ${getComputedStyle(document.getElementById("t")).fontSize}`);
+ }
+ root.innerHTML = "";
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/font-size-monospace-recascade-reads-parent-font.html b/Tests/LibWeb/Text/input/css/font-size-monospace-recascade-reads-parent-font.html
new file mode 100644
index 00000000000..b79ce730a4a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/font-size-monospace-recascade-reads-parent-font.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<style>
+ @font-face { font-family: Lato; src: url(../../../Ref/input/wpt-import/fonts/Lato-Medium.ttf); font-weight: 400; }
+ @font-face { font-family: Lato; src: url(../../../Ref/input/wpt-import/fonts/Lato-Bold.ttf); font-weight: 700; }
+</style>
+<div id="root"></div>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ await document.fonts.load("400 16px Lato");
+ await document.fonts.load("700 16px Lato");
+ // The monospace recascade resolves an ancestor's ex font size against its parent's font, in the parent's
+ // weight: Lato's bold face has a different x-height from its regular one.
+ for (const weight of [400, 700]) {
+ root.innerHTML = `<div style="font-family: Lato; font-weight: ${weight}"><div style="font-size: 10ex"><code id="target">x</code></div></div>`;
+ println(`weight ${weight}: ${getComputedStyle(document.getElementById("target")).fontSize}`);
+ }
+ root.innerHTML = "";
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/font-size-monospace-relative-size-restyle.html b/Tests/LibWeb/Text/input/css/font-size-monospace-relative-size-restyle.html
new file mode 100644
index 00000000000..e70acf3ce07
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/font-size-monospace-relative-size-restyle.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<div id="parent" style="font-size: 20px"><code id="child" style="font-size: 200%">x</code></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ // A monospace element's size relative to the one it inherits is computed again from its current
+ // declaration whenever what it inherits changes.
+ const child = document.getElementById("child");
+ const parent = document.getElementById("parent");
+ println(`200% of 20px: ${getComputedStyle(child).fontSize}`);
+ child.style.fontSize = "150%";
+ println(`150% of 20px: ${getComputedStyle(child).fontSize}`);
+ parent.style.fontSize = "22px";
+ println(`150% of 22px: ${getComputedStyle(child).fontSize}`);
+ parent.style.fontSize = "medium";
+ println(`150% of medium: ${getComputedStyle(child).fontSize}`);
+ child.style.fontSize = "inherit";
+ println(`inherit of medium: ${getComputedStyle(child).fontSize}`);
+ parent.style.fontSize = "large";
+ println(`inherit of large: ${getComputedStyle(child).fontSize}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/style-engine/monospace-recascade-over-viewport-and-calc-sizes.html b/Tests/LibWeb/Text/input/css/style-engine/monospace-recascade-over-viewport-and-calc-sizes.html
index e082bbb8c74..878fcc33fc6 100644
--- a/Tests/LibWeb/Text/input/css/style-engine/monospace-recascade-over-viewport-and-calc-sizes.html
+++ b/Tests/LibWeb/Text/input/css/style-engine/monospace-recascade-over-viewport-and-calc-sizes.html
@@ -10,8 +10,8 @@
<div id="calc"><div><code id="under-calc">b</code></div></div>
<script>
test(() => {
- // The monospace font-size recascade reads the viewport through its ancestors, and skips a
- // calc() it cannot recascade.
+ // The monospace font-size recascade reads the viewport through its ancestors, and goes on
+ // from the size a calc() computed to, which carries no keyword through.
for (const id of ["under-viewport", "under-calc"])
println(`${id}: ${getComputedStyle(document.getElementById(id)).fontSize}`);
});