2007-07-27 12:45:02 -04:00
|
|
|
/** The document base functionality
|
|
|
|
* @file */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "cache/cache.h"
|
|
|
|
#include "config/options.h"
|
|
|
|
#include "document/document.h"
|
|
|
|
#include "document/forms.h"
|
|
|
|
#include "document/html/frames.h"
|
|
|
|
#include "document/html/parser.h"
|
|
|
|
#include "document/html/parser/parse.h"
|
|
|
|
#include "document/html/renderer.h"
|
|
|
|
#include "document/options.h"
|
|
|
|
#include "document/refresh.h"
|
|
|
|
#include "main/module.h"
|
|
|
|
#include "main/object.h"
|
|
|
|
#include "protocol/uri.h"
|
|
|
|
#include "terminal/draw.h"
|
|
|
|
#include "util/color.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/lists.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
#include "viewer/text/link.h"
|
|
|
|
|
2007-07-26 15:39:08 -04:00
|
|
|
static INIT_LIST_OF(struct document, format_cache);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
struct document *
|
|
|
|
init_document(struct cache_entry *cached, struct document_options *options)
|
|
|
|
{
|
|
|
|
struct document *document = mem_calloc(1, sizeof(*document));
|
|
|
|
|
|
|
|
if (!document) return NULL;
|
|
|
|
|
|
|
|
document->uri = get_uri_reference(cached->uri);
|
|
|
|
|
|
|
|
object_lock(cached);
|
2008-04-20 16:25:10 -04:00
|
|
|
document->cache_id = cached->cache_id;
|
2006-12-09 22:11:04 -05:00
|
|
|
document->cached = cached;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
init_list(document->forms);
|
|
|
|
init_list(document->tags);
|
|
|
|
init_list(document->nodes);
|
|
|
|
|
|
|
|
#ifdef CONFIG_ECMASCRIPT
|
|
|
|
init_list(document->onload_snippets);
|
|
|
|
#endif
|
|
|
|
|
2008-01-19 12:56:50 -05:00
|
|
|
#ifdef CONFIG_COMBINE
|
2008-01-03 07:03:08 -05:00
|
|
|
document->comb_x = -1;
|
|
|
|
document->comb_y = -1;
|
|
|
|
#endif
|
2005-09-15 09:58:31 -04:00
|
|
|
object_nolock(document, "document");
|
|
|
|
object_lock(document);
|
|
|
|
|
|
|
|
copy_opt(&document->options, options);
|
|
|
|
|
|
|
|
add_to_list(format_cache, document);
|
|
|
|
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_frameset_desc(struct frameset_desc *frameset_desc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < frameset_desc->n; i++) {
|
|
|
|
struct frame_desc *frame_desc = &frameset_desc->frame_desc[i];
|
|
|
|
|
|
|
|
if (frame_desc->subframe)
|
|
|
|
free_frameset_desc(frame_desc->subframe);
|
|
|
|
mem_free_if(frame_desc->name);
|
|
|
|
if (frame_desc->uri)
|
|
|
|
done_uri(frame_desc->uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_free(frameset_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
done_link_members(struct link *link)
|
|
|
|
{
|
|
|
|
if (link->event_hooks) {
|
|
|
|
struct script_event_hook *evhook, *safety;
|
|
|
|
|
|
|
|
foreachsafe (evhook, safety, *link->event_hooks) {
|
|
|
|
mem_free_if(evhook->src);
|
|
|
|
mem_free(evhook);
|
|
|
|
}
|
|
|
|
mem_free(link->event_hooks);
|
|
|
|
}
|
|
|
|
mem_free_if(get_link_name(link));
|
|
|
|
mem_free_if(link->where);
|
|
|
|
mem_free_if(link->target);
|
|
|
|
mem_free_if(link->title);
|
|
|
|
mem_free_if(link->where_img);
|
|
|
|
mem_free_if(link->points);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
done_document(struct document *document)
|
|
|
|
{
|
|
|
|
assert(document);
|
|
|
|
if_assert_failed return;
|
|
|
|
|
|
|
|
assertm(!is_object_used(document), "Attempt to free locked formatted data.");
|
|
|
|
if_assert_failed return;
|
|
|
|
|
2006-12-09 22:11:04 -05:00
|
|
|
assert(document->cached);
|
|
|
|
object_unlock(document->cached);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (document->uri) done_uri(document->uri);
|
|
|
|
mem_free_if(document->title);
|
|
|
|
if (document->frame_desc) free_frameset_desc(document->frame_desc);
|
|
|
|
if (document->refresh) done_document_refresh(document->refresh);
|
|
|
|
|
|
|
|
if (document->links) {
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
for (pos = 0; pos < document->nlinks; pos++)
|
|
|
|
done_link_members(&document->links[pos]);
|
|
|
|
|
|
|
|
mem_free(document->links);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document->data) {
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
for (pos = 0; pos < document->height; pos++)
|
|
|
|
mem_free_if(document->data[pos].chars);
|
|
|
|
|
|
|
|
mem_free(document->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_free_if(document->lines1);
|
|
|
|
mem_free_if(document->lines2);
|
|
|
|
done_document_options(&document->options);
|
|
|
|
|
|
|
|
while (!list_empty(document->forms)) {
|
|
|
|
done_form(document->forms.next);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_CSS
|
|
|
|
free_uri_list(&document->css_imports);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ECMASCRIPT
|
|
|
|
free_string_list(&document->onload_snippets);
|
|
|
|
free_uri_list(&document->ecmascript_imports);
|
2006-10-24 10:47:41 -04:00
|
|
|
kill_timer(&document->timeout);
|
2005-09-15 09:58:31 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
free_list(document->tags);
|
|
|
|
free_list(document->nodes);
|
|
|
|
|
|
|
|
mem_free_if(document->search);
|
|
|
|
mem_free_if(document->slines1);
|
|
|
|
mem_free_if(document->slines2);
|
|
|
|
|
|
|
|
del_from_list(document);
|
|
|
|
mem_free(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
release_document(struct document *document)
|
|
|
|
{
|
|
|
|
assert(document);
|
|
|
|
if_assert_failed return;
|
|
|
|
|
|
|
|
if (document->refresh) kill_document_refresh(document->refresh);
|
2006-10-24 10:47:41 -04:00
|
|
|
#ifdef CONFIG_ECMASCRIPT
|
|
|
|
kill_timer(&document->timeout);
|
|
|
|
#endif
|
2005-09-15 09:58:31 -04:00
|
|
|
object_unlock(document);
|
|
|
|
move_to_top_of_list(format_cache, document);
|
|
|
|
}
|
|
|
|
|
2007-08-30 20:09:40 -04:00
|
|
|
int
|
|
|
|
find_tag(struct document *document, unsigned char *name, int namelen)
|
|
|
|
{
|
|
|
|
struct tag *tag;
|
|
|
|
|
|
|
|
foreach (tag, document->tags)
|
2008-10-18 21:25:00 -04:00
|
|
|
if (!c_strlcasecmp(tag->name, -1, name, namelen))
|
2007-08-30 20:09:40 -04:00
|
|
|
return tag->y;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* Formatted document cache management */
|
|
|
|
|
|
|
|
/* ECMAScript doesn't like anything like CSS since it doesn't modify the
|
|
|
|
* formatted document (yet). */
|
|
|
|
|
|
|
|
#if CONFIG_CSS
|
|
|
|
unsigned long
|
|
|
|
get_document_css_magic(struct document *document)
|
|
|
|
{
|
|
|
|
unsigned long css_magic = 0;
|
|
|
|
struct uri *uri;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
foreach_uri (uri, index, &document->css_imports) {
|
|
|
|
struct cache_entry *cached = find_in_cache(uri);
|
|
|
|
|
2008-04-20 16:25:10 -04:00
|
|
|
if (cached) css_magic += cached->cache_id + cached->data_size;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return css_magic;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define check_document_css_magic(document) \
|
|
|
|
((document)->css_magic == get_document_css_magic(document))
|
|
|
|
#else
|
|
|
|
#define check_document_css_magic(document) 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2007-08-30 15:08:24 -04:00
|
|
|
update_cached_document_options(struct session *ses)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct document *document;
|
|
|
|
struct active_link_options active_link;
|
|
|
|
|
|
|
|
memset(&active_link, 0, sizeof(active_link)); /* Safer. */
|
2007-10-12 06:24:53 -04:00
|
|
|
active_link.color.foreground = get_opt_color("document.browse.links.active_link.colors.text", ses);
|
|
|
|
active_link.color.background = get_opt_color("document.browse.links.active_link.colors.background", ses);
|
2007-10-12 06:19:27 -04:00
|
|
|
active_link.enable_color = get_opt_bool("document.browse.links.active_link.enable_color", ses);
|
2007-08-30 15:08:24 -04:00
|
|
|
active_link.invert = get_opt_bool("document.browse.links.active_link.invert", ses);
|
|
|
|
active_link.underline = get_opt_bool("document.browse.links.active_link.underline", ses);
|
|
|
|
active_link.bold = get_opt_bool("document.browse.links.active_link.bold", ses);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
foreach (document, format_cache) {
|
|
|
|
copy_struct(&document->options.active_link, &active_link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct document *
|
|
|
|
get_cached_document(struct cache_entry *cached, struct document_options *options)
|
|
|
|
{
|
|
|
|
struct document *document, *next;
|
|
|
|
|
|
|
|
foreachsafe (document, next, format_cache) {
|
|
|
|
if (!compare_uri(document->uri, cached->uri, 0)
|
|
|
|
|| compare_opt(&document->options, options))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (options->no_cache
|
2008-04-20 16:25:10 -04:00
|
|
|
|| cached->cache_id != document->cache_id
|
2005-09-15 09:58:31 -04:00
|
|
|
|| !check_document_css_magic(document)) {
|
|
|
|
if (!is_object_used(document)) {
|
|
|
|
done_document(document);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reactivate */
|
|
|
|
move_to_top_of_list(format_cache, document);
|
|
|
|
|
|
|
|
object_lock(document);
|
|
|
|
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
shrink_format_cache(int whole)
|
|
|
|
{
|
|
|
|
struct document *document, *next;
|
2007-08-28 12:41:18 -04:00
|
|
|
int format_cache_size = get_opt_int("document.cache.format.size", NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
int format_cache_entries = 0;
|
|
|
|
|
|
|
|
foreachsafe (document, next, format_cache) {
|
|
|
|
if (is_object_used(document)) continue;
|
|
|
|
|
|
|
|
format_cache_entries++;
|
|
|
|
|
|
|
|
/* Destroy obsolete renderer documents which are already
|
|
|
|
* out-of-sync. */
|
2008-04-20 16:25:10 -04:00
|
|
|
if (document->cached->cache_id == document->cache_id)
|
2006-12-09 22:11:04 -05:00
|
|
|
continue;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
done_document(document);
|
|
|
|
format_cache_entries--;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertm(format_cache_entries >= 0, "format_cache_entries underflow on entry");
|
|
|
|
if_assert_failed format_cache_entries = 0;
|
|
|
|
|
2006-10-15 17:33:56 -04:00
|
|
|
foreachbacksafe (document, next, format_cache) {
|
2005-09-15 09:58:31 -04:00
|
|
|
if (is_object_used(document)) continue;
|
|
|
|
|
|
|
|
/* If we are not purging the whole format cache, stop
|
|
|
|
* once we are below the maximum number of entries. */
|
|
|
|
if (!whole && format_cache_entries <= format_cache_size)
|
|
|
|
break;
|
|
|
|
|
2006-10-15 17:33:56 -04:00
|
|
|
done_document(document);
|
2005-09-15 09:58:31 -04:00
|
|
|
format_cache_entries--;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertm(format_cache_entries >= 0, "format_cache_entries underflow");
|
|
|
|
if_assert_failed format_cache_entries = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_format_cache_size(void)
|
|
|
|
{
|
|
|
|
return list_size(&format_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_format_cache_used_count(void)
|
|
|
|
{
|
|
|
|
struct document *document;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
foreach (document, format_cache)
|
|
|
|
i += is_object_used(document);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_format_cache_refresh_count(void)
|
|
|
|
{
|
|
|
|
struct document *document;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
foreach (document, format_cache)
|
|
|
|
if (document->refresh
|
|
|
|
&& document->refresh->timer != TIMER_ID_UNDEF)
|
|
|
|
i++;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_documents(struct module *module)
|
|
|
|
{
|
|
|
|
init_tags_lookup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
done_documents(struct module *module)
|
|
|
|
{
|
|
|
|
free_tags_lookup();
|
|
|
|
free_table_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct module document_module = struct_module(
|
2007-03-22 18:51:56 -04:00
|
|
|
/* Because this module is listed in main_modules rather than
|
|
|
|
* in builtin_modules, its name does not appear in the user
|
|
|
|
* interface and so need not be translatable. */
|
|
|
|
/* name: */ "Document",
|
2005-09-15 09:58:31 -04:00
|
|
|
/* options: */ NULL,
|
|
|
|
/* hooks: */ NULL,
|
|
|
|
/* submodules: */ NULL,
|
|
|
|
/* data: */ NULL,
|
|
|
|
/* init: */ init_documents,
|
|
|
|
/* done: */ done_documents
|
|
|
|
);
|