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

Merge with git+ssh://pasky/srv/git/elinks.git

This commit is contained in:
Jonas Fonseca 2006-01-28 03:36:47 +01:00 committed by Jonas Fonseca
commit 3b183c1685
19 changed files with 255 additions and 169 deletions

View File

@ -16,6 +16,7 @@ struct frame_desc;
struct frameset_desc;
struct module;
struct screen_char;
struct view_state;
/* Nodes are used for marking areas of text on the document canvas as
* searchable. */
@ -147,6 +148,8 @@ struct document {
* dependencies between the various entries so nothing gets removed
* unneeded. */
struct uri_list ecmascript_imports;
/* For ecmascript access */
struct view_state *vs;
#endif
#ifdef CONFIG_CSS
/* FIXME: We should externally maybe using cache_entry store the

View File

@ -1024,7 +1024,7 @@ render_dom_document(struct cache_entry *cached, struct document *document,
} else if (renderer.doctype == SGML_DOCTYPE_RSS) {
add_dom_stack_context(&parser->stack, &renderer,
&dom_rss_renderer_context_info);
add_dom_config_normalizer(&parser->stack, RSS_CONFIG_FLAGS);
add_dom_config_normalizer(&parser->stack, RSS_CONFIG_FLAGS);
}
/* FIXME: When rendering this way we don't really care about the code.

View File

@ -17,6 +17,7 @@
#include "config/options.h"
#include "document/css/apply.h"
#include "document/document.h"
#include "document/html/frames.h"
#include "document/html/parser/general.h"
#include "document/html/parser/link.h"
@ -26,6 +27,7 @@
#include "document/html/renderer.h"
#include "document/html/tables.h"
#include "document/options.h"
#include "ecmascript/ecmascript.h"
#include "intl/charsets.h"
#include "protocol/uri.h"
#include "terminal/draw.h"
@ -37,6 +39,7 @@
#include "util/memdebug.h"
#include "util/memory.h"
#include "util/string.h"
#include "viewer/text/vs.h"
/* Unsafe macros */
#include "document/html/internal.h"
@ -343,8 +346,39 @@ imported:
}
if (html_context->part->document && *html != '^') {
struct string code, ret;
struct part *part = html_context->part;
struct document *document = part->document;
struct view_state *vs = document->vs;
struct ecmascript_interpreter *interpreter = vs->ecmascript;
if (!interpreter) {
ecmascript_reset_state(vs);
interpreter = vs->ecmascript;
}
if (!interpreter) return;
if (!init_string(&code) || !init_string(&ret))
return;
add_bytes_to_string(&code, html, *end - html);
ecmascript_eval(interpreter, &code, &ret);
done_string(&code);
if (!ret.length) {
done_string(&ret);
return;
}
/* FIXME: it doesn't work */
html_top->invisible = 0;
parse_html(ret.source, ret.source + ret.length, part, NULL,
html_context);
done_string(&ret);
#if 0
add_to_string_list(&html_context->part->document->onload_snippets,
html, *end - html);
#endif
}
#endif
}

View File

