Skip to content

Commit

Permalink
web-dom: Simple dom Node structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Mar 9, 2024
1 parent becb659 commit 9bb326f
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/web/web-dom/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"id": "web-dom",
"type": "lib",
"description": "Implementation of the HTML standard (https://html.spec.whatwg.org/multipage/)",
"requires": [
"karm-logger",
"web-base"
]
}
93 changes: 93 additions & 0 deletions src/web/web-dom/node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#pragma once

#include <karm-base/list.h>

namespace Web::Dom {

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

virtual ~Node() {}

/* --- Parent --- */

Node &parentNode() {
if (not _parent)
panic("node has no parent");
return *_parent;
}

Node const &parentNode() const {
if (not _parent)
panic("node has no parent");
return *_parent;
}

/* --- Children --- */

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

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

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

Node &lastChild() {
auto *res = _children._tail;
if (not res)
panic("node has no children");
return *res;
}

Node const &lastChild() const {
auto *res = _children._tail;
if (not res)
panic("node has no children");
return *res;
}

/* --- Siblings --- */

Node &previousSibling() {
auto *res = _siblings.prev;
if (not res)
panic("node has no previous sibling");
return *res;
}

Node const &previousSibling() const {
auto *res = _siblings.prev;
if (not res)
panic("node has no previous sibling");
return *res;
}

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

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

} // namespace Web::Dom
15 changes: 15 additions & 0 deletions src/web/web-dom/tests/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"id": "web-dom-tests",
"type": "lib",
"props": {
"cpp-excluded": true
},
"requires": [
"web-dom",
"karm-test"
],
"injects": [
"__tests__"
]
}
13 changes: 13 additions & 0 deletions src/web/web-dom/tests/test-node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <karm-test/macros.h>
#include <web-dom/node.h>

namespace Web::Dom::Tests {

test$(domTree) {
auto &root = *new Node();

delete &root;
return Ok();
}

} // namespace Web::Dom::Tests

0 comments on commit 9bb326f

Please sign in to comment.