From 0bc77c8c0dbc1737e2909ff5a2d5e5af48b298f7 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Tue, 24 Nov 2020 22:28:40 +0100
Subject: Started HTML rendering

---
 apps/browser.c | 87 ++++------------------------------------------------------
 1 file changed, 5 insertions(+), 82 deletions(-)

(limited to 'apps/browser.c')

diff --git a/apps/browser.c b/apps/browser.c
index 83292e9..160b626 100644
--- a/apps/browser.c
+++ b/apps/browser.c
@@ -53,80 +53,6 @@ void print_indent(char *buf, u32 n)
 		strcat(buf, "\t");
 }
 
-void parse(void *data, u32 len, char *out)
-{
-	struct xml_token tokens[128];
-	struct xml parser;
-	xml_init(&parser);
-	void *buffer = data;
-	len = strlen(data);
-	out[0] = '\0';
-	enum xml_error err = xml_parse(&parser, buffer, len, tokens, 128);
-
-	if (err != XML_SUCCESS) {
-		printf("\nXML parse error: %d\n", err);
-		return;
-	}
-
-	u32 indent = 0;
-	char name[16] = { 0 };
-	for (u32 i = 0; i < parser.ntokens; i++) {
-		const struct xml_token *token = tokens + i;
-		name[0] = '\0';
-		switch (token->type) {
-		case XML_START_TAG:
-			memcpy(&name, (u8 *)buffer + token->start_pos,
-			       token->end_pos - token->start_pos);
-			name[token->end_pos - token->start_pos] = '\0';
-			if (html_self_closing(name))
-				print_indent(out, indent);
-			else
-				print_indent(out, indent++);
-			strcat(out, name);
-			strcat(out, "\n");
-			break;
-		case XML_END_TAG:
-			print_indent(out, --indent);
-			memcpy(&name, (u8 *)buffer + token->start_pos,
-			       token->end_pos - token->start_pos);
-			name[token->end_pos - token->start_pos] = '\0';
-			strcat(out, name);
-			strcat(out, "/\n");
-			break;
-		case XML_CHARACTER:
-			if (token->end_pos == token->start_pos + 2) {
-				const char *ptr = (char *)buffer + token->start_pos;
-
-				if (ptr[0] == '\r' && ptr[1] == '\n')
-					continue;
-			}
-			memcpy(&name, (u8 *)buffer + token->start_pos,
-			       token->end_pos - token->start_pos);
-			name[token->end_pos - token->start_pos] = '\0';
-			char *clean_name = name;
-			for (u32 j = 0; j < strlen(name); j++) {
-				if (name[j] == ' ' || name[j] == '\n' || name[j] == '\r' ||
-				    name[j] == '\t') {
-					clean_name++;
-				} else {
-					break;
-				}
-			}
-			if (!strlen(clean_name))
-				break;
-			print_indent(out, indent++);
-			strcat(out, clean_name);
-			strcat(out, "\n");
-			indent--;
-			break;
-		default:
-			break;
-		}
-
-		i += token->size;
-	}
-}
-
 void on_submit(void *event, struct element *box)
 {
 	(void)event;
@@ -152,22 +78,20 @@ void on_submit(void *event, struct element *box)
 		ip = dns_request(url);
 	}
 
-	struct element_text_box *l = output->data;
 	struct element_label *c = code_label->data;
 
 	struct socket *socket = net_open(S_TCP);
 	if (socket && net_connect(socket, ip, port, NET_TIMEOUT)) {
 		net_send(socket, query, strlen(query));
 		char buf[4096] = { 0 };
-		char parsed[4096] = { 0 };
-		if (!net_receive(socket, buf, 4096, NET_TIMEOUT))
+		if (!net_receive(socket, buf, 4096, NET_TIMEOUT) ||
+		    !html_render(output, http_data(buf), 4096))
 			return;
-		parse(http_data(buf), 4096, parsed);
-		l->text = parsed[0] ? parsed : http_data(buf);
+
 		c->text = http_code(buf);
 		c->color_fg = status_color(c->text);
 	} else {
-		l->text = strdup("Can't connect to server.");
+		/* l->text = strdup("Can't connect to server."); */
 		c->text = strdup("000");
 		c->color_fg = COLOR_RED;
 	}
@@ -183,8 +107,7 @@ int main()
 	code_label = gui_add_label(root, 0, 0, FONT_24, "000", COLOR_BLACK, COLOR_WHITE);
 	struct element *text_input =
 		gui_add_text_input(root, LABEL_WIDTH, 0, 100, FONT_24, COLOR_WHITE, COLOR_BLACK);
-	output = gui_add_text_box(root, 0, FONT_HEIGHT + 2, 100, 100, FONT_16,
-				  "Enter URL and press Enter :)", COLOR_WHITE, COLOR_BLACK);
+	output = gui_add_container(root, 0, FONT_HEIGHT + 2, 100, 100, COLOR_GREEN);
 
 	text_input->event.on_submit = on_submit;
 
-- 
cgit v1.2.3


From e983cfa7f8580e39a181184fb2ae3a990597c02a Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Sat, 28 Nov 2020 19:13:32 +0100
Subject: Kinda-working renderer

---
 apps/browser.c |  6 +++++-
 libtxt/html.c  | 27 +++++++++++++++++++++------
 2 files changed, 26 insertions(+), 7 deletions(-)

(limited to 'apps/browser.c')

