Node.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 <LibXML/DOM/Node.h>
  8. namespace XML {
  9. bool Node::operator==(Node const& other) const
  10. {
  11. return content.visit(
  12. [&](Text const& text) -> bool {
  13. auto other_text = other.content.get_pointer<Text>();
  14. if (!other_text)
  15. return false;
  16. return text.builder.string_view() == other_text->builder.string_view();
  17. },
  18. [&](Comment const& comment) -> bool {
  19. auto other_comment = other.content.get_pointer<Comment>();
  20. if (!other_comment)
  21. return false;
  22. return comment.text == other_comment->text;
  23. },
  24. [&](Element const& element) -> bool {
  25. auto other_element = other.content.get_pointer<Element>();
  26. if (!other_element)
  27. return false;
  28. if (element.name != other_element->name)
  29. return false;
  30. if (element.attributes.size() != other_element->attributes.size())
  31. return false;
  32. for (auto& entry : element.attributes) {
  33. auto it = other_element->attributes.find(entry.key);
  34. if (it == other_element->attributes.end())
  35. return false;
  36. if (it->value != entry.value)
  37. return false;
  38. }
  39. if (element.children.size() != other_element->children.size())
  40. return false;
  41. for (size_t i = 0; i < element.children.size(); ++i) {
  42. if (element.children[i].ptr() != other_element->children[i].ptr())
  43. return false;
  44. }
  45. return true;
  46. });
  47. }
  48. }