From a29a9112b9dfe6c525d1d9d627d220fbb3735529 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sat, 16 Nov 2024 18:56:12 +0100 Subject: [PATCH] [quickjs] Image.width, Image.height in constructor --- src/js/quickjs/element.c | 54 ++++++++++++++++++++++++++++++++++++++++ src/js/quickjs/image.c | 32 ++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/js/quickjs/element.c b/src/js/quickjs/element.c index 1edb78c9b..f2eb01552 100644 --- a/src/js/quickjs/element.c +++ b/src/js/quickjs/element.c @@ -706,6 +706,32 @@ fprintf(stderr, "Before: %s:%d\n", __FUNCTION__, __LINE__); return JS_NULL; } +static JSValue +js_element_get_property_height(JSContext *ctx, JSValueConst this_val) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + + dom_node *el = (dom_node *)(js_getopaque(this_val, js_element_class_id)); + dom_string *h = NULL; + dom_exception exc; + + if (!el) { + return JS_NULL; + } + exc = dom_element_get_attribute(el, corestring_dom_height, &h); + + if (exc != DOM_NO_ERR || !h) { + return JS_NewInt32(ctx, 0); + } + int height = atoi(dom_string_data(h)); + dom_string_unref(h); + + return JS_NewInt32(ctx, height); +} + static JSValue js_element_get_property_href(JSContext *ctx, JSValueConst this_val) { @@ -1664,6 +1690,32 @@ js_element_get_property_textContent(JSContext *ctx, JSValueConst this_val) RETURN_JS(ret); } +static JSValue +js_element_get_property_width(JSContext *ctx, JSValueConst this_val) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + + dom_node *el = (dom_node *)(js_getopaque(this_val, js_element_class_id)); + dom_string *w = NULL; + dom_exception exc; + + if (!el) { + return JS_NULL; + } + exc = dom_element_get_attribute(el, corestring_dom_width, &w); + + if (exc != DOM_NO_ERR || !w) { + return JS_NewInt32(ctx, 0); + } + int width = atoi(dom_string_data(w)); + dom_string_unref(w); + + return JS_NewInt32(ctx, width); +} + static JSValue js_element_set_property_className(JSContext *ctx, JSValueConst this_val, JSValue val) { @@ -3804,6 +3856,7 @@ static const JSCFunctionListEntry js_element_proto_funcs[] = { JS_CGETSET_DEF("dir", js_element_get_property_dir, js_element_set_property_dir), JS_CGETSET_DEF("firstChild", js_element_get_property_firstChild, NULL), JS_CGETSET_DEF("firstElementChild", js_element_get_property_firstElementChild, NULL), + JS_CGETSET_DEF("height", js_element_get_property_height, NULL), JS_CGETSET_DEF("href", js_element_get_property_href, js_element_set_property_href), JS_CGETSET_DEF("id", js_element_get_property_id, js_element_set_property_id), JS_CGETSET_DEF("innerHTML", js_element_get_property_innerHtml, js_element_set_property_innerHtml), @@ -3832,6 +3885,7 @@ static const JSCFunctionListEntry js_element_proto_funcs[] = { JS_CGETSET_DEF("textContent", js_element_get_property_textContent, js_element_set_property_textContent), // Node JS_CGETSET_DEF("title", js_element_get_property_title, js_element_set_property_title), JS_CGETSET_DEF("value", js_element_get_property_value, js_element_set_property_value), + JS_CGETSET_DEF("width", js_element_get_property_width, NULL), JS_CFUNC_DEF("addEventListener", 3, js_element_addEventListener), JS_CFUNC_DEF("appendChild", 1, js_element_appendChild), // Node JS_CFUNC_DEF("blur", 0, js_element_blur), diff --git a/src/js/quickjs/image.c b/src/js/quickjs/image.c index 22bdcc5a6..1e6cf4bd9 100644 --- a/src/js/quickjs/image.c +++ b/src/js/quickjs/image.c @@ -81,6 +81,38 @@ js_image_constructor(JSContext *ctx, JSValueConst new_target, int argc, JSValueC //dom_node_unref(doc); return JS_NULL; } + + if (argc > 0) { + size_t width_len; + const char *width = JS_ToCStringLen(ctx, &width_len, argv[0]); + + if (width) { + dom_string *value_str = NULL; + exc = dom_string_create((const uint8_t *)width, width_len, &value_str); + JS_FreeCString(ctx, width); + + if (exc == DOM_NO_ERR) { + (void)dom_element_set_attribute(element, corestring_dom_width, value_str); + dom_string_unref(value_str); + } + } + } + + if (argc > 1) { + size_t height_len; + const char *height = JS_ToCStringLen(ctx, &height_len, argv[1]); + + if (height) { + dom_string *value_str = NULL; + exc = dom_string_create((const uint8_t *)height, height_len, &value_str); + JS_FreeCString(ctx, height); + + if (exc == DOM_NO_ERR) { + (void)dom_element_set_attribute(element, corestring_dom_height, value_str); + dom_string_unref(value_str); + } + } + } //dom_node_unref(doc); JSValue rr = getNode(ctx, element); #ifdef ECMASCRIPT_DEBUG