SVGStopElement.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/SVG/AttributeNames.h>
  11. #include <LibWeb/SVG/AttributeParser.h>
  12. #include <LibWeb/SVG/SVGStopElement.h>
  13. namespace Web::SVG {
  14. SVGStopElement::SVGStopElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : SVGElement(document, qualified_name)
  16. {
  17. }
  18. void SVGStopElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  19. {
  20. SVGElement::parse_attribute(name, value);
  21. if (name == SVG::AttributeNames::offset) {
  22. m_offset = AttributeParser::parse_number_percentage(value);
  23. }
  24. }
  25. void SVGStopElement::apply_presentational_hints(CSS::StyleProperties& style) const
  26. {
  27. CSS::Parser::ParsingContext parsing_context { document() };
  28. for_each_attribute([&](auto& name, auto& value) {
  29. CSS::Parser::ParsingContext parsing_context { document() };
  30. if (name.equals_ignoring_ascii_case("stop-color"sv)) {
  31. if (auto stop_color = parse_css_value(parsing_context, value, CSS::PropertyID::StopColor).release_value_but_fixme_should_propagate_errors()) {
  32. style.set_property(CSS::PropertyID::StopColor, stop_color.release_nonnull());
  33. }
  34. } else if (name.equals_ignoring_ascii_case("stop-opacity"sv)) {
  35. if (auto stop_opacity = parse_css_value(parsing_context, value, CSS::PropertyID::StopOpacity).release_value_but_fixme_should_propagate_errors()) {
  36. style.set_property(CSS::PropertyID::StopOpacity, stop_opacity.release_nonnull());
  37. }
  38. }
  39. });
  40. }
  41. Gfx::Color SVGStopElement::stop_color() const
  42. {
  43. if (auto css_values = computed_css_values())
  44. return css_values->stop_color();
  45. return Color::Black;
  46. }
  47. float SVGStopElement::stop_opacity() const
  48. {
  49. if (auto css_values = computed_css_values())
  50. return css_values->stop_opacity();
  51. return 1;
  52. }
  53. JS::NonnullGCPtr<SVGAnimatedNumber> SVGStopElement::offset() const
  54. {
  55. TODO();
  56. }
  57. JS::ThrowCompletionOr<void> SVGStopElement::initialize(JS::Realm& realm)
  58. {
  59. MUST_OR_THROW_OOM(Base::initialize(realm));
  60. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGStopElementPrototype>(realm, "SVGStopElement"));
  61. return {};
  62. }
  63. }