| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02: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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-07 11:35:26 +01:00
										 |  |  | #include <AK/AnyOf.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-09 17:22:35 +01:00
										 |  |  | #include <LibWeb/CSS/Parser/DeprecatedCSSParser.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  | #include <LibWeb/CSS/PropertyID.h>
 | 
					
						
							| 
									
										
										
										
											2020-12-14 21:31:10 +00:00
										 |  |  | #include <LibWeb/CSS/StyleInvalidator.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  | #include <LibWeb/CSS/StyleResolver.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-19 19:13:08 +01:00
										 |  |  | #include <LibWeb/DOM/DOMException.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Element.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-19 19:13:08 +01:00
										 |  |  | #include <LibWeb/DOM/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | #include <LibWeb/DOM/HTMLCollection.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-10 18:21:35 +01:00
										 |  |  | #include <LibWeb/DOM/ShadowRoot.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  | #include <LibWeb/DOM/Text.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:15:15 +02:00
										 |  |  | #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/BlockBox.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/InlineNode.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/ListItemBox.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/TableBox.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/TableCellBox.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/TableRowBox.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/TableRowGroupBox.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-25 20:29:03 +01:00
										 |  |  | #include <LibWeb/Layout/TreeBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-07 23:44:01 +01:00
										 |  |  | #include <LibWeb/Namespace.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  | namespace Web::DOM { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-07 11:20:15 +01:00
										 |  |  | Element::Element(Document& document, QualifiedName qualified_name) | 
					
						
							| 
									
										
										
										
											2019-09-29 11:43:07 +02:00
										 |  |  |     : ParentNode(document, NodeType::ELEMENT_NODE) | 
					
						
							| 
									
										
										
										
											2021-02-07 11:20:15 +01:00
										 |  |  |     , m_qualified_name(move(qualified_name)) | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Element::~Element() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 13:10:04 +01:00
										 |  |  | Attribute* Element::find_attribute(const FlyString& name) | 
					
						
							| 
									
										
										
										
											2019-06-15 21:08:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     for (auto& attribute : m_attributes) { | 
					
						
							|  |  |  |         if (attribute.name() == name) | 
					
						
							|  |  |  |             return &attribute; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 13:10:04 +01:00
										 |  |  | const Attribute* Element::find_attribute(const FlyString& name) const | 
					
						
							| 
									
										
										
										
											2019-06-15 21:08:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     for (auto& attribute : m_attributes) { | 
					
						
							|  |  |  |         if (attribute.name() == name) | 
					
						
							|  |  |  |             return &attribute; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 13:10:04 +01:00
										 |  |  | String Element::attribute(const FlyString& name) const | 
					
						
							| 
									
										
										
										
											2019-06-15 21:08:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (auto* attribute = find_attribute(name)) | 
					
						
							|  |  |  |         return attribute->value(); | 
					
						
							| 
									
										
										
										
											2019-10-05 22:27:52 +02:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2019-06-15 21:08:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 19:13:08 +01:00
										 |  |  | ExceptionOr<void> Element::set_attribute(const FlyString& name, const String& value) | 
					
						
							| 
									
										
										
										
											2019-06-15 21:08:36 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-19 19:13:08 +01:00
										 |  |  |     // FIXME: Proper name validation
 | 
					
						
							|  |  |  |     if (name.is_empty()) | 
					
						
							|  |  |  |         return InvalidCharacterError::create("Attribute name must not be empty"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 21:31:10 +00:00
										 |  |  |     CSS::StyleInvalidator style_invalidator(document()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 21:08:36 +02:00
										 |  |  |     if (auto* attribute = find_attribute(name)) | 
					
						
							|  |  |  |         attribute->set_value(value); | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2019-08-01 16:50:15 +02:00
										 |  |  |         m_attributes.empend(name, value); | 
					
						
							| 
									
										
										
										
											2019-10-06 10:09:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     parse_attribute(name, value); | 
					
						
							| 
									
										
										
										
											2021-02-19 19:13:08 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2019-06-15 21:08:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:15:15 +02:00
										 |  |  | void Element::remove_attribute(const FlyString& name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-14 21:31:10 +00:00
										 |  |  |     CSS::StyleInvalidator style_invalidator(document()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:15:15 +02:00
										 |  |  |     m_attributes.remove_first_matching([&](auto& attribute) { return attribute.name() == name; }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-08 01:06:20 +01:00
										 |  |  | bool Element::has_class(const FlyString& class_name, CaseSensitivity case_sensitivity) const | 
					
						
							| 
									
										
										
										
											2019-06-27 20:40:21 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-08 01:06:20 +01:00
										 |  |  |     return any_of(m_classes.begin(), m_classes.end(), [&](auto& it) { | 
					
						
							|  |  |  |         return case_sensitivity == CaseSensitivity::CaseSensitive | 
					
						
							|  |  |  |             ? it == class_name | 
					
						
							|  |  |  |             : it.to_lowercase() == class_name.to_lowercase(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-06-27 20:40:21 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-05 22:27:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-06 14:27:40 +01:00
										 |  |  | RefPtr<Layout::Node> Element::create_layout_node() | 
					
						
							| 
									
										
										
										
											2019-10-05 22:27:52 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-06 14:27:40 +01:00
										 |  |  |     auto style = document().style_resolver().resolve_style(*this); | 
					
						
							| 
									
										
										
										
											2021-01-06 11:42:01 +01:00
										 |  |  |     const_cast<Element&>(*this).m_specified_css_values = style; | 
					
						
							| 
									
										
										
										
											2020-06-24 16:22:16 +02:00
										 |  |  |     auto display = style->display(); | 
					
						
							| 
									
										
										
										
											2019-10-05 22:27:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 16:22:16 +02:00
										 |  |  |     if (display == CSS::Display::None) | 
					
						
							| 
									
										
										
										
											2019-10-05 22:27:52 +02:00
										 |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2020-06-05 13:24:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 18:18:13 +02:00
										 |  |  |     if (local_name() == "noscript" && document().is_scripting_enabled()) | 
					
						
							| 
									
										
										
										
											2020-06-05 13:24:35 +02:00
										 |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     switch (display) { | 
					
						
							|  |  |  |     case CSS::Display::None: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case CSS::Display::Block: | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new Layout::BlockBox(document(), this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::Inline: | 
					
						
							| 
									
										
										
										
											2020-07-07 16:11:09 +02:00
										 |  |  |         if (style->float_().value_or(CSS::Float::None) != CSS::Float::None) | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |             return adopt_ref(*new Layout::BlockBox(document(), this, move(style))); | 
					
						
							|  |  |  |         return adopt_ref(*new Layout::InlineNode(document(), *this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::ListItem: | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new Layout::ListItemBox(document(), *this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::Table: | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new Layout::TableBox(document(), this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::TableRow: | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new Layout::TableRowBox(document(), this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::TableCell: | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new Layout::TableCellBox(document(), this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::TableRowGroup: | 
					
						
							|  |  |  |     case CSS::Display::TableHeaderGroup: | 
					
						
							|  |  |  |     case CSS::Display::TableFooterGroup: | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new Layout::TableRowGroupBox(document(), *this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::InlineBlock: { | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         auto inline_block = adopt_ref(*new Layout::BlockBox(document(), this, move(style))); | 
					
						
							| 
									
										
										
										
											2020-05-05 16:06:22 +02:00
										 |  |  |         inline_block->set_inline(true); | 
					
						
							|  |  |  |         return inline_block; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::Flex: | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new Layout::BlockBox(document(), this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     case CSS::Display::TableColumn: | 
					
						
							|  |  |  |     case CSS::Display::TableColumnGroup: | 
					
						
							|  |  |  |     case CSS::Display::TableCaption: | 
					
						
							| 
									
										
										
										
											2021-02-07 11:47:33 +01:00
										 |  |  |         // FIXME: This is just an incorrect placeholder until we improve table layout support.
 | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new Layout::BlockBox(document(), this, move(style))); | 
					
						
							| 
									
										
										
										
											2021-02-07 11:40:42 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-10-05 22:27:52 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-06 10:09:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 23:07:19 +02:00
										 |  |  | void Element::parse_attribute(const FlyString& name, const String& value) | 
					
						
							| 
									
										
										
										
											2019-10-06 10:09:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-07 19:56:53 +01:00
										 |  |  |     if (name == HTML::AttributeNames::class_) { | 
					
						
							| 
									
										
										
										
											2020-05-26 23:07:19 +02:00
										 |  |  |         auto new_classes = value.split_view(' '); | 
					
						
							|  |  |  |         m_classes.clear(); | 
					
						
							|  |  |  |         m_classes.ensure_capacity(new_classes.size()); | 
					
						
							|  |  |  |         for (auto& new_class : new_classes) { | 
					
						
							|  |  |  |             m_classes.unchecked_append(new_class); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-07 19:56:53 +01:00
										 |  |  |     } else if (name == HTML::AttributeNames::style) { | 
					
						
							|  |  |  |         m_inline_style = parse_css_declaration(CSS::ParsingContext(document()), value); | 
					
						
							| 
									
										
										
										
											2020-12-13 15:22:41 +01:00
										 |  |  |         set_needs_style_update(true); | 
					
						
							| 
									
										
										
										
											2020-05-26 23:07:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-06 10:09:55 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-14 18:32:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | enum class StyleDifference { | 
					
						
							|  |  |  |     None, | 
					
						
							|  |  |  |     NeedsRepaint, | 
					
						
							|  |  |  |     NeedsRelayout, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 20:01:35 +02:00
										 |  |  | static StyleDifference compute_style_difference(const CSS::StyleProperties& old_style, const CSS::StyleProperties& new_style, const Document& document) | 
					
						
							| 
									
										
										
										
											2019-10-14 18:32:02 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (old_style == new_style) | 
					
						
							|  |  |  |         return StyleDifference::None; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool needs_repaint = false; | 
					
						
							|  |  |  |     bool needs_relayout = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 16:22:16 +02:00
										 |  |  |     if (new_style.display() != old_style.display()) | 
					
						
							| 
									
										
										
										
											2020-06-15 17:52:08 +02:00
										 |  |  |         needs_relayout = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-14 18:32:02 +02:00
										 |  |  |     if (new_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black)) | 
					
						
							|  |  |  |         needs_repaint = true; | 
					
						
							|  |  |  |     else if (new_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black)) | 
					
						
							|  |  |  |         needs_repaint = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (needs_relayout) | 
					
						
							|  |  |  |         return StyleDifference::NeedsRelayout; | 
					
						
							|  |  |  |     if (needs_repaint) | 
					
						
							|  |  |  |         return StyleDifference::NeedsRepaint; | 
					
						
							|  |  |  |     return StyleDifference::None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Element::recompute_style() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-19 18:57:02 +02:00
										 |  |  |     set_needs_style_update(false); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(parent()); | 
					
						
							| 
									
										
										
										
											2021-01-06 12:44:58 +01:00
										 |  |  |     auto old_specified_css_values = m_specified_css_values; | 
					
						
							| 
									
										
										
										
											2021-01-06 14:27:40 +01:00
										 |  |  |     auto new_specified_css_values = document().style_resolver().resolve_style(*this); | 
					
						
							| 
									
										
										
										
											2021-01-06 12:44:58 +01:00
										 |  |  |     m_specified_css_values = new_specified_css_values; | 
					
						
							| 
									
										
										
										
											2019-10-19 18:57:02 +02:00
										 |  |  |     if (!layout_node()) { | 
					
						
							| 
									
										
										
										
											2021-01-06 12:44:58 +01:00
										 |  |  |         if (new_specified_css_values->display() == CSS::Display::None) | 
					
						
							| 
									
										
										
										
											2019-10-19 18:57:02 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         // We need a new layout tree here!
 | 
					
						
							| 
									
										
										
										
											2020-11-25 20:29:03 +01:00
										 |  |  |         Layout::TreeBuilder tree_builder; | 
					
						
							| 
									
										
										
										
											2019-10-19 18:57:02 +02:00
										 |  |  |         tree_builder.build(*this); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-05 21:47:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-06 12:44:58 +01:00
										 |  |  |     auto diff = StyleDifference::NeedsRelayout; | 
					
						
							|  |  |  |     if (old_specified_css_values) | 
					
						
							|  |  |  |         diff = compute_style_difference(*old_specified_css_values, *new_specified_css_values, document()); | 
					
						
							| 
									
										
										
										
											2019-10-14 18:32:02 +02:00
										 |  |  |     if (diff == StyleDifference::None) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2021-01-06 12:44:58 +01:00
										 |  |  |     layout_node()->apply_style(*new_specified_css_values); | 
					
						
							| 
									
										
										
										
											2019-10-14 18:32:02 +02:00
										 |  |  |     if (diff == StyleDifference::NeedsRelayout) { | 
					
						
							| 
									
										
										
										
											2021-01-09 15:07:37 +01:00
										 |  |  |         document().schedule_forced_layout(); | 
					
						
							| 
									
										
										
										
											2020-06-15 17:52:08 +02:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-10-14 18:32:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (diff == StyleDifference::NeedsRepaint) { | 
					
						
							|  |  |  |         layout_node()->set_needs_display(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-04 03:09:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 20:01:35 +02:00
										 |  |  | NonnullRefPtr<CSS::StyleProperties> Element::computed_style() | 
					
						
							| 
									
										
										
										
											2020-01-04 03:09:22 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-06 12:44:58 +01:00
										 |  |  |     // FIXME: This implementation is not doing anything it's supposed to.
 | 
					
						
							| 
									
										
										
										
											2021-01-06 11:42:01 +01:00
										 |  |  |     auto properties = m_specified_css_values->clone(); | 
					
						
							| 
									
										
										
										
											2020-01-04 03:09:22 +01:00
										 |  |  |     if (layout_node() && layout_node()->has_style()) { | 
					
						
							|  |  |  |         CSS::PropertyID box_model_metrics[] = { | 
					
						
							|  |  |  |             CSS::PropertyID::MarginTop, | 
					
						
							|  |  |  |             CSS::PropertyID::MarginBottom, | 
					
						
							|  |  |  |             CSS::PropertyID::MarginLeft, | 
					
						
							|  |  |  |             CSS::PropertyID::MarginRight, | 
					
						
							|  |  |  |             CSS::PropertyID::PaddingTop, | 
					
						
							|  |  |  |             CSS::PropertyID::PaddingBottom, | 
					
						
							|  |  |  |             CSS::PropertyID::PaddingLeft, | 
					
						
							|  |  |  |             CSS::PropertyID::PaddingRight, | 
					
						
							| 
									
										
										
										
											2020-01-05 17:04:37 +01:00
										 |  |  |             CSS::PropertyID::BorderTopWidth, | 
					
						
							|  |  |  |             CSS::PropertyID::BorderBottomWidth, | 
					
						
							|  |  |  |             CSS::PropertyID::BorderLeftWidth, | 
					
						
							|  |  |  |             CSS::PropertyID::BorderRightWidth, | 
					
						
							| 
									
										
										
										
											2020-01-04 03:09:22 +01:00
										 |  |  |         }; | 
					
						
							|  |  |  |         for (CSS::PropertyID id : box_model_metrics) { | 
					
						
							| 
									
										
										
										
											2021-01-06 12:44:58 +01:00
										 |  |  |             auto prop = m_specified_css_values->property(id); | 
					
						
							| 
									
										
										
										
											2020-03-06 10:26:05 +01:00
										 |  |  |             if (prop.has_value()) | 
					
						
							| 
									
										
										
										
											2020-01-04 03:09:22 +01:00
										 |  |  |                 properties->set_property(id, prop.value()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-05 17:38:52 +01:00
										 |  |  |     return properties; | 
					
						
							| 
									
										
										
										
											2020-01-04 03:09:22 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  | void Element::set_inner_html(StringView markup) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-28 18:20:36 +02:00
										 |  |  |     auto new_children = HTML::HTMLDocumentParser::parse_html_fragment(*this, markup); | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |     remove_all_children(); | 
					
						
							| 
									
										
										
										
											2020-06-25 23:42:08 +02:00
										 |  |  |     while (!new_children.is_empty()) { | 
					
						
							|  |  |  |         append_child(new_children.take_first()); | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set_needs_style_update(true); | 
					
						
							|  |  |  |     document().invalidate_layout(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String Element::inner_html() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-17 13:37:40 +00:00
										 |  |  |     auto escape_string = [](const StringView& string, bool attribute_mode) -> String { | 
					
						
							|  |  |  |         // https://html.spec.whatwg.org/multipage/parsing.html#escapingString
 | 
					
						
							|  |  |  |         StringBuilder builder; | 
					
						
							|  |  |  |         for (auto& ch : string) { | 
					
						
							|  |  |  |             if (ch == '&') | 
					
						
							|  |  |  |                 builder.append("&"); | 
					
						
							|  |  |  |             // FIXME: also replace U+00A0 NO-BREAK SPACE with  
 | 
					
						
							|  |  |  |             else if (ch == '"' && attribute_mode) | 
					
						
							|  |  |  |                 builder.append("""); | 
					
						
							|  |  |  |             else if (ch == '<' && !attribute_mode) | 
					
						
							|  |  |  |                 builder.append("<"); | 
					
						
							|  |  |  |             else if (ch == '>' && !attribute_mode) | 
					
						
							|  |  |  |                 builder.append(">"); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 builder.append(ch); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return builder.to_string(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |     StringBuilder builder; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Function<void(const Node&)> recurse = [&](auto& node) { | 
					
						
							|  |  |  |         for (auto* child = node.first_child(); child; child = child->next_sibling()) { | 
					
						
							|  |  |  |             if (child->is_element()) { | 
					
						
							| 
									
										
										
										
											2020-12-17 13:37:40 +00:00
										 |  |  |                 auto& element = downcast<Element>(*child); | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |                 builder.append('<'); | 
					
						
							| 
									
										
										
										
											2020-12-17 13:37:40 +00:00
										 |  |  |                 builder.append(element.local_name()); | 
					
						
							|  |  |  |                 element.for_each_attribute([&](auto& name, auto& value) { | 
					
						
							|  |  |  |                     builder.append(' '); | 
					
						
							|  |  |  |                     builder.append(name); | 
					
						
							|  |  |  |                     builder.append('='); | 
					
						
							|  |  |  |                     builder.append('"'); | 
					
						
							|  |  |  |                     builder.append(escape_string(value, true)); | 
					
						
							|  |  |  |                     builder.append('"'); | 
					
						
							|  |  |  |                 }); | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |                 builder.append('>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 recurse(*child); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-17 13:37:40 +00:00
										 |  |  |                 // FIXME: This should be skipped for void elements
 | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |                 builder.append("</"); | 
					
						
							| 
									
										
										
										
											2020-12-17 13:37:40 +00:00
										 |  |  |                 builder.append(element.local_name()); | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |                 builder.append('>'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (child->is_text()) { | 
					
						
							| 
									
										
										
										
											2020-12-17 14:26:46 +00:00
										 |  |  |                 auto& text = downcast<Text>(*child); | 
					
						
							|  |  |  |                 builder.append(escape_string(text.data(), false)); | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-12-17 14:26:46 +00:00
										 |  |  |             // FIXME: Also handle Comment, ProcessingInstruction, DocumentType
 | 
					
						
							| 
									
										
										
										
											2020-03-25 18:53:20 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     recurse(*this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return builder.to_string(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 19:40:37 +02:00
										 |  |  | bool Element::is_focused() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return document().focused_element() == this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | NonnullRefPtr<HTMLCollection> Element::get_elements_by_tag_name(FlyString const& tag_name) | 
					
						
							| 
									
										
										
										
											2021-02-07 23:44:01 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: Support "*" for tag_name
 | 
					
						
							|  |  |  |     // https://dom.spec.whatwg.org/#concept-getelementsbytagname
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  |     return HTMLCollection::create(*this, [tag_name](Element const& element) { | 
					
						
							|  |  |  |         if (element.namespace_() == Namespace::HTML) | 
					
						
							|  |  |  |             return element.local_name().to_lowercase() == tag_name.to_lowercase(); | 
					
						
							|  |  |  |         return element.local_name() == tag_name; | 
					
						
							| 
									
										
										
										
											2021-02-07 23:44:01 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | NonnullRefPtr<HTMLCollection> Element::get_elements_by_class_name(FlyString const& class_name) | 
					
						
							| 
									
										
										
										
											2021-02-07 23:44:01 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  |     return HTMLCollection::create(*this, [class_name, quirks_mode = document().in_quirks_mode()](Element const& element) { | 
					
						
							|  |  |  |         return element.has_class(class_name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive); | 
					
						
							| 
									
										
										
										
											2021-02-07 23:44:01 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 18:21:35 +01:00
										 |  |  | void Element::set_shadow_root(RefPtr<ShadowRoot> shadow_root) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_shadow_root == shadow_root) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_shadow_root = move(shadow_root); | 
					
						
							|  |  |  |     invalidate_style(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-13 22:39:55 +01:00
										 |  |  | NonnullRefPtr<CSS::CSSStyleDeclaration> Element::style_for_bindings() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!m_inline_style) | 
					
						
							| 
									
										
										
										
											2021-03-16 18:55:53 +01:00
										 |  |  |         m_inline_style = CSS::ElementInlineCSSStyleDeclaration::create(*this); | 
					
						
							| 
									
										
										
										
											2021-03-13 22:39:55 +01:00
										 |  |  |     return *m_inline_style; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |