1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:57:46 +00:00

Libraries: Create top level directory for libraries.

Things were getting a little crowded in the project root, so this patch
moves the Lib*/ directories into Libraries/.
This commit is contained in:
Andreas Kling 2019-07-04 16:16:50 +02:00
parent 63814ffebf
commit 04b9dc2d30
328 changed files with 36 additions and 36 deletions

1
Libraries/LibHTML/CSS/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
DefaultStyleSheetSource.cpp

View file

@ -0,0 +1,41 @@
html {
display: block;
font-family: Katica;
}
head, link, meta, script, style, title {
display: none;
}
body {
display: block;
margin-left: 8;
margin-top: 8;
margin-right: 8;
margin-bottom: 8;
}
h1 {
display: block;
font-family: Pebbleton;
font-weight: bold;
}
pre {
display: block;
font-family: Csilla;
}
u, ins {
text-decoration: underline;
}
strong, b {
font-family: KaticaBold;
}
p {
display: block;
margin-bottom: 8;
margin-top: 8;
}

View file

@ -0,0 +1,35 @@
#pragma once
#include <AK/AKString.h>
class Length {
public:
enum class Type {
Auto,
Absolute,
};
Length() {}
Length(int value, Type type)
: m_type(type)
, m_value(value)
{
}
~Length() {}
bool is_auto() const { return m_type == Type::Auto; }
bool is_absolute() const { return m_type == Type::Absolute; }
int value() const { return m_value; }
String to_string() const
{
if (is_auto())
return "auto";
return String::format("%d [Length/Absolute]", m_value);
}
private:
Type m_type { Type::Auto };
int m_value { 0 };
};

View file

@ -0,0 +1,10 @@
#pragma once
#include <LibHTML/CSS/Length.h>
struct LengthBox {
Length top;
Length right;
Length bottom;
Length left;
};

View file

@ -0,0 +1,35 @@
#include <LibHTML/CSS/Selector.h>
Selector::Selector(Vector<Component>&& components)
: m_components(move(components))
{
}
Selector::~Selector()
{
}
Specificity Selector::specificity() const
{
unsigned ids = 0;
unsigned tag_names = 0;
unsigned classes = 0;
for (auto& component : m_components) {
switch (component.type) {
case Component::Type::Id:
++ids;
break;
case Component::Type::Class:
++classes;
break;
case Component::Type::TagName:
++tag_names;
break;
default:
break;
}
}
return { ids, classes, tag_names };
}

View file

@ -0,0 +1,29 @@
#pragma once
#include <AK/AKString.h>
#include <AK/Vector.h>
#include <LibHTML/CSS/Specificity.h>
class Selector {
public:
struct Component {
enum class Type {
Invalid,
TagName,
Id,
Class
};
Type type { Type::Invalid };
String value;
};
explicit Selector(Vector<Component>&&);
~Selector();
const Vector<Component>& components() const { return m_components; }
Specificity specificity() const;
private:
Vector<Component> m_components;
};

View file

@ -0,0 +1,34 @@
#pragma once
class Specificity {
public:
Specificity(unsigned ids, unsigned classes, unsigned tag_names)
: m_ids(ids)
, m_classes(classes)
, m_tag_names(tag_names)
{
}
unsigned ids() const { return m_ids; }
unsigned classes() const { return m_classes; }
unsigned tag_names() const { return m_tag_names; }
bool operator<(const Specificity& other) const
{
return m_ids < other.m_ids
|| m_classes < other.m_classes
|| m_tag_names < other.m_tag_names;
}
bool operator==(const Specificity& other) const
{
return m_ids == other.m_ids
|| m_classes < other.m_classes
|| m_tag_names < other.m_tag_names;
}
private:
unsigned m_ids { 0 };
unsigned m_classes { 0 };
unsigned m_tag_names { 0 };
};

View file

@ -0,0 +1,11 @@
#include <LibHTML/CSS/StyleDeclaration.h>
StyleDeclaration::StyleDeclaration(const String& property_name, NonnullRefPtr<StyleValue>&& value)
: m_property_name(property_name)
, m_value(move(value))
{
}
StyleDeclaration::~StyleDeclaration()
{
}

View file

