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