From 6efff6f21d16c47823c1d4785398893ebf8e2f88 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Thu, 28 Jun 2007 14:47:35 +0200 Subject: [PATCH] DOM ecmascript: Defined properties of HTMLDocument. --- .../spidermonkey/html/HTMLDocument.c | 145 ++++++++++++++++-- .../spidermonkey/html/HTMLDocument.h | 17 ++ src/dom/node.h | 3 + 3 files changed, 151 insertions(+), 14 deletions(-) diff --git a/src/document/dom/ecmascript/spidermonkey/html/HTMLDocument.c b/src/document/dom/ecmascript/spidermonkey/html/HTMLDocument.c index a4d485dda..9763ead5a 100644 --- a/src/document/dom/ecmascript/spidermonkey/html/HTMLDocument.c +++ b/src/document/dom/ecmascript/spidermonkey/html/HTMLDocument.c @@ -7,47 +7,145 @@ #include "document/dom/ecmascript/spidermonkey.h" #include "document/dom/ecmascript/spidermonkey/Node.h" #include "document/dom/ecmascript/spidermonkey/Document.h" +#include "document/dom/ecmascript/spidermonkey/html/HTMLCollection.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLDocument.h" +#include "dom/node.h" static JSBool HTMLDocument_getProperty(JSContext *ctx, JSObject *obj, jsval id, jsval *vp) { + struct dom_node *node; + struct HTMLDocument_struct *html; + + if (!obj || (!JS_InstanceOf(ctx, obj, (JSClass *)&HTMLDocument_class, NULL))) + return JS_FALSE; + + node = JS_GetPrivate(ctx, obj); + if (!node) + return JS_FALSE; + html = node->data.document.html_data; + if (!html) + return JS_FALSE; + if (!JSVAL_IS_INT(id)) return JS_TRUE; switch (JSVAL_TO_INT(id)) { case JSP_HTML_DOCUMENT_TITLE: - /* Write me! */ + string_to_jsval(ctx, vp, html->title); break; case JSP_HTML_DOCUMENT_REFERER: - /* Write me! */ + string_to_jsval(ctx, vp, html->referrer); break; case JSP_HTML_DOCUMENT_DOMAIN: - /* Write me! */ + string_to_jsval(ctx, vp, html->domain); break; case JSP_HTML_DOCUMENT_URL: - /* Write me! */ + string_to_jsval(ctx, vp, html->URL); break; case JSP_HTML_DOCUMENT_BODY: - /* Write me! */ + if (html->body) { + if (ctx == html->body->ecmascript_ctx) + object_to_jsval(ctx, vp, html->body->ecmascript_obj); + } break; case JSP_HTML_DOCUMENT_IMAGES: - /* Write me! */ + if (!html->images) + undef_to_jsval(ctx, vp); + else { + if (html->images->ctx == ctx) + object_to_jsval(ctx, vp, html->images->ecmascript_obj); + else { + JSObject *new_obj; + + html->images->ctx = ctx; + new_obj = JS_NewObject(ctx, + (JSClass *)&HTMLCollection_class, NULL, NULL); + if (new_obj) + JS_SetPrivate(ctx, new_obj, html->images); + html->images->ecmascript_obj = new_obj; + object_to_jsval(ctx, vp, new_obj); + } + } break; case JSP_HTML_DOCUMENT_APPLETS: - /* Write me! */ + if (!html->applets) + undef_to_jsval(ctx, vp); + else { + if (html->images->ctx == ctx) + object_to_jsval(ctx, vp, html->applets->ecmascript_obj); + else { + JSObject *new_obj; + + html->applets->ctx = ctx; + new_obj = JS_NewObject(ctx, + (JSClass *)&HTMLCollection_class, NULL, NULL); + if (new_obj) + JS_SetPrivate(ctx, new_obj, html->applets); + html->applets->ecmascript_obj = new_obj; + object_to_jsval(ctx, vp, new_obj); + } + } break; case JSP_HTML_DOCUMENT_LINKS: - /* Write me! */ + if (!html->links) + undef_to_jsval(ctx, vp); + else { + if (html->links->ctx == ctx) + object_to_jsval(ctx, vp, html->links->ecmascript_obj); + else { + JSObject *new_obj; + + html->links->ctx = ctx; + new_obj = JS_NewObject(ctx, + (JSClass *)&HTMLCollection_class, NULL, NULL); + if (new_obj) + JS_SetPrivate(ctx, new_obj, html->links); + html->links->ecmascript_obj = new_obj; + object_to_jsval(ctx, vp, new_obj); + } + } break; case JSP_HTML_DOCUMENT_FORMS: - /* Write me! */ + if (!html->forms) + undef_to_jsval(ctx, vp); + else { + if (html->forms->ctx == ctx) + object_to_jsval(ctx, vp, html->forms->ecmascript_obj); + else { + JSObject *new_obj; + + html->forms->ctx = ctx; + new_obj = JS_NewObject(ctx, + (JSClass *)&HTMLCollection_class, NULL, NULL); + if (new_obj) + JS_SetPrivate(ctx, new_obj, html->forms); + html->forms->ecmascript_obj = new_obj; + object_to_jsval(ctx, vp, new_obj); + } + } break; case JSP_HTML_DOCUMENT_ANCHORS: - /* Write me! */ + if (!html->anchors) + undef_to_jsval(ctx, vp); + else { + if (html->anchors->ctx == ctx) + object_to_jsval(ctx, vp, html->anchors->ecmascript_obj); + else { + JSObject *new_obj; + + html->anchors->ctx = ctx; + new_obj = JS_NewObject(ctx, + (JSClass *)&HTMLCollection_class, NULL, NULL); + if (new_obj) + JS_SetPrivate(ctx, new_obj, html->anchors); + html->anchors->ecmascript_obj = new_obj; + object_to_jsval(ctx, vp, new_obj); + } + } break; case JSP_HTML_DOCUMENT_COOKIE: - /* Write me! */ + string_to_jsval(ctx, vp, html->cookie); break; default: return Document_getProperty(ctx, obj, id, vp); @@ -58,18 +156,37 @@ HTMLDocument_getProperty(JSContext *ctx, JSObject *obj, jsval id, jsval *vp) static JSBool HTMLDocument_setProperty(JSContext *ctx, JSObject *obj, jsval id, jsval *vp) { + struct dom_node *node; + struct HTMLDocument_struct *html; + JSObject *value; + + if (!obj || (!JS_InstanceOf(ctx, obj, (JSClass *)&HTMLDocument_class, NULL))) + return JS_FALSE; + + node = JS_GetPrivate(ctx, obj); + if (!node) + return JS_FALSE; + html = node->data.document.html_data; + if (!html) + return JS_FALSE; + if (!JSVAL_IS_INT(id)) return JS_TRUE; switch (JSVAL_TO_INT(id)) { case JSP_HTML_DOCUMENT_TITLE: - /* Write me! */ + mem_free_set(&html->title, stracpy(jsval_to_string(ctx, vp))); break; case JSP_HTML_DOCUMENT_BODY: - /* Write me! */ + value = JSVAL_TO_OBJECT(*vp); + if (value) { + struct dom_node *body = JS_GetPrivate(ctx, value); + + html->body = body; + } break; case JSP_HTML_DOCUMENT_COOKIE: - /* Write me! */ + mem_free_set(&html->cookie, stracpy(jsval_to_string(ctx, vp))); break; default: return Document_setProperty(ctx, obj, id, vp); diff --git a/src/document/dom/ecmascript/spidermonkey/html/HTMLDocument.h b/src/document/dom/ecmascript/spidermonkey/html/HTMLDocument.h index 1f10463f6..a810c05e2 100644 --- a/src/document/dom/ecmascript/spidermonkey/html/HTMLDocument.h +++ b/src/document/dom/ecmascript/spidermonkey/html/HTMLDocument.h @@ -7,4 +7,21 @@ extern const JSClass HTMLDocument_class; extern const JSFunctionSpec HTMLDocument_funcs[]; extern const JSPropertySpec HTMLDocument_props[]; +struct dom_node; +struct dom_node_list; + +struct HTMLDocument_struct { + unsigned char *title; + unsigned char *referrer; + unsigned char *domain; + unsigned char *URL; + unsigned char *cookie; + struct dom_node *body; + struct dom_node_list *images; + struct dom_node_list *applets; + struct dom_node_list *links; + struct dom_node_list *forms; + struct dom_node_list *anchors; +}; + #endif diff --git a/src/dom/node.h b/src/dom/node.h index 70c110bbd..c2780b20b 100644 --- a/src/dom/node.h +++ b/src/dom/node.h @@ -92,6 +92,9 @@ struct dom_document_node { /* The child nodes. May be NULL. Ordered like they where inserted. */ /* FIXME: Should be just one element (root) node reference. */ struct dom_node_list *children; + + /* Data used by ECMAScript engine. */ + void *html_data; }; struct dom_id {