Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/String.h>
30#include <LibHTML/DOM/ParentNode.h>
31#include <LibHTML/Layout/LayoutNode.h>
32
33class LayoutNodeWithStyle;
34
35class Attribute {
36public:
37 Attribute(const String& name, const String& value)
38 : m_name(name)
39 , m_value(value)
40 {
41 }
42
43 const String& name() const { return m_name; }
44 const String& value() const { return m_value; }
45
46 void set_value(const String& value) { m_value = value; }
47
48private:
49 String m_name;
50 String m_value;
51};
52
53class Element : public ParentNode {
54public:
55 Element(Document&, const String& tag_name);
56 virtual ~Element() override;
57
58 virtual String tag_name() const final { return m_tag_name; }
59
60 bool has_attribute(const String& name) const { return !attribute(name).is_null(); }
61 String attribute(const String& name) const;
62 void set_attribute(const String& name, const String& value);
63
64 void set_attributes(Vector<Attribute>&&);
65
66 template<typename Callback>
67 void for_each_attribute(Callback callback) const
68 {
69 for (auto& attribute : m_attributes)
70 callback(attribute.name(), attribute.value());
71 }
72
73 bool has_class(const StringView&) const;
74
75 virtual void apply_presentational_hints(StyleProperties&) const {}
76 virtual void parse_attribute(const String& name, const String& value);
77
78 void recompute_style();
79
80 LayoutNodeWithStyle* layout_node() { return static_cast<LayoutNodeWithStyle*>(Node::layout_node()); }
81 const LayoutNodeWithStyle* layout_node() const { return static_cast<const LayoutNodeWithStyle*>(Node::layout_node()); }
82
83 String name() const { return attribute("name"); }
84
85 const StyleProperties* resolved_style() const { return m_resolved_style.ptr(); }
86 NonnullRefPtr<StyleProperties> computed_style();
87
88private:
89 RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
90
91 Attribute* find_attribute(const String& name);
92 const Attribute* find_attribute(const String& name) const;
93
94 String m_tag_name;
95 Vector<Attribute> m_attributes;
96
97 RefPtr<StyleProperties> m_resolved_style;
98};
99
100template<>
101inline bool is<Element>(const Node& node)
102{
103 return node.is_element();
104}