1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:48:11 +00:00
serenity/Userland/Libraries/LibWeb/DOM/Attribute.h
Timothy Flynn e01dfaac9a LibWeb: Implement Attribute closer to the spec and with an IDL file
Note our Attribute class is what the spec refers to as just "Attr". The
main differences between the existing implementation and the spec are
just that the spec defines more fields.

Attributes can contain namespace URIs and prefixes. However, note that
these are not parsed in HTML documents unless the document content-type
is XML. So for now, these are initialized to null. Web pages are able to
set the namespace via JavaScript (setAttributeNS), so these fields may
be filled in when the corresponding APIs are implemented.

The main change to be aware of is that an attribute is a node. This has
implications on how attributes are stored in the Element class. Nodes
are non-copyable and non-movable because these constructors are deleted
by the EventTarget base class. This means attributes cannot be stored in
a Vector or HashMap as these containers assume copyability / movability.
So for now, the Vector holding attributes is changed to hold RefPtrs to
attributes instead. This might change when attribute storage is
implemented according to the spec (by way of NamedNodeMap).
2021-10-17 13:51:10 +01:00

48 lines
1.5 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/QualifiedName.h>
namespace Web::DOM {
// https://dom.spec.whatwg.org/#attr
class Attribute final : public Node {
public:
using WrapperType = Bindings::AttributeWrapper;
static NonnullRefPtr<Attribute> create(Document&, FlyString local_name, String value);
virtual ~Attribute() override = default;
virtual FlyString node_name() const override { return name(); }
FlyString const& namespace_uri() const { return m_qualified_name.namespace_(); }
FlyString const& prefix() const { return m_qualified_name.prefix(); }
FlyString const& local_name() const { return m_qualified_name.local_name(); }
String const& name() const { return m_qualified_name.as_string(); }
String const& value() const { return m_value; }
void set_value(String value) { m_value = move(value); }
Element const* owner_element() const { return m_owner_element; }
void set_owner_element(Element const* owner_element) { m_owner_element = owner_element; }
// Always returns true: https://dom.spec.whatwg.org/#dom-attr-specified
constexpr bool specified() const { return true; }
private:
Attribute(Document&, FlyString local_name, String value);
QualifiedName m_qualified_name;
String m_value;
Element const* m_owner_element { nullptr };
};
}