Attr.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/WeakPtr.h>
  9. #include <LibWeb/DOM/Node.h>
  10. #include <LibWeb/DOM/QualifiedName.h>
  11. namespace Web::DOM {
  12. // https://dom.spec.whatwg.org/#attr
  13. class Attr final : public Node {
  14. WEB_PLATFORM_OBJECT(Attr, Node);
  15. public:
  16. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create(Document&, QualifiedName, DeprecatedString value = "", Element* = nullptr);
  17. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create(Document&, DeprecatedFlyString local_name, DeprecatedString value = "", Element* = nullptr);
  18. JS::NonnullGCPtr<Attr> clone(Document&);
  19. virtual ~Attr() override = default;
  20. virtual DeprecatedFlyString node_name() const override { return name(); }
  21. DeprecatedFlyString const& namespace_uri() const { return m_qualified_name.namespace_(); }
  22. DeprecatedFlyString const& prefix() const { return m_qualified_name.prefix(); }
  23. DeprecatedFlyString const& local_name() const { return m_qualified_name.local_name(); }
  24. DeprecatedString const& name() const { return m_qualified_name.as_string(); }
  25. DeprecatedString const& value() const { return m_value; }
  26. void set_value(DeprecatedString value);
  27. Element* owner_element();
  28. Element const* owner_element() const;
  29. void set_owner_element(Element* owner_element);
  30. // Always returns true: https://dom.spec.whatwg.org/#dom-attr-specified
  31. constexpr bool specified() const { return true; }
  32. void handle_attribute_changes(Element&, DeprecatedString const& old_value, DeprecatedString const& new_value);
  33. private:
  34. Attr(Document&, QualifiedName, DeprecatedString value, Element*);
  35. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  36. virtual void visit_edges(Cell::Visitor&) override;
  37. QualifiedName m_qualified_name;
  38. DeprecatedString m_value;
  39. JS::GCPtr<Element> m_owner_element;
  40. };
  41. template<>
  42. inline bool Node::fast_is<Attr>() const { return is_attribute(); }
  43. }