Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Infos
# -----
.PHONY: help

## Display this help screen
help:
@printf "\e[36m%-35s %s\e[0m\n" "Command" "Usage"
@sed -n '/^## /{\
s/## //g;\
h;\
n;\
s/:.*//g;\
G;\
s/\n/ /g;\
p;}' Makefile | awk '{printf "\033[33m%-35s\033[0m%s\n", $$1, substr($$0,length($$1)+1)}'


# Zig commands
# ------------
.PHONY: build build-release run run-release shell test bench

## Build in debug mode
build:
@printf "\e[36mBuilding (debug)...\e[0m\n"
@zig build || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
@printf "\e[33mBuild OK\e[0m\n"

build-release:
@printf "\e[36mBuilding (release safe)...\e[0m\n"
@zig build -Drelease-safe || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
@printf "\e[33mBuild OK\e[0m\n"

## Run the server
run: build
@printf "\e[36mRunning...\e[0m\n"
@./zig-out/bin/browsercore || (printf "\e[33mRun ERROR\e[0m\n"; exit 1;)

## Run a JS shell in release-safe mode
shell:
@printf "\e[36mBuilding shell...\e[0m\n"
@zig build shell || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)

## Test
test:
@printf "\e[36mTesting...\e[0m\n"
@zig build test || (printf "\e[33mTest ERROR\e[0m\n"; exit 1;)
@printf "\e[33mTest OK\e[0m\n"
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn common(
}

