Node.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteString.h>
  8. #include <AK/GenericLexer.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/Variant.h>
  11. #include <AK/Vector.h>
  12. #include <LibXML/FundamentalTypes.h>
  13. namespace XML {
  14. struct Attribute {
  15. Name name;
  16. ByteString value;
  17. };
  18. struct Node {
  19. struct Text {
  20. StringBuilder builder;
  21. };
  22. struct Comment {
  23. ByteString text;
  24. };
  25. struct Element {
  26. Name name;
  27. HashMap<Name, ByteString> attributes;
  28. Vector<NonnullOwnPtr<Node>> children;
  29. };
  30. bool operator==(Node const&) const;
  31. LineTrackingLexer::Position offset;
  32. Variant<Text, Comment, Element> content;
  33. Node* parent { nullptr };
  34. bool is_text() const { return content.has<Text>(); }
  35. Text const& as_text() const { return content.get<Text>(); }
  36. bool is_comment() const { return content.has<Comment>(); }
  37. Comment const& as_comment() const { return content.get<Comment>(); }
  38. bool is_element() const { return content.has<Element>(); }
  39. Element const& as_element() const { return content.get<Element>(); }
  40. };
  41. }