2019-10-05 22:27:52 +02:00
|
|
|
#include <LibHTML/CSS/StyleResolver.h>
|
2019-06-15 23:41:15 +02:00
|
|
|
#include <LibHTML/DOM/Element.h>
|
|
|
|
#include <LibHTML/Layout/LayoutBlock.h>
|
|
|
|
#include <LibHTML/Layout/LayoutInline.h>
|
2019-10-11 23:16:53 +02:00
|
|
|
#include <LibHTML/Layout/LayoutListItem.h>
|
2019-06-15 18:55:47 +02:00
|
|
|
|
2019-09-29 11:43:07 +02:00
|
|
|
Element::Element(Document& document, const String& tag_name)
|
|
|
|
: ParentNode(document, NodeType::ELEMENT_NODE)
|
2019-06-15 18:55:47 +02:00
|
|
|
, m_tag_name(tag_name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Element::~Element()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-15 21:08:36 +02:00
|
|
|
Attribute* Element::find_attribute(const String& name)
|
|
|
|
{
|
|
|
|
for (auto& attribute : m_attributes) {
|
|
|
|
if (attribute.name() == name)
|
|
|
|
return &attribute;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Attribute* Element::find_attribute(const String& name) const
|
|
|
|
{
|
|
|
|
for (auto& attribute : m_attributes) {
|
|
|
|
if (attribute.name() == name)
|
|
|
|
return &attribute;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
String Element::attribute(const String& name) const
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Element::set_attribute(const String& name, const String& value)
|
|
|
|
{
|
|
|
|
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);
|
2019-06-15 21:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Element::set_attributes(Vector<Attribute>&& attributes)
|
|
|
|
{
|
|
|
|
m_attributes = move(attributes);
|
2019-10-06 10:09:55 +02:00
|
|
|
|
|
|
|
for (auto& attribute : m_attributes)
|
|
|
|
parse_attribute(attribute.name(), attribute.value());
|
2019-06-15 21:08:36 +02:00
|
|
|
}
|
2019-06-15 22:49:44 +02:00
|
|
|
|
2019-06-27 20:40:21 +02:00
|
|
|
bool Element::has_class(const StringView& class_name) const
|
|
|
|
{
|
|
|
|
auto value = attribute("class");
|
|
|
|
if (value.is_empty())
|
|
|
|
return false;
|
|
|
|
auto parts = value.split_view(' ');
|
|
|
|
for (auto& part : parts) {
|
|
|
|
if (part == class_name)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-05 22:27:52 +02:00
|
|
|
|
2019-10-07 09:23:53 +02:00
|
|
|
RefPtr<LayoutNode> Element::create_layout_node(const StyleResolver& resolver, const StyleProperties* parent_style) const
|
2019-10-05 22:27:52 +02:00
|
|
|
{
|
2019-10-07 09:23:53 +02:00
|
|
|
auto style = resolver.resolve_style(*this, parent_style);
|
2019-10-05 22:27:52 +02:00
|
|
|
|
2019-10-08 15:34:19 +02:00
|
|
|
auto display_property = style->property(CSS::PropertyID::Display);
|
2019-10-05 22:27:52 +02:00
|
|
|
String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
|
|
|
|
|
|
|
|
if (display == "none")
|
|
|
|
return nullptr;
|
2019-10-11 23:16:53 +02:00
|
|
|
if (display == "block")
|
2019-10-07 09:23:53 +02:00
|
|
|
return adopt(*new LayoutBlock(this, move(style)));
|
2019-10-05 22:27:52 +02:00
|
|
|
if (display == "inline")
|
2019-10-07 09:23:53 +02:00
|
|
|
return adopt(*new LayoutInline(*this, move(style)));
|
2019-10-11 23:16:53 +02:00
|
|
|
if (display == "list-item")
|
|
|
|
return adopt(*new LayoutListItem(*this, move(style)));
|
2019-10-05 22:27:52 +02:00
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-10-06 10:09:55 +02:00
|
|
|
|
|
|
|
void Element::parse_attribute(const String&, const String&)
|
|
|
|
{
|
|
|
|
}
|