CountersSet.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. namespace Web::CSS {
  11. // "UAs may have implementation-specific limits on the maximum or minimum value of a counter.
  12. // If a counter reset, set, or increment would push the value outside of that range, the value
  13. // must be clamped to that range." - https://drafts.csswg.org/css-lists-3/#auto-numbering
  14. // So, we use a Checked<i32> and saturating addition/subtraction.
  15. using CounterValue = Checked<i32>;
  16. // https://drafts.csswg.org/css-lists-3/#counter
  17. struct Counter {
  18. FlyString name;
  19. i32 originating_element_id; // "creator"
  20. bool reversed { false };
  21. Optional<CounterValue> value;
  22. };
  23. // https://drafts.csswg.org/css-lists-3/#css-counters-set
  24. class CountersSet {
  25. public:
  26. CountersSet() = default;
  27. ~CountersSet() = default;
  28. Counter& instantiate_a_counter(FlyString name, i32 originating_element_id, bool reversed, Optional<CounterValue>);
  29. void set_a_counter(FlyString name, i32 originating_element_id, CounterValue value);
  30. void increment_a_counter(FlyString name, i32 originating_element_id, CounterValue amount);
  31. void append_copy(Counter const&);
  32. Optional<Counter&> last_counter_with_name(FlyString const& name);
  33. Optional<Counter&> counter_with_same_name_and_creator(FlyString const& name, i32 originating_element_id);
  34. Vector<Counter> const& counters() const { return m_counters; }
  35. bool is_empty() const { return m_counters.is_empty(); }
  36. private:
  37. Vector<Counter> m_counters;
  38. };
  39. }