CountersSet.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Checked.h>
  8. #include <AK/FlyString.h>
  9. #include <AK/Optional.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::CSS {
  12. // "UAs may have implementation-specific limits on the maximum or minimum value of a counter.
  13. // If a counter reset, set, or increment would push the value outside of that range, the value
  14. // must be clamped to that range." - https://drafts.csswg.org/css-lists-3/#auto-numbering
  15. // So, we use a Checked<i32> and saturating addition/subtraction.
  16. using CounterValue = Checked<i32>;
  17. // https://drafts.csswg.org/css-lists-3/#counter
  18. struct Counter {
  19. FlyString name;
  20. UniqueNodeID originating_element_id; // "creator"
  21. bool reversed { false };
  22. Optional<CounterValue> value;
  23. };
  24. // https://drafts.csswg.org/css-lists-3/#css-counters-set
  25. class CountersSet {
  26. public:
  27. CountersSet() = default;
  28. ~CountersSet() = default;
  29. Counter& instantiate_a_counter(FlyString name, UniqueNodeID originating_element_id, bool reversed, Optional<CounterValue>);
  30. void set_a_counter(FlyString name, UniqueNodeID originating_element_id, CounterValue value);
  31. void increment_a_counter(FlyString name, UniqueNodeID originating_element_id, CounterValue amount);
  32. void append_copy(Counter const&);
  33. Optional<Counter&> last_counter_with_name(FlyString const& name);
  34. Optional<Counter&> counter_with_same_name_and_creator(FlyString const& name, UniqueNodeID originating_element_id);
  35. Vector<Counter> const& counters() const { return m_counters; }
  36. bool is_empty() const { return m_counters.is_empty(); }
  37. private:
  38. Vector<Counter> m_counters;
  39. };
  40. }