Skip to content

Truly independent web browser

LibWeb: Stretch progress and meter tracks without native appearance

Tim Ledbetterfaa30eab794d
diff --git a/Libraries/LibWeb/CSS/ComputedValues.h b/Libraries/LibWeb/CSS/ComputedValues.h
index 196a7df3485..851103e324c 100644
--- a/Libraries/LibWeb/CSS/ComputedValues.h
+++ b/Libraries/LibWeb/CSS/ComputedValues.h
@@ -433,6 +433,7 @@ public:
ReadonlySpan<ShadowData> text_shadow() const { return m_inherited.text->text_shadow_span(); }
Positioning position() const { return position_from_reset_bits(m_noninherited.reset_bits); }
Appearance appearance() const { return static_cast<Appearance>(m_noninherited.misc_layout->appearance); }
+ bool native_appearance_disabled() const { return m_noninherited.misc_layout->native_appearance_disabled; }
float opacity() const { return m_noninherited.effects->opacity; }
Visibility visibility() const { return visibility_from_inherited_bits(m_inherited.values.bits); }
ComputedFilterView backdrop_filter() const { return m_noninherited.effects->backdrop_filter_value(); }
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
index a301ee89950..960c30aec7a 100644
--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
@@ -40,7 +40,9 @@
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLLinkElement.h>
+#include <LibWeb/HTML/HTMLMeterElement.h>
#include <LibWeb/HTML/HTMLObjectElement.h>
+#include <LibWeb/HTML/HTMLProgressElement.h>
#include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/HTMLSlotElement.h>
#include <LibWeb/HTML/HTMLTableCellElement.h>
@@ -1516,6 +1518,7 @@ enum ElementConstructionFact : u32 {
IsSvgResourceElement = 1 << 7,
IsSvgGeometryElement = 1 << 8,
IsHtmlSelectElement = 1 << 9,
+ IsHtmlProgressOrMeterElement = 1 << 10,
// The layout algorithm a MathML element's tag name selects, as a MathMLLayoutKind in these bits.
MathMLLayoutKindShift = 16,
MathMLLayoutKindMask = 0x1f << MathMLLayoutKindShift,
@@ -1743,6 +1746,7 @@ static u32 kind_construction_facts(DOM::Element const& element)
set(!is_input && html_element && html_element->uses_button_layout(), ElementConstructionFact::UsesButtonLayout);
set(element.is_svg_geometry_element(), ElementConstructionFact::IsSvgGeometryElement);
set(is<HTML::HTMLSelectElement>(element), ElementConstructionFact::IsHtmlSelectElement);
+ set(is<HTML::HTMLProgressElement>(element) || is<HTML::HTMLMeterElement>(element), ElementConstructionFact::IsHtmlProgressOrMeterElement);
facts |= to_underlying(mathml_layout_kind(element)) << ElementConstructionFact::MathMLLayoutKindShift;
return facts;
}
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
index 915cdc6d05b..406dabaf6a9 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
@@ -11,7 +11,6 @@
#include <LibUnicode/CharacterTypes.h>
#include <LibUnicode/Segmenter.h>
#include <LibWeb/Bindings/CSS.h>
-#include <LibWeb/CSS/CSSStyleProperties.h>
#include <LibWeb/CSS/Invalidation/FormControlInvalidator.h>
#include <LibWeb/CSS/Invalidation/LanguageInvalidator.h>
#include <LibWeb/DOM/Document.h>
@@ -819,21 +818,6 @@ void FormAssociatedElement::RareData::visit_edges(JS::Cell::Visitor& visitor)
[](auto&) { });
}
-// A user-agent shadow tree's inner elements share their default declarations, parsed once, but each
-// holds its own copy: a declaration block is an element's own input, and one shared between elements
-// would change every one of them at once while telling none. The element's copy is replaced only
-// when the defaults it should hold differ from the ones it holds.
-void FormAssociatedTextControlElement::set_own_inline_style(DOM::Element& element, CSS::CSSStyleProperties const& defaults)
-{
- // Every value change asks again, and nearly always the element holds the defaults already. The element's copy
- // shares the declarations it was given until either side changes, so sharing answers that without serializing.
- if (auto current = element.inline_style(); current && current->owner_node().has_value() && current->declaration_block().shares_declarations_with(defaults.declaration_block()))
- return;
- auto style = CSS::CSSStyleProperties::create_element_inline_style({ element });
- style->set_declarations_from(defaults);
- element.set_inline_style(style);
-}
-
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
void FormAssociatedTextControlElement::relevant_value_was_changed()
{
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.h b/Libraries/LibWeb/HTML/FormAssociatedElement.h
index 41a0739565d..6c30101bc45 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.h
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.h
@@ -327,8 +327,6 @@ protected:
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
void relevant_value_was_changed();
- static void set_own_inline_style(DOM::Element&, CSS::CSSStyleProperties const& defaults);
-
private:
virtual GC::Ref<JS::Cell> as_cell() override;
diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp
index 97da5573fc0..7b9101e3504 100644
--- a/Libraries/LibWeb/HTML/HTMLElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLElement.cpp
@@ -9,6 +9,7 @@
#include <LibGC/RootVector.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibWeb/ARIA/Roles.h>
+#include <LibWeb/CSS/CSSStyleProperties.h>
#include <LibWeb/CSS/ComputedValues.h>
#include <LibWeb/CSS/Invalidation/ElementStateInvalidator.h>
#include <LibWeb/CSS/Invalidation/FormControlInvalidator.h>
@@ -3151,4 +3152,46 @@ WebIDL::UnsignedLong HTMLElement::computed_heading_offset() const
return offset;
}
+// A user-agent shadow tree's inner elements share their default declarations, parsed once, but each
+// holds its own copy: a declaration block is an element's own input, and one shared between elements
+// would change every one of them at once while telling none. The element's copy is replaced only
+// when the defaults it should hold differ from the ones it holds.
+void HTMLElement::set_own_inline_style(DOM::Element& element, CSS::CSSStyleProperties const& defaults)
+{
+ // Every value change asks again, and nearly always the element holds the defaults already. The element's copy
+ // shares the declarations it was given until either side changes, so sharing answers that without serializing.
+ if (auto current = element.inline_style(); current && current->owner_node().has_value() && current->declaration_block().shares_declarations_with(defaults.declaration_block()))
+ return;
+ auto style = CSS::CSSStyleProperties::create_element_inline_style({ element });
+ style->set_declarations_from(defaults);
+ element.set_inline_style(style);
+}
+
+static GC::Ref<CSS::CSSStyleProperties> slider_track_style_without_native_appearance()
+{
+ static auto& style = *new GC::Root<CSS::CSSStyleProperties>;
+ if (!style) {
+ style = CSS::CSSStyleProperties::create({}, {});
+ style->set_declarations_from_text(R"~~~(
+ height: 100%;
+ margin-top: 0;
+ border-radius: 0;
+ background-color: gray;
+ )~~~"sv);
+ }
+ return *style;
+}
+
+void HTMLElement::update_slider_track_style(DOM::Element& slider_track) const
+{
+ auto style = computed_style();
+ if (!style)
+ return;
+
+ if (style->appearance() != CSS::Appearance::None && !style->native_appearance_disabled())
+ slider_track.set_inline_style(nullptr);
+ else
+ set_own_inline_style(slider_track, slider_track_style_without_native_appearance());
+}
+
}
diff --git a/Libraries/LibWeb/HTML/HTMLElement.h b/Libraries/LibWeb/HTML/HTMLElement.h
index 679b8729179..9b6ab7d80af 100644
--- a/Libraries/LibWeb/HTML/HTMLElement.h
+++ b/Libraries/LibWeb/HTML/HTMLElement.h
@@ -220,6 +220,11 @@ protected:
[[nodiscard]] Utf16String get_the_text_steps();
+ static void set_own_inline_style(DOM::Element&, CSS::CSSStyleProperties const& defaults);
+
+ // Gives the slider track of a progress or meter element the style of the element's appearance.
+ void update_slider_track_style(DOM::Element& slider_track) const;
+
private:
struct RareData;
virtual OwnPtr<DOM::Node::RareData> create_rare_data() const override;
diff --git a/Libraries/LibWeb/HTML/HTMLMeterElement.cpp b/Libraries/LibWeb/HTML/HTMLMeterElement.cpp
index 96b2525944f..994aeee974a 100644
--- a/Libraries/LibWeb/HTML/HTMLMeterElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMeterElement.cpp
@@ -30,6 +30,7 @@ HTMLMeterElement::~HTMLMeterElement() = default;
void HTMLMeterElement::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
+ visitor.visit(m_meter_bar_element);
visitor.visit(m_meter_value_element);
}
@@ -173,12 +174,12 @@ void HTMLMeterElement::create_shadow_tree_if_needed()
shadow_root->set_user_agent_internal(true);
set_shadow_root(shadow_root);
- auto meter_bar_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
- MUST(shadow_root->append_child(*meter_bar_element));
- meter_bar_element->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::SliderTrack);
+ m_meter_bar_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
+ MUST(shadow_root->append_child(*m_meter_bar_element));
+ m_meter_bar_element->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::SliderTrack);
m_meter_value_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
- MUST(meter_bar_element->append_child(*m_meter_value_element));
+ MUST(m_meter_bar_element->append_child(*m_meter_value_element));
m_meter_value_element->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::SliderFill);
update_meter_value_element();
}
@@ -229,4 +230,10 @@ void HTMLMeterElement::update_meter_value_element()
MUST(m_meter_value_element->style()->set_property(CSS::PropertyID::Width, width.utf16_view()));
}
+void HTMLMeterElement::computed_properties_changed()
+{
+ if (m_meter_bar_element)
+ update_slider_track_style(*m_meter_bar_element);
+}
+
}
diff --git a/Libraries/LibWeb/HTML/HTMLMeterElement.h b/Libraries/LibWeb/HTML/HTMLMeterElement.h
index d5eae8eb04c..e0a3b0ba08b 100644
--- a/Libraries/LibWeb/HTML/HTMLMeterElement.h
+++ b/Libraries/LibWeb/HTML/HTMLMeterElement.h
@@ -59,10 +59,13 @@ private:
HTMLMeterElement(DOM::Document&, DOM::QualifiedName);
virtual void visit_edges(Cell::Visitor&) override;
+ virtual void computed_properties_changed() override;
+
void create_shadow_tree_if_needed();
void update_meter_value_element();
+ GC::Ptr<DOM::Element> m_meter_bar_element;
GC::Ptr<DOM::Element> m_meter_value_element;
ValueState m_cached_value_state { ValueState::Optimal };
};
diff --git a/Libraries/LibWeb/HTML/HTMLProgressElement.cpp b/Libraries/LibWeb/HTML/HTMLProgressElement.cpp
index a0a08c316fc..ea92b068656 100644
--- a/Libraries/LibWeb/HTML/HTMLProgressElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLProgressElement.cpp
@@ -32,6 +32,7 @@ HTMLProgressElement::~HTMLProgressElement() = default;
void HTMLProgressElement::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
+ visitor.visit(m_progress_bar_element);
visitor.visit(m_progress_value_element);
}
@@ -97,12 +98,12 @@ void HTMLProgressElement::create_shadow_tree_if_needed()
shadow_root->set_user_agent_internal(true);
set_shadow_root(shadow_root);
- auto progress_bar_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
- MUST(shadow_root->append_child(*progress_bar_element));
- progress_bar_element->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::SliderTrack);
+ m_progress_bar_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
+ MUST(shadow_root->append_child(*m_progress_bar_element));
+ m_progress_bar_element->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::SliderTrack);
m_progress_value_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
- MUST(progress_bar_element->append_child(*m_progress_value_element));
+ MUST(m_progress_bar_element->append_child(*m_progress_value_element));
m_progress_value_element->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::SliderFill);
update_progress_value_element();
}
@@ -122,4 +123,10 @@ void HTMLProgressElement::update_progress_value_element()
MUST(m_progress_value_element->style()->set_property(CSS::PropertyID::Width, width.utf16_view()));
}
+void HTMLProgressElement::computed_properties_changed()
+{
+ if (m_progress_bar_element)
+ update_slider_track_style(*m_progress_bar_element);
+}
+
}
diff --git a/Libraries/LibWeb/HTML/HTMLProgressElement.h b/Libraries/LibWeb/HTML/HTMLProgressElement.h
index 4cc77cbc28c..c8ceb54dd73 100644
--- a/Libraries/LibWeb/HTML/HTMLProgressElement.h
+++ b/Libraries/LibWeb/HTML/HTMLProgressElement.h
@@ -47,12 +47,15 @@ private:
virtual bool is_html_progress_element() const final { return true; }
virtual void visit_edges(Cell::Visitor&) override;
+ virtual void computed_properties_changed() override;
+
void create_shadow_tree_if_needed();
void update_progress_value_element();
bool is_determinate() const { return has_attribute(HTML::AttributeNames::value); }
+ GC::Ptr<DOM::Element> m_progress_bar_element;
GC::Ptr<DOM::Element> m_progress_value_element;
};
diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
index 5da09184189..85aec54df6b 100644
--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
@@ -782,6 +782,7 @@ pub mod element_construction_fact {
pub const IS_SVG_RESOURCE_ELEMENT: u32 = 1 << 7;
pub const IS_SVG_GEOMETRY_ELEMENT: u32 = 1 << 8;
pub const IS_HTML_SELECT_ELEMENT: u32 = 1 << 9;
+ pub const IS_HTML_PROGRESS_OR_METER_ELEMENT: u32 = 1 << 10;
/// The layout algorithm a MathML element's tag name selects, as a `MathLayoutKind` in these bits.
pub const MATHML_LAYOUT_KIND_SHIFT: u32 = 16;
pub const MATHML_LAYOUT_KIND_MASK: u32 = 0x1f << MATHML_LAYOUT_KIND_SHIFT;
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
index 823877b0ad9..99ccd75453d 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
@@ -272,6 +272,8 @@ impl RetainedState {
}
};
let was_none = old_values.is_none_or(is_none);
+ let native_appearance =
+ |values: ComputedValuesView<'_>| (values.appearance(), values.native_appearance_disabled());
let (damage_effects, rebuild_root) = super::super::style_invalidation::main_effects_of_damage(
facts.element_damage,
facts.pseudo_elements.damages.iter().copied(),
@@ -364,7 +366,10 @@ impl RetainedState {
construction_facts
& (element_construction_fact::IS_HTML_INPUT_ELEMENT
| element_construction_fact::IS_HTML_SELECT_ELEMENT)
- != 0,
+ != 0
+ || (construction_facts & element_construction_fact::IS_HTML_PROGRESS_OR_METER_ELEMENT != 0
+ && old_values
+ .is_none_or(|old_values| native_appearance(old_values) != native_appearance(new_values))),
);
InstalledElementEffects {
style_node: node.raw(),
diff --git a/Tests/LibWeb/Layout/input/element-use-pseudo-element.html b/Tests/LibWeb/Layout/input/element-use-pseudo-element.html
index 1f0f36d7a92..5e4019987be 100644
--- a/Tests/LibWeb/Layout/input/element-use-pseudo-element.html
+++ b/Tests/LibWeb/Layout/input/element-use-pseudo-element.html
@@ -6,5 +6,5 @@
</style>
<progress id="a"></progress>
<script>
- a.style.backgroundColor = 'red'; // Trigger style invalidation
+ a.style.color = 'red'; // Trigger style invalidation
</script>
diff --git a/Tests/LibWeb/Ref/expected/progress-and-meter-without-native-appearance-ref.html b/Tests/LibWeb/Ref/expected/progress-and-meter-without-native-appearance-ref.html
new file mode 100644
index 00000000000..16bc9f0c61e
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/progress-and-meter-without-native-appearance-ref.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<style>
+ div {
+ width: 200px;
+ height: 6px;
+ background-color: gray;
+ }
+ div > div {
+ width: 60px;
+ background-color: blue;
+ }
+</style>
+<div><div></div></div>
+<div><div></div></div>
+<div><div></div></div>
+<div><div></div></div>
diff --git a/Tests/LibWeb/Ref/input/progress-and-meter-without-native-appearance.html b/Tests/LibWeb/Ref/input/progress-and-meter-without-native-appearance.html
new file mode 100644
index 00000000000..81ce6d86334
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/progress-and-meter-without-native-appearance.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/progress-and-meter-without-native-appearance-ref.html">
+<style>
+ progress, meter {
+ display: block;
+ width: 200px;
+ height: 6px;
+ }
+ .appearance-none {
+ appearance: none;
+ }
+ .appearance-none::-webkit-progress-bar {
+ background-color: gray;
+ }
+ .background {
+ background: gray;
+ }
+ .border {
+ border: 0 solid;
+ }
+ progress::-webkit-progress-value {
+ background-color: blue;
+ }
+ meter::-moz-meter-bar {
+ background: blue;
+ }
+</style>
+<progress class="appearance-none" value="0.3"></progress>
+<progress class="background" value="0.3"></progress>
+<meter class="background" value="0.3"></meter>
+<progress class="border" value="0.3"></progress>