2003-06-30 16:44:03 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
2004-02-20 03:43:17 +03:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Content for text/html (implementation).
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2004-07-06 00:19:52 +04:00
|
|
|
#include <ctype.h>
|
2003-02-09 15:58:15 +03:00
|
|
|
#include <string.h>
|
2003-04-06 01:38:06 +04:00
|
|
|
#include <strings.h>
|
2003-02-09 15:58:15 +03:00
|
|
|
#include <stdlib.h>
|
2004-03-08 21:21:21 +03:00
|
|
|
#include "libxml/parserInternals.h"
|
2004-01-05 05:10:59 +03:00
|
|
|
#include "netsurf/utils/config.h"
|
2003-06-17 23:24:21 +04:00
|
|
|
#include "netsurf/content/content.h"
|
2003-04-04 19:19:32 +04:00
|
|
|
#include "netsurf/content/fetch.h"
|
|
|
|
#include "netsurf/content/fetchcache.h"
|
2004-03-27 01:16:31 +03:00
|
|
|
#include "netsurf/desktop/imagemap.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/desktop/gui.h"
|
2004-07-31 03:40:01 +04:00
|
|
|
#include "netsurf/desktop/options.h"
|
2004-08-11 23:02:32 +04:00
|
|
|
#include "netsurf/render/box.h"
|
|
|
|
#include "netsurf/render/font.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/render/html.h"
|
|
|
|
#include "netsurf/render/layout.h"
|
|
|
|
#include "netsurf/utils/log.h"
|
2004-03-02 21:02:41 +03:00
|
|
|
#include "netsurf/utils/messages.h"
|
|
|
|
#include "netsurf/utils/url.h"
|
|
|
|
#include "netsurf/utils/utils.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
#define CHUNK 4096
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
static bool html_set_parser_encoding(struct content *c, const char *encoding);
|
|
|
|
static const char *html_detect_encoding(const char *data, unsigned int size);
|
2003-06-17 23:24:21 +04:00
|
|
|
static void html_convert_css_callback(content_msg msg, struct content *css,
|
2004-04-25 03:42:32 +04:00
|
|
|
void *p1, void *p2, union content_msg_data data);
|
2005-01-02 01:05:21 +03:00
|
|
|
static bool html_head(struct content *c, xmlNode *head);
|
|
|
|
static bool html_find_stylesheets(struct content *c, xmlNode *head);
|
2003-06-17 23:24:21 +04:00
|
|
|
static void html_object_callback(content_msg msg, struct content *object,
|
2004-04-25 03:42:32 +04:00
|
|
|
void *p1, void *p2, union content_msg_data data);
|
2004-06-09 23:55:06 +04:00
|
|
|
static void html_object_done(struct box *box, struct content *object,
|
2004-06-13 00:08:47 +04:00
|
|
|
bool background);
|
2004-01-24 20:08:16 +03:00
|
|
|
static bool html_object_type_permitted(const content_type type,
|
|
|
|
const content_type *permitted_types);
|
2003-02-09 15:58:15 +03:00
|
|
|
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
/**
|
|
|
|
* Create a CONTENT_HTML.
|
|
|
|
*
|
|
|
|
* The content_html_data structure is initialized and the HTML parser is
|
|
|
|
* created.
|
|
|
|
*/
|
|
|
|
|
2004-06-11 00:41:26 +04:00
|
|
|
bool html_create(struct content *c, const char *params[])
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2003-12-27 23:15:23 +03:00
|
|
|
unsigned int i;
|
2004-02-20 03:43:17 +03:00
|
|
|
struct content_html_data *html = &c->data.html;
|
2004-06-11 00:41:26 +04:00
|
|
|
union content_msg_data msg_data;
|
2004-02-20 03:43:17 +03:00
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
html->parser = 0;
|
|
|
|
html->encoding_handler = 0;
|
|
|
|
html->encoding = 0;
|
2004-03-08 21:21:21 +03:00
|
|
|
html->getenc = true;
|
2004-06-11 00:41:26 +04:00
|
|
|
html->base_url = strdup(c->url);
|
2004-02-20 03:43:17 +03:00
|
|
|
html->layout = 0;
|
|
|
|
html->background_colour = TRANSPARENT;
|
|
|
|
html->stylesheet_count = 0;
|
|
|
|
html->stylesheet_content = 0;
|
|
|
|
html->style = 0;
|
|
|
|
html->object_count = 0;
|
|
|
|
html->object = 0;
|
2004-06-10 03:13:55 +04:00
|
|
|
html->imagemaps = 0;
|
2004-02-20 03:43:17 +03:00
|
|
|
html->box_pool = pool_create(sizeof (struct box) * 100);
|
2005-01-02 01:05:21 +03:00
|
|
|
html->string_pool = pool_create(8000);
|
2004-08-12 02:08:26 +04:00
|
|
|
html->bw = 0;
|
2004-06-11 00:41:26 +04:00
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!html->base_url || !html->string_pool || !html->box_pool)
|
|
|
|
goto no_memory;
|
2004-06-11 00:41:26 +04:00
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
for (i = 0; params[i]; i += 2) {
|
|
|
|
if (strcasecmp(params[i], "charset") == 0) {
|
|
|
|
html->encoding = strdup(params[i + 1]);
|
|
|
|
if (!html->encoding)
|
|
|
|
goto no_memory;
|
|
|
|
html->encoding_source = ENCODING_SOURCE_HEADER;
|
|
|
|
html->getenc = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
html->parser = htmlCreatePushParserCtxt(0, 0, "", 0, 0,
|
|
|
|
XML_CHAR_ENCODING_NONE);
|
|
|
|
if (!html->parser)
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
if (html->encoding) {
|
|
|
|
/* an encoding was specified in the Content-Type header */
|
|
|
|
if (!html_set_parser_encoding(c, html->encoding))
|
|
|
|
return false;
|
2004-06-11 00:41:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2005-01-02 01:05:21 +03:00
|
|
|
|
|
|
|
no_memory:
|
|
|
|
/* memory allocated will be freed in html_destroy() */
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return false;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
/**
|
|
|
|
* Process data for CONTENT_HTML.
|
|
|
|
*
|
|
|
|
* The data is parsed in chunks of size CHUNK, multitasking in between.
|
|
|
|
*/
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2004-06-11 00:41:26 +04:00
|
|
|
bool html_process_data(struct content *c, char *data, unsigned int size)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
|
|
|
unsigned long x;
|
2004-04-17 20:00:16 +04:00
|
|
|
|
2004-03-08 21:21:21 +03:00
|
|
|
if (c->data.html.getenc) {
|
2005-01-02 01:05:21 +03:00
|
|
|
/* No encoding was specified in the Content-Type header.
|
|
|
|
* Attempt to detect if the encoding is not 8-bit. If the
|
|
|
|
* encoding is 8-bit, leave the parser unchanged, so that it
|
|
|
|
* searches for a <meta http-equiv="content-type"
|
|
|
|
* content="text/html; charset=...">. */
|
|
|
|
const char *encoding;
|
|
|
|
encoding = html_detect_encoding(data, size);
|
|
|
|
if (encoding) {
|
|
|
|
if (!html_set_parser_encoding(c, encoding))
|
|
|
|
return false;
|
|
|
|
c->data.html.encoding = strdup(encoding);
|
|
|
|
if (!c->data.html.encoding)
|
|
|
|
return false;
|
|
|
|
c->data.html.encoding_source =
|
|
|
|
ENCODING_SOURCE_DETECTED;
|
2004-04-17 20:00:16 +04:00
|
|
|
}
|
2004-03-08 21:21:21 +03:00
|
|
|
c->data.html.getenc = false;
|
|
|
|
}
|
2004-04-17 20:00:16 +04:00
|
|
|
|
2003-02-26 21:22:24 +03:00
|
|
|
for (x = 0; x + CHUNK <= size; x += CHUNK) {
|
2003-02-09 15:58:15 +03:00
|
|
|
htmlParseChunk(c->data.html.parser, data + x, CHUNK, 0);
|
|
|
|
gui_multitask();
|
|
|
|
}
|
2003-03-04 14:59:36 +03:00
|
|
|
htmlParseChunk(c->data.html.parser, data + x, (int) (size - x), 0);
|
2004-06-11 00:41:26 +04:00
|
|
|
|
|
|
|
return true;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
/**
|
|
|
|
* Set the HTML parser character encoding.
|
|
|
|
*
|
|
|
|
* \param c content of type CONTENT_HTML
|
|
|
|
* \param encoding name of encoding
|
|
|
|
* \return true on success, false on error and error reported
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool html_set_parser_encoding(struct content *c, const char *encoding)
|
|
|
|
{
|
|
|
|
struct content_html_data *html = &c->data.html;
|
|
|
|
xmlError *error;
|
|
|
|
char error_message[500];
|
|
|
|
union content_msg_data msg_data;
|
|
|
|
|
|
|
|
html->encoding_handler = xmlFindCharEncodingHandler(encoding);
|
|
|
|
if (!html->encoding_handler) {
|
|
|
|
/* either out of memory, or no handler available */
|
|
|
|
/* assume no handler available, which is not a fatal error */
|
|
|
|
LOG(("no encoding handler for \"%s\"", encoding));
|
|
|
|
/* \todo warn user and ask them to install iconv? */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlCtxtResetLastError(html->parser);
|
|
|
|
if (xmlSwitchToEncoding(html->parser, html->encoding_handler)) {
|
|
|
|
error = xmlCtxtGetLastError(html->parser);
|
|
|
|
snprintf(error_message, sizeof error_message,
|
|
|
|
"%s xmlSwitchToEncoding(): %s",
|
|
|
|
messages_get("MiscError"),
|
|
|
|
error ? error->message : "failed");
|
|
|
|
msg_data.error = error_message;
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to detect the encoding of some HTML data.
|
|
|
|
*
|
|
|
|
* \param data HTML source data
|
|
|
|
* \param size length of data
|
|
|
|
* \return a constant string giving the encoding, or 0 if the encoding
|
|
|
|
* appears to be some 8-bit encoding
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *html_detect_encoding(const char *data, unsigned int size)
|
|
|
|
{
|
|
|
|
/* this detection assumes that the first two characters are <= 0xff */
|
|
|
|
if (size < 4)
|
|
|
|
return 0;
|
|
|
|
if (data[0] == 0xfe && data[1] == 0xff) /* BOM fe ff */
|
|
|
|
return "UTF-16BE";
|
|
|
|
else if (data[0] == 0xfe && data[1] == 0xff) /* BOM ff fe */
|
|
|
|
return "UTF-16LE";
|
|
|
|
else if (data[0] == 0x00 && data[1] != 0x00 &&
|
|
|
|
data[2] == 0x00 && data[3] != 0x00) /* 00 xx 00 xx */
|
|
|
|
return "UTF-16BE";
|
|
|
|
else if (data[0] != 0x00 && data[1] == 0x00 &&
|
|
|
|
data[2] != 0x00 && data[3] == 0x00) /* xx 00 xx 00 */
|
|
|
|
return "UTF-16BE";
|
|
|
|
else if (data[0] == 0x00 && data[1] == 0x00 &&
|
|
|
|
data[2] == 0x00 && data[3] != 0x00) /* 00 00 00 xx */
|
|
|
|
return "ISO-10646-UCS-4";
|
|
|
|
else if (data[0] != 0x00 && data[1] == 0x00 &&
|
|
|
|
data[2] == 0x00 && data[3] == 0x00) /* xx 00 00 00 */
|
|
|
|
return "ISO-10646-UCS-4";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
/**
|
|
|
|
* Convert a CONTENT_HTML for display.
|
|
|
|
*
|
|
|
|
* The following steps are carried out in order:
|
|
|
|
*
|
|
|
|
* - parsing to an XML tree is completed
|
|
|
|
* - stylesheets are fetched
|
|
|
|
* - the XML tree is converted to a box tree and object fetches are started
|
|
|
|
* - the box tree is laid out
|
|
|
|
*
|
|
|
|
* On exit, the content status will be either CONTENT_STATUS_DONE if the
|
|
|
|
* document is completely loaded or CONTENT_STATUS_READY if objects are still
|
|
|
|
* being fetched.
|
|
|
|
*/
|
|
|
|
|
2004-06-11 00:41:26 +04:00
|
|
|
bool html_convert(struct content *c, int width, int height)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2003-04-11 01:44:45 +04:00
|
|
|
xmlDoc *document;
|
|
|
|
xmlNode *html, *head;
|
2004-06-11 00:41:26 +04:00
|
|
|
union content_msg_data msg_data;
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2003-04-11 01:44:45 +04:00
|
|
|
/* finish parsing */
|
2003-02-09 15:58:15 +03:00
|
|
|
htmlParseChunk(c->data.html.parser, "", 0, 1);
|
2004-03-24 23:10:03 +03:00
|
|
|
document = c->data.html.parser->myDoc;
|
2003-02-09 15:58:15 +03:00
|
|
|
/*xmlDebugDumpDocument(stderr, c->data.html.parser->myDoc);*/
|
2003-04-11 01:44:45 +04:00
|
|
|
htmlFreeParserCtxt(c->data.html.parser);
|
2004-02-20 03:43:17 +03:00
|
|
|
c->data.html.parser = 0;
|
2004-06-11 00:41:26 +04:00
|
|
|
if (!document) {
|
2003-04-11 01:44:45 +04:00
|
|
|
LOG(("Parsing failed"));
|
2004-06-11 00:41:26 +04:00
|
|
|
msg_data.error = messages_get("ParsingFail");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2005-01-02 01:05:21 +03:00
|
|
|
|
|
|
|
if (!c->data.html.encoding && document->encoding) {
|
|
|
|
/* The encoding was not in headers or detected, and the parser
|
|
|
|
* found a <meta http-equiv="content-type"
|
|
|
|
* content="text/html; charset=...">. */
|
|
|
|
c->data.html.encoding = strdup(document->encoding);
|
|
|
|
if (!c->data.html.encoding) {
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
c->data.html.encoding_source = ENCODING_SOURCE_META;
|
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2003-04-11 01:44:45 +04:00
|
|
|
/* locate html and head elements */
|
|
|
|
for (html = document->children;
|
|
|
|
html != 0 && html->type != XML_ELEMENT_NODE;
|
|
|
|
html = html->next)
|
|
|
|
;
|
|
|
|
if (html == 0 || strcmp((const char *) html->name, "html") != 0) {
|
|
|
|
LOG(("html element not found"));
|
|
|
|
xmlFreeDoc(document);
|
2004-06-11 00:41:26 +04:00
|
|
|
msg_data.error = messages_get("ParsingFail");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2003-04-11 01:44:45 +04:00
|
|
|
for (head = html->children;
|
|
|
|
head != 0 && head->type != XML_ELEMENT_NODE;
|
|
|
|
head = head->next)
|
|
|
|
;
|
2003-04-12 01:06:51 +04:00
|
|
|
if (strcmp((const char *) head->name, "head") != 0) {
|
|
|
|
head = 0;
|
2003-04-11 01:44:45 +04:00
|
|
|
LOG(("head element not found"));
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2003-04-11 01:44:45 +04:00
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
if (head) {
|
|
|
|
if (!html_head(c, head)) {
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2003-04-06 01:38:06 +04:00
|
|
|
/* get stylesheets */
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!html_find_stylesheets(c, head)) {
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
|
|
|
}
|
2003-04-04 19:19:32 +04:00
|
|
|
|
2003-04-15 21:53:00 +04:00
|
|
|
/* convert xml tree to box tree */
|
2003-02-09 15:58:15 +03:00
|
|
|
LOG(("XML to box"));
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("Processing"));
|
2004-06-11 00:41:26 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, msg_data);
|
2004-12-14 01:17:43 +03:00
|
|
|
if (!xml_to_box(html, c)) {
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
|
|
|
}
|
2003-03-26 00:51:29 +03:00
|
|
|
/*box_dump(c->data.html.layout->children, 0);*/
|
2003-04-12 01:06:51 +04:00
|
|
|
|
2004-03-27 01:16:31 +03:00
|
|
|
/* extract image maps - can't do this sensibly in xml_to_box */
|
2004-12-08 03:33:25 +03:00
|
|
|
if (!imagemap_extract(html, c)) {
|
|
|
|
LOG(("imagemap extraction failed"));
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
|
|
|
}
|
2004-03-27 01:16:31 +03:00
|
|
|
/*imagemap_dump(c);*/
|
|
|
|
|
2004-02-17 15:41:38 +03:00
|
|
|
/* XML tree not required past this point */
|
2004-03-24 23:10:03 +03:00
|
|
|
xmlFreeDoc(document);
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2003-04-15 21:53:00 +04:00
|
|
|
/* layout the box tree */
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("Formatting"));
|
2004-06-11 00:41:26 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, msg_data);
|
2003-02-09 15:58:15 +03:00
|
|
|
LOG(("Layout document"));
|
2004-10-18 01:10:19 +04:00
|
|
|
layout_document(c->data.html.layout, width,
|
2004-06-03 01:09:14 +04:00
|
|
|
c->data.html.box_pool);
|
2003-03-26 00:51:29 +03:00
|
|
|
/*box_dump(c->data.html.layout->children, 0);*/
|
2004-10-18 01:10:19 +04:00
|
|
|
c->width = c->data.html.layout->descendant_x1;
|
|
|
|
c->height = c->data.html.layout->descendant_y1;
|
2003-04-15 21:53:00 +04:00
|
|
|
|
2003-09-17 16:57:43 +04:00
|
|
|
if (c->active == 0) {
|
2003-06-17 23:24:21 +04:00
|
|
|
c->status = CONTENT_STATUS_DONE;
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("Done"));
|
2003-09-17 16:57:43 +04:00
|
|
|
} else {
|
2003-06-17 23:24:21 +04:00
|
|
|
c->status = CONTENT_STATUS_READY;
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("FetchObjs"), c->active);
|
2003-09-17 16:57:43 +04:00
|
|
|
}
|
2003-06-05 17:17:55 +04:00
|
|
|
|
2004-06-11 00:41:26 +04:00
|
|
|
return true;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-26 03:17:55 +03:00
|
|
|
/**
|
|
|
|
* Process elements in <head>.
|
2004-02-20 03:43:17 +03:00
|
|
|
*
|
|
|
|
* \param c content structure
|
|
|
|
* \param head xml node of head element
|
2005-01-02 01:05:21 +03:00
|
|
|
* \return true on success, false on memory exhaustion
|
2004-02-20 03:43:17 +03:00
|
|
|
*
|
|
|
|
* The title and base href are extracted if present.
|
2003-12-26 03:17:55 +03:00
|
|
|
*/
|
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
bool html_head(struct content *c, xmlNode *head)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2003-04-06 01:38:06 +04:00
|
|
|
xmlNode *node;
|
2003-02-09 15:58:15 +03:00
|
|
|
|
|
|
|
c->title = 0;
|
|
|
|
|
2003-04-06 01:38:06 +04:00
|
|
|
for (node = head->children; node != 0; node = node->next) {
|
2004-02-20 03:43:17 +03:00
|
|
|
if (node->type != XML_ELEMENT_NODE)
|
|
|
|
continue;
|
|
|
|
|
2003-12-26 03:17:55 +03:00
|
|
|
if (!c->title && strcmp(node->name, "title") == 0) {
|
|
|
|
xmlChar *title = xmlNodeGetContent(node);
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!title)
|
|
|
|
return false;
|
2004-07-06 00:19:52 +04:00
|
|
|
c->title = squash_whitespace(title);
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!c->title)
|
|
|
|
return false;
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(title);
|
2003-12-26 03:17:55 +03:00
|
|
|
|
|
|
|
} else if (strcmp(node->name, "base") == 0) {
|
|
|
|
char *href = (char *) xmlGetProp(node, (const xmlChar *) "href");
|
|
|
|
if (href) {
|
2004-08-09 20:11:58 +04:00
|
|
|
char *url;
|
|
|
|
url_func_result res;
|
|
|
|
res = url_normalize(href, &url);
|
|
|
|
if (res == URL_FUNC_OK) {
|
2004-02-20 03:43:17 +03:00
|
|
|
free(c->data.html.base_url);
|
2003-12-26 03:17:55 +03:00
|
|
|
c->data.html.base_url = url;
|
2004-02-20 03:43:17 +03:00
|
|
|
}
|
2003-12-26 03:17:55 +03:00
|
|
|
xmlFree(href);
|
|
|
|
}
|
2003-04-06 01:38:06 +04:00
|
|
|
}
|
|
|
|
}
|
2005-01-02 01:05:21 +03:00
|
|
|
return true;
|
2003-04-06 01:38:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
/**
|
|
|
|
* Process inline stylesheets and fetch linked stylesheets.
|
|
|
|
*
|
|
|
|
* \param c content structure
|
|
|
|
* \param head xml node of head element, or 0 if none
|
2005-01-02 01:05:21 +03:00
|
|
|
* \return true on success, false on memory exhaustion
|
2004-02-20 03:43:17 +03:00
|
|
|
*/
|
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
bool html_find_stylesheets(struct content *c, xmlNode *head)
|
2003-04-06 01:38:06 +04:00
|
|
|
{
|
2003-04-15 21:53:00 +04:00
|
|
|
xmlNode *node, *node2;
|
2003-04-13 16:50:10 +04:00
|
|
|
char *rel, *type, *media, *href, *data, *url;
|
2004-07-31 03:40:01 +04:00
|
|
|
unsigned int i = STYLESHEET_START;
|
2003-06-17 23:24:21 +04:00
|
|
|
unsigned int last_active = 0;
|
2004-04-25 03:42:32 +04:00
|
|
|
union content_msg_data msg_data;
|
2004-08-09 20:11:58 +04:00
|
|
|
url_func_result res;
|
2005-01-02 01:05:21 +03:00
|
|
|
struct content **stylesheet_content;
|
2003-04-13 16:50:10 +04:00
|
|
|
|
2004-07-31 03:40:01 +04:00
|
|
|
/* stylesheet 0 is the base style sheet,
|
|
|
|
* stylesheet 1 is the adblocking stylesheet,
|
|
|
|
* stylesheet 2 is any <style> elements */
|
2005-01-02 01:05:21 +03:00
|
|
|
c->data.html.stylesheet_content = malloc(STYLESHEET_START *
|
|
|
|
sizeof *c->data.html.stylesheet_content);
|
|
|
|
if (!c->data.html.stylesheet_content)
|
|
|
|
return false;
|
2004-07-31 03:40:01 +04:00
|
|
|
c->data.html.stylesheet_content[STYLESHEET_ADBLOCK] = 0;
|
|
|
|
c->data.html.stylesheet_content[STYLESHEET_STYLE] = 0;
|
|
|
|
c->data.html.stylesheet_count = STYLESHEET_START;
|
2003-04-06 01:38:06 +04:00
|
|
|
|
2003-04-15 21:53:00 +04:00
|
|
|
c->active = 0;
|
|
|
|
|
2004-07-31 03:40:01 +04:00
|
|
|
c->data.html.stylesheet_content[STYLESHEET_BASE] = fetchcache(
|
2004-10-18 02:13:35 +04:00
|
|
|
default_stylesheet_url,
|
2004-07-31 03:40:01 +04:00
|
|
|
html_convert_css_callback, c,
|
|
|
|
(void *) STYLESHEET_BASE, c->width, c->height,
|
2005-01-03 05:09:20 +03:00
|
|
|
true, 0, 0, false, false);
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!c->data.html.stylesheet_content[STYLESHEET_BASE])
|
|
|
|
return false;
|
2004-06-11 03:55:23 +04:00
|
|
|
c->active++;
|
2004-07-31 03:40:01 +04:00
|
|
|
fetchcache_go(c->data.html.stylesheet_content[STYLESHEET_BASE], 0,
|
|
|
|
html_convert_css_callback, c,
|
2005-01-25 02:02:37 +03:00
|
|
|
(void *) STYLESHEET_BASE, c->width, c->height,
|
|
|
|
0, 0, false);
|
2004-07-31 03:40:01 +04:00
|
|
|
|
|
|
|
if (option_block_ads) {
|
2005-01-02 01:05:21 +03:00
|
|
|
c->data.html.stylesheet_content[STYLESHEET_ADBLOCK] =
|
|
|
|
fetchcache(adblock_stylesheet_url,
|
|
|
|
html_convert_css_callback, c,
|
|
|
|
(void *) STYLESHEET_ADBLOCK, c->width,
|
2005-01-03 05:09:20 +03:00
|
|
|
c->height, true, 0, 0, false, false);
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!c->data.html.stylesheet_content[STYLESHEET_ADBLOCK])
|
|
|
|
return false;
|
|
|
|
c->active++;
|
|
|
|
fetchcache_go(c->data.html.
|
|
|
|
stylesheet_content[STYLESHEET_ADBLOCK],
|
2004-07-31 03:40:01 +04:00
|
|
|
0, html_convert_css_callback, c,
|
2005-01-25 02:02:37 +03:00
|
|
|
(void *) STYLESHEET_ADBLOCK, c->width,
|
|
|
|
c->height, 0, 0, false);
|
2004-07-31 03:40:01 +04:00
|
|
|
}
|
2003-04-06 01:38:06 +04:00
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
for (node = head == 0 ? 0 : head->children; node; node = node->next) {
|
2004-02-20 03:43:17 +03:00
|
|
|
if (node->type != XML_ELEMENT_NODE)
|
|
|
|
continue;
|
|
|
|
|
2003-04-06 01:38:06 +04:00
|
|
|
if (strcmp(node->name, "link") == 0) {
|
|
|
|
/* rel='stylesheet' */
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((rel = (char *) xmlGetProp(node, (const xmlChar *) "rel")) == NULL)
|
2003-04-06 01:38:06 +04:00
|
|
|
continue;
|
|
|
|
if (strcasecmp(rel, "stylesheet") != 0) {
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(rel);
|
2003-02-09 15:58:15 +03:00
|
|
|
continue;
|
|
|
|
}
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(rel);
|
2003-04-06 01:38:06 +04:00
|
|
|
|
2003-04-12 01:06:51 +04:00
|
|
|
/* type='text/css' or not present */
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((type = (char *) xmlGetProp(node, (const xmlChar *) "type")) != NULL) {
|
2003-04-12 01:06:51 +04:00
|
|
|
if (strcmp(type, "text/css") != 0) {
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(type);
|
2003-04-12 01:06:51 +04:00
|
|
|
continue;
|
|
|
|
}
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(type);
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2003-04-06 01:38:06 +04:00
|
|
|
|
2003-04-12 01:06:51 +04:00
|
|
|
/* media contains 'screen' or 'all' or not present */
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((media = (char *) xmlGetProp(node, (const xmlChar *) "media")) != NULL) {
|
2003-04-12 01:06:51 +04:00
|
|
|
if (strstr(media, "screen") == 0 &&
|
|
|
|
strstr(media, "all") == 0) {
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(media);
|
2003-04-06 01:38:06 +04:00
|
|
|
continue;
|
|
|
|
}
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(media);
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2003-06-05 17:17:55 +04:00
|
|
|
|
2003-04-06 01:38:06 +04:00
|
|
|
/* href='...' */
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((href = (char *) xmlGetProp(node, (const xmlChar *) "href")) == NULL)
|
2003-04-06 01:38:06 +04:00
|
|
|
continue;
|
|
|
|
|
2003-06-30 16:44:03 +04:00
|
|
|
/* TODO: only the first preferred stylesheets (ie. those with a
|
|
|
|
* title attribute) should be loaded (see HTML4 14.3) */
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
res = url_join(href, c->data.html.base_url, &url);
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(href);
|
2004-08-09 20:11:58 +04:00
|
|
|
if (res != URL_FUNC_OK)
|
2003-12-26 03:17:55 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
LOG(("linked stylesheet %i '%s'", i, url));
|
2003-04-13 16:50:10 +04:00
|
|
|
|
|
|
|
/* start fetch */
|
2005-01-02 01:05:21 +03:00
|
|
|
stylesheet_content = realloc(
|
|
|
|
c->data.html.stylesheet_content,
|
|
|
|
(i + 1) * sizeof
|
|
|
|
*c->data.html.stylesheet_content);
|
|
|
|
if (!stylesheet_content)
|
|
|
|
return false;
|
|
|
|
c->data.html.stylesheet_content = stylesheet_content;
|
2004-06-11 03:55:23 +04:00
|
|
|
c->data.html.stylesheet_content[i] = fetchcache(url,
|
|
|
|
html_convert_css_callback,
|
|
|
|
c, (void *) i, c->width, c->height,
|
2005-01-03 05:09:20 +03:00
|
|
|
true, 0, 0, false, false);
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!c->data.html.stylesheet_content[i])
|
|
|
|
return false;
|
|
|
|
c->active++;
|
|
|
|
fetchcache_go(c->data.html.stylesheet_content[i],
|
|
|
|
c->url,
|
|
|
|
html_convert_css_callback,
|
2005-01-25 02:02:37 +03:00
|
|
|
c, (void *) i, c->width, c->height,
|
2005-01-02 01:05:21 +03:00
|
|
|
0, 0, false);
|
2003-04-13 16:50:10 +04:00
|
|
|
free(url);
|
|
|
|
i++;
|
|
|
|
|
|
|
|
} else if (strcmp(node->name, "style") == 0) {
|
2003-06-17 23:24:21 +04:00
|
|
|
/* type='text/css', or not present (invalid but common) */
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((type = (char *) xmlGetProp(node, (const xmlChar *) "type")) != NULL) {
|
2003-06-17 23:24:21 +04:00
|
|
|
if (strcmp(type, "text/css") != 0) {
|
|
|
|
xmlFree(type);
|
|
|
|
continue;
|
|
|
|
}
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(type);
|
2003-04-13 16:50:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* media contains 'screen' or 'all' or not present */
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((media = (char *) xmlGetProp(node, (const xmlChar *) "media")) != NULL) {
|
2003-04-13 16:50:10 +04:00
|
|
|
if (strstr(media, "screen") == 0 &&
|
|
|
|
strstr(media, "all") == 0) {
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(media);
|
2003-04-13 16:50:10 +04:00
|
|
|
continue;
|
|
|
|
}
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(media);
|
2003-04-13 16:50:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create stylesheet */
|
|
|
|
LOG(("style element"));
|
2004-07-31 03:40:01 +04:00
|
|
|
if (c->data.html.stylesheet_content[STYLESHEET_STYLE] == 0) {
|
2003-12-27 23:15:23 +03:00
|
|
|
const char *params[] = { 0 };
|
2004-07-31 03:40:01 +04:00
|
|
|
c->data.html.stylesheet_content[STYLESHEET_STYLE] =
|
2004-06-11 03:55:23 +04:00
|
|
|
content_create(c->data.html.
|
|
|
|
base_url);
|
2004-07-31 03:40:01 +04:00
|
|
|
if (!c->data.html.stylesheet_content[STYLESHEET_STYLE])
|
2005-01-02 01:05:21 +03:00
|
|
|
return false;
|
2004-06-11 03:55:23 +04:00
|
|
|
if (!content_set_type(c->data.html.
|
2004-07-31 03:40:01 +04:00
|
|
|
stylesheet_content[STYLESHEET_STYLE],
|
2004-06-11 03:55:23 +04:00
|
|
|
CONTENT_CSS, "text/css",
|
|
|
|
params))
|
2005-01-02 01:05:21 +03:00
|
|
|
/** \todo not necessarily caused by
|
|
|
|
* memory exhaustion */
|
|
|
|
return false;
|
2003-06-17 23:24:21 +04:00
|
|
|
}
|
2003-04-15 21:53:00 +04:00
|
|
|
|
|
|
|
/* can't just use xmlNodeGetContent(node), because that won't give
|
|
|
|
* the content of comments which may be used to 'hide' the content */
|
|
|
|
for (node2 = node->children; node2 != 0; node2 = node2->next) {
|
|
|
|
data = xmlNodeGetContent(node2);
|
2004-06-11 03:55:23 +04:00
|
|
|
if (!content_process_data(c->data.html.
|
2004-07-31 03:40:01 +04:00
|
|
|
stylesheet_content[STYLESHEET_STYLE],
|
2004-06-11 03:55:23 +04:00
|
|
|
data, strlen(data))) {
|
|
|
|
xmlFree(data);
|
2005-01-02 01:05:21 +03:00
|
|
|
/** \todo not necessarily caused by
|
|
|
|
* memory exhaustion */
|
|
|
|
return false;
|
2004-06-11 03:55:23 +04:00
|
|
|
}
|
2003-04-25 12:03:15 +04:00
|
|
|
xmlFree(data);
|
2003-04-15 21:53:00 +04:00
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
}
|
2003-04-06 01:38:06 +04:00
|
|
|
|
2003-04-13 16:50:10 +04:00
|
|
|
c->data.html.stylesheet_count = i;
|
|
|
|
|
2004-07-31 03:40:01 +04:00
|
|
|
if (c->data.html.stylesheet_content[STYLESHEET_STYLE] != 0) {
|
|
|
|
if (css_convert(c->data.html.stylesheet_content[STYLESHEET_STYLE], c->width,
|
2003-10-17 21:39:29 +04:00
|
|
|
c->height)) {
|
2005-01-02 06:58:21 +03:00
|
|
|
if (!content_add_user(c->data.html.stylesheet_content[STYLESHEET_STYLE],
|
2004-06-11 03:55:23 +04:00
|
|
|
html_convert_css_callback,
|
2005-01-02 06:58:21 +03:00
|
|
|
c, (void *) STYLESHEET_STYLE)) {
|
|
|
|
/* no memory */
|
|
|
|
c->data.html.stylesheet_content[STYLESHEET_STYLE] = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2004-06-11 03:55:23 +04:00
|
|
|
} else {
|
2003-10-17 21:39:29 +04:00
|
|
|
/* conversion failed */
|
2004-07-31 03:40:01 +04:00
|
|
|
c->data.html.stylesheet_content[STYLESHEET_STYLE] = 0;
|
2003-10-17 21:39:29 +04:00
|
|
|
}
|
|
|
|
}
|
2003-04-15 21:53:00 +04:00
|
|
|
|
|
|
|
/* complete the fetches */
|
|
|
|
while (c->active != 0) {
|
2003-06-17 23:24:21 +04:00
|
|
|
if (c->active != last_active) {
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("FetchStyle"),
|
2004-02-27 20:45:19 +03:00
|
|
|
c->active);
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, msg_data);
|
2003-06-17 23:24:21 +04:00
|
|
|
last_active = c->active;
|
2003-04-15 21:53:00 +04:00
|
|
|
}
|
|
|
|
fetch_poll();
|
|
|
|
gui_multitask();
|
|
|
|
}
|
|
|
|
|
2004-06-11 00:41:26 +04:00
|
|
|
/* if (c->error) { */
|
|
|
|
/* content_set_status(c, "Warning: some stylesheets failed to load"); */
|
|
|
|
/* content_broadcast(c, CONTENT_MSG_STATUS, msg_data); */
|
|
|
|
/* } */
|
2005-01-02 01:05:21 +03:00
|
|
|
|
|
|
|
return true;
|
2003-04-15 21:53:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
/**
|
|
|
|
* Callback for fetchcache() for linked stylesheets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void html_convert_css_callback(content_msg msg, struct content *css,
|
2004-04-25 03:42:32 +04:00
|
|
|
void *p1, void *p2, union content_msg_data data)
|
2004-02-20 03:43:17 +03:00
|
|
|
{
|
|
|
|
struct content *c = p1;
|
|
|
|
unsigned int i = (unsigned int) p2;
|
2004-04-25 03:42:32 +04:00
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
switch (msg) {
|
|
|
|
case CONTENT_MSG_LOADING:
|
|
|
|
/* check that the stylesheet is really CSS */
|
|
|
|
if (css->type != CONTENT_CSS) {
|
|
|
|
c->data.html.stylesheet_content[i] = 0;
|
|
|
|
c->active--;
|
2004-06-11 00:41:26 +04:00
|
|
|
content_add_error(c, "NotCSS", 0);
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("NotCSS"));
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, data);
|
2004-02-20 03:43:17 +03:00
|
|
|
content_remove_user(css, html_convert_css_callback, c, (void*)i);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_READY:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_DONE:
|
|
|
|
LOG(("got stylesheet '%s'", css->url));
|
|
|
|
c->active--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_ERROR:
|
|
|
|
c->data.html.stylesheet_content[i] = 0;
|
|
|
|
c->active--;
|
2004-06-11 00:41:26 +04:00
|
|
|
content_add_error(c, "?", 0);
|
2004-02-20 03:43:17 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_STATUS:
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("FetchStyle2"),
|
2004-02-20 03:43:17 +03:00
|
|
|
c->active, css->status_message);
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, data);
|
2004-02-20 03:43:17 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_REDIRECT:
|
|
|
|
c->active--;
|
|
|
|
c->data.html.stylesheet_content[i] = fetchcache(
|
2004-06-11 03:55:23 +04:00
|
|
|
data.redirect,
|
2004-04-25 03:42:32 +04:00
|
|
|
html_convert_css_callback,
|
2004-06-11 03:55:23 +04:00
|
|
|
c, (void *) i, css->width, css->height,
|
2005-01-03 05:09:20 +03:00
|
|
|
true, 0, 0, false, false);
|
2004-06-11 03:55:23 +04:00
|
|
|
if (c->data.html.stylesheet_content[i]) {
|
2004-02-20 03:43:17 +03:00
|
|
|
c->active++;
|
2004-06-11 03:55:23 +04:00
|
|
|
fetchcache_go(c->data.html.stylesheet_content[i],
|
|
|
|
c->url,
|
|
|
|
html_convert_css_callback,
|
2005-01-25 02:02:37 +03:00
|
|
|
c, (void *) i, css->width,
|
|
|
|
css->height, 0, 0, false);
|
2004-06-11 03:55:23 +04:00
|
|
|
}
|
2004-02-20 03:43:17 +03:00
|
|
|
break;
|
|
|
|
|
2004-08-11 23:02:32 +04:00
|
|
|
case CONTENT_MSG_NEWPTR:
|
|
|
|
c->data.html.stylesheet_content[i] = css;
|
|
|
|
break;
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
#ifdef WITH_AUTH
|
|
|
|
case CONTENT_MSG_AUTH:
|
2004-06-13 00:08:47 +04:00
|
|
|
c->data.html.stylesheet_content[i] = 0;
|
2004-02-20 03:43:17 +03:00
|
|
|
c->active--;
|
2004-06-11 00:41:26 +04:00
|
|
|
content_add_error(c, "?", 0);
|
2004-06-13 00:08:47 +04:00
|
|
|
break;
|
2004-02-20 03:43:17 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a fetch for an object required by a page.
|
|
|
|
*
|
|
|
|
* \param c content structure
|
2004-06-10 03:13:55 +04:00
|
|
|
* \param url URL of object to fetch (not copied, must be on heap)
|
2004-02-20 03:43:17 +03:00
|
|
|
* \param box box that will contain the object
|
2004-06-10 03:13:55 +04:00
|
|
|
* \param permitted_types array of types, terminated by CONTENT_UNKNOWN,
|
2004-06-13 00:08:47 +04:00
|
|
|
* or 0 if all types except OTHER and UNKNOWN acceptable
|
2004-06-10 03:13:55 +04:00
|
|
|
* \param available_width estimate of width of object
|
|
|
|
* \param available_height estimate of height of object
|
2004-06-13 00:08:47 +04:00
|
|
|
* \param background this is a background image
|
2005-01-02 01:05:21 +03:00
|
|
|
* \return true on success, false on memory exhaustion
|
2004-02-20 03:43:17 +03:00
|
|
|
*/
|
|
|
|
|
2005-01-02 01:05:21 +03:00
|
|
|
bool html_fetch_object(struct content *c, char *url, struct box *box,
|
2004-05-21 18:26:59 +04:00
|
|
|
const content_type *permitted_types,
|
2004-06-10 03:13:55 +04:00
|
|
|
int available_width, int available_height,
|
|
|
|
bool background)
|
2003-04-15 21:53:00 +04:00
|
|
|
{
|
2003-06-17 23:24:21 +04:00
|
|
|
unsigned int i = c->data.html.object_count;
|
2005-01-02 01:05:21 +03:00
|
|
|
struct content_html_object *object;
|
2003-06-05 17:17:55 +04:00
|
|
|
|
2003-04-15 21:53:00 +04:00
|
|
|
/* add to object list */
|
2005-01-02 01:05:21 +03:00
|
|
|
object = realloc(c->data.html.object,
|
|
|
|
(i + 1) * sizeof *c->data.html.object);
|
|
|
|
if (!object)
|
|
|
|
return false;
|
|
|
|
c->data.html.object = object;
|
2003-06-17 23:24:21 +04:00
|
|
|
c->data.html.object[i].url = url;
|
|
|
|
c->data.html.object[i].box = box;
|
2004-01-24 20:08:16 +03:00
|
|
|
c->data.html.object[i].permitted_types = permitted_types;
|
2004-06-10 03:13:55 +04:00
|
|
|
c->data.html.object[i].background = background;
|
2005-01-02 01:05:21 +03:00
|
|
|
c->data.html.object_count++;
|
2003-04-15 21:53:00 +04:00
|
|
|
|
|
|
|
/* start fetch */
|
2004-06-11 03:55:23 +04:00
|
|
|
c->data.html.object[i].content = fetchcache(url, html_object_callback,
|
|
|
|
c, (void *) i, available_width, available_height,
|
2005-01-03 05:09:20 +03:00
|
|
|
true, 0, 0, false, false);
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!c->data.html.object[i].content)
|
|
|
|
return false;
|
|
|
|
c->active++;
|
|
|
|
fetchcache_go(c->data.html.object[i].content, c->url,
|
|
|
|
html_object_callback, c, (void *) i,
|
2005-01-25 02:02:37 +03:00
|
|
|
available_width, available_height,
|
2005-01-02 01:05:21 +03:00
|
|
|
0, 0, false);
|
|
|
|
|
|
|
|
return true;
|
2003-04-15 21:53:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
/**
|
|
|
|
* Callback for fetchcache() for objects.
|
|
|
|
*/
|
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
void html_object_callback(content_msg msg, struct content *object,
|
2004-04-25 03:42:32 +04:00
|
|
|
void *p1, void *p2, union content_msg_data data)
|
2003-04-15 21:53:00 +04:00
|
|
|
{
|
2003-06-17 23:24:21 +04:00
|
|
|
struct content *c = p1;
|
|
|
|
unsigned int i = (unsigned int) p2;
|
2004-04-25 03:42:32 +04:00
|
|
|
int x, y;
|
2003-04-15 21:53:00 +04:00
|
|
|
struct box *box = c->data.html.object[i].box;
|
2004-01-24 20:08:16 +03:00
|
|
|
|
2003-04-15 21:53:00 +04:00
|
|
|
switch (msg) {
|
2003-06-17 23:24:21 +04:00
|
|
|
case CONTENT_MSG_LOADING:
|
2004-01-24 20:08:16 +03:00
|
|
|
/* check if the type is acceptable for this object */
|
|
|
|
if (html_object_type_permitted(object->type,
|
2004-08-12 02:08:26 +04:00
|
|
|
c->data.html.object[i].permitted_types)) {
|
|
|
|
if (c->data.html.bw)
|
|
|
|
content_open(object,
|
|
|
|
c->data.html.bw, c,
|
|
|
|
box,
|
|
|
|
box->object_params);
|
2004-01-24 20:08:16 +03:00
|
|
|
break;
|
2004-08-12 02:08:26 +04:00
|
|
|
}
|
2004-01-24 20:08:16 +03:00
|
|
|
|
|
|
|
/* not acceptable */
|
|
|
|
c->data.html.object[i].content = 0;
|
|
|
|
c->active--;
|
2004-06-11 00:41:26 +04:00
|
|
|
content_add_error(c, "?", 0);
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("BadObject"));
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, data);
|
2004-01-24 20:08:16 +03:00
|
|
|
content_remove_user(object, html_object_callback, c, (void*)i);
|
2003-06-17 23:24:21 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_READY:
|
2004-05-21 18:26:59 +04:00
|
|
|
if (object->type == CONTENT_HTML) {
|
2004-06-11 03:55:23 +04:00
|
|
|
html_object_done(box, object,
|
|
|
|
c->data.html.object[i].background);
|
|
|
|
if (c->status == CONTENT_STATUS_READY ||
|
|
|
|
c->status ==
|
|
|
|
CONTENT_STATUS_DONE)
|
|
|
|
content_reformat(c,
|
|
|
|
c->available_width, 0);
|
2004-05-21 18:26:59 +04:00
|
|
|
}
|
2003-06-17 23:24:21 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_DONE:
|
2004-06-09 23:55:06 +04:00
|
|
|
html_object_done(box, object, c->data.html.object[i].background);
|
2003-04-15 21:53:00 +04:00
|
|
|
c->active--;
|
|
|
|
break;
|
2003-06-17 23:24:21 +04:00
|
|
|
|
|
|
|
case CONTENT_MSG_ERROR:
|
|
|
|
c->data.html.object[i].content = 0;
|
|
|
|
c->active--;
|
2004-06-11 00:41:26 +04:00
|
|
|
content_add_error(c, "?", 0);
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("ObjError"),
|
|
|
|
data.error);
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, data);
|
2003-06-17 23:24:21 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_STATUS:
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("FetchObjs2"),
|
2003-06-17 23:24:21 +04:00
|
|
|
c->active, object->status_message);
|
2003-10-01 20:12:04 +04:00
|
|
|
/* content_broadcast(c, CONTENT_MSG_STATUS, 0); */
|
2003-04-15 21:53:00 +04:00
|
|
|
break;
|
2003-06-17 23:24:21 +04:00
|
|
|
|
2003-06-26 15:41:26 +04:00
|
|
|
case CONTENT_MSG_REDIRECT:
|
|
|
|
c->active--;
|
|
|
|
free(c->data.html.object[i].url);
|
2005-01-02 01:05:21 +03:00
|
|
|
c->data.html.object[i].url = strdup(data.redirect);
|
|
|
|
if (!c->data.html.object[i].url) {
|
|
|
|
/** \todo report oom */
|
|
|
|
} else {
|
|
|
|
c->data.html.object[i].content = fetchcache(
|
|
|
|
data.redirect,
|
|
|
|
html_object_callback,
|
|
|
|
c, (void * ) i, 0, 0, true,
|
2005-01-03 05:09:20 +03:00
|
|
|
0, 0, false, false);
|
2005-01-02 01:05:21 +03:00
|
|
|
if (!c->data.html.object[i].content) {
|
|
|
|
/** \todo report oom */
|
|
|
|
} else {
|
|
|
|
c->active++;
|
|
|
|
fetchcache_go(c->data.html.object[i].
|
|
|
|
content,
|
|
|
|
c->url,
|
|
|
|
html_object_callback,
|
|
|
|
c, (void * ) i,
|
2005-01-25 02:02:37 +03:00
|
|
|
0, 0,
|
2005-01-02 01:05:21 +03:00
|
|
|
0, 0, false);
|
|
|
|
}
|
2003-09-28 21:37:19 +04:00
|
|
|
}
|
2003-07-08 02:10:51 +04:00
|
|
|
break;
|
2003-06-26 15:41:26 +04:00
|
|
|
|
2003-09-11 01:44:11 +04:00
|
|
|
case CONTENT_MSG_REFORMAT:
|
|
|
|
break;
|
|
|
|
|
2004-03-21 15:50:10 +03:00
|
|
|
case CONTENT_MSG_REDRAW:
|
2004-04-25 03:42:32 +04:00
|
|
|
box_coords(box, &x, &y);
|
2004-06-21 03:09:52 +04:00
|
|
|
if (object == data.redraw.object) {
|
2004-04-26 17:47:51 +04:00
|
|
|
data.redraw.x = data.redraw.x *
|
2004-06-21 03:09:52 +04:00
|
|
|
box->width / object->width;
|
2004-04-26 17:47:51 +04:00
|
|
|
data.redraw.y = data.redraw.y *
|
2004-06-21 03:09:52 +04:00
|
|
|
box->height / object->height;
|
2004-04-26 17:47:51 +04:00
|
|
|
data.redraw.width = data.redraw.width *
|
2004-06-21 03:09:52 +04:00
|
|
|
box->width / object->width;
|
2004-04-26 17:47:51 +04:00
|
|
|
data.redraw.height = data.redraw.height *
|
2004-06-21 03:09:52 +04:00
|
|
|
box->height / object->height;
|
2004-04-26 17:47:51 +04:00
|
|
|
data.redraw.object_width = box->width;
|
|
|
|
data.redraw.object_height = box->height;
|
|
|
|
}
|
|
|
|
data.redraw.x += x + box->padding[LEFT];
|
|
|
|
data.redraw.y += y + box->padding[TOP];
|
|
|
|
data.redraw.object_x += x + box->padding[LEFT];
|
|
|
|
data.redraw.object_y += y + box->padding[TOP];
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_REDRAW, data);
|
2004-03-21 15:50:10 +03:00
|
|
|
break;
|
|
|
|
|
2004-08-11 23:02:32 +04:00
|
|
|
case CONTENT_MSG_NEWPTR:
|
|
|
|
c->data.html.object[i].content = object;
|
|
|
|
break;
|
|
|
|
|
2004-01-05 05:10:59 +03:00
|
|
|
#ifdef WITH_AUTH
|
2003-10-25 23:20:13 +04:00
|
|
|
case CONTENT_MSG_AUTH:
|
2004-06-13 00:08:47 +04:00
|
|
|
c->data.html.object[i].content = 0;
|
2003-10-26 02:51:45 +04:00
|
|
|
c->active--;
|
2004-06-11 00:41:26 +04:00
|
|
|
content_add_error(c, "?", 0);
|
2004-06-13 00:08:47 +04:00
|
|
|
break;
|
2004-01-05 05:10:59 +03:00
|
|
|
#endif
|
2003-10-25 23:20:13 +04:00
|
|
|
|
2003-04-15 21:53:00 +04:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2003-06-17 23:24:21 +04:00
|
|
|
|
2004-05-21 18:26:59 +04:00
|
|
|
if (c->status == CONTENT_STATUS_READY && c->active == 0 &&
|
|
|
|
(msg == CONTENT_MSG_LOADING ||
|
|
|
|
msg == CONTENT_MSG_DONE ||
|
|
|
|
msg == CONTENT_MSG_ERROR ||
|
|
|
|
msg == CONTENT_MSG_REDIRECT ||
|
|
|
|
msg == CONTENT_MSG_AUTH)) {
|
2003-06-17 23:24:21 +04:00
|
|
|
/* all objects have arrived */
|
|
|
|
content_reformat(c, c->available_width, 0);
|
|
|
|
c->status = CONTENT_STATUS_DONE;
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("Done"));
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_DONE, data);
|
2003-06-17 23:24:21 +04:00
|
|
|
}
|
2003-06-30 19:56:35 +04:00
|
|
|
if (c->status == CONTENT_STATUS_READY)
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("FetchObjs"), c->active);
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-21 18:26:59 +04:00
|
|
|
/**
|
|
|
|
* Update a box whose content has completed rendering.
|
|
|
|
*/
|
|
|
|
|
2004-06-09 23:55:06 +04:00
|
|
|
void html_object_done(struct box *box, struct content *object,
|
2004-06-13 00:08:47 +04:00
|
|
|
bool background)
|
2004-05-21 18:26:59 +04:00
|
|
|
{
|
|
|
|
struct box *b;
|
|
|
|
|
2004-06-13 00:08:47 +04:00
|
|
|
if (background) {
|
|
|
|
box->background = object;
|
2004-06-25 03:29:31 +04:00
|
|
|
return;
|
2004-06-13 00:08:47 +04:00
|
|
|
}
|
2004-06-25 03:29:31 +04:00
|
|
|
|
|
|
|
box->object = object;
|
2004-05-21 18:26:59 +04:00
|
|
|
|
|
|
|
if (box->width != UNKNOWN_WIDTH &&
|
|
|
|
object->available_width != box->width)
|
|
|
|
content_reformat(object, box->width, box->height);
|
|
|
|
|
|
|
|
/* invalidate parent min, max widths */
|
|
|
|
for (b = box->parent; b; b = b->parent)
|
|
|
|
b->max_width = UNKNOWN_MAX_WIDTH;
|
|
|
|
|
|
|
|
/* delete any clones of this box */
|
|
|
|
while (box->next && box->next->clone) {
|
|
|
|
/* box_free_box(box->next); */
|
|
|
|
box->next = box->next->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-24 20:08:16 +03:00
|
|
|
/**
|
|
|
|
* Check if a type is in a list.
|
|
|
|
*
|
|
|
|
* \param type the content_type to search for
|
|
|
|
* \param permitted_types array of types, terminated by CONTENT_UNKNOWN,
|
2004-06-13 00:08:47 +04:00
|
|
|
* or 0 if all types except OTHER and UNKNOWN acceptable
|
2004-01-24 20:08:16 +03:00
|
|
|
* \return the type is in the list or acceptable
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool html_object_type_permitted(const content_type type,
|
|
|
|
const content_type *permitted_types)
|
|
|
|
{
|
|
|
|
if (permitted_types) {
|
|
|
|
for (; *permitted_types != CONTENT_UNKNOWN; permitted_types++)
|
|
|
|
if (*permitted_types == type)
|
|
|
|
return true;
|
|
|
|
} else if (type < CONTENT_OTHER) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-21 03:09:52 +04:00
|
|
|
/**
|
|
|
|
* Stop loading a CONTENT_HTML in state READY.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void html_stop(struct content *c)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
struct content *object;
|
|
|
|
|
|
|
|
assert(c->status == CONTENT_STATUS_READY);
|
|
|
|
|
|
|
|
for (i = 0; i != c->data.html.object_count; i++) {
|
|
|
|
object = c->data.html.object[i].content;
|
|
|
|
if (!object)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (object->status == CONTENT_STATUS_DONE)
|
|
|
|
; /* already loaded: do nothing */
|
|
|
|
else if (object->status == CONTENT_STATUS_READY)
|
|
|
|
content_stop(object, html_object_callback,
|
|
|
|
c, (void *) i);
|
|
|
|
else {
|
|
|
|
content_remove_user(c->data.html.object[i].content,
|
|
|
|
html_object_callback, c, (void *) i);
|
|
|
|
c->data.html.object[i].content = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->status = CONTENT_STATUS_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
/**
|
|
|
|
* Reformat a CONTENT_HTML to a new width.
|
|
|
|
*/
|
|
|
|
|
2004-06-11 00:41:26 +04:00
|
|
|
void html_reformat(struct content *c, int width, int height)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2004-10-18 01:10:19 +04:00
|
|
|
layout_document(c->data.html.layout, width,
|
2004-06-03 01:09:14 +04:00
|
|
|
c->data.html.box_pool);
|
2004-10-18 01:10:19 +04:00
|
|
|
c->width = c->data.html.layout->descendant_x1;
|
|
|
|
c->height = c->data.html.layout->descendant_y1;
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
/**
|
|
|
|
* Destroy a CONTENT_HTML and free all resources it owns.
|
|
|
|
*/
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
void html_destroy(struct content *c)
|
|
|
|
{
|
2003-04-15 21:53:00 +04:00
|
|
|
unsigned int i;
|
2003-02-09 15:58:15 +03:00
|
|
|
LOG(("content %p", c));
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
free(c->title);
|
|
|
|
|
2004-06-13 00:08:47 +04:00
|
|
|
imagemap_destroy(c);
|
2004-03-27 01:16:31 +03:00
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
if (c->data.html.parser)
|
|
|
|
htmlFreeParserCtxt(c->data.html.parser);
|
|
|
|
|
2004-05-09 20:49:53 +04:00
|
|
|
free(c->data.html.encoding);
|
|
|
|
|
2004-02-20 03:43:17 +03:00
|
|
|
free(c->data.html.base_url);
|
|
|
|
|
|
|
|
if (c->data.html.layout)
|
|
|
|
box_free(c->data.html.layout);
|
|
|
|
|
|
|
|
/* Free stylesheets */
|
|
|
|
if (c->data.html.stylesheet_count) {
|
2004-06-21 19:09:59 +04:00
|
|
|
for (i = 0; i != c->data.html.stylesheet_count; i++) {
|
2004-02-20 03:43:17 +03:00
|
|
|
if (c->data.html.stylesheet_content[i])
|
2004-06-21 19:09:59 +04:00
|
|
|
content_remove_user(c->data.html.
|
|
|
|
stylesheet_content[i],
|
|
|
|
html_convert_css_callback,
|
|
|
|
c, (void *) i);
|
|
|
|
}
|
2004-02-20 03:43:17 +03:00
|
|
|
}
|
|
|
|
free(c->data.html.stylesheet_content);
|
2005-01-17 00:32:10 +03:00
|
|
|
|
|
|
|
if (c->data.html.style)
|
|
|
|
css_free_style(c->data.html.style);
|
2004-02-20 03:43:17 +03:00
|
|
|
|
|
|
|
/* Free objects */
|
2003-07-10 01:33:01 +04:00
|
|
|
for (i = 0; i != c->data.html.object_count; i++) {
|
|
|
|
LOG(("object %i %p", i, c->data.html.object[i].content));
|
2004-02-20 03:43:17 +03:00
|
|
|
if (c->data.html.object[i].content)
|
2003-07-10 01:33:01 +04:00
|
|
|
content_remove_user(c->data.html.object[i].content,
|
2003-12-27 05:03:48 +03:00
|
|
|
html_object_callback, c, (void*)i);
|
2003-07-10 01:33:01 +04:00
|
|
|
free(c->data.html.object[i].url);
|
|
|
|
}
|
|
|
|
free(c->data.html.object);
|
|
|
|
|
2004-01-02 15:04:04 +03:00
|
|
|
pool_destroy(c->data.html.string_pool);
|
|
|
|
pool_destroy(c->data.html.box_pool);
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
2004-08-12 02:08:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a window containing a CONTENT_HTML being opened.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void html_open(struct content *c, struct browser_window *bw,
|
|
|
|
struct content *page, struct box *box,
|
|
|
|
struct object_params *params)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
c->data.html.bw = bw;
|
|
|
|
for (i = 0; i != c->data.html.object_count; i++) {
|
|
|
|
if (c->data.html.object[i].content == 0)
|
|
|
|
continue;
|
|
|
|
if (c->data.html.object[i].content->type == CONTENT_UNKNOWN)
|
|
|
|
continue;
|
|
|
|
content_open(c->data.html.object[i].content,
|
|
|
|
bw, c,
|
|
|
|
c->data.html.object[i].box,
|
|
|
|
c->data.html.object[i].box->object_params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a window containing a CONTENT_HTML being closed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void html_close(struct content *c)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
c->data.html.bw = 0;
|
|
|
|
for (i = 0; i != c->data.html.object_count; i++) {
|
|
|
|
if (c->data.html.object[i].content == 0)
|
|
|
|
continue;
|
|
|
|
if (c->data.html.object[i].content->type == CONTENT_UNKNOWN)
|
|
|
|
continue;
|
|
|
|
content_close(c->data.html.object[i].content);
|
|
|
|
}
|
|
|
|
}
|