Skip to content

Commit

Permalink
web-html+dom: Started implementing tree construction.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Mar 12, 2024
1 parent 6912873 commit 5d0d6b0
Show file tree
Hide file tree
Showing 15 changed files with 495 additions and 264 deletions.
8 changes: 8 additions & 0 deletions src/libs/karm-base/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ struct Strong {
return true;
return unwrap() == other.unwrap();
}

auto operator<=>(T const &other) const {
return unwrap() <=> other;
}

auto operator==(T const &other) const {
return unwrap() == other;
}
};

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions src/libs/karm-base/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ constexpr auto *end(MutSliceable auto &slice) {
return slice.buf() + slice.len();
}

constexpr auto const &first(Sliceable auto &slice) {
constexpr auto const &first(Sliceable auto const &slice) {
return slice.buf()[0];
}

constexpr auto &first(MutSliceable auto &slice) {
return slice.buf()[0];
}

constexpr auto const &last(Sliceable auto &slice) {
constexpr auto const &last(Sliceable auto const &slice) {
return slice.buf()[slice.len() - 1];
}

Expand Down
11 changes: 11 additions & 0 deletions src/web/web-dom/character-data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "node.h"

namespace Web::Dom {

struct CharacterData : public Dom::Node {
String data;
};

} // namespace Web::Dom
13 changes: 13 additions & 0 deletions src/web/web-dom/comment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "character-data.h"

namespace Web::Dom {

struct Comment : public CharacterData {
virtual NodeType nodeType() override {
return NodeType::COMMENT_NODE;
}
};

} // namespace Web::Dom
17 changes: 17 additions & 0 deletions src/web/web-dom/document-type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "node.h"

namespace Web::Dom {

struct DocumentType : public Node {
String name;
String publicId;
String systemId;

virtual NodeType nodeType() override {
return NodeType::DOCUMENT_TYPE_NODE;
}
};

} // namespace Web::Dom
21 changes: 21 additions & 0 deletions src/web/web-dom/document.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "node.h"

namespace Web::Dom {

enum struct QuirkMode {
NO,
LIMITED,
YES
};

struct Document : public Node {
QuirkMode quirkMode{QuirkMode::NO};

virtual NodeType nodeType() override {
return NodeType::DOCUMENT_NODE;
}
};

} // namespace Web::Dom
13 changes: 13 additions & 0 deletions src/web/web-dom/element.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "node.h"

namespace Web::Dom {

struct Element : public Node {
virtual NodeType nodeType() override {
return NodeType::ELEMENT_NODE;
}
};

} // namespace Web::Dom
99 changes: 55 additions & 44 deletions src/web/web-dom/node.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
#pragma once

#include <karm-base/list.h>
#include <karm-base/rc.h>
#include <karm-base/vec.h>

namespace Web::Dom {

enum struct NodeType {
ELEMENT_NODE = 1,
ATTRIBUTE_NODE = 2,
TEXT_NODE = 3,
CDATA_SECTION_NODE = 4,
PROCESSING_INSTRUCTION_NODE = 7,
COMMENT_NODE = 8,
DOCUMENT_NODE = 9,
DOCUMENT_TYPE_NODE = 10,
DOCUMENT_FRAGMENT_NODE = 11,
};

struct Node : public Meta::Static {
Node *_parent = nullptr;
LlItem<Node> _siblings;
Ll<Node, &Node::_siblings> _children;
Vec<Strong<Node>> _children;

virtual ~Node() = default;

virtual NodeType nodeType() = 0;

/* --- Parent --- */

Expand All @@ -17,74 +35,67 @@ struct Node : public Meta::Static {
return *_parent;
}

Node const &parentNode() const {
if (not _parent)
panic("node has no parent");
return *_parent;
usize _parentIndex() {
return indexOf(parentNode()._children, *this).unwrap();
}

void _detachParent() {
if (_parent) {
_parent->_children.removeAt(_parentIndex());
_parent = nullptr;
}
}

/* --- Children --- */

bool hasChildren() const {
bool hasChildren() {
return _children.len() > 0;
}

Node &firstChild() {
auto *res = _children._head;
if (not res)
Strong<Node> firstChild() {
if (not _children.len())
panic("node has no children");
return *res;
return first(_children);
}

Node const &firstChild() const {
auto *res = _children._head;
if (not res)
Strong<Node> lastChild() {
if (not _children.len())
panic("node has no children");
return *res;
return last(_children);
}

Node &lastChild() {
auto *res = _children._tail;
if (not res)
panic("node has no children");
return *res;
void appendChild(Strong<Node> child) {
child->_detachParent();
_children.pushBack(child);
child->_parent = this;
}

Node const &lastChild() const {
auto *res = _children._tail;
if (not res)
panic("node has no children");
return *res;
void removeChild(Strong<Node> child) {
if (child->_parent != this)
panic("node is not a child");
child->_detachParent();
}

/* --- Siblings --- */

Node &previousSibling() {
auto *res = _siblings.prev;
if (not res)
panic("node has no previous sibling");
return *res;
Strong<Node> previousSibling() {
usize index = _parentIndex();
return parentNode()._children[index - 1];
}

Node const &previousSibling() const {
auto *res = _siblings.prev;
if (not res)
panic("node has no previous sibling");
return *res;
Strong<Node> nextSibling() {
usize index = _parentIndex();
return parentNode()._children[index + 1];
}

Node &nextSibling() {
auto *res = _siblings.next;
if (not res)
panic("node has no next sibling");
return *res;
/* --- Operators --- */

bool operator==(Node const &other) const {
return this == &other;
}

Node const &nextSibling() const {
auto *res = _siblings.next;
if (not res)
panic("node has no next sibling");
return *res;
auto operator<=>(Node const &other) const {
return this <=> &other;
}
};

Expand Down
13 changes: 13 additions & 0 deletions src/web/web-dom/text.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "character-data.h"

namespace Web::Dom {

struct Text : public CharacterData {
virtual NodeType nodeType() override {
return NodeType::TEXT_NODE;
}
};

} // namespace Web::Dom
Loading

0 comments on commit 5d0d6b0

Please sign in to comment.