fn linkLexbor(step: *std.build.LibExeObjStep) void {
// cmake . -DLEXBOR_BUILD_SHARED=OFF
const lib_path = "../lexbor/liblexbor_static.a";
step.addObjectFile(lib_path);
step.addIncludePath("../lexbor/source");
Expand Down
89 changes: 78 additions & 11 deletions src/dom.zig
Original file line number Diff line number Diff line change
@@ -1,24 +1,91 @@
const Console = @import("jsruntime").Console;

pub const EventTarget = @import("dom/event_target.zig").EventTarget;
pub const Node = @import("dom/node.zig").Node;
// DOM
const EventTarget = @import("dom/event_target.zig").EventTarget;
const Node = @import("dom/node.zig").Node;
const Element = @import("dom/element.zig").Element;
const Document = @import("dom/document.zig").Document;

pub const Element = @import("dom/element.zig").Element;
pub const HTMLElement = @import("dom/element.zig").HTMLElement;
pub const HTMLBodyElement = @import("dom/element.zig").HTMLBodyElement;
// HTML
pub const HTMLDocument = @import("html/document.zig").HTMLDocument;

pub const Document = @import("dom/document.zig").Document;
pub const HTMLDocument = @import("dom/document.zig").HTMLDocument;
const E = @import("html/elements.zig");

// Interfaces
pub const Interfaces = .{
Console,

// DOM
EventTarget,
Node,

Element,
HTMLElement,
HTMLBodyElement,

Document,

// HTML
HTMLDocument,

E.HTMLElement,
E.HTMLMediaElement,

// TODO: generate HTMLElements comptime
E.HTMLUnknownElement,
E.HTMLAnchorElement,
E.HTMLAreaElement,
E.HTMLAudioElement,
E.HTMLBRElement,
E.HTMLBaseElement,
E.HTMLBodyElement,
E.HTMLButtonElement,
E.HTMLCanvasElement,
E.HTMLDListElement,
E.HTMLDialogElement,
E.HTMLDataElement,
E.HTMLDivElement,
E.HTMLEmbedElement,
E.HTMLFieldSetElement,
E.HTMLFormElement,
E.HTMLFrameSetElement,
E.HTMLHRElement,
E.HTMLHeadElement,
E.HTMLHeadingElement,
E.HTMLHtmlElement,
E.HTMLIFrameElement,
E.HTMLImageElement,
E.HTMLInputElement,
E.HTMLLIElement,
E.HTMLLabelElement,
E.HTMLLegendElement,
E.HTMLLinkElement,
E.HTMLMapElement,
E.HTMLMetaElement,
E.HTMLMeterElement,
E.HTMLModElement,
E.HTMLOListElement,
E.HTMLObjectElement,
E.HTMLOptGroupElement,
E.HTMLOptionElement,
E.HTMLOutputElement,
E.HTMLParagraphElement,
E.HTMLPictureElement,
E.HTMLPreElement,
E.HTMLProgressElement,
E.HTMLQuoteElement,
E.HTMLScriptElement,
E.HTMLSelectElement,
E.HTMLSourceElement,
E.HTMLSpanElement,
E.HTMLStyleElement,
E.HTMLTableElement,
E.HTMLTableCaptionElement,
E.HTMLTableCellElement,
E.HTMLTableColElement,
E.HTMLTableRowElement,
E.HTMLTableSectionElement,
E.HTMLTemplateElement,
E.HTMLTextAreaElement,
E.HTMLTimeElement,
E.HTMLTitleElement,
E.HTMLTrackElement,
E.HTMLUListElement,
E.HTMLVideoElement,
};
72 changes: 3 additions & 69 deletions src/dom/document.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
const std = @import("std");

const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;

const parser = @import("../parser.zig");

const DOM = @import("../dom.zig");
const Node = DOM.Node;
const Element = DOM.Element;
const HTMLElement = DOM.HTMLElement;
const HTMLBodyElement = DOM.HTMLBodyElement;
const Node = @import("node.zig").Node;
const Element = @import("element.zig").Element;

pub const Document = struct {
proto: Node,
Expand All @@ -29,7 +22,7 @@ pub const Document = struct {
return Document.init(null);
}

fn getElementById(self: Document, elem_dom: *parser.Element, id: []const u8) ?Element {
pub fn getElementById(self: Document, elem_dom: *parser.Element, id: []const u8) ?Element {
if (self.base == null) {
return null;
}
Expand Down Expand Up @@ -61,62 +54,3 @@ pub const Document = struct {
return null;
}
};

pub const HTMLDocument = struct {
proto: Document,
base: *parser.DocumentHTML,

pub const prototype = *Document;

pub fn init() HTMLDocument {
return .{
.proto = Document.init(null),
.base = parser.documentHTMLInit(),
};
}

pub fn deinit(self: HTMLDocument) void {
parser.documentHTMLDeinit(self.base);
}

pub fn parse(self: *HTMLDocument, html: []const u8) !void {
try parser.documentHTMLParse(self.base, html);
self.proto.base = parser.documentHTMLToDocument(self.base);
}

// JS funcs
// --------

pub fn get_body(self: HTMLDocument) ?HTMLBodyElement {
const body_dom = parser.documentHTMLBody(self.base);
return HTMLBodyElement.init(body_dom);
}

pub fn _getElementById(self: HTMLDocument, id: []u8) ?HTMLElement {
const body_dom = parser.documentHTMLBody(self.base);
if (self.proto.getElementById(body_dom, id)) |elem| {
return HTMLElement.init(elem.base);
}
return null;
}
};

pub fn testExecFn(
js_env: *jsruntime.Env,
comptime _: []jsruntime.API,
) !void {
var constructor = [_]Case{
.{ .src = "document.__proto__.constructor.name", .ex = "HTMLDocument" },
.{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" },
.{ .src = "document.__proto__.__proto__.__proto__.constructor.name", .ex = "Node" },
.{ .src = "document.__proto__.__proto__.__proto__.__proto__.constructor.name", .ex = "EventTarget" },
};
try checkCases(js_env, &constructor);

var getElementById = [_]Case{
.{ .src = "let getElementById = document.getElementById('content')", .ex = "undefined" },
.{ .src = "getElementById.constructor.name", .ex = "HTMLElement" },
.{ .src = "getElementById.localName", .ex = "main" },
};
try checkCases(js_env, &getElementById);
}
30 changes: 1 addition & 29 deletions src/dom/element.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const std = @import("std");

const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;

const parser = @import("../parser.zig");

const DOM = @import("../dom.zig");
const Node = DOM.Node;
const Node = @import("node.zig").Node;

pub const Element = struct {
proto: Node,
Expand All @@ -29,26 +24,3 @@ pub const Element = struct {
return parser.elementLocalName(self.base);
}
};

// HTML elements
// -------------

pub const HTMLElement = struct {
proto: Element,

pub const prototype = *Element;

pub fn init(elem_base: *parser.Element) HTMLElement {
return .{ .proto = Element.init(elem_base) };
}
};

pub const HTMLBodyElement = struct {
proto: HTMLElement,

pub const prototype = *HTMLElement;

pub fn init(elem_base: *parser.Element) HTMLBodyElement {
return .{ .proto = HTMLElement.init(elem_base) };
}
};
Loading