summaryrefslogtreecommitdiff
path: root/content/handlers/javascript/duktape/DOMImplementation.bnd
blob: 05c481ddcc6720cb9b613ff2f55f5bd700458585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* DOMImplementation binding for browser using duktape and libdom
 *
 * Copyright 2020 Daniel Silverstone <dsilvers@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * Released under the terms of the MIT License,
 *         http://www.opensource.org/licenses/mit-license
 */

prologue DOMImplementation ()
%{
#include "utils/corestrings.h"
%}

method DOMImplemention::hasFeature ()
%{
	/* Always return true */
	duk_push_bool(ctx, true);
	return 1;
%}

method DOMImplementation::createHTMLDocument ()
%{
	duk_size_t text_len;
	const char *text = duk_safe_to_lstring(ctx, 0, &text_len);
	struct dom_document *doc = NULL, *ret = NULL;
	struct dom_document_type *doctype = NULL;
	struct dom_html_element *html = NULL, *head = NULL, *title = NULL, *body = NULL;
	struct dom_node *spare_ref = NULL;
	dom_string *text_str = NULL;
	dom_exception exc;

	exc = dom_string_create((const uint8_t*)text, text_len, &text_str);
	if (exc != DOM_NO_ERR) goto out;

	exc = dom_implementation_create_document(
		DOM_IMPLEMENTATION_HTML,
		NULL, NULL,
		NULL,
		NULL, NULL,
		&doc
		);
	if (exc != DOM_NO_ERR) goto out;

	exc = dom_implementation_create_document_type(
		"html", NULL, NULL, &doctype);
	if (exc != DOM_NO_ERR) goto out;

	exc = dom_node_append_child(doc, doctype, &spare_ref);
	if (exc != DOM_NO_ERR) goto out;
	if (spare_ref != NULL) { dom_node_unref(spare_ref); spare_ref = NULL; }

	exc = dom_document_create_element_ns(doc, corestring_dom_html_namespace,
					     corestring_dom_HTML, &html);
	if (exc != DOM_NO_ERR) goto out;

	exc = dom_document_create_element_ns(doc, corestring_dom_html_namespace,
					     corestring_dom_HEAD, &head);
	if (exc != DOM_NO_ERR) goto out;

	exc = dom_document_create_element_ns(doc, corestring_dom_html_namespace,
					     corestring_dom_TITLE, &title);
	if (exc != DOM_NO_ERR) goto out;

	exc = dom_document_create_element_ns(doc, corestring_dom_html_namespace,
					     corestring_dom_BODY, &body);
	if (exc != DOM_NO_ERR) goto out;

	exc = dom_node_set_text_content(title, text_str);
	if (exc != DOM_NO_ERR) goto out;

	exc = dom_node_append_child(head, title, &spare_ref);
	if (exc != DOM_NO_ERR) goto out;
	if (spare_ref != NULL) { dom_node_unref(spare_ref); spare_ref = NULL; }

	exc = dom_node_append_child(html, head, &spare_ref);
	if (exc != DOM_NO_ERR) goto out;
	if (spare_ref != NULL) { dom_node_unref(spare_ref); spare_ref = NULL; }

	exc = dom_node_append_child(html, body, &spare_ref);
	if (exc != DOM_NO_ERR) goto out;
	if (spare_ref != NULL) { dom_node_unref(spare_ref); spare_ref = NULL; }

	exc = dom_node_append_child(doc, html, &spare_ref);
	if (exc != DOM_NO_ERR) goto out;
	if (spare_ref != NULL) { dom_node_unref(spare_ref); spare_ref = NULL; }

	ret = doc;
	doc = NULL;

out:
	if (text_str != NULL) {
		dom_string_unref(text_str);
	}
	if (doc != NULL) {
		dom_node_unref(doc);
	}
	if (html != NULL) {
		dom_node_unref(html);
	}
	if (head != NULL) {
		dom_node_unref(head);
	}
	if (title != NULL) {
		dom_node_unref(title);
	}
	if (body != NULL) {
		dom_node_unref(body);
	}
	if (doctype != NULL) {
		dom_node_unref(doctype);
	}
	if (ret != NULL) {
		dukky_push_node(ctx, (struct dom_node *)ret);
		dom_node_unref(ret);
		return 1;
	}
	return 0; /* Coerced to undefined */
%}