diff --git a/apps/browser.c b/apps/browser.c
index 160b626..0c2a430 100644
--- a/apps/browser.c
+++ b/apps/browser.c
@@ -92,6 +92,8 @@ void on_submit(void *event, struct element *box)
 		c->color_fg = status_color(c->text);
 	} else {
 		/* l->text = strdup("Can't connect to server."); */
+		gui_add_label(output, 0, 0, FONT_16, "Can't connect to server.", COLOR_WHITE,
+			      COLOR_BLACK);
 		c->text = strdup("000");
 		c->color_fg = COLOR_RED;
 	}
@@ -107,7 +109,9 @@ int main()
 	code_label = gui_add_label(root, 0, 0, FONT_24, "000", COLOR_BLACK, COLOR_WHITE);
 	struct element *text_input =
 		gui_add_text_input(root, LABEL_WIDTH, 0, 100, FONT_24, COLOR_WHITE, COLOR_BLACK);
-	output = gui_add_container(root, 0, FONT_HEIGHT + 2, 100, 100, COLOR_GREEN);
+	output = gui_add_container(root, 0, FONT_HEIGHT + 2, 100, 100, COLOR_WHITE);
+	gui_add_label(output, 0, 0, FONT_16, "Enter URL and press Enter :)", COLOR_WHITE,
+		      COLOR_BLACK);
 
 	text_input->event.on_submit = on_submit;
 
diff --git a/libtxt/html.c b/libtxt/html.c
index ae50ed2..b54577f 100644
--- a/libtxt/html.c
+++ b/libtxt/html.c
@@ -32,7 +32,7 @@ static char *normalize_tag_name(char *tag)
 static struct dom *new_object(const char *tag, struct dom *parent)
 {
 	struct dom *object = malloc(sizeof(*object));
-	object->tag = normalize_tag_name(strdup(tag));
+	object->tag = strdup(tag);
 	object->parent = parent;
 	object->content = NULL;
 	object->children = list_new();
@@ -69,7 +69,7 @@ static struct dom *generate_dom(char *data, u32 length)
 	struct dom *root = new_object("root", NULL);
 	struct dom *current = root;
 
-	char name[256] = { 0 };
+	static char name[256] = { 0 };
 	for (u32 i = 0; i < parser.ntokens; i++) {
 		const struct xml_token *token = tokens + i;
 		name[0] = '\0';
@@ -78,15 +78,20 @@ static struct dom *generate_dom(char *data, u32 length)
 			memcpy(&name, (u8 *)buffer + token->start_pos,
 			       token->end_pos - token->start_pos);
 			name[token->end_pos - token->start_pos] = '\0';
+			normalize_tag_name(name);
 			current = new_object(name, current);
 			printf("Adding %s to %s\n", current->tag, current->parent->tag);
 			list_add(current->parent->children, current);
+			if (is_self_closing(name))
+				current = current->parent;
 			break;
 		case XML_END_TAG:
 			memcpy(&name, (u8 *)buffer + token->start_pos,
 			       token->end_pos - token->start_pos);
 			name[token->end_pos - token->start_pos] = '\0';
-			assert(current && !strcmp(normalize_tag_name(name), current->tag));
+			normalize_tag_name(name);
+			if (!current || !current->parent || strcmp(name, current->tag))
+				return NULL;
 			current = current->parent;
 			break;
 		case XML_CHARACTER:
@@ -122,6 +127,7 @@ static struct dom *generate_dom(char *data, u32 length)
 		i += token->size;
 	}
 
+	assert(root);
 	print("GENERATED!\n");
 	print_dom(root, 0);
 	return root;
@@ -144,6 +150,7 @@ static struct html_element *render_object(struct html_element *container, struct
 {
 	char *tag = dom->tag;
 
+	assert(container);
 	if (CMP(tag, "html")) {
 		struct element *obj =
 			gui_add_container(container->obj, 0, 0, 100, 100, COLOR_WHITE);
@@ -155,21 +162,21 @@ static struct html_element *render_object(struct html_element *container, struct
 	} else if (CMP(tag, "h1")) {
 		struct element *obj =
 			gui_add_label(container->obj, container->x_offset, container->y_offset,
-				      FONT_64, dom->content, COLOR_WHITE, COLOR_BLACK);
+				      FONT_32, dom->content, COLOR_WHITE, COLOR_BLACK);
 		container->x_offset = 0;
 		container->y_offset += obj->ctx->height;
 		return new_html_element(obj, dom);
 	} else if (CMP(tag, "h2")) {
 		struct element *obj =
 			gui_add_label(container->obj, container->x_offset, container->y_offset,
-				      FONT_32, dom->content, COLOR_WHITE, COLOR_BLACK);
+				      FONT_24, dom->content, COLOR_WHITE, COLOR_BLACK);
 		container->x_offset = 0;
 		container->y_offset += obj->ctx->height;
 		return new_html_element(obj, dom);
 	} else if (CMP(tag, "h3")) {
 		struct element *obj =
 			gui_add_label(container->obj, container->x_offset, container->y_offset,
-				      FONT_24, dom->content, COLOR_WHITE, COLOR_BLACK);
+				      FONT_16, dom->content, COLOR_WHITE, COLOR_BLACK);
 		container->x_offset = 0;
 		container->y_offset += obj->ctx->height;
 		return new_html_element(obj, dom);
@@ -189,6 +196,14 @@ static struct html_element *render_object(struct html_element *container, struct
 		return container;
 	} else {
 		printf("UNKNOWN %s\n", tag);
+		if (dom->content && strlen(dom->content) > 0) {
+			struct element *obj = gui_add_label(container->obj, container->x_offset,
+							    container->y_offset, FONT_16,
+							    dom->content, COLOR_WHITE, COLOR_BLACK);
+			container->x_offset = 0;
+			container->y_offset += obj->ctx->height;
+			return new_html_element(obj, dom);
+		}
 		return container;
 	}
 }
-- 
cgit v1.2.3