Node.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashMap.h>
  7. #include <AK/NonnullOwnPtr.h>
  8. #include <LibXML/DOM/Node.h>
  9. namespace XML {
  10. bool Node::operator==(Node const& other) const
  11. {
  12. return content.visit(
  13. [&](Text const& text) -> bool {
  14. auto other_text = other.content.get_pointer<Text>();
  15. if (!other_text)
  16. return false;
  17. return text.builder.string_view() == other_text->builder.string_view();
  18. },
  19. [&](Comment const& comment) -> bool {
  20. auto other_comment = other.content.get_pointer<Comment>();
  21. if (!other_comment)
  22. return false;
  23. return comment.text == other_comment->text;
  24. },
  25. [&](Element const& element) -> bool {
  26. auto other_element = other.content.get_pointer<Element>();
  27. if (!other_element)
  28. return false;
  29. if (element.name != other_element->name)
  30. return false;
  31. if (element.attributes.size() != other_element->attributes.size())
  32. return false;
  33. for (auto& entry : element.attributes) {
  34. auto it = other_element->attributes.find(entry.key);
  35. if (it == other_element->attributes.end())
  36. return false;
  37. if (it->value != entry.value)
  38. return false;
  39. }
  40. if (element.children.size() != other_element->children.size())
  41. return false;
  42. for (size_t i = 0; i < element.children.size(); ++i) {
  43. if (element.children[i].ptr() != other_element->children[i].ptr())
  44. return false;
  45. }
  46. return true;
  47. });
  48. }
  49. }