SVGStopElement.cpp 2.3 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. JS_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& value)
  20. {
  21. SVGElement::attribute_changed(name, value);
  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())
  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())
  51. return css_values->stop_opacity();
  52. return 1;
  53. }
  54. JS::NonnullGCPtr<SVGAnimatedNumber> SVGStopElement::offset() const
  55. {
  56. TODO();
  57. }
  58. void SVGStopElement::initialize(JS::Realm& realm)
  59. {
  60. Base::initialize(realm);
  61. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGStopElementPrototype>(realm, "SVGStopElement"_fly_string));
  62. }
  63. }