summaryrefslogtreecommitdiff
path: root/javascript/jsapi/document.c
blob: a40ce2011887147c47d55163b175c48df2d27827 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <dom/dom.h>

#include "utils/config.h"
#include "utils/log.h"

#include "javascript/jsapi.h"

/* IDL http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-document

CAUTION - write, writeln are not part of the DOM they come from:
http://www.w3.org/TR/html5/apis-in-html-documents.html#document.write

interface Document : Node {
  readonly attribute DOMImplementation implementation;
  readonly attribute DOMString URL;
  readonly attribute DOMString documentURI;
  readonly attribute DOMString compatMode;
  readonly attribute DOMString characterSet;
  readonly attribute DOMString contentType;

  readonly attribute DocumentType? doctype;
  readonly attribute Element? documentElement;
  HTMLCollection getElementsByTagName(DOMString localName);
  HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
  HTMLCollection getElementsByClassName(DOMString classNames);
  Element? getElementById(DOMString elementId);

  Element createElement(DOMString localName);
  Element createElementNS(DOMString? namespace, DOMString qualifiedName);
  DocumentFragment createDocumentFragment();
  Text createTextNode(DOMString data);
  Comment createComment(DOMString data);
  ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);

  Node importNode(Node node, optional boolean deep = true);
  Node adoptNode(Node node);

  Event createEvent(DOMString interface);

  Range createRange();

  // NodeFilter.SHOW_ALL = 0xFFFFFFFF
  NodeIterator createNodeIterator(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null);
  TreeWalker createTreeWalker(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null);

  // NEW
  void prepend((Node or DOMString)... nodes);
  void append((Node or DOMString)... nodes);
};


 */

static void jsfinalize_document(JSContext *cx, JSObject *obj);

struct jsclass_document_priv {
	struct html_content *htmlc;
	dom_document *node;
};

static JSClass jsclass_document =
{
        "document", 
	JSCLASS_HAS_PRIVATE, 
	JS_PropertyStub, 
	JS_PropertyStub, 
	JS_PropertyStub, 
	JS_StrictPropertyStub,
        JS_EnumerateStub, 
	JS_ResolveStub, 
	JS_ConvertStub, 
	jsfinalize_document, 
	JSCLASS_NO_OPTIONAL_MEMBERS
};

#define JSCLASS_NAME document

#include "node.c"

static JSBool JSAPI_NATIVE(getElementById, JSContext *cx, uintN argc, jsval *vp)
{
	JSString* u16_txt;
	char *txt;
	unsigned long txtlen;
	dom_string *idstr;
	dom_element *idelement;
	struct jsclass_document_priv *document;

	document = JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx,vp), &jsclass_document, NULL);
	if (document == NULL) {
		return JS_FALSE;
	}

	if (document->node == NULL) {
		/* no document available, this is obviously a problem
		 * for finding elements 
		 */
		JSAPI_SET_RVAL(cx, vp, JSVAL_NULL);

		return JS_TRUE;
	}

	if (!JS_ConvertArguments(cx, argc, JSAPI_ARGV(cx, vp), "S", &u16_txt))
		return JS_FALSE;

	JSString_to_char(u16_txt, txt, txtlen);

	dom_string_create((unsigned char*)txt, txtlen, &idstr);

	dom_document_get_element_by_id(document->node, idstr, &idelement);

	JSAPI_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(jsapi_new_element(cx, JS_GetGlobalObject(cx), document->htmlc, idelement)));

	return JS_TRUE;
}

static JSBool JSAPI_NATIVE(write, JSContext *cx, uintN argc, jsval *vp)
{
	JSString* u16_txt;
	char *txt;
	unsigned long length;
	struct jsclass_document_priv *document;

	document = JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx,vp), &jsclass_document, NULL);
	if (document == NULL) {
		return JS_FALSE;
	}

	if (!JS_ConvertArguments(cx, argc, JSAPI_ARGV(cx, vp), "S", &u16_txt)) {
		return JS_FALSE;
	}

	JSString_to_char(u16_txt, txt, length);

	LOG(("content %p parser %p writing %s",
	     document->htmlc, document->htmlc->parser, txt));
	if (document->htmlc->parser != NULL) {
		dom_hubbub_parser_insert_chunk(document->htmlc->parser, (uint8_t *)txt, length);
	}
	JSAPI_SET_RVAL(cx, vp, JSVAL_VOID);

	return JS_TRUE;
}

static JSFunctionSpec jsfunctions_document[] = {
	JSAPI_FS_NODE,
	JSAPI_FS(write, 1, 0),
	JSAPI_FS(getElementById, 1, 0),
	JSAPI_FS_END
};



static void jsfinalize_document(JSContext *cx, JSObject *obj)
{
	struct jsclass_document_priv *document;

	document = JS_GetInstancePrivate(cx, obj, &jsclass_document, NULL);
	if (document != NULL) {
		free(document);
	}
}

JSObject *jsapi_new_document(JSContext *cx, JSObject *parent, struct html_content *htmlc)
{
	/* create document object and return it */
	JSObject *jsdocument;
	struct jsclass_document_priv *document;

	document = malloc(sizeof(document));
	if (document == NULL) {
		return NULL;
	}
	document->htmlc = htmlc;
	document->node = htmlc->document;

	jsdocument = JS_InitClass(cx, 
		     parent, 
		     NULL, 
		     &jsclass_document, 
		     NULL, 
		     0, 
		     NULL, 
		     jsfunctions_document, 
		     NULL, 
		     NULL);
	if (jsdocument == NULL) {
		free(document);
		return NULL;
	}

	LOG(("setting document private to %p", document));
	/* private pointer to browsing context */
	if (JS_SetPrivate(cx, jsdocument, document) != JS_TRUE) {
		LOG(("failed to set document private"));
		free(document);
		return NULL;
	}
	
	return jsdocument;
}