Serenity Operating System
at portability 217 lines 7.4 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#include <LibHTML/CSS/StyleResolver.h> 28#include <LibHTML/CSS/PropertyID.h> 29#include <LibHTML/CSS/Length.h> 30#include <LibHTML/DOM/Document.h> 31#include <LibHTML/DOM/Element.h> 32#include <LibHTML/Layout/LayoutBlock.h> 33#include <LibHTML/Layout/LayoutInline.h> 34#include <LibHTML/Layout/LayoutListItem.h> 35#include <LibHTML/Layout/LayoutTable.h> 36#include <LibHTML/Layout/LayoutTableCell.h> 37#include <LibHTML/Layout/LayoutTableRow.h> 38#include <LibHTML/Layout/LayoutTreeBuilder.h> 39 40Element::Element(Document& document, const String& tag_name) 41 : ParentNode(document, NodeType::ELEMENT_NODE) 42 , m_tag_name(tag_name) 43{ 44} 45 46Element::~Element() 47{ 48} 49 50Attribute* Element::find_attribute(const String& name) 51{ 52 for (auto& attribute : m_attributes) { 53 if (attribute.name() == name) 54 return &attribute; 55 } 56 return nullptr; 57} 58 59const Attribute* Element::find_attribute(const String& name) const 60{ 61 for (auto& attribute : m_attributes) { 62 if (attribute.name() == name) 63 return &attribute; 64 } 65 return nullptr; 66} 67 68String Element::attribute(const String& name) const 69{ 70 if (auto* attribute = find_attribute(name)) 71 return attribute->value(); 72 return {}; 73} 74 75void Element::set_attribute(const String& name, const String& value) 76{ 77 if (auto* attribute = find_attribute(name)) 78 attribute->set_value(value); 79 else 80 m_attributes.empend(name, value); 81 82 parse_attribute(name, value); 83} 84 85void Element::set_attributes(Vector<Attribute>&& attributes) 86{ 87 m_attributes = move(attributes); 88 89 for (auto& attribute : m_attributes) 90 parse_attribute(attribute.name(), attribute.value()); 91} 92 93bool Element::has_class(const StringView& class_name) const 94{ 95 auto value = attribute("class"); 96 if (value.is_empty()) 97 return false; 98 auto parts = value.split_view(' '); 99 for (auto& part : parts) { 100 if (part == class_name) 101 return true; 102 } 103 return false; 104} 105 106RefPtr<LayoutNode> Element::create_layout_node(const StyleProperties* parent_style) const 107{ 108 auto style = document().style_resolver().resolve_style(*this, parent_style); 109 const_cast<Element&>(*this).m_resolved_style = style; 110 auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline"); 111 112 if (display == "none") 113 return nullptr; 114 if (display == "block") 115 return adopt(*new LayoutBlock(this, move(style))); 116 if (display == "inline") 117 return adopt(*new LayoutInline(*this, move(style))); 118 if (display == "list-item") 119 return adopt(*new LayoutListItem(*this, move(style))); 120 if (display == "table") 121 return adopt(*new LayoutTable(*this, move(style))); 122 if (display == "table-row") 123 return adopt(*new LayoutTableRow(*this, move(style))); 124 if (display == "table-cell") 125 return adopt(*new LayoutTableCell(*this, move(style))); 126 if (display == "inline-block") 127 return adopt(*new LayoutBlock(this, move(style))); 128 129 ASSERT_NOT_REACHED(); 130} 131 132void Element::parse_attribute(const String&, const String&) 133{ 134} 135 136enum class StyleDifference { 137 None, 138 NeedsRepaint, 139 NeedsRelayout, 140}; 141 142static StyleDifference compute_style_difference(const StyleProperties& old_style, const StyleProperties& new_style, const Document& document) 143{ 144 if (old_style == new_style) 145 return StyleDifference::None; 146 147 bool needs_repaint = false; 148 bool needs_relayout = false; 149 150 if (new_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black)) 151 needs_repaint = true; 152 else if (new_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black)) 153 needs_repaint = true; 154 155 if (needs_relayout) 156 return StyleDifference::NeedsRelayout; 157 if (needs_repaint) 158 return StyleDifference::NeedsRepaint; 159 return StyleDifference::None; 160} 161 162void Element::recompute_style() 163{ 164 set_needs_style_update(false); 165 ASSERT(parent()); 166 auto* parent_layout_node = parent()->layout_node(); 167 if (!parent_layout_node) 168 return; 169 ASSERT(parent_layout_node); 170 auto style = document().style_resolver().resolve_style(*this, &parent_layout_node->style()); 171 m_resolved_style = style; 172 if (!layout_node()) { 173 if (style->string_or_fallback(CSS::PropertyID::Display, "inline") == "none") 174 return; 175 // We need a new layout tree here! 176 LayoutTreeBuilder tree_builder; 177 tree_builder.build(*this); 178 return; 179 } 180 auto diff = compute_style_difference(layout_node()->style(), *style, document()); 181 if (diff == StyleDifference::None) 182 return; 183 layout_node()->set_style(*style); 184 if (diff == StyleDifference::NeedsRelayout) { 185 ASSERT_NOT_REACHED(); 186 } 187 if (diff == StyleDifference::NeedsRepaint) { 188 layout_node()->set_needs_display(); 189 } 190} 191 192NonnullRefPtr<StyleProperties> Element::computed_style() 193{ 194 auto properties = m_resolved_style->clone(); 195 if (layout_node() && layout_node()->has_style()) { 196 CSS::PropertyID box_model_metrics[] = { 197 CSS::PropertyID::MarginTop, 198 CSS::PropertyID::MarginBottom, 199 CSS::PropertyID::MarginLeft, 200 CSS::PropertyID::MarginRight, 201 CSS::PropertyID::PaddingTop, 202 CSS::PropertyID::PaddingBottom, 203 CSS::PropertyID::PaddingLeft, 204 CSS::PropertyID::PaddingRight, 205 CSS::PropertyID::BorderTopWidth, 206 CSS::PropertyID::BorderBottomWidth, 207 CSS::PropertyID::BorderLeftWidth, 208 CSS::PropertyID::BorderRightWidth, 209 }; 210 for (CSS::PropertyID id : box_model_metrics) { 211 auto prop = layout_node()->style().property(id); 212 if (prop) 213 properties->set_property(id, prop.value()); 214 } 215 } 216 return properties; 217}