Skip to content

Truly independent web browser

LibWeb: Ignore padding, borders, and backgrounds on native checkboxes

sideshowbarkerbd4a6136991b
diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
index 8d2ed9234ee..6e4715abdd5 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
@@ -657,6 +657,15 @@ pub(crate) fn kind_is_block_container(kind: NodeKind) -> bool {
)
}
+/// A checkbox or radio button that keeps its native appearance. HTML makes both non-devolvable widgets, which CSS UI
+/// says keep that appearance "regardless of any properties that disable native appearance for widgets": their border
+/// and background.
+/// https://html.spec.whatwg.org/multipage/rendering.html#the-input-element-as-a-checkbox-and-radio-button-widgets
+/// https://drafts.csswg.org/css-ui/#non-devolvable
+pub(crate) fn kind_is_non_devolvable_widget(kind: NodeKind) -> bool {
+ matches!(kind, NodeKind::CheckBox | NodeKind::RadioButton)
+}
+
pub(crate) fn kind_is_replaced_box(kind: NodeKind) -> bool {
matches!(
kind,
@@ -753,6 +762,7 @@ impl<'pass> NodeFacts<'pass> {
self.style_payloads
.expect("styled node must publish its style container before layout"),
)
+ .for_kind(self.data().kind.get())
}
pub(super) fn computed_values_view_if_styled(&self) -> Option<ComputedValuesView<'pass>> {
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 0356b446b5e..6e5bd2915d7 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -84,14 +84,15 @@ impl<'pass> SizingContext<'pass> {
}
pub(super) fn style(&self, node: Node) -> StyleValues<'pass> {
+ let kind = self.callbacks.node_data(node).kind.get();
if let Some((cached_node, payloads)) = self.style_cache.get()
&& cached_node == node
{
- return StyleValues::new(payloads);
+ return StyleValues::new(payloads).for_kind(kind);
}
let payloads = self.callbacks.style_payloads(node);
self.style_cache.set(Some((node, payloads)));
- StyleValues::new(payloads)
+ StyleValues::new(payloads).for_kind(kind)
}
#[track_caller]
diff --git a/Libraries/LibWeb/Rust/src/layout/style_values.rs b/Libraries/LibWeb/Rust/src/layout/style_values.rs
index d67d9f171c1..f0974b55f1c 100644
--- a/Libraries/LibWeb/Rust/src/layout/style_values.rs
+++ b/Libraries/LibWeb/Rust/src/layout/style_values.rs
@@ -84,16 +84,40 @@ impl InsetValue<'_> {
}
}
+/// A padding read: the style's, or zero for a non-devolvable widget.
+#[derive(Clone, Copy)]
+pub(crate) enum PaddingValue<'a> {
+ FromStyle(&'a ComputedLengthPercentageOrAuto),
+ Zero,
+}
+
+impl PaddingValue<'_> {
+ pub(crate) fn to_px(self, reference: CssPixels) -> CssPixels {
+ match self {
+ Self::FromStyle(value) => value.to_px(reference),
+ Self::Zero => CssPixels::default(),
+ }
+ }
+
+ pub(crate) fn contains_percentage(self) -> bool {
+ match self {
+ Self::FromStyle(value) => value.contains_percentage(),
+ Self::Zero => false,
+ }
+ }
+}
+
/// The layout pass view of a node's computed style: the pure payload view
/// plus the resolved anchor insets the abspos engine threads to the four
-/// inset reads of the box it is placing, and the line builder's
-/// vertical-align keyword substitution. Every other read derefs to
-/// ComputedValuesView.
+/// inset reads of the box it is placing, the line builder's vertical-align
+/// keyword substitution, and the padding and border widths a non-devolvable
+/// widget drops. Every other read derefs to ComputedValuesView.
#[derive(Clone, Copy)]
pub(crate) struct StyleValues<'a> {
style: ComputedValuesView<'a>,
resolved_anchor_insets: Option<&'a formatting_context::ResolvedAnchorInsets>,
vertical_align_override: Option<u8>,
+ is_non_devolvable_widget: bool,
}
impl<'a> std::ops::Deref for StyleValues<'a> {
@@ -111,12 +135,21 @@ impl<'a> StyleValues<'a> {
style: ComputedValuesView::new(&payloads.groups),
resolved_anchor_insets: None,
vertical_align_override: None,
+ is_non_devolvable_widget: false,
}
}
#[inline]
pub(crate) fn for_node(callbacks: &LayoutPass<'a>, node: Node) -> Self {
- StyleValues::new(callbacks.style_payloads(node))
+ StyleValues::new(callbacks.style_payloads(node)).for_kind(callbacks.node_data(node).kind.get())
+ }
+
+ /// The view for a box of `kind`. A non-devolvable widget is drawn natively to fill its box, so author padding and
+ /// borders don't apply to it.
+ #[inline]
+ pub(crate) fn for_kind(mut self, kind: NodeKind) -> Self {
+ self.is_non_devolvable_widget = node_facts::kind_is_non_devolvable_widget(kind);
+ self
}
pub(crate) fn with_resolved_insets(
@@ -180,6 +213,57 @@ impl<'a> StyleValues<'a> {
self.inset_value(InsetField::Left)
}
+ // AD-HOC: CSS UI only says a non-devolvable widget keeps its native appearance whatever its border; it doesn't say
+ // the border takes no room, nor anything about padding. A native control fills its box, so its border
+ // widths and its padding read as zero.
+ fn padding_unless_non_devolvable(self, padding: &'a ComputedLengthPercentageOrAuto) -> PaddingValue<'a> {
+ if self.is_non_devolvable_widget {
+ PaddingValue::Zero
+ } else {
+ PaddingValue::FromStyle(padding)
+ }
+ }
+
+ fn border_width_unless_non_devolvable(self, width: CssPixels) -> CssPixels {
+ if self.is_non_devolvable_widget {
+ CssPixels::default()
+ } else {
+ width
+ }
+ }
+
+ pub(crate) fn padding_top(self) -> PaddingValue<'a> {
+ self.padding_unless_non_devolvable(self.style.padding_top())
+ }
+
+ pub(crate) fn padding_right(self) -> PaddingValue<'a> {
+ self.padding_unless_non_devolvable(self.style.padding_right())
+ }
+
+ pub(crate) fn padding_bottom(self) -> PaddingValue<'a> {
+ self.padding_unless_non_devolvable(self.style.padding_bottom())
+ }
+
+ pub(crate) fn padding_left(self) -> PaddingValue<'a> {
+ self.padding_unless_non_devolvable(self.style.padding_left())
+ }
+
+ pub(crate) fn border_top_width(self) -> CssPixels {
+ self.border_width_unless_non_devolvable(self.style.border_top_width())
+ }
+
+ pub(crate) fn border_right_width(self) -> CssPixels {
+ self.border_width_unless_non_devolvable(self.style.border_right_width())
+ }
+
+ pub(crate) fn border_bottom_width(self) -> CssPixels {
+ self.border_width_unless_non_devolvable(self.style.border_bottom_width())
+ }
+
+ pub(crate) fn border_left_width(self) -> CssPixels {
+ self.border_width_unless_non_devolvable(self.style.border_left_width())
+ }
+
pub(crate) fn with_vertical_align_keyword(mut self, keyword: u8) -> Self {
self.vertical_align_override = Some(keyword);
self
diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rs
index 5a9214fa6e2..232ca5037c4 100644
--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs
+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs
@@ -429,7 +429,7 @@ impl Default for UsedValues {
impl UsedValues {
/// Resolves the left and right margins, borders and padding of `style`, with percentages against `basis`.
- pub(crate) fn resolve_horizontal_box_model(&self, style: &ComputedValuesView<'_>, basis: CssPixels) {
+ pub(crate) fn resolve_horizontal_box_model(&self, style: &StyleValues<'_>, basis: CssPixels) {
self.margin_left.set(style.margin_left().to_px(basis));
self.margin_right.set(style.margin_right().to_px(basis));
self.border_left.set(style.border_left_width());
@@ -439,7 +439,7 @@ impl UsedValues {
}
/// Resolves the top and bottom margins, borders and padding of `style`, with percentages against `basis`.
- pub(crate) fn resolve_vertical_box_model(&self, style: &ComputedValuesView<'_>, basis: CssPixels) {
+ pub(crate) fn resolve_vertical_box_model(&self, style: &StyleValues<'_>, basis: CssPixels) {
self.margin_top.set(style.margin_top().to_px(basis));
self.margin_bottom.set(style.margin_bottom().to_px(basis));
self.border_top.set(style.border_top_width());
@@ -449,7 +449,7 @@ impl UsedValues {
}
/// Resolves every margin, border and padding of `style`, with percentages against `basis`.
- pub(crate) fn resolve_box_model(&self, style: &ComputedValuesView<'_>, basis: CssPixels) {
+ pub(crate) fn resolve_box_model(&self, style: &StyleValues<'_>, basis: CssPixels) {
self.resolve_horizontal_box_model(style, basis);
self.resolve_vertical_box_model(style, basis);
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/mod.rs
index 0b9d459f1fc..0505230e461 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/mod.rs
@@ -132,7 +132,14 @@ pub(crate) fn paint<O: Observer>(recorder: &mut PaintRecorder<'_, O>, paintable:
if !recorder.is_visible(paintable) {
return;
}
- paint_base(recorder, paintable, phase);
+ if crate::layout::node_facts::kind_is_non_devolvable_widget(kind) {
+ // A non-devolvable widget keeps its native appearance whatever background it has. Layout already gave
+ // it zero border widths, so only its background is left to skip.
+ // https://drafts.csswg.org/css-ui/#non-devolvable
+ paint_base_with(recorder, paintable, phase, |_, _| {});
+ } else {
+ paint_base(recorder, paintable, phase);
+ }
if phase == PaintPhase::Foreground {
match kind {
NodeKind::ImageBox => replaced::paint_image_foreground(recorder, paintable),
diff --git a/Tests/LibWeb/Layout/expected/inline-button-line-height.txt b/Tests/LibWeb/Layout/expected/inline-button-line-height.txt
index 996e8683d49..d967c7e9ccc 100644
--- a/Tests/LibWeb/Layout/expected/inline-button-line-height.txt
+++ b/Tests/LibWeb/Layout/expected/inline-button-line-height.txt
@@ -7,56 +7,56 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
Box <div.radios--container> at [8,8] flex-container(row) [0+0+0 300 0+0+0] [0+0+0 189 0+0+0] [FFC] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- Box <div.radios--main> at [8,8] flex-container(row) flex-item [0+0+0 170 6+0+0] [0+0+0 47 16+0+0] [FFC] children: not-inline
+ Box <div.radios--main> at [8,8] flex-container(row) flex-item [0+0+0 157 6+0+0] [0+0+0 47 16+0+0] [FFC] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- RadioButton <input.radios--input> at [15,10] flex-item [0+2+5 0 5+2+0] [0+2+0 0 0+2+0] children: not-inline
+ RadioButton <input.radios--input> at [8,8] flex-item [0+0+0 1 0+0+0] [0+0+0 1 0+0+0] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <label.radios--value-button> at [40,10] flex-item [0+2+16 120 16+2+0] [0+2+0 43 0+2+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 6, rect: [40,21.5 120x20] baseline: 16
+ BlockContainer <label.radios--value-button> at [27,10] flex-item [0+2+16 120 16+2+0] [0+2+0 43 0+2+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [27,21.5 120x20] baseline: 16
"100 ML"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- Box <div.radios--main> at [8,71] flex-container(row) flex-item [0+0+0 150 6+0+0] [0+0+0 47 16+0+0] [FFC] children: not-inline
+ Box <div.radios--main> at [8,71] flex-container(row) flex-item [0+0+0 137 6+0+0] [0+0+0 47 16+0+0] [FFC] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- RadioButton <input.radios--input> at [15,73] flex-item [0+2+5 0 5+2+0] [0+2+0 0 0+2+0] children: not-inline
+ RadioButton <input.radios--input> at [8,71] flex-item [0+0+0 1 0+0+0] [0+0+0 1 0+0+0] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <label.radios--value-button> at [40,73] flex-item [0+2+16 100 16+2+0] [0+2+0 43 0+2+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 5, rect: [40,84.5 100x20] baseline: 16
+ BlockContainer <label.radios--value-button> at [27,73] flex-item [0+2+16 100 16+2+0] [0+2+0 43 0+2+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [27,84.5 100x20] baseline: 16
"50 ML"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- Box <div.radios--main> at [8,134] flex-container(row) flex-item [0+0+0 150 6+0+0] [0+0+0 47 16+0+0] [FFC] children: not-inline
+ Box <div.radios--main> at [151,71] flex-container(row) flex-item [0+0+0 137 6+0+0] [0+0+0 47 16+0+0] [FFC] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- RadioButton <input.radios--input> at [15,136] flex-item [0+2+5 0 5+2+0] [0+2+0 0 0+2+0] children: not-inline
+ RadioButton <input.radios--input> at [151,71] flex-item [0+0+0 1 0+0+0] [0+0+0 1 0+0+0] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <label.radios--value-button> at [40,136] flex-item [0+2+16 100 16+2+0] [0+2+0 43 0+2+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 5, rect: [40,147.5 100x20] baseline: 16
+ BlockContainer <label.radios--value-button> at [170,73] flex-item [0+2+16 100 16+2+0] [0+2+0 43 0+2+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [170,84.5 100x20] baseline: 16
"30 ML"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- Box <div.radios--main> at [164,134] flex-container(row) flex-item [0+0+0 130 0+0+0] [0+0+0 47 16+0+0] [FFC] children: not-inline
+ Box <div.radios--main> at [8,134] flex-container(row) flex-item [0+0+0 117 0+0+0] [0+0+0 47 16+0+0] [FFC] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- RadioButton <input.radios--input> at [171,136] flex-item [0+2+5 0 5+2+0] [0+2+0 0 0+2+0] children: not-inline
+ RadioButton <input.radios--input> at [8,134] flex-item [0+0+0 1 0+0+0] [0+0+0 1 0+0+0] children: not-inline
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
- BlockContainer <label.radios--value-button> at [196,136] flex-item [0+2+16 80 16+2+0] [0+2+0 43 0+2+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 4, rect: [196,147.5 80x20] baseline: 16
+ BlockContainer <label.radios--value-button> at [27,136] flex-item [0+2+16 80 16+2+0] [0+2+0 43 0+2+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 4, rect: [27,147.5 80x20] baseline: 16
"15ML"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
@@ -129,7 +129,7 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
SC for BlockContainer<HTML> [0,0 800x359] (z-index: auto)
- SC for RadioButton<INPUT>.radios--input [15,10 0x0] (z-index: auto)
- SC for RadioButton<INPUT>.radios--input [15,73 0x0] (z-index: auto)
- SC for RadioButton<INPUT>.radios--input [15,136 0x0] (z-index: auto)
- SC for RadioButton<INPUT>.radios--input [171,136 0x0] (z-index: auto)
+ SC for RadioButton<INPUT>.radios--input [8,8 1x1] (z-index: auto)
+ SC for RadioButton<INPUT>.radios--input [8,71 1x1] (z-index: auto)
+ SC for RadioButton<INPUT>.radios--input [151,71 1x1] (z-index: auto)
+ SC for RadioButton<INPUT>.radios--input [8,134 1x1] (z-index: auto)
diff --git a/Tests/LibWeb/Ref/expected/checkbox-radio-native-appearance-ignores-padding-border-background-ref.html b/Tests/LibWeb/Ref/expected/checkbox-radio-native-appearance-ignores-padding-border-background-ref.html
new file mode 100644
index 00000000000..4bec5c26766
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/checkbox-radio-native-appearance-ignores-padding-border-background-ref.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<input type="checkbox" checked>
+<input type="checkbox">
+<input type="radio" checked>
+<input type="radio">
diff --git a/Tests/LibWeb/Ref/input/checkbox-radio-native-appearance-ignores-padding-border-background.html b/Tests/LibWeb/Ref/input/checkbox-radio-native-appearance-ignores-padding-border-background.html
new file mode 100644
index 00000000000..9a6ae83ed0d
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/checkbox-radio-native-appearance-ignores-padding-border-background.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/checkbox-radio-native-appearance-ignores-padding-border-background-ref.html">
+<style>
+ input {
+ box-sizing: border-box;
+ padding: 9px 11px;
+ border: 3px solid red;
+ background: red;
+ }
+</style>
+<input type="checkbox" checked>
+<input type="checkbox">
+<input type="radio" checked>
+<input type="radio">