SVGStopElement.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/Bindings/SVGStopElementPrototype.h>
  8. #include <LibWeb/CSS/Parser/Parser.h>
  9. #include <LibWeb/CSS/StyleProperties.h>
  10. #include <LibWeb/SVG/AttributeNames.h>
  11. #include <LibWeb/SVG/AttributeParser.h>
  12. #include <LibWeb/SVG/SVGStopElement.h>
  13. namespace Web::SVG {
  14. GC_DEFINE_ALLOCATOR(SVGStopElement);
  15. SVGStopElement::SVGStopElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : SVGElement(document, qualified_name)
  17. {
  18. }
  19. void SVGStopElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  20. {
  21. Base::attribute_changed(name, old_value, value, namespace_);
  22. if (name == SVG::AttributeNames::offset) {
  23. m_offset = AttributeParser::parse_number_percentage(value.value_or(String {}));
  24. }
  25. }
  26. void SVGStopElement::apply_presentational_hints(CSS::StyleProperties& style) const
  27. {
  28. CSS::Parser::ParsingContext parsing_context { document() };
  29. for_each_attribute([&](auto& name, auto& value) {
  30. CSS::Parser::ParsingContext parsing_context { document() };
  31. if (name.equals_ignoring_ascii_case("stop-color"sv)) {
  32. if (auto stop_color = parse_css_value(parsing_context, value, CSS::PropertyID::StopColor)) {
  33. style.set_property(CSS::PropertyID::StopColor, stop_color.release_nonnull());
  34. }
  35. } else if (name.equals_ignoring_ascii_case("stop-opacity"sv)) {
  36. if (auto stop_opacity = parse_css_value(parsing_context, value, CSS::PropertyID::StopOpacity)) {
  37. style.set_property(CSS::PropertyID::StopOpacity, stop_opacity.release_nonnull());
  38. }
  39. }
  40. });
  41. }
  42. Gfx::Color SVGStopElement::stop_color() const
  43. {
  44. if (auto css_values = computed_css_values(); css_values.has_value())
  45. return css_values->stop_color();
  46. return Color::Black;
  47. }
  48. float SVGStopElement::stop_opacity() const
  49. {
  50. if (auto css_values = computed_css_values(); css_values.has_value())
  51. return css_values->stop_opacity();
  52. return 1;
  53. }
  54. GC::Ref<SVGAnimatedNumber> SVGStopElement::offset() const
  55. {
  56. // FIXME: Implement this properly.
  57. return SVGAnimatedNumber::create(realm(), 0, 0);
  58. }
  59. void SVGStopElement::initialize(JS::Realm& realm)
  60. {
  61. Base::initialize(realm);
  62. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGStopElement);
  63. }
  64. }