1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:07:35 +00:00

LibWeb: Use width & height to create fallback viewBox for SVG-as-image

When embedding an SVG in an img element, if the external SVG's root
element has both width and height attributes, but no viewBox attribute,
we now create a fallback viewBox with "0 0 width height".

This appears to match the behavior of other browsers. Inspired by
discussion on Mozilla's bug tracker:
https://bugzilla.mozilla.org/show_bug.cgi?id=614649
This commit is contained in:
Andreas Kling 2023-06-20 09:39:14 +02:00
parent a0b4987e92
commit 9f24c1b34c
6 changed files with 66 additions and 2 deletions

View file

@ -7,6 +7,7 @@
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
@ -77,6 +78,53 @@ void SVGSVGElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedS
m_view_box = try_parse_view_box(value);
if (name.equals_ignoring_ascii_case(SVG::AttributeNames::preserveAspectRatio))
m_preserve_aspect_ratio = AttributeParser::parse_preserve_aspect_ratio(value);
if (name.equals_ignoring_ascii_case(SVG::AttributeNames::width) || name.equals_ignoring_ascii_case(SVG::AttributeNames::height))
update_fallback_view_box_for_svg_as_image();
}
void SVGSVGElement::update_fallback_view_box_for_svg_as_image()
{
// AD-HOC: This creates a fallback viewBox for SVGs used as images.
// If the <svg> element has width and height, but no viewBox,
// we fall back to a synthetic viewBox="0 0 width height".
Optional<double> width;
Optional<double> height;
auto width_attribute = attribute(SVG::AttributeNames::width);
auto parsing_context = CSS::Parser::ParsingContext { document() };
if (auto width_value = parse_css_value(parsing_context, attribute(Web::HTML::AttributeNames::width), CSS::PropertyID::Width).release_value_but_fixme_should_propagate_errors()) {
if (width_value->is_length() && width_value->as_length().length().is_absolute())
width = width_value->as_length().length().absolute_length_to_px().to_double();
}
auto height_attribute = attribute(SVG::AttributeNames::height);
if (auto height_value = parse_css_value(parsing_context, attribute(Web::HTML::AttributeNames::height), CSS::PropertyID::Height).release_value_but_fixme_should_propagate_errors()) {
if (height_value->is_length() && height_value->as_length().length().is_absolute())
height = height_value->as_length().length().absolute_length_to_px().to_double();
}
if (width.has_value() && height.has_value()) {
m_fallback_view_box_for_svg_as_image = ViewBox { 0, 0, width.value(), height.value() };
} else {
m_fallback_view_box_for_svg_as_image = {};
}
}
void SVGSVGElement::set_fallback_view_box_for_svg_as_image(Optional<ViewBox> view_box)
{
m_fallback_view_box_for_svg_as_image = view_box;
}
Optional<ViewBox> SVGSVGElement::view_box() const
{
if (m_view_box.has_value())
return m_view_box;
if (m_fallback_view_box_for_svg_as_image.has_value())
return m_fallback_view_box_for_svg_as_image;
return {};
}
}