CSSKeywordValue.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.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::Unset: {
  33. static ValueComparingNonnullRefPtr<CSSKeywordValue> const unset_instance = adopt_ref(*new (nothrow) CSSKeywordValue(Keyword::Unset));
  34. return unset_instance;
  35. }
  36. default:
  37. return adopt_ref(*new (nothrow) CSSKeywordValue(keyword));
  38. }
  39. }
  40. virtual ~CSSKeywordValue() override = default;
  41. Keyword keyword() const { return m_keyword; }
  42. static bool is_color(Keyword);
  43. virtual bool has_color() const override;
  44. virtual Color to_color(Optional<Layout::NodeWithStyle const&> node) const override;
  45. virtual String to_string() const override;
  46. bool properties_equal(CSSKeywordValue const& other) const { return m_keyword == other.m_keyword; }
  47. private:
  48. explicit CSSKeywordValue(Keyword keyword)
  49. : StyleValueWithDefaultOperators(Type::Keyword)
  50. , m_keyword(keyword)
  51. {
  52. }
  53. Keyword m_keyword { Keyword::Invalid };
  54. };
  55. }