diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 53f8b796d5a..21247544e10 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -921,7 +921,6 @@ set(SOURCES SVG/SVGUseElement.cpp SVG/SVGViewElement.cpp SVG/TagNames.cpp - SVG/ViewBox.cpp TrustedTypes/TrustedHTML.cpp TrustedTypes/TrustedScript.cpp TrustedTypes/TrustedScriptURL.cpp diff --git a/Libraries/LibWeb/SVG/AttributeParser.cpp b/Libraries/LibWeb/SVG/AttributeParser.cpp index e266731e985..977ef3846c3 100644 --- a/Libraries/LibWeb/SVG/AttributeParser.cpp +++ b/Libraries/LibWeb/SVG/AttributeParser.cpp @@ -702,6 +702,48 @@ Optional> AttributeParser::parse_transform() return transform_list; } +Optional AttributeParser::parse_viewbox(StringView input) +{ + AttributeParser parser { input }; + ViewBox viewbox; + + parser.parse_whitespace(); + auto maybe_min_x = parser.parse_coordinate(); + if (maybe_min_x.is_error()) + return {}; + viewbox.min_x = maybe_min_x.value(); + + if (!parser.match_comma_whitespace()) + return {}; + parser.parse_comma_whitespace(); + auto maybe_min_y = parser.parse_coordinate(); + if (maybe_min_y.is_error()) + return {}; + viewbox.min_y = maybe_min_y.value(); + + if (!parser.match_comma_whitespace()) + return {}; + parser.parse_comma_whitespace(); + auto maybe_width = parser.parse_length(); + if (maybe_width.is_error()) + return {}; + viewbox.width = maybe_width.value(); + + if (!parser.match_comma_whitespace()) + return {}; + parser.parse_comma_whitespace(); + auto maybe_height = parser.parse_length(); + if (maybe_height.is_error()) + return {}; + viewbox.height = maybe_height.value(); + + parser.parse_whitespace(); + if (!parser.done()) + return {}; + + return viewbox; +} + bool AttributeParser::match_whitespace() const { if (done()) diff --git a/Libraries/LibWeb/SVG/AttributeParser.h b/Libraries/LibWeb/SVG/AttributeParser.h index ae836c19a87..703376860d1 100644 --- a/Libraries/LibWeb/SVG/AttributeParser.h +++ b/Libraries/LibWeb/SVG/AttributeParser.h @@ -76,6 +76,13 @@ enum class SVGUnits { UserSpaceOnUse }; +struct ViewBox { + double min_x { 0 }; + double min_y { 0 }; + double width { 0 }; + double height { 0 }; +}; + using GradientUnits = SVGUnits; using MaskUnits = SVGUnits; using MaskContentUnits = SVGUnits; @@ -141,6 +148,7 @@ public: static Optional parse_preserve_aspect_ratio(StringView input); static Optional parse_units(StringView input); static Optional parse_spread_method(StringView input); + static Optional parse_viewbox(StringView input); private: AttributeParser(StringView source); diff --git a/Libraries/LibWeb/SVG/SVGFitToViewBox.cpp b/Libraries/LibWeb/SVG/SVGFitToViewBox.cpp index 94dfc456b83..fe4ea3bd56e 100644 --- a/Libraries/LibWeb/SVG/SVGFitToViewBox.cpp +++ b/Libraries/LibWeb/SVG/SVGFitToViewBox.cpp @@ -28,7 +28,7 @@ void SVGFitToViewBox::attribute_changed(DOM::Element& element, FlyString const& if (!value.has_value()) { m_view_box_for_bindings->set_nulled(true); } else { - m_view_box = try_parse_view_box(value.value_or(String {})); + m_view_box = AttributeParser::parse_viewbox(value.value_or(String {})); 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 }); diff --git a/Libraries/LibWeb/SVG/SVGFitToViewBox.h b/Libraries/LibWeb/SVG/SVGFitToViewBox.h index bd9dd324b43..db224c2d7f6 100644 --- a/Libraries/LibWeb/SVG/SVGFitToViewBox.h +++ b/Libraries/LibWeb/SVG/SVGFitToViewBox.h @@ -9,7 +9,6 @@ #include #include #include -#include namespace Web::SVG { diff --git a/Libraries/LibWeb/SVG/SVGGraphicsElement.h b/Libraries/LibWeb/SVG/SVGGraphicsElement.h index de7b17198bc..77f255756f4 100644 --- a/Libraries/LibWeb/SVG/SVGGraphicsElement.h +++ b/Libraries/LibWeb/SVG/SVGGraphicsElement.h @@ -17,7 +17,6 @@ #include #include #include -#include namespace Web::SVG { diff --git a/Libraries/LibWeb/SVG/SVGSVGElement.h b/Libraries/LibWeb/SVG/SVGSVGElement.h index b4ac4fcc4c0..8c23136f2cc 100644 --- a/Libraries/LibWeb/SVG/SVGSVGElement.h +++ b/Libraries/LibWeb/SVG/SVGSVGElement.h @@ -15,7 +15,6 @@ #include #include #include -#include #include namespace Web::SVG { diff --git a/Libraries/LibWeb/SVG/ViewBox.cpp b/Libraries/LibWeb/SVG/ViewBox.cpp deleted file mode 100644 index bb4fa363abb..00000000000 --- a/Libraries/LibWeb/SVG/ViewBox.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2021, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include -#include -#include - -namespace Web::SVG { - -Optional try_parse_view_box(StringView string) -{ - // FIXME: This should handle all valid viewBox values. - - GenericLexer lexer(string); - - enum State { - MinX, - MinY, - Width, - Height, - }; - int state { State::MinX }; - ViewBox view_box; - - while (!lexer.is_eof()) { - lexer.consume_while([](auto ch) { return is_ascii_space(ch); }); - auto token = lexer.consume_until([](auto ch) { return is_ascii_space(ch) && ch != ','; }); - auto maybe_number = token.to_number(); - if (!maybe_number.has_value()) - return {}; - switch (state) { - case State::MinX: - view_box.min_x = maybe_number.value(); - break; - case State::MinY: - view_box.min_y = maybe_number.value(); - break; - case State::Width: - if (*maybe_number < 0) - return {}; - view_box.width = maybe_number.value(); - break; - case State::Height: - if (*maybe_number < 0) - return {}; - view_box.height = maybe_number.value(); - break; - default: - return {}; - } - state += 1; - } - - return view_box; -} - -} diff --git a/Libraries/LibWeb/SVG/ViewBox.h b/Libraries/LibWeb/SVG/ViewBox.h deleted file mode 100644 index b925a5bef0b..00000000000 --- a/Libraries/LibWeb/SVG/ViewBox.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2021, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include - -namespace Web::SVG { - -struct ViewBox { - double min_x { 0 }; - double min_y { 0 }; - double width { 0 }; - double height { 0 }; -}; - -Optional try_parse_view_box(StringView); - -} diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/SVG/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/SVG/BUILD.gn index e233f17e92c..e620d0c16aa 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/SVG/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/SVG/BUILD.gn @@ -48,6 +48,5 @@ source_set("SVG") { "SVGTransformList.cpp", "SVGUseElement.cpp", "TagNames.cpp", - "ViewBox.cpp", ] } diff --git a/Tests/LibWeb/Text/expected/SVG/svg-viewbox-syntax.txt b/Tests/LibWeb/Text/expected/SVG/svg-viewbox-syntax.txt new file mode 100644 index 00000000000..c06be80e0aa --- /dev/null +++ b/Tests/LibWeb/Text/expected/SVG/svg-viewbox-syntax.txt @@ -0,0 +1,18 @@ +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +null +null +null +null +null +null diff --git a/Tests/LibWeb/Text/input/SVG/svg-viewbox-syntax.html b/Tests/LibWeb/Text/input/SVG/svg-viewbox-syntax.html new file mode 100644 index 00000000000..ff2529015ad --- /dev/null +++ b/Tests/LibWeb/Text/input/SVG/svg-viewbox-syntax.html @@ -0,0 +1,35 @@ + + + +