@ -129,7 +129,7 @@ process_snippets(struct ecmascript_interpreter *interpreter,
if (*string->source != '^') {
/* Evaluate <script>code</script> snippet */
ecmascript_eval(interpreter, string);
ecmascript_eval(interpreter, string, NULL);
continue;
}
@ -194,7 +194,7 @@ process_snippets(struct ecmascript_interpreter *interpreter,
if (fragment) {
struct string code = INIT_STRING(fragment->data, fragment->length);
ecmascript_eval(interpreter, &code);
ecmascript_eval(interpreter, &code, NULL);
}
}
}
@ -284,7 +284,6 @@ render_document(struct view_state *vs, struct document_view *doc_view,
options->gradual_rerendering, struri(vs->uri),
doc_view, doc_view->name, vs);
#endif
name = doc_view->name;
doc_view->name = NULL;
@ -328,13 +327,18 @@ render_document(struct view_state *vs, struct document_view *doc_view,
document = get_cached_document(cached, options);
if (document) {
doc_view->document = document;
#ifdef CONFIG_ECMASCRIPT
document->vs = vs;
#endif
} else {
document = init_document(cached, options);
if (!document) return;
doc_view->document = document;
shrink_memory(0);
#ifdef CONFIG_ECMASCRIPT
document->vs = vs;
#endif
render_encoded_document(cached, document);
sort_links(document);
if (!document->title) {

View File

@ -7,7 +7,7 @@ struct dom_stack;
/* API Doc :: dom-config */
/** DOM Configuration
*
*
* The DOMConfiguration interface represents the configuration of a document.
* Using the configuration, it is possible to change the behaviour of how
* document normalization is done, such as replacing the CDATASection nodes
@ -33,7 +33,7 @@ enum dom_config_flag {
DOM_CONFIG_COMMENTS = 2,
/** "element-content-whitespace"
*
*
* The default is true and will keep all whitespaces in the document.
* When false, discard all Text nodes that contain only whitespaces. */
DOM_CONFIG_ELEMENT_CONTENT_WHITESPACE = 4,

View File

@ -12,6 +12,8 @@
#include "document/document.h"
#include "document/view.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/see.h"
#include "ecmascript/spidermonkey.h"
#include "intl/gettext/libintl.h"
#include "main/module.h"
#include "protocol/uri.h"
@ -24,12 +26,6 @@
#include "viewer/text/form.h" /* <-ecmascript_reset_state() */
#include "viewer/text/vs.h"
#ifdef CONFIG_ECMASCRIPT_SEE
#include "ecmascript/see.h"
#elif defined(CONFIG_ECMASCRIPT_SMJS)
#include "ecmascript/spidermonkey.h"
#endif
/* TODO: We should have some kind of ACL for the scripts - i.e. ability to
* disallow the scripts to open new windows (or so that the windows are always
@ -66,6 +62,104 @@ static struct option_info ecmascript_options[] = {
NULL_OPTION_INFO,
};
void
ecmascript_init(struct module *module)
{
#ifdef CONFIG_ECMASCRIPT_SEE
see_init();
#else
spidermonkey_init();
#endif
}
void
ecmascript_done(struct module *module)
{
#ifdef CONFIG_ECMASCRIPT_SEE
see_done();
#else
spidermonkey_done();
#endif
}
struct ecmascript_interpreter *
ecmascript_get_interpreter(struct view_state *vs)
{
struct ecmascript_interpreter *interpreter;
assert(vs);
interpreter = mem_calloc(1, sizeof(*interpreter));
if (!interpreter)
return NULL;
interpreter->vs = vs;
init_list(interpreter->onload_snippets);
#ifdef CONFIG_ECMASCRIPT_SEE
see_get_interpreter(interpreter);
#else
spidermonkey_get_interpreter(interpreter);
#endif
return interpreter;
}
void
ecmascript_put_interpreter(struct ecmascript_interpreter *interpreter)
{
assert(interpreter);
#ifdef CONFIG_ECMASCRIPT_SEE
see_put_interpreter(interpreter);
#else
spidermonkey_put_interpreter(interpreter);
#endif
free_string_list(&interpreter->onload_snippets);
interpreter->vs->ecmascript = NULL;
mem_free(interpreter);
}
void
ecmascript_eval(struct ecmascript_interpreter *interpreter,
struct string *code, struct string *ret)
{
if (!get_ecmascript_enable())
return;
assert(interpreter);
#ifdef CONFIG_ECMASCRIPT_SEE
see_eval(interpreter, code, ret);
#else
spidermonkey_eval(interpreter, code, ret);
#endif
}
unsigned char *
ecmascript_eval_stringback(struct ecmascript_interpreter *interpreter,
struct string *code)
{
if (!get_ecmascript_enable())
return NULL;
assert(interpreter);
#ifdef CONFIG_ECMASCRIPT_SEE
return see_eval_stringback(interpreter, code);
#else
return spidermonkey_eval_stringback(interpreter, code);
#endif
}
int
ecmascript_eval_boolback(struct ecmascript_interpreter *interpreter,
struct string *code)
{
if (!get_ecmascript_enable())
return -1;
assert(interpreter);
#ifdef CONFIG_ECMASCRIPT_SEE
return see_eval_boolback(interpreter, code);
#else
return spidermonkey_eval_boolback(interpreter, code);
#endif
}
void
ecmascript_reset_state(struct view_state *vs)
{

View File

@ -18,6 +18,8 @@ struct view_state;
struct ecmascript_interpreter {
struct view_state *vs;
void *backend_data;
/* Used by document.write() */
struct string *ret;
time_t exec_start;
/* This is a cross-rerenderings accumulator of
@ -57,7 +59,7 @@ void ecmascript_put_interpreter(struct ecmascript_interpreter *interpreter);
void ecmascript_reset_state(struct view_state *vs);
void ecmascript_eval(struct ecmascript_interpreter *interpreter, struct string *code);
void ecmascript_eval(struct ecmascript_interpreter *interpreter, struct string *code, struct string *ret);
unsigned char *ecmascript_eval_stringback(struct ecmascript_interpreter *interpreter, struct string *code);
/* Returns -1 if undefined. */
int ecmascript_eval_boolback(struct ecmascript_interpreter *interpreter, struct string *code);

View File

@ -56,79 +56,6 @@
/* TODO? Are there any which need to be implemented? */
/*** The ELinks interface */
void
ecmascript_init(struct module *module)
{
see_init();
}
void
ecmascript_done(struct module *module)
{
see_done();
}
struct ecmascript_interpreter *
ecmascript_get_interpreter(struct view_state *vs)
{
struct ecmascript_interpreter *interpreter;
assert(vs);
interpreter = mem_calloc(1, sizeof(*interpreter));
if (!interpreter)
return NULL;
interpreter->vs = vs;
init_list(interpreter->onload_snippets);
see_get_interpreter(interpreter);
return interpreter;
}
void
ecmascript_put_interpreter(struct ecmascript_interpreter *interpreter)
{
assert(interpreter);
see_put_interpreter(interpreter);
free_string_list(&interpreter->onload_snippets);
mem_free(interpreter);
}
void
ecmascript_eval(struct ecmascript_interpreter *interpreter,
struct string *code)
{
if (!get_ecmascript_enable())
return;
assert(interpreter);
see_eval(interpreter, code);
}
unsigned char *
ecmascript_eval_stringback(struct ecmascript_interpreter *interpreter,
struct string *code)
{
if (!get_ecmascript_enable())
return NULL;
assert(interpreter);
return see_eval_stringback(interpreter, code);
}
int
ecmascript_eval_boolback(struct ecmascript_interpreter *interpreter,
struct string *code)
{
if (!get_ecmascript_enable())
return -1;
assert(interpreter);
return see_eval_boolback(interpreter, code);
}
void
see_init(void)
{
@ -169,7 +96,7 @@ see_put_interpreter(struct ecmascript_interpreter *interpreter)
void
see_eval(struct ecmascript_interpreter *interpreter,
struct string *code)
struct string *code, struct string *ret)
{
struct SEE_interpreter *interp = interpreter->backend_data;
@ -179,6 +106,7 @@ see_eval(struct ecmascript_interpreter *interpreter,
struct SEE_value result;
g->exec_start = time(NULL);
g->ret = ret;
SEE_TRY(interp, try_ctxt) {
SEE_Global_eval(interp, input, &result);
}
@ -203,6 +131,7 @@ see_eval_stringback(struct ecmascript_interpreter *interpreter,
unsigned char *volatile string = NULL;
g->exec_start = time(NULL);
g->ret = NULL;
SEE_TRY(interp, try_ctxt) {
SEE_Global_eval(interp, input, &result);
if (SEE_VALUE_GET_TYPE(&result) != SEE_NULL)
@ -231,6 +160,7 @@ see_eval_boolback(struct ecmascript_interpreter *interpreter,
SEE_int32_t volatile res = 0;
g->exec_start = time(NULL);
g->ret = NULL;
SEE_TRY(interp, try_ctxt) {
SEE_Global_eval(interp, input, &result);
/* history.back() returns SEE_NULL */

View File

@ -10,7 +10,7 @@ void see_done();
void *see_get_interpreter(struct ecmascript_interpreter *interpreter);
void see_put_interpreter(struct ecmascript_interpreter *interpreter);
void see_eval(struct ecmascript_interpreter *interpreter, struct string *code);
void see_eval(struct ecmascript_interpreter *interpreter, struct string *code, struct string *ret);
unsigned char *see_eval_stringback(struct ecmascript_interpreter *interpreter, struct string *code);
int see_eval_boolback(struct ecmascript_interpreter *interpreter, struct string *code);

View File

@ -51,6 +51,7 @@ static void document_put(struct SEE_interpreter *, struct SEE_object *, struct S
static int document_canput(struct SEE_interpreter *, struct SEE_object *, struct SEE_string *);
static int document_hasproperty(struct SEE_interpreter *, struct SEE_object *, struct SEE_string *);
static void js_document_write(struct SEE_interpreter *, struct SEE_object *, struct SEE_object *, int, struct SEE_value **, struct SEE_value *);
static void js_document_writeln(struct SEE_interpreter *, struct SEE_object *, struct SEE_object *, int, struct SEE_value **, struct SEE_value *);
void location_goto(struct document_view *, unsigned char *);
@ -140,6 +141,8 @@ document_get(struct SEE_interpreter *interp, struct SEE_object *o,
SEE_SET_OBJECT(res, doc->forms);
} else if (p == s_write) {
SEE_SET_OBJECT(res, doc->write);
} else if (p == s_writeln) {
SEE_SET_OBJECT(res, doc->writeln);
} else {
struct form *form;
unsigned char *string = SEE_string_to_unsigned_char(p);
@ -204,10 +207,51 @@ js_document_write(struct SEE_interpreter *interp, struct SEE_object *self,
struct SEE_object *thisobj, int argc, struct SEE_value **argv,
struct SEE_value *res)
{
#ifdef CONFIG_LEDS
struct global_object *g = (struct global_object *)interp;
struct view_state *vs = g->win->vs;
struct string *ret = g->ret;
if (argc >= 1 && ret) {
unsigned char *code = SEE_value_to_unsigned_char(interp, argv[0]);
if (code) {
add_to_string(ret, code);
mem_free(code);
}
}
#ifdef CONFIG_LEDS
/* XXX: I don't know about you, but I have *ENOUGH* of those 'Undefined
* function' errors, I want to see just the useful ones. So just
* lighting a led and going away, no muss, no fuss. --pasky */
/* TODO: Perhaps we can introduce ecmascript.error_report_unsupported
* -> "Show information about the document using some valid,
* nevertheless unsupported methods/properties." --pasky too */
set_led_value(vs->doc_view->session->status.ecmascript_led, 'J');
#endif
checktime(interp);
SEE_SET_BOOLEAN(res, 0);
}
static void
js_document_writeln(struct SEE_interpreter *interp, struct SEE_object *self,
struct SEE_object *thisobj, int argc, struct SEE_value **argv,
struct SEE_value *res)
{
struct global_object *g = (struct global_object *)interp;
struct view_state *vs = g->win->vs;
struct string *ret = g->ret;
if (argc >= 1 && ret) {
unsigned char *code = SEE_value_to_unsigned_char(interp, argv[0]);
if (code) {
add_to_string(ret, code);
mem_free(code);
add_char_to_string(ret, '\n');
}
}
#ifdef CONFIG_LEDS
/* XXX: I don't know about you, but I have *ENOUGH* of those 'Undefined
* function' errors, I want to see just the useful ones. So just
* lighting a led and going away, no muss, no fuss. --pasky */
@ -258,4 +302,5 @@ init_js_document_object(struct ecmascript_interpreter *interpreter)
SEE_OBJECT_PUT(interp, interp->Global, s_document, &v, 0);
doc->write = SEE_cfunction_make(interp, js_document_write, s_write, 1);
doc->writeln = SEE_cfunction_make(interp, js_document_writeln, s_writeln, 1);
}

View File

@ -6,6 +6,7 @@ struct ecmascript_interpreter;
struct js_document_object {
struct SEE_object object;
struct SEE_object *write;
struct SEE_object *writeln;
struct SEE_object *forms;
};

View File

@ -71,6 +71,7 @@ struct SEE_string *s_referrer;
struct SEE_string *s_title;
struct SEE_string *s_url;
struct SEE_string *s_write;
struct SEE_string *s_writeln;
struct SEE_string *s_Mozilla;
struct SEE_string *s_ELinks_;
@ -228,6 +229,8 @@ init_intern_strings(void)
static struct SEE_string S_url = SEE_STRING_DECL(SA_url);
static SEE_char_t SA_write[] = {'w','r','i','t','e'};
static struct SEE_string S_write = SEE_STRING_DECL(SA_write);
static SEE_char_t SA_writeln[] = {'w','r','i','t','e','l','n'};
static struct SEE_string S_writeln = SEE_STRING_DECL(SA_writeln);
static SEE_char_t SA_Mozilla[] = {'M','o','z','i','l','l','a'};
static struct SEE_string S_Mozilla = SEE_STRING_DECL(SA_Mozilla);
@ -354,6 +357,7 @@ init_intern_strings(void)
SEE_intern_global(s_title = &S_title);
SEE_intern_global(s_url = &S_url);
SEE_intern_global(s_write = &S_write);
SEE_intern_global(s_writeln = &S_writeln);
SEE_intern_global(s_Mozilla = &S_Mozilla);
SEE_intern_global(s_ELinks_ = &S_ELinks_);

View File

@ -77,6 +77,7 @@ extern struct SEE_string *s_referrer;
extern struct SEE_string *s_title;
extern struct SEE_string *s_url;
extern struct SEE_string *s_write;
extern struct SEE_string *s_writeln;
extern struct SEE_string *s_Mozilla;
extern struct SEE_string *s_ELinks_;

View File

@ -17,6 +17,7 @@ struct js_window_object {
struct global_object {
struct SEE_interpreter interp;
struct js_window_object *win;
struct string *ret;
int exec_start;
int max_exec_time;
};

View File

@ -55,82 +55,8 @@
/* TODO? Are there any which need to be implemented? */
/*** The ELinks interface */
static JSRuntime *jsrt;
void
ecmascript_init(struct module *module)
{
spidermonkey_init();
}
void
ecmascript_done(struct module *module)
{
spidermonkey_done();
}
struct ecmascript_interpreter *
ecmascript_get_interpreter(struct view_state *vs)
{
struct ecmascript_interpreter *interpreter;
assert(vs);
interpreter = mem_calloc(1, sizeof(*interpreter));
if (!interpreter)
return NULL;
interpreter->vs = vs;
init_list(interpreter->onload_snippets);
spidermonkey_get_interpreter(interpreter);
return interpreter;
}
void
ecmascript_put_interpreter(struct ecmascript_interpreter *interpreter)
{
assert(interpreter);
spidermonkey_put_interpreter(interpreter);
free_string_list(&interpreter->onload_snippets);
mem_free(interpreter);
}
void
ecmascript_eval(struct ecmascript_interpreter *interpreter,
struct string *code)
{
if (!get_ecmascript_enable())
return;
assert(interpreter);
spidermonkey_eval(interpreter, code);
}
unsigned char *
ecmascript_eval_stringback(struct ecmascript_interpreter *interpreter,
struct string *code)
{
if (!get_ecmascript_enable())
return NULL;
assert(interpreter);
return spidermonkey_eval_stringback(interpreter, code);
}
int
ecmascript_eval_boolback(struct ecmascript_interpreter *interpreter,
struct string *code)
{
if (!get_ecmascript_enable())
return -1;
assert(interpreter);
return spidermonkey_eval_boolback(interpreter, code);
}
static void
error_reporter(JSContext *ctx, const char *message, JSErrorReport *report)
{
@ -313,7 +239,7 @@ spidermonkey_put_interpreter(struct ecmascript_interpreter *interpreter)
void
spidermonkey_eval(struct ecmascript_interpreter *interpreter,
struct string *code)
struct string *code, struct string *ret)
{
JSContext *ctx;
jsval rval;
@ -321,6 +247,7 @@ spidermonkey_eval(struct ecmascript_interpreter *interpreter,
assert(interpreter);
ctx = interpreter->backend_data;
setup_safeguard(interpreter, ctx);
interpreter->ret = ret;
JS_EvaluateScript(ctx, JS_GetGlobalObject(ctx),
code->source, code->length, "", 0, &rval);
}
@ -336,6 +263,7 @@ spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter,
assert(interpreter);
ctx = interpreter->backend_data;
setup_safeguard(interpreter, ctx);
interpreter->ret = NULL;
if (JS_EvaluateScript(ctx, JS_GetGlobalObject(ctx),
code->source, code->length, "", 0, &rval)
== JS_FALSE) {
@ -361,6 +289,7 @@ spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter,
assert(interpreter);
ctx = interpreter->backend_data;
setup_safeguard(interpreter, ctx);
interpreter->ret = NULL;
ret = JS_EvaluateScript(ctx, JS_GetGlobalObject(ctx),
code->source, code->length, "", 0, &rval);
if (ret == 2) { /* onClick="history.back()" */

View File

@ -10,7 +10,7 @@ void spidermonkey_done();
void *spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter);
void spidermonkey_put_interpreter(struct ecmascript_interpreter *interpreter);
void spidermonkey_eval(struct ecmascript_interpreter *interpreter, struct string *code);
void spidermonkey_eval(struct ecmascript_interpreter *interpreter, struct string *code, struct string *ret);
unsigned char *spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter, struct string *code);
int spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter, struct string *code);

View File

@ -192,19 +192,25 @@ document_set_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
}
static JSBool document_write(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
static JSBool document_writeln(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
const JSFunctionSpec document_funcs[] = {
{ "write", document_write, 1 },
{ "writeln", document_writeln, 1 },
{ NULL }
};
static JSBool
document_write(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
#ifdef CONFIG_LEDS
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
#endif
struct string *ret = interpreter->ret;
if (argc >= 1 && ret) {
unsigned char *code = jsval_to_string(ctx, &argv[0]);
add_to_string(ret, code);
}
/* XXX: I don't know about you, but I have *ENOUGH* of those 'Undefined
* function' errors, I want to see just the useful ones. So just
* lighting a led and going away, no muss, no fuss. --pasky */
@ -220,3 +226,25 @@ document_write(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rv
return JS_TRUE;
}
static JSBool
document_writeln(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
struct string *ret = interpreter->ret;
if (argc >= 1 && ret) {
unsigned char *code = jsval_to_string(ctx, &argv[0]);
add_to_string(ret, code);
add_char_to_string(ret, '\n');
}
#ifdef CONFIG_LEDS
set_led_value(interpreter->vs->doc_view->session->status.ecmascript_led, 'J');
#endif
boolean_to_jsval(ctx, rval, 0);
return JS_TRUE;
}

View File

@ -296,7 +296,7 @@ end:
abort_connection(conn, S_OUT_OF_MEM);
return;
}
read_from_socket(conn->data_socket, buf, S_CONN, fsp_got_data);
read_from_socket(conn->data_socket, buf, S_CONN, fsp_got_data);
}
#undef READ_SIZE

View File

@ -0,0 +1,10 @@
<html>
<body>
<a href="history.html">history.html</a><br/>
<script type="text/javascript">
document.writeln('<a href="ontest.html">Test</a><br/>');
document.write('<a href="infinite2.html">infinite2.html</a><br/>');
</script>
<a href="onload.html">onload.html</a><br/>
</body>
</html>