CSSKeywordValue.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <LibWeb/CSS/CSSStyleValue.h>
  11. #include <LibWeb/CSS/Keyword.h>
  12. namespace Web::CSS {
  13. // https://drafts.css-houdini.org/css-typed-om-1/#csskeywordvalue
  14. class CSSKeywordValue : public StyleValueWithDefaultOperators<CSSKeywordValue> {
  15. public:
  16. static ValueComparingNonnullRefPtr<CSSKeywordValue> create(Keyword keyword)
  17. {
  18. // NOTE: We'll have to be much more careful with caching once we expose CSSKeywordValue to JS, as it's mutable.
  19. switch (keyword) {
  20. case Keyword::Inherit: {
  21. static ValueComparingNonnullRefPtr<CSSKeywordValue> const inherit_instance = adopt_ref(*new (nothrow) CSSKeywordValue(Keyword::Inherit));
  22. return inherit_instance;
  23. }
  24. case Keyword::Initial: {
  25. static ValueComparingNonnullRefPtr<CSSKeywordValue> const initial_instance = adopt_ref(*new (nothrow) CSSKeywordValue(Keyword::Initial));
  26. return initial_instance;
  27. }
  28. case Keyword::Revert: {
  29. static ValueComparingNonnullRefPtr<CSSKeywordValue> const revert_instance = adopt_ref(*new (nothrow) CSSKeywordValue(Keyword::Revert));
  30. return revert_instance;
  31. }
  32. case Keyword::RevertLayer: {
  33. static ValueComparingNonnullRefPtr<CSSKeywordValue> const revert_layer_instance = adopt_ref(*new (nothrow) CSSKeywordValue(Keyword::RevertLayer));
  34. return revert_layer_instance;
  35. }
  36. case Keyword::Unset: {
  37. static ValueComparingNonnullRefPtr<CSSKeywordValue> const unset_instance = adopt_ref(*new (nothrow) CSSKeywordValue(Keyword::Unset));
  38. return unset_instance;
  39. }
  40. default:
  41. return adopt_ref(*new (nothrow) CSSKeywordValue(keyword));
  42. }
  43. }
  44. virtual ~CSSKeywordValue() override = default;
  45. Keyword keyword() const { return m_keyword; }
  46. static bool is_color(Keyword);
  47. virtual bool has_color() const override;
  48. virtual Color to_color(Optional<Layout::NodeWithStyle const&> node) const override;
  49. virtual String to_string() const override;
  50. bool properties_equal(CSSKeywordValue const& other) const { return m_keyword == other.m_keyword; }
  51. private:
  52. explicit CSSKeywordValue(Keyword keyword)
  53. : StyleValueWithDefaultOperators(Type::Keyword)
  54. , m_keyword(keyword)
  55. {
  56. }
  57. Keyword m_keyword { Keyword::Invalid };
  58. };
  59. }