Truly independent web browser
LibWeb: Evaluate SVG conditional processing attributes
Andreas Kling59830af62d95
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cppindex 471efe525b2..5d05d72cf5e 100644--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp@@ -902,6 +902,17 @@ void record_element_associated_with_animation(DOM::Element& element)note_waiting_children_may_arrive(*style_engine, element.document());}+// The box the element asks layout for.+static CSS::ElementBoxKind box_kind_for_layout(DOM::Element const& element)+{+ // https://svgwg.org/svg2-draft/struct.html#ConditionalProcessing+ // NB: An element whose conditional processing attributes evaluate to false, and its children, are skipped and thus+ // will not be rendered.+ if (auto const* svg_graphics_element = as_if<SVG::SVGGraphicsElement>(element); svg_graphics_element && !svg_graphics_element->conditional_processing_attributes_evaluate_to_true())+ return CSS::ElementBoxKind::NoBox;+ return element.box_kind();+}+void record_waiting_children_may_be_revealed(DOM::Element& element){if (auto* style_engine = style_engine_for(element))@@ -1019,7 +1030,7 @@ static void publish_element_selector_features(StyleEngine& style_engine, DOM::El.heading_level = heading_level,.is_slot = is_slot,.associated_pseudo_kind_plus_one = associated_pseudo_kind_plus_one(element),- .box_kind = to_underlying(element.box_kind()),+ .box_kind = to_underlying(box_kind_for_layout(element)),.adjustment_facts = element_style_adjustment_facts(element),.construction_facts = element_construction_facts(element),},@@ -1366,7 +1377,7 @@ void record_element_adjustment_facts(DOM::Element& element)return;StyleEngineFFI::style_engine_set_element_adjustment_facts(style_engine->host(), element.style_node_id(), element_style_adjustment_facts(element));StyleEngineFFI::style_engine_set_element_construction_facts(style_engine->host(), element.style_node_id(), element_construction_facts(element));- StyleEngineFFI::style_engine_set_element_box_kind(style_engine->host(), element.style_node_id(), to_underlying(element.box_kind()));+ StyleEngineFFI::style_engine_set_element_box_kind(style_engine->host(), element.style_node_id(), to_underlying(box_kind_for_layout(element)));StyleEngineFFI::style_engine_set_element_associated_pseudo_kind(style_engine->host(), element.style_node_id(), associated_pseudo_kind_plus_one(element));}@@ -1382,7 +1393,7 @@ void record_element_box_kind(DOM::Element& element)auto* style_engine = style_engine_for_identified(element);if (!style_engine)return;- StyleEngineFFI::style_engine_set_element_box_kind(style_engine->host(), element.style_node_id(), to_underlying(element.box_kind()));+ StyleEngineFFI::style_engine_set_element_box_kind(style_engine->host(), element.style_node_id(), to_underlying(box_kind_for_layout(element)));}static void set_replaced_content_input(StyleEngine& style_engine, StyleNodeID node, StyleEngineFFI::FfiReplacedContentInputKind kind, u8 present, u32 first, u32 second = 0, u32 third = 0, u32 fourth = 0)diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rsindex cc436ab3ac8..2580f382b92 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs@@ -1782,8 +1782,10 @@ fn update_layout_tree_for_svg_switch_children(let mut rendered_child = None;let mut child = host.first_dom_child(switch_element);while let Some(current) = child {- // FIXME: Evaluate the requiredExtensions and systemLanguage attributes.- if host.element_type_facts(Some(current)) & element_adjustment_fact::IS_SVG_ELEMENT != 0 {+ // NB: A child whose conditional processing attributes evaluate to false asks for no box.+ if host.element_type_facts(Some(current)) & element_adjustment_fact::IS_SVG_ELEMENT != 0+ && host.arena().element_box_kind(Some(current)) != ElementBoxKind::NoBox+ {rendered_child = Some(current);break;}diff --git a/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp b/Libraries/LibWeb/SVG/SVGGraphicsElement.cppindex 1c7fa98c7d9..e7749315c36 100644--- a/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp+++ b/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp@@ -7,8 +7,10 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/AllOf.h>#include <LibWeb/Bindings/SVGGraphicsElement.h>#include <LibWeb/CSS/Parser/Parser.h>+#include <LibWeb/CSS/StyleEngineInput.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/DOM/ShadowRoot.h>#include <LibWeb/Geometry/DOMMatrix.h>@@ -16,6 +18,8 @@#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/Layout/Node.h>#include <LibWeb/Layout/Viewport.h>+#include <LibWeb/Loader/ResourceLoader.h>+#include <LibWeb/Namespace.h>#include <LibWeb/Page/Page.h>#include <LibWeb/Painting/BoxViews.h>#include <LibWeb/SVG/AttributeNames.h>@@ -50,14 +54,80 @@ void SVGGraphicsElement::attribute_changed(Utf16FlyString const& name, Optional<// immediately after the value changed.if (namespace_.has_value())return;- if (name == AttributeNames::requiredExtensions && m_required_extensions)- m_required_extensions->synchronize();- else if (name == AttributeNames::systemLanguage && m_system_language)- m_system_language->synchronize();- else if (name == AttributeNames::transform && m_transform)+ if (name == AttributeNames::requiredExtensions || name == AttributeNames::systemLanguage) {+ if (name == AttributeNames::requiredExtensions && m_required_extensions)+ m_required_extensions->synchronize();+ else if (name == AttributeNames::systemLanguage && m_system_language)+ m_system_language->synchronize();++ // NB: Whether the conditional processing attributes evaluate to true decides whether the element has a box.+ CSS::record_element_box_kind(*this);+ set_needs_layout_tree_update(true, DOM::SetNeedsLayoutTreeUpdateReason::StyleChange);+ return;+ }+ if (name == AttributeNames::transform && m_transform)m_transform->synchronize();}+// https://svgwg.org/svg2-draft/struct.html#ConditionalProcessingRequiredExtensionsAttribute+static bool required_extensions_evaluate_to_true(Utf16View value)+{+ // The value is a list of URL references which identify the required extensions, with the individual values+ // separated by white space. Determines whether all of the named extensions are supported by the user agent. If all+ // of the given extensions are supported, then the attribute evaluates to true; otherwise, the current element and+ // its children are skipped and thus will not be rendered.+ // If a null string or empty string value is given to attribute ‘requiredExtensions’, the attribute evaluates to+ // "false".+ auto extensions = SVGStringList::parse(value, SVGStringList::Separator::Whitespace);+ if (extensions.is_empty())+ return false;++ // INTEROP: Other browsers treat the HTML and MathML namespaces as supported extensions, since their content can be+ // embedded with foreignObject.+ return all_of(extensions, [](auto const& extension) {+ return extension == Namespace::HTML || extension == Namespace::MathML;+ });+}++// https://svgwg.org/svg2-draft/struct.html#ConditionalProcessingSystemLanguageAttribute+static bool system_language_evaluates_to_true(Utf16View value)+{+ // If a null string or empty string value is given to attribute ‘systemLanguage’, the attribute evaluates to+ // "false".+ if (value.is_empty())+ return false;++ // Evaluates to "true" if one of the language tags indicated by user preferences is a case-insensitive match of one+ // of the language tags given in the value of this parameter, or if one of the language tags indicated by user+ // preferences is a case-insensitive prefix of one of the language tags given in the value of this parameter such+ // that the first tag character following the prefix is "-".+ auto language_tags = SVGStringList::parse(value, SVGStringList::Separator::Comma);+ for (auto const& preferred_language : ResourceLoader::the().preferred_languages()) {+ auto preferred_tag = Utf16String::from_utf8(preferred_language);+ for (auto const& language_tag : language_tags) {+ if (language_tag.equals_ignoring_ascii_case(preferred_tag))+ return true;+ if (language_tag.length_in_code_units() > preferred_tag.length_in_code_units()+ && language_tag.code_unit_at(preferred_tag.length_in_code_units()) == '-'+ && language_tag.utf16_view().substring_view(0, preferred_tag.length_in_code_units()).equals_ignoring_ascii_case(preferred_tag))+ return true;+ }+ }++ // Evaluates to "false" otherwise.+ return false;+}++bool SVGGraphicsElement::conditional_processing_attributes_evaluate_to_true() const+{+ // If the attribute is not present, then it implicitly evaluates to "true".+ if (auto required_extensions = attribute(AttributeNames::requiredExtensions); required_extensions.has_value() && !required_extensions_evaluate_to_true(*required_extensions))+ return false;+ if (auto system_language = attribute(AttributeNames::systemLanguage); system_language.has_value() && !system_language_evaluates_to_true(*system_language))+ return false;+ return true;+}+// https://svgwg.org/svg2-draft/types.html#__svg__SVGTests__requiredExtensionsGC::Ref<SVGStringList> SVGGraphicsElement::required_extensions_for_bindings(){diff --git a/Libraries/LibWeb/SVG/SVGGraphicsElement.h b/Libraries/LibWeb/SVG/SVGGraphicsElement.hindex 6965b17dde9..3990a3f42f0 100644--- a/Libraries/LibWeb/SVG/SVGGraphicsElement.h+++ b/Libraries/LibWeb/SVG/SVGGraphicsElement.h@@ -52,6 +52,9 @@ public:GC::Ref<SVGStringList> required_extensions_for_bindings();GC::Ref<SVGStringList> system_language_for_bindings();+ // https://svgwg.org/svg2-draft/struct.html#ConditionalProcessing+ bool conditional_processing_attributes_evaluate_to_true() const;+protected:SVGGraphicsElement(DOM::Document&, DOM::QualifiedName);diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/struct/systemLanguage-matching.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/struct/systemLanguage-matching.txtindex f2cf61301fe..3fede760320 100644--- a/Tests/LibWeb/Text/expected/wpt-import/svg/struct/systemLanguage-matching.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/struct/systemLanguage-matching.txt@@ -2,19 +2,18 @@ Harness status: OKFound 14 tests-8 Pass-6 Fail+14 PassPass Exact match with navigator.language renders the elementPass Case-insensitive exact match renders the element-Fail Non-matching language tag does not render-Fail Empty systemLanguage evaluates to false+Pass Non-matching language tag does not render+Pass Empty systemLanguage evaluates to falsePass Absent systemLanguage evaluates to truePass Comma-separated list with matching tag first rendersPass Comma-separated list with matching tag second rendersPass Comma-separated list with matching tag in the middle renders-Fail Comma-separated list with no matching tag does not render+Pass Comma-separated list with no matching tag does not renderPass BCP 47 prefix matching: user language prefix matches listed tag with extra subtagPass switch element: first matching child renders, others do not-Fail switch element: skips non-matching, renders first match-Fail switch element: fallback child without systemLanguage renders when no match-Fail switch element: nothing renders when no child matches and no fallback\ No newline at end of file+Pass switch element: skips non-matching, renders first match+Pass switch element: fallback child without systemLanguage renders when no match+Pass switch element: nothing renders when no child matches and no fallback\ No newline at end of file