1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[ecmascript] Added camel_to_html function

This function converts text from camelCase to dash-style
and prepends "data-" to string.
This commit is contained in:
Witold Filipczyk 2024-06-29 09:42:48 +02:00
parent 2eb2c2c3f0
commit 8d7ec1a81c
6 changed files with 37 additions and 22 deletions

View File

@ -759,3 +759,18 @@ get_elements_by_class_name(dom_html_document *doc, dom_node *node, char *classes
return col;
}
void
camel_to_html(struct string *result, const char *text)
{
add_to_string(result, "data-");
for (; *text; text++) {
if (*text >= 'A' && *text <= 'Z') {
add_char_to_string(result, '-');
add_char_to_string(result, *text + 32);
} else {
add_char_to_string(result, *text);
}
}
}

View File

@ -58,6 +58,8 @@ void *walk_tree_query(dom_node *node, const char *selector, int depth);
void walk_tree_query_append(dom_node *root, dom_node *node, const char *selector, int depth, LIST_OF(struct selector_node) *result_list);
void *get_elements_by_class_name(dom_html_document *doc, dom_node *node, char *classes);
void camel_to_html(struct string *result, const char *camel);
extern struct module ecmascript_module;
#ifdef __cplusplus

View File

@ -17,6 +17,7 @@
#include "document/libdom/corestrings.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/ecmascript-c.h"
#include "ecmascript/mujs/mapa.h"
#include "ecmascript/mujs.h"
#include "ecmascript/mujs/dataset.h"
@ -50,9 +51,7 @@ mjs_obj_dataset_has(js_State *J, void *p, const char *property)
if (!el ||!init_string(&data)) {
return 0;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
dom_string *attr_name = NULL;
dom_exception exc = dom_string_create(data.source, data.length, &attr_name);
done_string(&data);
@ -95,9 +94,7 @@ mjs_obj_dataset_put(js_State *J, void *p, const char *property)
if (!el ||!init_string(&data)) {
return 0;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
dom_string *attr_name = NULL;
dom_exception exc = dom_string_create(data.source, data.length, &attr_name);
done_string(&data);
@ -137,9 +134,7 @@ mjs_obj_dataset_del(js_State *J, void *p, const char *property)
if (!el ||!init_string(&data)) {
return 0;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
dom_string *attr_name = NULL;
dom_exception exc = dom_string_create(data.source, data.length, &attr_name);
done_string(&data);

View File

@ -17,6 +17,7 @@
#include "document/libdom/corestrings.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/ecmascript-c.h"
#include "ecmascript/quickjs/mapa.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/dataset.h"
@ -60,8 +61,7 @@ js_obj_delete_property(JSContext *ctx, JSValueConst obj, JSAtom prop)
JS_FreeCString(ctx, property);
return 0;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
JS_FreeCString(ctx, property);
dom_string *attr_name = NULL;
@ -110,8 +110,7 @@ js_obj_get_property(JSContext *ctx, JSValueConst obj, JSAtom atom, JSValueConst
JS_FreeCString(ctx, property);
return JS_UNDEFINED;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
JS_FreeCString(ctx, property);
dom_string *attr_name = NULL;
@ -162,8 +161,7 @@ js_obj_set_property(JSContext *ctx, JSValueConst obj, JSAtom atom, JSValueConst
JS_FreeCString(ctx, value);
return 0;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
JS_FreeCString(ctx, property);
dom_string *attr_name = NULL;

View File

@ -27,6 +27,7 @@
#include "document/libdom/corestrings.h"
#include "document/view.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/ecmascript-c.h"
#include "ecmascript/spidermonkey/dataset.h"
#include "ecmascript/spidermonkey/element.h"
#include "intl/libintl.h"
@ -88,8 +89,7 @@ dataset_obj_getProperty(JSContext* ctx, JS::HandleObject obj, JS::HandleValue re
mem_free(property);
return true;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
mem_free(property);
dom_string *attr_name = NULL;
@ -150,8 +150,7 @@ dataset_obj_setProperty(JSContext* ctx, JS::HandleObject obj, JS::HandleId id, J
mem_free(value);
return true;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
mem_free(property);
dom_string *attr_name = NULL;
@ -208,8 +207,7 @@ dataset_obj_deleteProperty(JSContext* ctx, JS::HandleObject obj, JS::HandleId id
mem_free(property);
return true;
}
add_to_string(&data, "data-");
add_to_string(&data, property);
camel_to_html(&data, property);
mem_free(property);
dom_string *attr_name = NULL;

View File

@ -2,7 +2,7 @@
<html>
<body>
<div id="a" data-a="1" data-b="abc" data-cd="123"></div>
<div id="a" data-a="1" data-b="abc" data-abc-cd="1234"></div>
<button onclick="myFunction()">Try it</button>
@ -18,6 +18,13 @@ function myFunction() {
delete(div.dataset.b);
console.assert(div.dataset.b === undefined, 'undefined because deleted');
console.assert(div.dataset.abcCd === '1234', 'camelCase');
div.dataset.abcCd = '2345';
console.assert(div.dataset.abcCd === '2345', 'camelCase set');
delete(div.dataset.abcCd);
console.assert(div.dataset.abcCd === undefined, 'undefined after deletion');
}
console.error('element.dataset.html');