add document.createElement

This commit is contained in:
Vincent Sanders 2012-11-09 10:52:32 +00:00
parent e25cb44814
commit 9318e66449
3 changed files with 48 additions and 12 deletions

View File

@ -82,6 +82,8 @@ JSObject *jsapi_InitClass_Navigator(JSContext *cx, JSObject *parent);
*/ */
JSObject *jsapi_new_Navigator(JSContext *cx, JSObject *proto, JSObject *parent); JSObject *jsapi_new_Navigator(JSContext *cx, JSObject *proto, JSObject *parent);
extern JSClass JSClass_HTMLElement;
JSObject *jsapi_InitClass_HTMLElement(JSContext *cx, JSObject *parent); JSObject *jsapi_InitClass_HTMLElement(JSContext *cx, JSObject *parent);
/** Create a new javascript element object /** Create a new javascript element object
* *

View File

@ -6,25 +6,25 @@ webidlfile "dom.idl";
getter textContent %{ getter textContent %{
dom_exception exc; dom_exception exc;
dom_string *content; dom_string *content;
exc = dom_node_get_text_content(private->node, &content); exc = dom_node_get_text_content(private->node, &content);
if (exc != DOM_NO_ERR) { if (exc != DOM_NO_ERR) {
return JS_FALSE; return JS_FALSE;
} }
if (content != NULL) { if (content != NULL) {
jsret = JS_NewStringCopyN(cx, dom_string_data(content), dom_string_length(content)); jsret = JS_NewStringCopyN(cx, dom_string_data(content), dom_string_length(content));
dom_string_unref(content); dom_string_unref(content);
} }
%} %}
operation appendChild %{ operation appendChild %{
struct dom_node *result = NULL; struct dom_node *result = NULL;
dom_exception exc; dom_exception exc;
struct jsclass_private *node_private; struct jsclass_private *node_private;
dom_node_type node_type; dom_node_type node_type;
@ -34,12 +34,22 @@ operation appendChild %{
/* CAUTION this expects all Node objects private pointers to /* CAUTION this expects all Node objects private pointers to
* have private->node in the same place * have private->node in the same place
*/ */
/* text */
node_private = JS_GetInstancePrivate(cx, node, &JSClass_Text, NULL); node_private = JS_GetInstancePrivate(cx, node, &JSClass_Text, NULL);
if (node_private != NULL) { if (node_private == NULL) {
exc = dom_node_append_child(private->node, node_private->node, &result); /* element */
if (exc != DOM_NO_ERR) { node_private = JS_GetInstancePrivate(cx, node, &JSClass_HTMLElement, NULL);
return JS_FALSE; }
}
if (node_private == NULL) {
/* type error? */
return JS_FALSE;
}
/* append the found element */
exc = dom_node_append_child(private->node, node_private->node, &result);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
} }
if (result != NULL) { if (result != NULL) {

View File

@ -198,3 +198,27 @@ operation createTextNode %{
JSLOG("returning jsobject %p",jsret); JSLOG("returning jsobject %p",jsret);
%} %}
/* in dom Document */
operation createElement %{
dom_string *localName_dom;
dom_exception exc;
dom_element *element;
JSLOG("Creating text node for string \"%s\"", localName);
exc = dom_string_create((unsigned char*)localName, localName_len, &localName_dom);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
}
exc = dom_document_create_element(private->node, localName_dom, &element);
dom_string_unref(localName_dom);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
}
jsret = jsapi_new_HTMLElement(cx, NULL, NULL, element, private->htmlc);
JSLOG("returning jsobject %p",jsret);
%}