From 9a6edd69db3deecf61425fde7159a6ae80d0d2ea Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 15 May 2023 19:13:38 +0200 Subject: [PATCH] [libdom] Possibility to build libcss code without ecmascript. meson config option libcss enabled by default. To enable libcss in elinks: set document.css.libcss = 1 --- meson.build | 17 +++- meson_options.txt | 1 + src/document/document.cpp | 4 +- src/document/document.h | 3 + src/document/libdom/Makefile | 2 +- src/document/libdom/doc.c | 91 +++++++++++++++++++ src/document/libdom/doc.h | 19 ++++ src/document/libdom/meson.build | 2 +- src/document/libdom/renderer.c | 2 +- src/document/libdom/renderer2.c | 1 + src/document/renderer.cpp | 2 +- src/ecmascript/ecmascript.h | 1 - src/ecmascript/libdom/mujs/document.c | 1 + src/ecmascript/libdom/mujs/implementation.c | 1 + src/ecmascript/libdom/parse.c | 71 --------------- src/ecmascript/libdom/parse.h | 2 - src/ecmascript/libdom/quickjs/document.c | 1 + .../libdom/quickjs/implementation.c | 1 + .../libdom/spidermonkey/document.cpp | 1 + .../libdom/spidermonkey/implementation.cpp | 1 + 20 files changed, 143 insertions(+), 81 deletions(-) create mode 100644 src/document/libdom/doc.c create mode 100644 src/document/libdom/doc.h diff --git a/meson.build b/meson.build index f33ee670..d6547b0c 100644 --- a/meson.build +++ b/meson.build @@ -412,6 +412,7 @@ if conf_data.get('CONFIG_ECMASCRIPT_SMJS') or conf_data.get('CONFIG_QUICKJS') or endif conf_data.set('CONFIG_LIBCSS', false) +cssdeps = false if conf_data.get('CONFIG_ECMASCRIPT_SMJS') or conf_data.get('CONFIG_QUICKJS') or conf_data.get('CONFIG_MUJS') cssdeps = dependency('libcss', static: st, version: '>=0.9.1') @@ -420,13 +421,25 @@ if conf_data.get('CONFIG_ECMASCRIPT_SMJS') or conf_data.get('CONFIG_QUICKJS') or endif conf_data.set('CONFIG_LIBDOM', false) +libdomdeps = false if conf_data.get('CONFIG_ECMASCRIPT_SMJS') or conf_data.get('CONFIG_QUICKJS') or conf_data.get('CONFIG_MUJS') - cssdeps = dependency('libdom', static: st, version: '>=0.4.1') - deps += cssdeps + libdomdeps = dependency('libdom', static: st, version: '>=0.4.1') + deps += libdomdeps conf_data.set('CONFIG_LIBDOM', true) endif +if not cssdeps and get_option('libcss') + cssdeps = dependency('libcss', static: st, version: '>=0.9.1') + deps += cssdeps + conf_data.set('CONFIG_LIBCSS', true) + if not libdomdeps + libdomdeps = dependency('libdom', static: st, version: '>=0.4.1') + deps += libdomdeps + conf_data.set('CONFIG_LIBDOM', true) + endif +endif + if conf_data.get('CONFIG_SCRIPTING_LUA') luadeps = dependency(luapkg, static: st) deps += luadeps diff --git a/meson_options.txt b/meson_options.txt index 12e15b68..19d9c9dc 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -76,3 +76,4 @@ option('docdir', type: 'string', value: '', description: 'Documentation installa option('apidoc', type: 'boolean', value: true, description: 'whether to generate API docs with doxygen') option('htmldoc', type: 'boolean', value: true, description: 'whether to build html docs') option('pdfdoc', type: 'boolean', value: true, description: 'whether to build manual.pdf') +option('libcss', type: 'boolean', value: true, description: 'whether to compile libcss support, requires libdom. This option is automatically enabled with js support') diff --git a/src/document/document.cpp b/src/document/document.cpp index 3d202e26..70a2fa81 100644 --- a/src/document/document.cpp +++ b/src/document/document.cpp @@ -69,6 +69,7 @@ #endif #ifdef CONFIG_LIBDOM +#include "document/libdom/doc.h" #include "document/libdom/mapa.h" #endif @@ -379,7 +380,9 @@ done_document(struct document *document) } } free_list(document->timeouts); +#endif +#ifdef CONFIG_LIBDOM mem_free_if(document->text); free_document(document->dom); @@ -390,7 +393,6 @@ done_document(struct document *document) delete_map(mapa); } #endif - free_list(document->tags); free_list(document->nodes); diff --git a/src/document/document.h b/src/document/document.h index b7994cdb..9361cece 100644 --- a/src/document/document.h +++ b/src/document/document.h @@ -220,11 +220,14 @@ struct document { /** used by setTimeout */ LIST_OF(struct ecmascript_timeout) timeouts; int ecmascript_counter; +#endif +#ifdef CONFIG_LIBDOM void *dom; void *element_map; char *text; void *forms_nodeset; #endif + #ifdef CONFIG_CSS /** @todo FIXME: We should externally maybe using cache_entry store the * dependencies between the various entries so nothing gets removed diff --git a/src/document/libdom/Makefile b/src/document/libdom/Makefile index ad1c02c4..d056a8c8 100644 --- a/src/document/libdom/Makefile +++ b/src/document/libdom/Makefile @@ -1,6 +1,6 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config -OBJS = corestrings.o css.o mapa.obj renderer.o renderer2.o +OBJS = corestrings.o css.o doc.o mapa.obj renderer.o renderer2.o include $(top_srcdir)/Makefile.lib diff --git a/src/document/libdom/doc.c b/src/document/libdom/doc.c new file mode 100644 index 00000000..4db70467 --- /dev/null +++ b/src/document/libdom/doc.c @@ -0,0 +1,91 @@ +/* Parse document. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +#ifdef CONFIG_LIBDOM +#include +#include +#endif + +#include "elinks.h" + +#include "cache/cache.h" +#include "document/document.h" +#include "document/libdom/doc.h" +#include "util/string.h" + +void * +document_parse_text(char *data, size_t length) +{ + dom_hubbub_parser *parser = NULL; + dom_hubbub_error error; + dom_hubbub_parser_params params; + dom_document *doc; + + params.enc = NULL; + params.fix_enc = true; + params.enable_script = false; + params.msg = NULL; + params.script = NULL; + params.ctx = NULL; + params.daf = NULL; + + /* Create Hubbub parser */ + error = dom_hubbub_parser_create(¶ms, &parser, &doc); + if (error != DOM_HUBBUB_OK) { + fprintf(stderr, "Can't create Hubbub Parser\n"); + return NULL; + } + + /* Parse */ + error = dom_hubbub_parser_parse_chunk(parser, (const uint8_t *)data, length); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + + /* Done parsing file */ + error = dom_hubbub_parser_completed(parser); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Parsing error when construct DOM\n"); + return NULL; + } + + /* Finished with parser */ + dom_hubbub_parser_destroy(parser); + + return doc; +} + +void * +document_parse(struct document *document) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct cache_entry *cached = document->cached; + struct fragment *f = get_cache_fragment(cached); + + if (!f || !f->length) { + return NULL; + } + + return document_parse_text(f->data, f->length); +} + +void +free_document(void *doc) +{ + if (!doc) { + return; + } + dom_node *ddd = (dom_node *)doc; + dom_node_unref(ddd); +} diff --git a/src/document/libdom/doc.h b/src/document/libdom/doc.h new file mode 100644 index 00000000..f1347b51 --- /dev/null +++ b/src/document/libdom/doc.h @@ -0,0 +1,19 @@ +#ifndef EL__DOCUMENT_LIBDOM_DOC_H +#define EL__DOCUMENT_LIBDOM_DOC_H + +#ifdef __cplusplus +extern "C" { +#endif + +struct document; +struct string; + +void *document_parse_text(char *data, size_t length); +void *document_parse(struct document *document); +void free_document(void *doc); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/document/libdom/meson.build b/src/document/libdom/meson.build index 77629118..1826702e 100644 --- a/src/document/libdom/meson.build +++ b/src/document/libdom/meson.build @@ -1 +1 @@ -srcs += files('corestrings.c', 'css.c', 'mapa.cpp', 'renderer.c', 'renderer2.c') +srcs += files('corestrings.c', 'css.c', 'doc.c', 'mapa.cpp', 'renderer.c', 'renderer2.c') diff --git a/src/document/libdom/renderer.c b/src/document/libdom/renderer.c index de68fc22..72d5d3d1 100644 --- a/src/document/libdom/renderer.c +++ b/src/document/libdom/renderer.c @@ -13,12 +13,12 @@ #include "cache/cache.h" #include "document/document.h" #include "document/renderer.h" +#include "document/libdom/doc.h" #include "document/libdom/mapa.h" #include "document/libdom/renderer.h" #include "document/plain/renderer.h" #include "ecmascript/libdom/parse.h" - static bool dump_dom_element_closing(struct string *buf, dom_node *node) { diff --git a/src/document/libdom/renderer2.c b/src/document/libdom/renderer2.c index 2bda5133..55c2dcdc 100644 --- a/src/document/libdom/renderer2.c +++ b/src/document/libdom/renderer2.c @@ -15,6 +15,7 @@ #include "document/renderer.h" #include "document/html/renderer.h" #include "document/libdom/corestrings.h" +#include "document/libdom/doc.h" #include "document/libdom/mapa.h" #include "document/libdom/renderer2.h" #include "ecmascript/libdom/parse.h" diff --git a/src/document/renderer.cpp b/src/document/renderer.cpp index 58536ea5..cced837c 100644 --- a/src/document/renderer.cpp +++ b/src/document/renderer.cpp @@ -293,7 +293,7 @@ render_encoded_document(struct cache_entry *cached, struct document *document) && (!c_strlcasecmp("text/gemini", 11, cached->content_type, -1))) render_gemini_document(cached, document, &buffer); else -#if defined(CONFIG_LIBDOM) && defined(CONFIG_ECMASCRIPT) +#ifdef CONFIG_LIBDOM if (1) { if (encoding != ENCODING_NONE) { done_string(&buffer); diff --git a/src/ecmascript/ecmascript.h b/src/ecmascript/ecmascript.h index f3def198..34ee199b 100644 --- a/src/ecmascript/ecmascript.h +++ b/src/ecmascript/ecmascript.h @@ -196,7 +196,6 @@ void check_for_rerender(struct ecmascript_interpreter *interpreter, const char* void toggle_ecmascript(struct session *ses); -void free_document(void *doc); void location_goto(struct document_view *doc_view, char *url); void location_goto_const(struct document_view *doc_view, const char *url); diff --git a/src/ecmascript/libdom/mujs/document.c b/src/ecmascript/libdom/mujs/document.c index 0af00381..5c52a98c 100644 --- a/src/ecmascript/libdom/mujs/document.c +++ b/src/ecmascript/libdom/mujs/document.c @@ -25,6 +25,7 @@ #include "document/html/frames.h" #include "document/document.h" #include "document/forms.h" +#include "document/libdom/doc.h" #include "document/view.h" #include "ecmascript/ecmascript.h" #include "ecmascript/libdom/mujs/mapa.h" diff --git a/src/ecmascript/libdom/mujs/implementation.c b/src/ecmascript/libdom/mujs/implementation.c index 7ef58891..6671ac2e 100644 --- a/src/ecmascript/libdom/mujs/implementation.c +++ b/src/ecmascript/libdom/mujs/implementation.c @@ -10,6 +10,7 @@ #include "elinks.h" +#include "document/libdom/doc.h" #include "ecmascript/ecmascript.h" #include "ecmascript/libdom/parse.h" #include "ecmascript/mujs.h" diff --git a/src/ecmascript/libdom/parse.c b/src/ecmascript/libdom/parse.c index 38e79772..8662579d 100644 --- a/src/ecmascript/libdom/parse.c +++ b/src/ecmascript/libdom/parse.c @@ -18,77 +18,6 @@ #include "document/document.h" #include "util/string.h" -void * -document_parse_text(char *data, size_t length) -{ - dom_hubbub_parser *parser = NULL; - dom_hubbub_error error; - dom_hubbub_parser_params params; - dom_document *doc; - - params.enc = NULL; - params.fix_enc = true; - params.enable_script = false; - params.msg = NULL; - params.script = NULL; - params.ctx = NULL; - params.daf = NULL; - - /* Create Hubbub parser */ - error = dom_hubbub_parser_create(¶ms, &parser, &doc); - if (error != DOM_HUBBUB_OK) { - fprintf(stderr, "Can't create Hubbub Parser\n"); - return NULL; - } - - /* Parse */ - error = dom_hubbub_parser_parse_chunk(parser, (const uint8_t *)data, length); - if (error != DOM_HUBBUB_OK) { - dom_hubbub_parser_destroy(parser); - fprintf(stderr, "Parsing errors occur\n"); - return NULL; - } - - /* Done parsing file */ - error = dom_hubbub_parser_completed(parser); - if (error != DOM_HUBBUB_OK) { - dom_hubbub_parser_destroy(parser); - fprintf(stderr, "Parsing error when construct DOM\n"); - return NULL; - } - - /* Finished with parser */ - dom_hubbub_parser_destroy(parser); - - return doc; -} - -void * -document_parse(struct document *document) -{ -#ifdef ECMASCRIPT_DEBUG - fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); -#endif - struct cache_entry *cached = document->cached; - struct fragment *f = get_cache_fragment(cached); - - if (!f || !f->length) { - return NULL; - } - - return document_parse_text(f->data, f->length); -} - -void -free_document(void *doc) -{ - if (!doc) { - return; - } - dom_node *ddd = (dom_node *)doc; - dom_node_unref(ddd); -} - void el_insert_before(struct document *document, void *element, struct string *source) { diff --git a/src/ecmascript/libdom/parse.h b/src/ecmascript/libdom/parse.h index 52287d98..8af013fd 100644 --- a/src/ecmascript/libdom/parse.h +++ b/src/ecmascript/libdom/parse.h @@ -8,8 +8,6 @@ extern "C" { struct document; struct string; -void *document_parse_text(char *data, size_t length); -void *document_parse(struct document *document); void el_insert_before(struct document *document, void *element, struct string *source); #ifdef __cplusplus diff --git a/src/ecmascript/libdom/quickjs/document.c b/src/ecmascript/libdom/quickjs/document.c index 2fa9943e..8e896b72 100644 --- a/src/ecmascript/libdom/quickjs/document.c +++ b/src/ecmascript/libdom/quickjs/document.c @@ -18,6 +18,7 @@ #include "cookies/cookies.h" #include "dialogs/status.h" #include "document/document.h" +#include "document/libdom/doc.h" #include "document/view.h" #include "ecmascript/ecmascript.h" #include "ecmascript/libdom/parse.h" diff --git a/src/ecmascript/libdom/quickjs/implementation.c b/src/ecmascript/libdom/quickjs/implementation.c index 6990d113..2d4be2ce 100644 --- a/src/ecmascript/libdom/quickjs/implementation.c +++ b/src/ecmascript/libdom/quickjs/implementation.c @@ -15,6 +15,7 @@ #include "elinks.h" +#include "document/libdom/doc.h" #include "ecmascript/ecmascript.h" #include "ecmascript/libdom/parse.h" #include "ecmascript/quickjs.h" diff --git a/src/ecmascript/libdom/spidermonkey/document.cpp b/src/ecmascript/libdom/spidermonkey/document.cpp index 12de7eb2..e67367b1 100644 --- a/src/ecmascript/libdom/spidermonkey/document.cpp +++ b/src/ecmascript/libdom/spidermonkey/document.cpp @@ -24,6 +24,7 @@ #include "document/html/frames.h" #include "document/document.h" #include "document/forms.h" +#include "document/libdom/doc.h" #include "document/view.h" #include "ecmascript/css2xpath.h" #include "ecmascript/ecmascript.h" diff --git a/src/ecmascript/libdom/spidermonkey/implementation.cpp b/src/ecmascript/libdom/spidermonkey/implementation.cpp index 30092a25..522efe45 100644 --- a/src/ecmascript/libdom/spidermonkey/implementation.cpp +++ b/src/ecmascript/libdom/spidermonkey/implementation.cpp @@ -10,6 +10,7 @@ #include "elinks.h" +#include "document/libdom/doc.h" #include "ecmascript/ecmascript.h" #include "ecmascript/libdom/parse.h" #include "ecmascript/spidermonkey/document.h"