1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

[quickjs] element.style (getter)

This commit is contained in:
Witold Filipczyk 2023-09-19 16:48:16 +02:00
parent e2ea308100
commit b34d1b6b60
8 changed files with 393 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* The MuJS attr object implementation. */
/* The MuJS style object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"

View File

@ -2,6 +2,6 @@ top_builddir=../../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o attributes.o collection.o console.o document.o element.o form.o forms.o heartbeat.o history.o implementation.o input.o \
keyboard.o localstorage.o location.o mapa.obj message.o navigator.o nodelist.o screen.o unibar.o window.o xhr.o
keyboard.o localstorage.o location.o mapa.obj message.o navigator.o nodelist.o screen.o style.o unibar.o window.o xhr.o
include $(top_srcdir)/Makefile.lib

View File

@ -30,6 +30,7 @@
#include "ecmascript/quickjs/heartbeat.h"
#include "ecmascript/quickjs/keyboard.h"
#include "ecmascript/quickjs/nodelist.h"
#include "ecmascript/quickjs/style.h"
#include "ecmascript/quickjs/window.h"
#include "session/session.h"
#include "terminal/event.h"
@ -715,6 +716,23 @@ js_element_get_property_previousSibling(JSContext *ctx, JSValueConst this_val)
return getElement(ctx, node);
}
static JSValue
js_element_get_property_style(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));
if (!el) {
return JS_NULL;
}
return getStyle(ctx, el);
}
static JSValue
js_element_get_property_tagName(JSContext *ctx, JSValueConst this_val)
{
@ -2560,6 +2578,7 @@ static const JSCFunctionListEntry js_element_proto_funcs[] = {
JS_CGETSET_DEF("parentNode", js_element_get_property_parentNode, NULL),
JS_CGETSET_DEF("previousElementSibling", js_element_get_property_previousElementSibling, NULL),
JS_CGETSET_DEF("previousSibling", js_element_get_property_previousSibling, NULL),
JS_CGETSET_DEF("style", js_element_get_property_style, NULL),
JS_CGETSET_DEF("tagName", js_element_get_property_tagName, NULL),
JS_CGETSET_DEF("textContent", js_element_get_property_textContent, NULL),
JS_CGETSET_DEF("title", js_element_get_property_title, js_element_set_property_title),

View File

@ -479,3 +479,127 @@ get_output_header(const char *header_name, struct Xhr *x)
return NULL;
}
const std::map<std::string, bool> good = {
{ "background", true },
{ "background-color", true },
{ "color", true },
{ "display", true },
{ "font-style", true },
{ "font-weight", true },
{ "list-style", true },
{ "list-style-type", true },
{ "text-align", true },
{ "text-decoration", true },
{ "white-space", true }
};
static std::string
trimString(std::string str)
{
const std::string whiteSpaces = " \t\n\r\f\v";
// Remove leading whitespace
size_t first_non_space = str.find_first_not_of(whiteSpaces);
str.erase(0, first_non_space);
// Remove trailing whitespace
size_t last_non_space = str.find_last_not_of(whiteSpaces);
str.erase(last_non_space + 1);
return str;
}
void *
set_elstyle(const char *text)
{
if (!text || !*text) {
return NULL;
}
std::stringstream str(text);
std::string word;
std::string param, value;
std::map<std::string, std::string> *css = NULL;
while (!str.eof()) {
getline(str, word, ';');
std::stringstream params(word);
getline(params, param, ':');
getline(params, value, ':');
param = trimString(param);
value = trimString(value);
if (good.find(param) != good.end()) {
if (!css) {
css = new std::map<std::string, std::string>;
}
if (css) {
(*css)[param] = value;
}
}
}
return (void *)css;
}
const char *
get_elstyle(void *m)
{
std::map<std::string, std::string> *css = static_cast<std::map<std::string, std::string> *>(m);
std::string delimiter("");
std::stringstream output("");
std::map<std::string, std::string>::iterator it;
for (it = css->begin(); it != css->end(); it++) {
output << delimiter << it->first << ":" << it->second;
delimiter = ";";
}
return stracpy(output.str().c_str());
}
const char *
get_css_value(const char *text, const char *param)
{
void *m = set_elstyle(text);
char *res = NULL;
if (!m) {
return stracpy("");
}
std::map<std::string, std::string> *css = static_cast<std::map<std::string, std::string> *>(m);
if (css->find(param) != css->end()) {
res = stracpy((*css)[param].c_str());
} else {
res = stracpy("");
}
css->clear();
delete css;
return res;
}
const char *
set_css_value(const char *text, const char *param, const char *value)
{
void *m = set_elstyle(text);
std::map<std::string, std::string> *css = NULL;
if (m) {
css = static_cast<std::map<std::string, std::string> *>(m);
if (good.find(param) != good.end()) {
(*css)[param] = value;
}
return get_elstyle(m);
}
if (good.find(param) != good.end()) {
css = new std::map<std::string, std::string>;
if (!css) {
return stracpy("");
}
(*css)[param] = value;
return get_elstyle((void *)css);
}
return stracpy("");
}

View File

@ -75,6 +75,11 @@ void set_xhr_header(char *normalized_value, const char *h_name, struct Xhr *x);
const char *get_output_headers(struct Xhr *x);
const char *get_output_header(const char *header_name, struct Xhr *x);
const char *get_elstyle(void *m);
void *set_elstyle(const char *text);
const char *get_css_value(const char *text, const char *param);
const char *set_css_value(const char *text, const char *param, const char *value);
#ifdef __cplusplus
}

View File

@ -1,3 +1,3 @@
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'document.c', 'element.c', 'form.c', 'forms.c', 'heartbeat.c', 'history.c',
'implementation.c', 'input.c', 'keyboard.c', 'localstorage.c', 'location.c',
'mapa.cpp', 'message.c', 'navigator.c', 'nodelist.c', 'screen.c', 'unibar.c', 'window.c', 'xhr.c')
'mapa.cpp', 'message.c', 'navigator.c', 'nodelist.c', 'screen.c', 'style.c', 'unibar.c', 'window.c', 'xhr.c')

View File

@ -0,0 +1,226 @@
/* The QuickJS style object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#ifdef CONFIG_LIBDOM
#include <dom/dom.h>
#include <dom/bindings/hubbub/parser.h>
#endif
#include "elinks.h"
#include "document/libdom/corestrings.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/libdom/quickjs/mapa.h"
#include "ecmascript/quickjs/style.h"
#include "ecmascript/quickjs.h"
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSClassID js_style_class_id;
static JSValue
js_style(JSContext *ctx, JSValueConst this_val, const char *property)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
dom_exception exc;
dom_node *el = (dom_node *)(JS_GetOpaque(this_val, js_style_class_id));
dom_string *style = NULL;
const char *res = NULL;
JSValue r;
if (!el) {
return JS_NULL;
}
exc = dom_element_get_attribute(el, corestring_dom_style, &style);
if (exc != DOM_NO_ERR) {
r = JS_NewString(ctx, "");
RETURN_JS(r);
}
if (!style || !dom_string_length(style)) {
r = JS_NewString(ctx, "");
if (style) {
dom_string_unref(style);
}
RETURN_JS(r);
}
res = get_css_value(dom_string_data(style), property);
dom_string_unref(style);
if (!res) {
r = JS_NewString(ctx, "");
RETURN_JS(r);
}
r = JS_NewString(ctx, res);
mem_free(res);
RETURN_JS(r);
}
static JSValue
js_style_get_property_background(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "background");
}
static JSValue
js_style_get_property_backgroundColor(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "background-color");
}
static JSValue
js_style_get_property_color(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "color");
}
static JSValue
js_style_get_property_display(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "display");
}
static JSValue
js_style_get_property_fontStyle(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "font-style");
}
static JSValue
js_style_get_property_fontWeight(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "font-weight");
}
static JSValue
js_style_get_property_lineStyle(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "line-style");
}
static JSValue
js_style_get_property_lineStyleType(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "line-style-type");
}
static JSValue
js_style_get_property_textAlign(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "text-align");
}
static JSValue
js_style_get_property_textDecoration(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "text-decoration");
}
static JSValue
js_style_get_property_whiteSpace(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return js_style(ctx, this_val, "white-space");
}
void js_style_finalizer(JSRuntime *rt, JSValue val)
{
REF_JS(val);
}
static JSValue
js_style_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
return JS_NewString(ctx, "[style object]");
}
static const JSCFunctionListEntry js_style_proto_funcs[] = {
JS_CGETSET_DEF("background", js_style_get_property_background, NULL),
JS_CGETSET_DEF("backgroundColor", js_style_get_property_backgroundColor, NULL),
JS_CGETSET_DEF("color", js_style_get_property_color, NULL),
JS_CGETSET_DEF("display", js_style_get_property_display, NULL),
JS_CGETSET_DEF("fontStyle", js_style_get_property_fontStyle, NULL),
JS_CGETSET_DEF("fontWeight", js_style_get_property_fontWeight, NULL),
JS_CGETSET_DEF("lineStyle", js_style_get_property_lineStyle, NULL),
JS_CGETSET_DEF("lineStyleType", js_style_get_property_lineStyleType, NULL),
JS_CGETSET_DEF("textAlign", js_style_get_property_textAlign, NULL),
JS_CGETSET_DEF("textDecoration", js_style_get_property_textDecoration, NULL),
JS_CGETSET_DEF("whiteSpace", js_style_get_property_whiteSpace, NULL),
JS_CFUNC_DEF("toString", 0, js_style_toString)
};
static JSClassDef js_style_class = {
"style",
js_style_finalizer
};
JSValue
getStyle(JSContext *ctx, void *node)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JSValue second = JS_NULL;
static int initialized;
/* create the element class */
if (!initialized) {
JS_NewClassID(&js_style_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_style_class_id, &js_style_class);
initialized = 1;
}
JSValue style_obj = JS_NewObjectClass(ctx, js_style_class_id);
JS_SetPropertyFunctionList(ctx, style_obj, js_style_proto_funcs, countof(js_style_proto_funcs));
JS_SetClassProto(ctx, js_style_class_id, style_obj);
JS_SetOpaque(style_obj, node);
JSValue rr = JS_DupValue(ctx, style_obj);
RETURN_JS(rr);
}

View File

@ -0,0 +1,16 @@
#ifndef EL__ECMASCRIPT_QUICKJS_STYLE_H
#define EL__ECMASCRIPT_QUICKJS_STYLE_H
#include <quickjs/quickjs.h>
#ifdef __cplusplus
extern "C" {
#endif
JSValue getStyle(JSContext *ctx, void *node);
#ifdef __cplusplus
}
#endif
#endif