Skip to content

Truly independent web browser

LibWeb: Reset viewBox rectangles when the attribute is absent or invalid

Andreas Kling25227bdb7611
diff --git a/Libraries/LibWeb/SVG/SVGAnimatedRect.cpp b/Libraries/LibWeb/SVG/SVGAnimatedRect.cpp
index 6299feb1879..0670b0f254e 100644
--- a/Libraries/LibWeb/SVG/SVGAnimatedRect.cpp
+++ b/Libraries/LibWeb/SVG/SVGAnimatedRect.cpp
@@ -31,25 +31,6 @@ void SVGAnimatedRect::visit_edges(Visitor& visitor)
visitor.visit(m_anim_val);
}
-GC::Ptr<Geometry::DOMRect> SVGAnimatedRect::base_val() const
-{
- if (m_nulled)
- return nullptr;
- return m_base_val;
-}
-
-GC::Ptr<Geometry::DOMRect> SVGAnimatedRect::anim_val() const
-{
- if (m_nulled)
- return nullptr;
- return m_anim_val;
-}
-
-void SVGAnimatedRect::set_nulled(bool nulled)
-{
- m_nulled = nulled;
-}
-
void SVGAnimatedRect::set_base_val(Gfx::DoubleRect const& rect)
{
m_base_val->set(rect);
diff --git a/Libraries/LibWeb/SVG/SVGAnimatedRect.h b/Libraries/LibWeb/SVG/SVGAnimatedRect.h
index 26d54d4bfa6..7e105d17728 100644
--- a/Libraries/LibWeb/SVG/SVGAnimatedRect.h
+++ b/Libraries/LibWeb/SVG/SVGAnimatedRect.h
@@ -19,23 +19,19 @@ public:
[[nodiscard]] static GC::Ref<SVGAnimatedRect> create();
virtual ~SVGAnimatedRect();
- GC::Ptr<Geometry::DOMRect> base_val() const;
- GC::Ptr<Geometry::DOMRect> anim_val() const;
+ GC::Ref<Geometry::DOMRect> base_val() const { return m_base_val; }
+ GC::Ref<Geometry::DOMRect> anim_val() const { return m_anim_val; }
void set_base_val(Gfx::DoubleRect const&);
void set_anim_val(Gfx::DoubleRect const&);
- void set_nulled(bool);
-
private:
virtual void visit_edges(Visitor&) override;
SVGAnimatedRect();
- GC::Ptr<Geometry::DOMRect> m_base_val;
- GC::Ptr<Geometry::DOMRect> m_anim_val;
-
- bool m_nulled { true };
+ GC::Ref<Geometry::DOMRect> m_base_val;
+ GC::Ref<Geometry::DOMRect> m_anim_val;
};
}
diff --git a/Libraries/LibWeb/SVG/SVGAnimatedRect.idl b/Libraries/LibWeb/SVG/SVGAnimatedRect.idl
index 98dec30d35c..b0a52c0c241 100644
--- a/Libraries/LibWeb/SVG/SVGAnimatedRect.idl
+++ b/Libraries/LibWeb/SVG/SVGAnimatedRect.idl
@@ -1,9 +1,8 @@
// https://svgwg.org/svg2-draft/types.html#InterfaceSVGAnimatedRect
[Exposed=Window]
interface SVGAnimatedRect {
- // NOTE: The spec says that baseVal and animVal are not nullable, but they are nullable in some other engines.
- [SameObject] readonly attribute DOMRect? baseVal;
+ [SameObject] readonly attribute DOMRect baseVal;
- // NOTE: animVal is a DOMRectReadOnly in the spec, but other engines expose a DOMRect (sometimes aliased as SVGRect).
- [SameObject] readonly attribute DOMRect? animVal;
+ // INTEROP: Other engines expose animVal as DOMRect instead of DOMRectReadOnly.
+ [SameObject] readonly attribute DOMRect animVal;
};
diff --git a/Libraries/LibWeb/SVG/SVGFitToViewBox.cpp b/Libraries/LibWeb/SVG/SVGFitToViewBox.cpp
index 9e80ef1b4be..a1bdeaccdcd 100644
--- a/Libraries/LibWeb/SVG/SVGFitToViewBox.cpp
+++ b/Libraries/LibWeb/SVG/SVGFitToViewBox.cpp
@@ -27,17 +27,18 @@ void SVGFitToViewBox::visit_edges(JS::Cell::Visitor& visitor)
void SVGFitToViewBox::attribute_changed(DOM::Element& element, Utf16FlyString const& name, Optional<Utf16String> const& value)
{
if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox)) {
- if (!value.has_value()) {
- m_view_box_for_bindings->set_nulled(true);
- m_view_box = {};
- } else {
- m_view_box = parse_viewbox(value.value_or({}));
- m_view_box_for_bindings->set_nulled(!m_view_box.has_value());
- if (m_view_box.has_value()) {
- m_view_box_for_bindings->set_base_val(Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height });
- m_view_box_for_bindings->set_anim_val(Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height });
- }
- }
+ // https://svgwg.org/svg2-draft/types.html#InterfaceSVGAnimatedRect
+ // 1. Let value be the value of the reflected attribute (using the attribute's initial value if it is not present or invalid).
+ m_view_box = parse_viewbox(value.value_or({}));
+
+ // 2. Let x, y, width and height be those corresponding components of value.
+ auto rect = m_view_box.has_value()
+ ? Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height }
+ : Gfx::DoubleRect {};
+
+ // 3. Set the DOMRect object's x coordinate, y coordinate, width and height to x, y, width and height, respectively.
+ m_view_box_for_bindings->set_base_val(rect);
+ m_view_box_for_bindings->set_anim_val(rect);
element.set_needs_layout_update(DOM::SetNeedsLayoutReason::SVGViewBoxChange);
} else if (name.equals_ignoring_ascii_case(SVG::AttributeNames::preserveAspectRatio)) {
m_preserve_aspect_ratio = parse_preserve_aspect_ratio(value.value_or({}));
diff --git a/Tests/LibWeb/Text/expected/SVG/svg-viewBox-attribute.txt b/Tests/LibWeb/Text/expected/SVG/svg-viewBox-attribute.txt
index b8b49130145..1861676f4a1 100644
--- a/Tests/LibWeb/Text/expected/SVG/svg-viewBox-attribute.txt
+++ b/Tests/LibWeb/Text/expected/SVG/svg-viewBox-attribute.txt
@@ -1,7 +1,7 @@
[object SVGSVGElement]
[object SVGAnimatedRect]
-null
-null
+[object DOMRect]
+[object DOMRect]
[object DOMRect]
1
2
@@ -22,5 +22,5 @@ null
6
7
8
-null
-null
+[object DOMRect]
+[object DOMRect]
diff --git a/Tests/LibWeb/Text/expected/SVG/svg-viewbox-syntax.txt b/Tests/LibWeb/Text/expected/SVG/svg-viewbox-syntax.txt
index c06be80e0aa..39ddc666355 100644
--- a/Tests/LibWeb/Text/expected/SVG/svg-viewbox-syntax.txt
+++ b/Tests/LibWeb/Text/expected/SVG/svg-viewbox-syntax.txt
@@ -10,9 +10,9 @@
9
10
11
-null
-null
-null
-null
-null
-null
+[object DOMRect]
+[object DOMRect]
+[object DOMRect]
+[object DOMRect]
+[object DOMRect]
+[object DOMRect]
diff --git a/Tests/LibWeb/Text/expected/svg-viewbox-rectangle-reset.txt b/Tests/LibWeb/Text/expected/svg-viewbox-rectangle-reset.txt
index 8bcf4641a0e..80f1230bf13 100644
--- a/Tests/LibWeb/Text/expected/svg-viewbox-rectangle-reset.txt
+++ b/Tests/LibWeb/Text/expected/svg-viewbox-rectangle-reset.txt
@@ -1,40 +1,40 @@
-svg initial: null / null
-svg null: 1 2 3 4 / 1 2 3 4
-svg null identity: false
-svg "invalid": 1 2 3 4 / 1 2 3 4
-svg "invalid" identity: false
-svg "": 1 2 3 4 / 1 2 3 4
-svg "" identity: false
+svg initial: 0 0 0 0 / 0 0 0 0
+svg null: 0 0 0 0 / 0 0 0 0
+svg null identity: true
+svg "invalid": 0 0 0 0 / 0 0 0 0
+svg "invalid" identity: true
+svg "": 0 0 0 0 / 0 0 0 0
+svg "" identity: true
svg restored: 5 6 7 8 / 5 6 7 8
-symbol initial: null / null
-symbol null: 1 2 3 4 / 1 2 3 4
-symbol null identity: false
-symbol "invalid": 1 2 3 4 / 1 2 3 4
-symbol "invalid" identity: false
-symbol "": 1 2 3 4 / 1 2 3 4
-symbol "" identity: false
+symbol initial: 0 0 0 0 / 0 0 0 0
+symbol null: 0 0 0 0 / 0 0 0 0
+symbol null identity: true
+symbol "invalid": 0 0 0 0 / 0 0 0 0
+symbol "invalid" identity: true
+symbol "": 0 0 0 0 / 0 0 0 0
+symbol "" identity: true
symbol restored: 5 6 7 8 / 5 6 7 8
-marker initial: null / null
-marker null: 1 2 3 4 / 1 2 3 4
-marker null identity: false
-marker "invalid": 1 2 3 4 / 1 2 3 4
-marker "invalid" identity: false
-marker "": 1 2 3 4 / 1 2 3 4
-marker "" identity: false
+marker initial: 0 0 0 0 / 0 0 0 0
+marker null: 0 0 0 0 / 0 0 0 0
+marker null identity: true
+marker "invalid": 0 0 0 0 / 0 0 0 0
+marker "invalid" identity: true
+marker "": 0 0 0 0 / 0 0 0 0
+marker "" identity: true
marker restored: 5 6 7 8 / 5 6 7 8
-pattern initial: null / null
-pattern null: 1 2 3 4 / 1 2 3 4
-pattern null identity: false
-pattern "invalid": 1 2 3 4 / 1 2 3 4
-pattern "invalid" identity: false
-pattern "": 1 2 3 4 / 1 2 3 4
-pattern "" identity: false
+pattern initial: 0 0 0 0 / 0 0 0 0
+pattern null: 0 0 0 0 / 0 0 0 0
+pattern null identity: true
+pattern "invalid": 0 0 0 0 / 0 0 0 0
+pattern "invalid" identity: true
+pattern "": 0 0 0 0 / 0 0 0 0
+pattern "" identity: true
pattern restored: 5 6 7 8 / 5 6 7 8
-view initial: null / null
-view null: 1 2 3 4 / 1 2 3 4
-view null identity: false
-view "invalid": 1 2 3 4 / 1 2 3 4
-view "invalid" identity: false
-view "": 1 2 3 4 / 1 2 3 4
-view "" identity: false
+view initial: 0 0 0 0 / 0 0 0 0
+view null: 0 0 0 0 / 0 0 0 0
+view null identity: true
+view "invalid": 0 0 0 0 / 0 0 0 0
+view "invalid" identity: true
+view "": 0 0 0 0 / 0 0 0 0
+view "" identity: true
view restored: 5 6 7 8 / 5 6 7 8
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
index 084990c247d..f850963239b 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 1780 tests
-1593 Pass
-187 Fail
+1595 Pass
+185 Fail
Pass idl_test setup
Pass idl_test validation
Pass Partial interface Document: original interface defined
@@ -362,8 +362,8 @@ Pass SVGAnimatedRect interface: attribute baseVal
Pass SVGAnimatedRect interface: attribute animVal
Pass SVGAnimatedRect must be primary interface of objects.svg.viewBox
Pass Stringification of objects.svg.viewBox
-Fail SVGAnimatedRect interface: objects.svg.viewBox must inherit property "baseVal" with the proper type
-Fail SVGAnimatedRect interface: objects.svg.viewBox must inherit property "animVal" with the proper type
+Pass SVGAnimatedRect interface: objects.svg.viewBox must inherit property "baseVal" with the proper type
+Pass SVGAnimatedRect interface: objects.svg.viewBox must inherit property "animVal" with the proper type
Pass SVGAnimatedNumberList interface: existence and properties of interface object
Pass SVGAnimatedNumberList interface object length
Pass SVGAnimatedNumberList interface object name
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/types/scripted/SVGAnimatedRect.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/types/scripted/SVGAnimatedRect.txt
index 997c7b5e2dd..651a2e3a36f 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/types/scripted/SVGAnimatedRect.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/types/scripted/SVGAnimatedRect.txt
@@ -2,10 +2,10 @@ Harness status: OK
Found 5 tests
-2 Pass
-3 Fail
+4 Pass
+1 Fail
Fail SVGAnimatedRect interface - utilizing the viewBox property of SVGSVGElement
-Fail baseVal/animVal are zero rects when the viewBox attribute is unset
+Pass baseVal/animVal are zero rects when the viewBox attribute is unset
Pass baseVal/animVal return the same SVGRect across reads
Pass setAttribute('viewBox', ...) is reflected in baseVal/animVal
-Fail removeAttribute('viewBox') resets baseVal/animVal to a zero rect
\ No newline at end of file
+Pass removeAttribute('viewBox') resets baseVal/animVal to a zero rect
\ No newline at end of file