2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-02-07 07:20:15 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-06-15 13:55:47 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-03-22 09:10:04 -03:00
|
|
|
#include <AK/FlyString.h>
|
2019-09-06 10:34:26 -03:00
|
|
|
#include <AK/String.h>
|
2021-05-28 16:21:44 -03:00
|
|
|
#include <LibWeb/CSS/CSSStyleDeclaration.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleResolver.h>
|
2020-05-22 16:45:00 -03:00
|
|
|
#include <LibWeb/DOM/Attribute.h>
|
2021-02-19 15:13:08 -03:00
|
|
|
#include <LibWeb/DOM/ExceptionOr.h>
|
2020-08-03 15:30:02 -03:00
|
|
|
#include <LibWeb/DOM/NonDocumentTypeChildNode.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/DOM/ParentNode.h>
|
2020-08-12 09:46:53 -03:00
|
|
|
#include <LibWeb/HTML/AttributeNames.h>
|
2021-09-08 19:59:09 -03:00
|
|
|
#include <LibWeb/HTML/EventLoop/Task.h>
|
2020-12-13 13:54:40 -03:00
|
|
|
#include <LibWeb/HTML/TagNames.h>
|
2020-11-22 11:53:01 -03:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2020-10-09 22:48:05 -03:00
|
|
|
#include <LibWeb/QualifiedName.h>
|
2019-10-14 13:32:02 -03:00
|
|
|
|
2020-07-26 14:37:56 -03:00
|
|
|
namespace Web::DOM {
|
2019-06-15 13:55:47 -03:00
|
|
|
|
2020-08-03 15:30:02 -03:00
|
|
|
class Element
|
|
|
|
|
: public ParentNode
|
|
|
|
|
, public NonDocumentTypeChildNode<Element> {
|
|
|
|
|
|
2019-06-15 13:55:47 -03:00
|
|
|
public:
|
2020-03-25 14:53:20 -03:00
|
|
|
using WrapperType = Bindings::ElementWrapper;
|
|
|
|
|
|
2021-02-07 07:20:15 -03:00
|
|
|
Element(Document&, QualifiedName);
|
2019-06-15 13:55:47 -03:00
|
|
|
virtual ~Element() override;
|
|
|
|
|
|
2021-05-04 18:25:43 -03:00
|
|
|
const String& qualified_name() const { return m_qualified_name.as_string(); }
|
2021-05-11 11:17:11 -03:00
|
|
|
const String& html_uppercased_qualified_name() const { return m_html_uppercased_qualified_name; }
|
2021-05-04 18:25:43 -03:00
|
|
|
virtual FlyString node_name() const final { return html_uppercased_qualified_name(); }
|
2020-10-09 22:48:05 -03:00
|
|
|
const FlyString& local_name() const { return m_qualified_name.local_name(); }
|
2020-07-23 13:18:13 -03:00
|
|
|
|
|
|
|
|
// NOTE: This is for the JS bindings
|
2021-05-11 11:17:11 -03:00
|
|
|
const String& tag_name() const { return html_uppercased_qualified_name(); }
|
2019-06-15 13:55:47 -03:00
|
|
|
|
2021-04-13 20:23:39 -03:00
|
|
|
const FlyString& prefix() const { return m_qualified_name.prefix(); }
|
2020-10-09 22:48:05 -03:00
|
|
|
const FlyString& namespace_() const { return m_qualified_name.namespace_(); }
|
|
|
|
|
|
|
|
|
|
// NOTE: This is for the JS bindings
|
|
|
|
|
const FlyString& namespace_uri() const { return namespace_(); }
|
|
|
|
|
|
2021-07-28 08:34:05 -03:00
|
|
|
bool has_attribute(const FlyString& name) const { return find_attribute(name) != nullptr; }
|
2020-12-29 17:37:40 -03:00
|
|
|
bool has_attributes() const { return !m_attributes.is_empty(); }
|
2020-03-22 09:10:04 -03:00
|
|
|
String attribute(const FlyString& name) const;
|
2020-06-21 06:39:32 -03:00
|
|
|
String get_attribute(const FlyString& name) const { return attribute(name); }
|
2021-02-19 15:13:08 -03:00
|
|
|
ExceptionOr<void> set_attribute(const FlyString& name, const String& value);
|
2020-08-02 11:15:15 -03:00
|
|
|
void remove_attribute(const FlyString& name);
|
2021-09-13 07:54:24 -03:00
|
|
|
size_t attribute_list_size() const { return m_attributes.size(); }
|
2019-06-15 16:08:36 -03:00
|
|
|
|
|
|
|
|
template<typename Callback>
|
2019-06-15 17:49:44 -03:00
|
|
|
void for_each_attribute(Callback callback) const
|
2019-06-15 16:08:36 -03:00
|
|
|
{
|
|
|
|
|
for (auto& attribute : m_attributes)
|
|
|
|
|
callback(attribute.name(), attribute.value());
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 21:06:20 -03:00
|
|
|
bool has_class(const FlyString&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2020-06-12 08:23:07 -03:00
|
|
|
const Vector<FlyString>& class_names() const { return m_classes; }
|
2019-06-27 15:40:21 -03:00
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
virtual void apply_presentational_hints(CSS::StyleProperties&) const { }
|
2020-03-22 09:10:04 -03:00
|
|
|
virtual void parse_attribute(const FlyString& name, const String& value);
|
2019-10-04 16:05:52 -03:00
|
|
|
|
2019-10-14 13:32:02 -03:00
|
|
|
void recompute_style();
|
|
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
Layout::NodeWithStyle* layout_node() { return static_cast<Layout::NodeWithStyle*>(Node::layout_node()); }
|
|
|
|
|
const Layout::NodeWithStyle* layout_node() const { return static_cast<const Layout::NodeWithStyle*>(Node::layout_node()); }
|
2019-10-14 13:32:02 -03:00
|
|
|
|
2020-06-03 15:51:28 -03:00
|
|
|
String name() const { return attribute(HTML::AttributeNames::name); }
|
2019-10-20 04:09:15 -03:00
|
|
|
|
2021-01-06 07:42:01 -03:00
|
|
|
const CSS::StyleProperties* specified_css_values() const { return m_specified_css_values.ptr(); }
|
2020-07-26 15:01:35 -03:00
|
|
|
NonnullRefPtr<CSS::StyleProperties> computed_style();
|
2020-01-02 10:52:34 -03:00
|
|
|
|
2021-03-13 16:11:33 -03:00
|
|
|
const CSS::CSSStyleDeclaration* inline_style() const { return m_inline_style; }
|
2020-12-07 15:56:53 -03:00
|
|
|
|
2021-03-13 18:39:55 -03:00
|
|
|
NonnullRefPtr<CSS::CSSStyleDeclaration> style_for_bindings();
|
|
|
|
|
|
2020-03-25 14:53:20 -03:00
|
|
|
String inner_html() const;
|
2021-09-13 18:42:57 -03:00
|
|
|
ExceptionOr<void> set_inner_html(String const&);
|
2020-03-25 14:53:20 -03:00
|
|
|
|
2020-08-14 14:40:37 -03:00
|
|
|
bool is_focused() const;
|
|
|
|
|
virtual bool is_focusable() const { return false; }
|
|
|
|
|
|
2021-06-18 19:42:34 -03:00
|
|
|
bool is_active() const;
|
|
|
|
|
|
2021-04-22 16:11:20 -03:00
|
|
|
NonnullRefPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
|
|
|
|
|
NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
|
2021-02-07 19:44:01 -03:00
|
|
|
|
2021-02-10 14:21:35 -03:00
|
|
|
ShadowRoot* shadow_root() { return m_shadow_root; }
|
|
|
|
|
const ShadowRoot* shadow_root() const { return m_shadow_root; }
|
|
|
|
|
void set_shadow_root(RefPtr<ShadowRoot>);
|
|
|
|
|
|
2021-05-28 16:21:44 -03:00
|
|
|
Optional<CSS::StyleResolver::CustomPropertyResolutionTuple> resolve_custom_property(const String& custom_property_name)
|
|
|
|
|
{
|
|
|
|
|
return m_custom_properties.get(custom_property_name);
|
|
|
|
|
}
|
|
|
|
|
void add_custom_property(const String& custom_property_name, CSS::StyleResolver::CustomPropertyResolutionTuple style_property)
|
|
|
|
|
{
|
|
|
|
|
m_custom_properties.set(custom_property_name, style_property);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 19:59:09 -03:00
|
|
|
void queue_an_element_task(HTML::Task::Source, Function<void()>);
|
|
|
|
|
|
2021-09-13 18:42:15 -03:00
|
|
|
bool is_void_element() const;
|
|
|
|
|
bool serializes_as_void() const;
|
|
|
|
|
|
2020-06-13 17:24:49 -03:00
|
|
|
protected:
|
2021-01-06 10:27:40 -03:00
|
|
|
RefPtr<Layout::Node> create_layout_node() override;
|
2019-10-05 17:27:52 -03:00
|
|
|
|
2020-06-13 17:24:49 -03:00
|
|
|
private:
|
2020-03-22 09:10:04 -03:00
|
|
|
Attribute* find_attribute(const FlyString& name);
|
|
|
|
|
const Attribute* find_attribute(const FlyString& name) const;
|
2019-06-15 16:08:36 -03:00
|
|
|
|
2021-05-04 18:25:43 -03:00
|
|
|
void make_html_uppercased_qualified_name();
|
|
|
|
|
|
2020-10-09 22:48:05 -03:00
|
|
|
QualifiedName m_qualified_name;
|
2021-05-04 18:25:43 -03:00
|
|
|
String m_html_uppercased_qualified_name;
|
2019-06-15 13:55:47 -03:00
|
|
|
Vector<Attribute> m_attributes;
|
2020-01-02 10:52:34 -03:00
|
|
|
|
2021-03-13 16:11:33 -03:00
|
|
|
RefPtr<CSS::CSSStyleDeclaration> m_inline_style;
|
2020-12-07 15:56:53 -03:00
|
|
|
|
2021-01-06 07:42:01 -03:00
|
|
|
RefPtr<CSS::StyleProperties> m_specified_css_values;
|
2021-05-28 16:21:44 -03:00
|
|
|
HashMap<String, CSS::StyleResolver::CustomPropertyResolutionTuple> m_custom_properties;
|
2020-05-26 18:07:19 -03:00
|
|
|
|
|
|
|
|
Vector<FlyString> m_classes;
|
2021-02-10 14:21:35 -03:00
|
|
|
|
|
|
|
|
RefPtr<ShadowRoot> m_shadow_root;
|
2019-06-15 13:55:47 -03:00
|
|
|
};
|
2019-10-06 15:37:39 -03:00
|
|
|
|
2021-01-07 13:31:26 -03:00
|
|
|
template<>
|
2021-01-17 05:34:01 -03:00
|
|
|
inline bool Node::fast_is<Element>() const { return is_element(); }
|
2021-01-07 13:31:26 -03:00
|
|
|
|
|
|
|
|
}
|