@ -0,0 +1,23 @@
#pragma once
#include <AK/AKString.h>
#include <LibHTML/CSS/StyleValue.h>
class StyleDeclaration : public RefCounted<StyleDeclaration> {
public:
static NonnullRefPtr<StyleDeclaration> create(const String& property_name, NonnullRefPtr<StyleValue>&& value)
{
return adopt(*new StyleDeclaration(property_name, move(value)));
}
~StyleDeclaration();
const String& property_name() const { return m_property_name; }
const StyleValue& value() const { return *m_value; }
public:
StyleDeclaration(const String& property_name, NonnullRefPtr<StyleValue>&&);
String m_property_name;
NonnullRefPtr<StyleValue> m_value;
};

View file

@ -0,0 +1,71 @@
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/CSS/StyleSheet.h>
#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Dump.h>
#include <stdio.h>
StyleResolver::StyleResolver(Document& document)
: m_document(document)
{
}
StyleResolver::~StyleResolver()
{
}
static bool matches(const Selector& selector, const Element& element)
{
// FIXME: Support compound selectors.
ASSERT(selector.components().size() == 1);
auto& component = selector.components().first();
switch (component.type) {
case Selector::Component::Type::Id:
return component.value == element.attribute("id");
case Selector::Component::Type::Class:
return element.has_class(component.value);
case Selector::Component::Type::TagName:
return component.value == element.tag_name();
default:
ASSERT_NOT_REACHED();
}
}
NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const
{
NonnullRefPtrVector<StyleRule> matching_rules;
for (auto& sheet : document().stylesheets()) {
for (auto& rule : sheet.rules()) {
for (auto& selector : rule.selectors()) {
if (matches(selector, element)) {
matching_rules.append(rule);
break;
}
}
}
}
printf("Rules matching Element{%p}\n", &element);
for (auto& rule : matching_rules) {
dump_rule(rule);
}
return matching_rules;
}
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Document& document)
{
return StyledNode::create(document);
}
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Element& element)
{
auto style = StyledNode::create(element);
auto matching_rules = collect_matching_rules(element);
for (auto& rule : matching_rules) {
for (auto& declaration : rule.declarations()) {
style->set_property(declaration.property_name(), declaration.value());
}
}
return style;
}

View file

@ -0,0 +1,29 @@
#pragma once
#include <AK/OwnPtr.h>
#include <AK/NonnullRefPtrVector.h>
class Document;
class Element;
class ParentNode;
class StyleRule;
class StyleSheet;
class StyledNode;
class StyleResolver {
public:
explicit StyleResolver(Document&);
~StyleResolver();
Document& document() { return m_document; }
const Document& document() const { return m_document; }
NonnullRefPtr<StyledNode> create_styled_node(const Element&);
NonnullRefPtr<StyledNode> create_styled_node(const Document&);
NonnullRefPtrVector<StyleRule> collect_matching_rules(const Element&) const;
private:
Document& m_document;
};

View file

@ -0,0 +1,11 @@
#include <LibHTML/CSS/StyleRule.h>
StyleRule::StyleRule(Vector<Selector>&& selectors, NonnullRefPtrVector<StyleDeclaration>&& declarations)
: m_selectors(move(selectors))
, m_declarations(move(declarations))
{
}
StyleRule::~StyleRule()
{
}

View file

@ -0,0 +1,31 @@
#pragma once
#include <AK/NonnullRefPtrVector.h>
#include <LibHTML/CSS/Selector.h>
#include <LibHTML/CSS/StyleDeclaration.h>
class StyleRule : public RefCounted<StyleRule> {
public:
static NonnullRefPtr<StyleRule> create(Vector<Selector>&& selectors, NonnullRefPtrVector<StyleDeclaration>&& declarations)
{
return adopt(*new StyleRule(move(selectors), move(declarations)));
}
~StyleRule();
const Vector<Selector>& selectors() const { return m_selectors; }
const NonnullRefPtrVector<StyleDeclaration>& declarations() const { return m_declarations; }
template<typename C>
void for_each_declaration(C callback) const
{
for (auto& declaration : m_declarations)
callback(declaration);
}
private:
StyleRule(Vector<Selector>&&, NonnullRefPtrVector<StyleDeclaration>&&);
Vector<Selector> m_selectors;
NonnullRefPtrVector<StyleDeclaration> m_declarations;
};

