ListOfActiveFormattingElements.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Element.h>
  7. #include <LibWeb/HTML/Parser/ListOfActiveFormattingElements.h>
  8. namespace Web::HTML {
  9. ListOfActiveFormattingElements::~ListOfActiveFormattingElements()
  10. {
  11. }
  12. void ListOfActiveFormattingElements::add(DOM::Element& element)
  13. {
  14. // FIXME: Implement the Noah's Ark clause https://html.spec.whatwg.org/multipage/parsing.html#push-onto-the-list-of-active-formatting-elements
  15. m_entries.append({ element });
  16. }
  17. void ListOfActiveFormattingElements::add_marker()
  18. {
  19. m_entries.append({ nullptr });
  20. }
  21. bool ListOfActiveFormattingElements::contains(const DOM::Element& element) const
  22. {
  23. for (auto& entry : m_entries) {
  24. if (entry.element == &element)
  25. return true;
  26. }
  27. return false;
  28. }
  29. DOM::Element* ListOfActiveFormattingElements::last_element_with_tag_name_before_marker(const FlyString& tag_name)
  30. {
  31. for (ssize_t i = m_entries.size() - 1; i >= 0; --i) {
  32. auto& entry = m_entries[i];
  33. if (entry.is_marker())
  34. return nullptr;
  35. if (entry.element->local_name() == tag_name)
  36. return entry.element;
  37. }
  38. return nullptr;
  39. }
  40. void ListOfActiveFormattingElements::remove(DOM::Element& element)
  41. {
  42. m_entries.remove_first_matching([&](auto& entry) {
  43. return entry.element == &element;
  44. });
  45. }
  46. void ListOfActiveFormattingElements::clear_up_to_the_last_marker()
  47. {
  48. while (!m_entries.is_empty()) {
  49. auto entry = m_entries.take_last();
  50. if (entry.is_marker())
  51. break;
  52. }
  53. }
  54. }