Implemented DOM_NewText.

Added stuff for node destruction, including making node_finalize module-public
for use by other node types.
Added new methods to Element (setAttribute + stubs).
This commit is contained in:
shaver%netscape.com 1998-09-03 23:51:58 +00:00
parent 801d3e6ac9
commit 15bd1de2ff
6 changed files with 81 additions and 25 deletions

View File

@ -192,9 +192,12 @@ struct DOM_Node {
JSBool
DOM_Init(JSContext *cx, JSObject *scope);
JSBool
void
DOM_DestroyNode(JSContext *cx, DOM_Node *node);
void
DOM_DestroyTree(JSContext *cx, DOM_Node *top);
JSObject *
DOM_NewNodeObject(JSContext *cx, DOM_Node *node);
@ -270,7 +273,8 @@ struct DOM_Text {
};
DOM_Text *
DOM_NewText(const char *data, int64 len, DOM_CDataOp notify);
DOM_NewText(const char *data, int64 len, DOM_CDataOp notify,
DOM_NodeOps *ops);
JSObject *
DOM_NewTextObject(JSContext *cx, DOM_Text *text);

View File

@ -54,6 +54,9 @@ dom_node_getter(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
JSBool
dom_node_setter(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
void
dom_node_finalize(JSContext *cx, JSObject *obj);
/* if you adjust these enums, be sure to adjust the various setters */
enum {
DOM_NODE_NODENAME = -1,

View File

@ -128,10 +128,26 @@ DOM_ObjectForNodeDowncast(JSContext *cx, DOM_Node *node)
if (!node)
return NULL;
if (node->mocha_object)
return node->mocha_object;
if (!node->mocha_object)
node->mocha_object = node->ops->reflectNode(cx, node);
return node->mocha_object;
}
return node->ops->reflectNode(cx, node);
void
DOM_DestroyTree(JSContext *cx, DOM_Node *top)
{
DOM_Node *iter;
for (iter = top->child; iter; iter = iter->sibling) {
if (!iter->mocha_object)
DOM_DestroyTree(cx, iter);
#ifdef DEBUG_shaver
else {
fprintf(stderr, "node %s type %d has mocha_object\n",
iter->name ? iter->name : "<none>", iter->type);
}
#endif
}
DOM_DestroyNode(cx, top);
}
/*

View File

@ -87,13 +87,19 @@ element_setter(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
return JS_TRUE;
}
static JSClass DOM_ElementClass = {
"Element", JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub, element_getter, element_setter,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, dom_node_finalize
};
static JSBool
element_getAttribute(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
DOM_Element *element;
JSString *name;
char *value;
const char *value;
JSBool cache;
if (!JS_ConvertArguments(cx, argc, argv, "S", &name))
@ -176,16 +182,14 @@ element_removeAttributeNode(JSContext *cx, JSObject *obj, uintN argc,
return JS_TRUE;
}
static void
element_finalize(JSContext *cx, JSObject *obj)
{
return;
}
static JSClass DOM_ElementClass = {
"Element", JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub, element_getter, element_setter,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, element_finalize
static JSFunctionSpec element_methods[] = {
{"setAttribute", element_setAttribute, 2},
{"getAttribute", element_getAttribute, 1},
{"removeAttribute", element_removeAttribute, 1},
{"setAttributeNode", element_setAttributeNode, 1},
{"getAttributeNode", element_getAttributeNode, 1},
{"removeAttributeNode", element_removeAttributeNode, 1},
{0}
};
JSObject *
@ -223,7 +227,7 @@ dom_ElementInit(JSContext *cx, JSObject *scope, JSObject *node_proto)
{
JSObject *proto = JS_InitClass(cx, scope, node_proto, &DOM_ElementClass,
Element, 0,
element_props, NULL,
element_props, element_methods,
NULL, NULL);
if (!JS_DefineProperties(cx, proto, dom_node_props))
return NULL;

View File

@ -69,7 +69,6 @@ DOM_ObjectForNode(JSContext *cx, DOM_Node *node)
return DOM_NewNodeObject(cx, node);
}
JSBool
dom_node_getter(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
@ -137,20 +136,20 @@ dom_node_setter(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
return JS_TRUE;
}
static void
node_finalize(JSContext *cx, JSObject *obj)
void
dom_node_finalize(JSContext *cx, JSObject *obj)
{
DOM_Node *priv = (DOM_Node *)JS_GetPrivate(cx, obj);
if (!priv)
return;
priv->mocha_object = NULL;
/* XXX walk tree, freeing until we find an object rooting a subgraph */
DOM_DestroyTree(cx, priv);
}
static JSClass DOM_NodeClass = {
"Node", JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub, dom_node_getter, dom_node_setter,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, node_finalize
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, dom_node_finalize
};
JSObject *
@ -171,6 +170,17 @@ DOM_NewNodeObject(JSContext *cx, DOM_Node *node)
return obj;
}
void
DOM_DestroyNode(JSContext *cx, DOM_Node *node)
{
XP_ASSERT(!node->mocha_object);
if (node->ops->destroyNode)
node->ops->destroyNode(cx, node);
if (node->name && node->type != NODE_TYPE_TEXT)
JS_free(cx, node->name);
JS_free(cx, node);
}
#define REMOVE_FROM_TREE(node) \
PR_BEGIN_MACRO \
if (node->prev_sibling) \

View File

@ -355,10 +355,29 @@ DOM_NewTextObject(JSContext *cx, DOM_Text *text)
}
DOM_Text *
DOM_NewText(const char *data, int64 length, DOM_CDataOp notify)
DOM_NewText(const char *data, int64 length, DOM_CDataOp notify,
DOM_NodeOps *ops)
{
XP_ASSERT((0 && "DOM_NewText NYI"));
return NULL;
DOM_Node *node;
DOM_CharacterData *cdata;
DOM_Text *text = XP_NEW_ZAP(DOM_Text);
if (!text)
return NULL;
node = (DOM_Node *)text;
node->type = NODE_TYPE_TEXT;
node->ops = ops;
node->name = "#text";
cdata = (DOM_CharacterData *)text;
cdata->data = XP_ALLOC(length);
cdata->notify = notify;
if (!cdata->data) {
XP_FREE(text);
return NULL;
}
XP_MEMCPY(cdata->data, data, length);
return text;
}
JSObject *