View file

@ -0,0 +1,10 @@
#include <LibHTML/CSS/StyleSheet.h>
StyleSheet::StyleSheet(NonnullRefPtrVector<StyleRule>&& rules)
: m_rules(move(rules))
{
}
StyleSheet::~StyleSheet()
{
}

View file

@ -0,0 +1,21 @@
#pragma once
#include <AK/NonnullRefPtrVector.h>
#include <LibHTML/CSS/StyleRule.h>
class StyleSheet : public RefCounted<StyleSheet> {
public:
static NonnullRefPtr<StyleSheet> create(NonnullRefPtrVector<StyleRule>&& rules)
{
return adopt(*new StyleSheet(move(rules)));
}
~StyleSheet();
const NonnullRefPtrVector<StyleRule>& rules() const { return m_rules; }
private:
explicit StyleSheet(NonnullRefPtrVector<StyleRule>&&);
NonnullRefPtrVector<StyleRule> m_rules;
};

View file

@ -0,0 +1,10 @@
#include "StyleValue.h"
StyleValue::StyleValue(Type type)
: m_type(type)
{
}
StyleValue::~StyleValue()
{
}

View file

@ -0,0 +1,70 @@
#pragma once
#include <AK/AKString.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <LibHTML/CSS/Length.h>
class StyleValue : public RefCounted<StyleValue> {
public:
virtual ~StyleValue();
enum class Type {
Invalid,
Inherit,
Initial,
String,
Length,
};
Type type() const { return m_type; }
virtual String to_string() const = 0;
protected:
explicit StyleValue(Type);
private:
Type m_type { Type::Invalid };
};
class StringStyleValue : public StyleValue {
public:
static NonnullRefPtr<StringStyleValue> create(const String& string)
{
return adopt(*new StringStyleValue(string));
}
virtual ~StringStyleValue() override {}
String to_string() const override { return m_string; }
private:
explicit StringStyleValue(const String& string)
: StyleValue(Type::String)
, m_string(string)
{
}
String m_string;
};
class LengthStyleValue : public StyleValue {
public:
static NonnullRefPtr<LengthStyleValue> create(const Length& length)
{
return adopt(*new LengthStyleValue(length));
}
virtual ~LengthStyleValue() override {}
String to_string() const override { return m_length.to_string(); }
private:
explicit LengthStyleValue(const Length& length)
: StyleValue(Type::Length)
, m_length(length)
{
}
Length m_length;
};

View file

@ -0,0 +1,25 @@
#include <LibHTML/CSS/StyledNode.h>
StyledNode::StyledNode(const Node* node)
: m_node(node)
{
}
StyledNode::~StyledNode()
{
}
Display StyledNode::display() const
{
auto it = m_property_values.find("display");
if (it == m_property_values.end())
return Display::Inline;
auto value = it->value->to_string();
if (value == "none")
return Display::None;
if (value == "block")
return Display::Block;
if (value == "inline")
return Display::Inline;
ASSERT_NOT_REACHED();
}

View file

@ -0,0 +1,61 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
#include <AK/AKString.h>
#include <LibHTML/TreeNode.h>
#include <LibHTML/CSS/StyleValue.h>
class Node;
enum class Display {
None,
Block,
Inline,
};
class StyledNode : public TreeNode<StyledNode> {
public:
static NonnullRefPtr<StyledNode> create(const Node& node)
{
return adopt(*new StyledNode(&node));
}
~StyledNode();
const Node* node() const { return m_node; }
template<typename Callback>
inline void for_each_child(Callback callback) const
{
for (auto* node = first_child(); node; node = node->next_sibling())
callback(*node);
}
template<typename Callback>
inline void for_each_child(Callback callback)
{
for (auto* node = first_child(); node; node = node->next_sibling())
callback(*node);
}
template<typename Callback>
inline void for_each_property(Callback callback) const
{
for (auto& it : m_property_values)
callback(it.key, *it.value);
}
void set_property(const String& name, NonnullRefPtr<StyleValue> value)
{
m_property_values.set(name, move(value));
}
Display display() const;
protected:
explicit StyledNode(const Node*);
private:
const Node* m_node { nullptr };
HashMap<String, NonnullRefPtr<StyleValue>> m_property_values;
};