|
@@ -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 {};
|
|
|
}
|
|
|
|
|
|
}
|