[project @ 2004-07-30 23:40:00 by jmb]

Rework stylesheet fetching code to remove dependence on magic numbers (now uses
values defined in html.h)
Make ad blocking optional.

svn path=/import/netsurf/; revision=1168
This commit is contained in:
John Mark Bell 2004-07-30 23:40:01 +00:00
parent d601aa7188
commit 8e650e3e39
7 changed files with 62 additions and 31 deletions

View File

@ -1,9 +1,7 @@
/*
* This file is part of NetSurf, http://netsurf.sourceforge.net/
*/
@import url('AdBlock');
/* Elements ordered as in the HTML 4.01 specification. */
html { display: block; }

View File

@ -432,7 +432,8 @@ void browser_window_reload(struct browser_window *bw, bool all)
c->data.html.object[i].content->fresh = false;
}
/* invalidate stylesheets */
for (i=2; i!=c->data.html.stylesheet_count; i++) {
for (i=STYLESHEET_START;
i!=c->data.html.stylesheet_count; i++) {
if (c->data.html.stylesheet_content[i] != 0)
c->data.html.stylesheet_content[i]->fresh = false;
}

View File

@ -51,6 +51,8 @@ char *option_accept_language = 0;
bool option_ssl_verify_certificates = true;
/** Preferred maximum size of memory cache / bytes. */
int option_memory_cache_size = 2 * 1024 * 1024;
/** Whether to block advertisements */
bool option_block_ads = false;
EXTRA_OPTION_DEFINE
@ -71,6 +73,7 @@ struct {
{ "accept_language", OPTION_STRING, &option_accept_language },
{ "ssl_verify_certificates", OPTION_BOOL, &option_ssl_verify_certificates },
{ "memory_cache_size", OPTION_INTEGER, &option_memory_cache_size },
{ "block_advertisements", OPTION_BOOL, &option_block_ads },
EXTRA_OPTION_TABLE
};

View File

@ -38,6 +38,7 @@ extern int option_font_min_size;
extern char *option_accept_language;
extern bool option_ssl_verify_certificates;
extern int option_memory_cache_size;
extern bool option_block_ads;
void options_read(const char *path);
void options_write(const char *path);

View File

@ -23,6 +23,7 @@
#ifdef riscos
#include "netsurf/desktop/gui.h"
#endif
#include "netsurf/desktop/options.h"
#include "netsurf/render/html.h"
#include "netsurf/render/layout.h"
#include "netsurf/utils/log.h"
@ -326,30 +327,52 @@ void html_find_stylesheets(struct content *c, xmlNode *head)
{
xmlNode *node, *node2;
char *rel, *type, *media, *href, *data, *url;
unsigned int i = 2;
unsigned int i = STYLESHEET_START;
unsigned int last_active = 0;
union content_msg_data msg_data;
/* stylesheet 0 is the base style sheet, stylesheet 1 is any <style> elements */
c->data.html.stylesheet_content = xcalloc(2, sizeof(*c->data.html.stylesheet_content));
c->data.html.stylesheet_content[1] = 0;
c->data.html.stylesheet_count = 2;
/* stylesheet 0 is the base style sheet,
* stylesheet 1 is the adblocking stylesheet,
* stylesheet 2 is any <style> elements */
c->data.html.stylesheet_content = xcalloc(STYLESHEET_START, sizeof(*c->data.html.stylesheet_content));
c->data.html.stylesheet_content[STYLESHEET_ADBLOCK] = 0;
c->data.html.stylesheet_content[STYLESHEET_STYLE] = 0;
c->data.html.stylesheet_count = STYLESHEET_START;
c->active = 0;
c->data.html.stylesheet_content[0] = fetchcache(
c->data.html.stylesheet_content[STYLESHEET_BASE] = fetchcache(
#ifdef riscos
"file:/<NetSurf$Dir>/Resources/CSS",
#else
"file:///home/james/Projects/netsurf/CSS",
#endif
html_convert_css_callback, c, 0,
c->width, c->height, true, 0, 0, false);
assert(c->data.html.stylesheet_content[0]);
html_convert_css_callback, c,
(void *) STYLESHEET_BASE, c->width, c->height,
true, 0, 0, false);
assert(c->data.html.stylesheet_content[STYLESHEET_BASE]);
c->active++;
fetchcache_go(c->data.html.stylesheet_content[0], 0,
html_convert_css_callback, c, 0,
0, 0, false);
fetchcache_go(c->data.html.stylesheet_content[STYLESHEET_BASE], 0,
html_convert_css_callback, c,
(void *) STYLESHEET_BASE, 0, 0, false);
if (option_block_ads) {
c->data.html.stylesheet_content[STYLESHEET_ADBLOCK] = fetchcache(
#ifdef riscos
"file:/<NetSurf$Dir>/Resources/AdBlock",
#else
"file:///home/james/Projects/netsurf/AdBlock",
#endif
html_convert_css_callback, c,
(void *) STYLESHEET_ADBLOCK, c->width,
c->height, true, 0, 0, false);
if (c->data.html.stylesheet_content[STYLESHEET_ADBLOCK]) {
c->active++;
fetchcache_go(c->data.html.stylesheet_content[STYLESHEET_ADBLOCK],
0, html_convert_css_callback, c,
(void *) STYLESHEET_ADBLOCK, 0, 0, false);
}
}
for (node = head == 0 ? 0 : head->children; node != 0; node = node->next) {
if (node->type != XML_ELEMENT_NODE)
@ -438,15 +461,15 @@ void html_find_stylesheets(struct content *c, xmlNode *head)
/* create stylesheet */
LOG(("style element"));
if (c->data.html.stylesheet_content[1] == 0) {
if (c->data.html.stylesheet_content[STYLESHEET_STYLE] == 0) {
const char *params[] = { 0 };
c->data.html.stylesheet_content[1] =
c->data.html.stylesheet_content[STYLESHEET_STYLE] =
content_create(c->data.html.
base_url);
if (!c->data.html.stylesheet_content[1])
if (!c->data.html.stylesheet_content[STYLESHEET_STYLE])
return;
if (!content_set_type(c->data.html.
stylesheet_content[1],
stylesheet_content[STYLESHEET_STYLE],
CONTENT_CSS, "text/css",
params))
return;
@ -457,7 +480,7 @@ void html_find_stylesheets(struct content *c, xmlNode *head)
for (node2 = node->children; node2 != 0; node2 = node2->next) {
data = xmlNodeGetContent(node2);
if (!content_process_data(c->data.html.
stylesheet_content[1],
stylesheet_content[STYLESHEET_STYLE],
data, strlen(data))) {
xmlFree(data);
return;
@ -469,15 +492,15 @@ void html_find_stylesheets(struct content *c, xmlNode *head)
c->data.html.stylesheet_count = i;
if (c->data.html.stylesheet_content[1] != 0) {
if (css_convert(c->data.html.stylesheet_content[1], c->width,
if (c->data.html.stylesheet_content[STYLESHEET_STYLE] != 0) {
if (css_convert(c->data.html.stylesheet_content[STYLESHEET_STYLE], c->width,
c->height)) {
content_add_user(c->data.html.stylesheet_content[1],
content_add_user(c->data.html.stylesheet_content[STYLESHEET_STYLE],
html_convert_css_callback,
c, (void *) 1);
c, (void *) STYLESHEET_STYLE);
} else {
/* conversion failed */
c->data.html.stylesheet_content[1] = 0;
c->data.html.stylesheet_content[STYLESHEET_STYLE] = 0;
}
}

View File

@ -26,6 +26,12 @@ struct content;
struct object_params;
struct imagemap;
/* entries in stylesheet_content */
#define STYLESHEET_BASE 0 /* base style sheet */
#define STYLESHEET_ADBLOCK 1 /* adblocking stylesheet */
#define STYLESHEET_STYLE 2 /* <style> elements (not cached) */
#define STYLESHEET_START 3 /* start of document stylesheets */
/** Data specific to CONTENT_HTML. */
struct content_html_data {
htmlParserCtxt *parser; /**< HTML parser context. */
@ -41,8 +47,7 @@ struct content_html_data {
/** Number of entries in stylesheet_content. */
unsigned int stylesheet_count;
/** Stylesheets. Each may be 0. Stylesheet 0 is the base style sheet,
* stylesheet 1 is any <style> elements (not cached). */
/** Stylesheets. Each may be 0. */
struct content **stylesheet_content;
struct css_style *style; /**< Base style. */

View File

@ -101,8 +101,8 @@ bool save_complete_html(struct content *c, const char *path, bool index)
if (save_complete_list_check(c))
return true;
/* save stylesheets, ignoring the base sheet */
for (i = 1; i != c->data.html.stylesheet_count; i++) {
/* save stylesheets, ignoring the base and adblocking sheets */
for (i = STYLESHEET_STYLE; i != c->data.html.stylesheet_count; i++) {
struct content *css = c->data.html.stylesheet_content[i];
char *source;
int source_len;
@ -120,7 +120,7 @@ bool save_complete_html(struct content *c, const char *path, bool index)
if (!save_imported_sheets(css, path))
return false;
if (i == 1)
if (i == STYLESHEET_STYLE)
continue; /* don't save <style> elements */
snprintf(spath, sizeof spath, "%s.%x", path,