From 1618038dc35f1e6ccea9af7a6394b811478fb8ae Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 4 Oct 2021 15:28:26 +0200 Subject: [PATCH] [querySelector] implementation of querySelector --- src/ecmascript/spidermonkey/Makefile | 2 +- src/ecmascript/spidermonkey/css2xpath.c | 27 ++++++++++++++----------- src/ecmascript/spidermonkey/css2xpath.h | 8 ++++++++ src/ecmascript/spidermonkey/document.c | 7 +------ src/ecmascript/spidermonkey/meson.build | 2 +- test/ecmascript/querySelector.html | 19 +++++++++++++++++ 6 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 src/ecmascript/spidermonkey/css2xpath.h create mode 100644 test/ecmascript/querySelector.html diff --git a/src/ecmascript/spidermonkey/Makefile b/src/ecmascript/spidermonkey/Makefile index a003f6b6..4247b68c 100644 --- a/src/ecmascript/spidermonkey/Makefile +++ b/src/ecmascript/spidermonkey/Makefile @@ -2,7 +2,7 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config INCLUDES += $(SPIDERMONKEY_CFLAGS) -OBJS = console.o document.o element.o form.o heartbeat.o implementation.o location.o \ +OBJS = console.o css2xpath.o document.o element.o form.o heartbeat.o implementation.o location.o \ localstorage.o localstorage-db.o navigator.o screen.o unibar.o window.o include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/spidermonkey/css2xpath.c b/src/ecmascript/spidermonkey/css2xpath.c index 96e61ccc..8916b06c 100644 --- a/src/ecmascript/spidermonkey/css2xpath.c +++ b/src/ecmascript/spidermonkey/css2xpath.c @@ -1,3 +1,5 @@ +// This code is based on github.com/theseer/css2xpath and other sources + #include #include #include @@ -13,6 +15,8 @@ #include #include +#include "ecmascript/spidermonkey/css2xpath.h" + namespace std { @@ -167,8 +171,6 @@ class RegexRule : public Rule { std::string r(pattern); -// std::cout << "RegexRule: pattern=" << r << " replacement=" << replacement << " selector=" << selector << "\n"; - return preg_replace(r, replacement, selector); } }; @@ -278,8 +280,6 @@ class DollarEqualRule : public Rule std::string apply(std::string &selector) { -// std::cout << "DollarEqualRule: selector=" << selector << "\n"; - std::string pattern("\\[([a-zA-Z0-9\\_\\-]+)\\$=([^\\]]+)\\]"); return preg_replace_callback(pattern, dollar_equal_rule_callback, selector); @@ -395,8 +395,6 @@ NotRule::NotRule(Translator *tt) : t(tt) std::string NotRule::apply(std::string &selector) { -// std::cout << "NotRule: selector=" << selector << "\n"; - std::string pat("([a-zA-Z0-9\\_\\-\\*]+):not\\(([^\\)]*)\\)"); return preg_replace_callback(pat, not_rule_callback, selector); } @@ -411,9 +409,19 @@ NotRule::callback(const std::smatch &matches) return matches[1].str() + "[not(" + subresult + ")]"; } +std::string +css2xpath(std::string &selector) +{ + static Translator *translator; + if (!translator) + { + translator = new Translator(); + } + return translator->translate(selector); +} -#if 1 +#if 0 std::string next_year(const std::smatch& matches) @@ -491,11 +499,6 @@ tests() std::string result = translator->translate(selector); std::cout << t[0] << " "; std::cout << ((result == expected) ? "\033[32mOK\033[0m" : "\033[31mFAIL\033[0m"); - -// if (result != expected) -// { -// std::cout << " " << result << " " << expected; -// } std::cout << "\n"; } } diff --git a/src/ecmascript/spidermonkey/css2xpath.h b/src/ecmascript/spidermonkey/css2xpath.h new file mode 100644 index 00000000..bcaaa724 --- /dev/null +++ b/src/ecmascript/spidermonkey/css2xpath.h @@ -0,0 +1,8 @@ +#ifndef EL__ECMASCRIPT_SPIDERMONKEY_CSS2XPATH_H +#define EL__ECMASCRIPT_SPIDERMONKEY_CSS2XPATH_H + +#include + +std::string css2xpath(std::string &selector); + +#endif diff --git a/src/ecmascript/spidermonkey/document.c b/src/ecmascript/spidermonkey/document.c index d940a24c..96100ae4 100644 --- a/src/ecmascript/spidermonkey/document.c +++ b/src/ecmascript/spidermonkey/document.c @@ -24,6 +24,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/spidermonkey/css2xpath.h" #include "ecmascript/spidermonkey/form.h" #include "ecmascript/spidermonkey/implementation.h" #include "ecmascript/spidermonkey/location.h" @@ -57,12 +58,6 @@ #include -std::string css2xpath(std::string css) -{ - /* TODO */ - return css; -} - static xmlpp::Document emptyDoc; static JSObject *getDoctype(JSContext *ctx, void *node); diff --git a/src/ecmascript/spidermonkey/meson.build b/src/ecmascript/spidermonkey/meson.build index 151130f6..1b358608 100644 --- a/src/ecmascript/spidermonkey/meson.build +++ b/src/ecmascript/spidermonkey/meson.build @@ -1,3 +1,3 @@ #INCLUDES += $(SPIDERMONKEY_CFLAGS) -srcs += files('console.c', 'document.c', 'element.c', 'form.c', 'heartbeat.c', 'implementation.c', 'location.c', 'localstorage.c', 'localstorage-db.c', 'navigator.c', 'screen.c', 'unibar.c', 'window.c') +srcs += files('console.c', 'css2xpath.c', 'document.c', 'element.c', 'form.c', 'heartbeat.c', 'implementation.c', 'location.c', 'localstorage.c', 'localstorage-db.c', 'navigator.c', 'screen.c', 'unibar.c', 'window.c') diff --git a/test/ecmascript/querySelector.html b/test/ecmascript/querySelector.html new file mode 100644 index 00000000..890dc7cd --- /dev/null +++ b/test/ecmascript/querySelector.html @@ -0,0 +1,19 @@ + + + + +

A heading with class="example"

+

A paragraph with class="example".

+ +

Click the button to add a background color to the first element in the document with class="example".

+ + + + + + +