diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-23 19:48:41 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-24 15:01:49 +0200 |
commit | 1ca33598da3e9b06fd59c806655473bd9a0d50d3 (patch) | |
tree | 019162aa12ea52a9736300f203f01ce48a00c334 /Userland | |
parent | c5b4928f4a8f00d31d5b62a33f9246876866ecb8 (diff) | |
download | serenity-1ca33598da3e9b06fd59c806655473bd9a0d50d3.zip |
LibWeb: Give DOM::Document some default style properties
Add StyleResolver::create_document_style() to help in creating an
"empty" style with nothing but default values.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleResolver.cpp | 41 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleResolver.h | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 2 |
3 files changed, 31 insertions, 21 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp b/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp index 122d41a86f..1e5d95f8f4 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp @@ -586,17 +586,17 @@ static NonnullRefPtr<StyleValue> get_initial_value(CSS::PropertyID property_id) return value.release_nonnull(); }; -static NonnullRefPtr<StyleValue> get_inherit_value(CSS::PropertyID property_id, DOM::Element const& element) +static NonnullRefPtr<StyleValue> get_inherit_value(CSS::PropertyID property_id, DOM::Element const* element) { - if (!element.parent_element() || !element.parent_element()->specified_css_values()) + if (!element || !element->parent_element() || !element->parent_element()->specified_css_values()) return get_initial_value(property_id); - auto& map = element.parent_element()->specified_css_values()->properties(); + auto& map = element->parent_element()->specified_css_values()->properties(); auto it = map.find(property_id); VERIFY(it != map.end()); return *it->value; }; -void StyleResolver::compute_defaulted_property_value(StyleProperties& style, DOM::Element const& element, CSS::PropertyID property_id) const +void StyleResolver::compute_defaulted_property_value(StyleProperties& style, DOM::Element const* element, CSS::PropertyID property_id) const { // FIXME: If we don't know the correct initial value for a property, we fall back to InitialStyleValue. @@ -621,7 +621,7 @@ void StyleResolver::compute_defaulted_property_value(StyleProperties& style, DOM } // https://drafts.csswg.org/css-cascade/#defaulting -void StyleResolver::compute_defaulted_values(StyleProperties& style, DOM::Element const& element) const +void StyleResolver::compute_defaulted_values(StyleProperties& style, DOM::Element const* element) const { // Walk the list of all known CSS properties and: // - Add them to `style` if they are missing. @@ -632,7 +632,7 @@ void StyleResolver::compute_defaulted_values(StyleProperties& style, DOM::Elemen } } -void StyleResolver::compute_font(StyleProperties& style, DOM::Element const& element) const +void StyleResolver::compute_font(StyleProperties& style, DOM::Element const* element) const { // To compute the font, first ensure that we've defaulted the relevant CSS font properties. // FIXME: This should be more sophisticated. @@ -705,12 +705,12 @@ void StyleResolver::compute_font(StyleProperties& style, DOM::Element const& ele } } else { float root_font_size = 10; - if (element.document().document_element() != &element) - root_font_size = element.document().document_element()->layout_node()->font().presentation_size(); + if (element->document().document_element() != element) + root_font_size = element->document().document_element()->layout_node()->font().presentation_size(); Gfx::FontMetrics font_metrics; - if (element.parent_element()) - font_metrics = element.parent_element()->specified_css_values()->computed_font().metrics('M'); + if (element && element->parent_element()) + font_metrics = element->parent_element()->specified_css_values()->computed_font().metrics('M'); else font_metrics = Gfx::FontDatabase::default_font().metrics('M'); @@ -719,8 +719,8 @@ void StyleResolver::compute_font(StyleProperties& style, DOM::Element const& ele maybe_length = font_size->to_length(); if (maybe_length->is_percentage()) { auto parent_font_size = size; - if (element.parent_element() && element.parent_element()->layout_node() && element.parent_element()->specified_css_values()) { - auto value = element.parent_element()->specified_css_values()->property(CSS::PropertyID::FontSize).value(); + if (element && element->parent_element() && element->parent_element()->layout_node() && element->parent_element()->specified_css_values()) { + auto value = element->parent_element()->specified_css_values()->property(CSS::PropertyID::FontSize).value(); if (value->is_length()) { auto length = static_cast<LengthStyleValue const&>(*value).to_length(); if (length.is_absolute() || length.is_relative()) @@ -811,7 +811,7 @@ void StyleResolver::compute_font(StyleProperties& style, DOM::Element const& ele style.set_computed_font(found_font.release_nonnull()); } -void StyleResolver::absolutize_values(StyleProperties& style, DOM::Element const&) const +void StyleResolver::absolutize_values(StyleProperties& style, DOM::Element const*) const { auto viewport_rect = document().browsing_context()->viewport_rect(); auto font_metrics = style.computed_font().metrics('M'); @@ -829,6 +829,15 @@ void StyleResolver::absolutize_values(StyleProperties& style, DOM::Element const } } +NonnullRefPtr<StyleProperties> StyleResolver::create_document_style() const +{ + auto style = StyleProperties::create(); + compute_font(style, nullptr); + compute_defaulted_values(style, nullptr); + absolutize_values(style, nullptr); + return style; +} + NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(DOM::Element& element) const { auto style = StyleProperties::create(); @@ -836,13 +845,13 @@ NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(DOM::Element& elemen compute_cascaded_values(style, element); // 2. Compute the font, since that may be needed for font-relative CSS units - compute_font(style, element); + compute_font(style, &element); // 3. Absolutize values, turning font/viewport relative lengths into absolute lengths - absolutize_values(style, element); + absolutize_values(style, &element); // 4. Default the values, applying inheritance and 'initial' as needed - compute_defaulted_values(style, element); + compute_defaulted_values(style, &element); return style; } diff --git a/Userland/Libraries/LibWeb/CSS/StyleResolver.h b/Userland/Libraries/LibWeb/CSS/StyleResolver.h index fc2f789467..b654d70df7 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleResolver.h +++ b/Userland/Libraries/LibWeb/CSS/StyleResolver.h @@ -30,6 +30,7 @@ public: DOM::Document& document() { return m_document; } DOM::Document const& document() const { return m_document; } + NonnullRefPtr<StyleProperties> create_document_style() const; NonnullRefPtr<StyleProperties> resolve_style(DOM::Element&) const; // https://www.w3.org/TR/css-cascade/#origin @@ -53,11 +54,11 @@ public: private: void compute_cascaded_values(StyleProperties&, DOM::Element&) const; - void compute_font(StyleProperties&, DOM::Element const&) const; - void compute_defaulted_values(StyleProperties&, DOM::Element const&) const; - void absolutize_values(StyleProperties&, DOM::Element const&) const; + void compute_font(StyleProperties&, DOM::Element const*) const; + void compute_defaulted_values(StyleProperties&, DOM::Element const*) const; + void absolutize_values(StyleProperties&, DOM::Element const*) const; - void compute_defaulted_property_value(StyleProperties&, DOM::Element const&, CSS::PropertyID) const; + void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID) const; template<typename Callback> void for_each_stylesheet(CascadeOrigin, Callback) const; diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index d29c28cba1..cb4295322e 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -451,7 +451,7 @@ void Document::update_style() RefPtr<Layout::Node> Document::create_layout_node() { - return adopt_ref(*new Layout::InitialContainingBlock(*this, CSS::StyleProperties::create())); + return adopt_ref(*new Layout::InitialContainingBlock(*this, style_resolver().create_document_style())); } void Document::set_link_color(Color color) |