Serenity Operating System
at hosted 114 lines 3.8 kB view raw
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/FlyString.h> 30#include <AK/String.h> 31#include <LibWeb/DOM/ParentNode.h> 32#include <LibWeb/Layout/LayoutNode.h> 33 34namespace Web { 35 36class LayoutNodeWithStyle; 37 38class Attribute { 39public: 40 Attribute(const FlyString& name, const String& value) 41 : m_name(name) 42 , m_value(value) 43 { 44 } 45 46 const FlyString& name() const { return m_name; } 47 const String& value() const { return m_value; } 48 49 void set_value(const String& value) { m_value = value; } 50 51private: 52 FlyString m_name; 53 String m_value; 54}; 55 56class Element : public ParentNode { 57public: 58 using WrapperType = Bindings::ElementWrapper; 59 60 Element(Document&, const FlyString& tag_name); 61 virtual ~Element() override; 62 63 virtual FlyString tag_name() const final { return m_tag_name; } 64 65 bool has_attribute(const FlyString& name) const { return !attribute(name).is_null(); } 66 String attribute(const FlyString& name) const; 67 void set_attribute(const FlyString& name, const String& value); 68 69 void set_attributes(Vector<Attribute>&&); 70 71 template<typename Callback> 72 void for_each_attribute(Callback callback) const 73 { 74 for (auto& attribute : m_attributes) 75 callback(attribute.name(), attribute.value()); 76 } 77 78 bool has_class(const StringView&) const; 79 80 virtual void apply_presentational_hints(StyleProperties&) const {} 81 virtual void parse_attribute(const FlyString& name, const String& value); 82 83 void recompute_style(); 84 85 LayoutNodeWithStyle* layout_node() { return static_cast<LayoutNodeWithStyle*>(Node::layout_node()); } 86 const LayoutNodeWithStyle* layout_node() const { return static_cast<const LayoutNodeWithStyle*>(Node::layout_node()); } 87 88 String name() const { return attribute("name"); } 89 90 const StyleProperties* resolved_style() const { return m_resolved_style.ptr(); } 91 NonnullRefPtr<StyleProperties> computed_style(); 92 93 String inner_html() const; 94 void set_inner_html(StringView); 95 96private: 97 RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override; 98 99 Attribute* find_attribute(const FlyString& name); 100 const Attribute* find_attribute(const FlyString& name) const; 101 102 FlyString m_tag_name; 103 Vector<Attribute> m_attributes; 104 105 RefPtr<StyleProperties> m_resolved_style; 106}; 107 108template<> 109inline bool is<Element>(const Node& node) 110{ 111 return node.is_element(); 112} 113 114}