mirror of
https://github.com/rkd77/elinks.git
synced 2025-02-02 15:09:23 -05:00
[querySelector] implementation of querySelector
This commit is contained in:
parent
af059861ba
commit
1618038dc3
src/ecmascript/spidermonkey
test/ecmascript
@ -2,7 +2,7 @@ top_builddir=../../..
|
|||||||
include $(top_builddir)/Makefile.config
|
include $(top_builddir)/Makefile.config
|
||||||
INCLUDES += $(SPIDERMONKEY_CFLAGS)
|
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
|
localstorage.o localstorage-db.o navigator.o screen.o unibar.o window.o
|
||||||
|
|
||||||
include $(top_srcdir)/Makefile.lib
|
include $(top_srcdir)/Makefile.lib
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
// This code is based on github.com/theseer/css2xpath and other sources
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -13,6 +15,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
|
#include "ecmascript/spidermonkey/css2xpath.h"
|
||||||
|
|
||||||
namespace std
|
namespace std
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -167,8 +171,6 @@ class RegexRule : public Rule
|
|||||||
{
|
{
|
||||||
std::string r(pattern);
|
std::string r(pattern);
|
||||||
|
|
||||||
// std::cout << "RegexRule: pattern=" << r << " replacement=" << replacement << " selector=" << selector << "\n";
|
|
||||||
|
|
||||||
return preg_replace(r, replacement, selector);
|
return preg_replace(r, replacement, selector);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -278,8 +280,6 @@ class DollarEqualRule : public Rule
|
|||||||
|
|
||||||
std::string apply(std::string &selector)
|
std::string apply(std::string &selector)
|
||||||
{
|
{
|
||||||
// std::cout << "DollarEqualRule: selector=" << selector << "\n";
|
|
||||||
|
|
||||||
std::string pattern("\\[([a-zA-Z0-9\\_\\-]+)\\$=([^\\]]+)\\]");
|
std::string pattern("\\[([a-zA-Z0-9\\_\\-]+)\\$=([^\\]]+)\\]");
|
||||||
|
|
||||||
return preg_replace_callback(pattern, dollar_equal_rule_callback, selector);
|
return preg_replace_callback(pattern, dollar_equal_rule_callback, selector);
|
||||||
@ -395,8 +395,6 @@ NotRule::NotRule(Translator *tt) : t(tt)
|
|||||||
std::string
|
std::string
|
||||||
NotRule::apply(std::string &selector)
|
NotRule::apply(std::string &selector)
|
||||||
{
|
{
|
||||||
// std::cout << "NotRule: selector=" << selector << "\n";
|
|
||||||
|
|
||||||
std::string pat("([a-zA-Z0-9\\_\\-\\*]+):not\\(([^\\)]*)\\)");
|
std::string pat("([a-zA-Z0-9\\_\\-\\*]+):not\\(([^\\)]*)\\)");
|
||||||
return preg_replace_callback(pat, not_rule_callback, selector);
|
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 + ")]";
|
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
|
std::string
|
||||||
next_year(const std::smatch& matches)
|
next_year(const std::smatch& matches)
|
||||||
@ -491,11 +499,6 @@ tests()
|
|||||||
std::string result = translator->translate(selector);
|
std::string result = translator->translate(selector);
|
||||||
std::cout << t[0] << " ";
|
std::cout << t[0] << " ";
|
||||||
std::cout << ((result == expected) ? "\033[32mOK\033[0m" : "\033[31mFAIL\033[0m");
|
std::cout << ((result == expected) ? "\033[32mOK\033[0m" : "\033[31mFAIL\033[0m");
|
||||||
|
|
||||||
// if (result != expected)
|
|
||||||
// {
|
|
||||||
// std::cout << " " << result << " " << expected;
|
|
||||||
// }
|
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
src/ecmascript/spidermonkey/css2xpath.h
Normal file
8
src/ecmascript/spidermonkey/css2xpath.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef EL__ECMASCRIPT_SPIDERMONKEY_CSS2XPATH_H
|
||||||
|
#define EL__ECMASCRIPT_SPIDERMONKEY_CSS2XPATH_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string css2xpath(std::string &selector);
|
||||||
|
|
||||||
|
#endif
|
@ -24,6 +24,7 @@
|
|||||||
#include "document/forms.h"
|
#include "document/forms.h"
|
||||||
#include "document/view.h"
|
#include "document/view.h"
|
||||||
#include "ecmascript/ecmascript.h"
|
#include "ecmascript/ecmascript.h"
|
||||||
|
#include "ecmascript/spidermonkey/css2xpath.h"
|
||||||
#include "ecmascript/spidermonkey/form.h"
|
#include "ecmascript/spidermonkey/form.h"
|
||||||
#include "ecmascript/spidermonkey/implementation.h"
|
#include "ecmascript/spidermonkey/implementation.h"
|
||||||
#include "ecmascript/spidermonkey/location.h"
|
#include "ecmascript/spidermonkey/location.h"
|
||||||
@ -57,12 +58,6 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
std::string css2xpath(std::string css)
|
|
||||||
{
|
|
||||||
/* TODO */
|
|
||||||
return css;
|
|
||||||
}
|
|
||||||
|
|
||||||
static xmlpp::Document emptyDoc;
|
static xmlpp::Document emptyDoc;
|
||||||
|
|
||||||
static JSObject *getDoctype(JSContext *ctx, void *node);
|
static JSObject *getDoctype(JSContext *ctx, void *node);
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
#INCLUDES += $(SPIDERMONKEY_CFLAGS)
|
#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')
|
||||||
|
19
test/ecmascript/querySelector.html
Normal file
19
test/ecmascript/querySelector.html
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h2 class="example">A heading with class="example"</h2>
|
||||||
|
<p class="example">A paragraph with class="example".</p>
|
||||||
|
|
||||||
|
<p>Click the button to add a background color to the first element in the document with class="example".</p>
|
||||||
|
|
||||||
|
<button onclick="myFunction()">Try it</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function myFunction() {
|
||||||
|
alert(document.querySelector(".example").innerHTML);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user