2003-06-30 16:44:03 +04:00
|
|
|
/*
|
2009-07-24 03:05:34 +04:00
|
|
|
* Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-04-04 19:19:32 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2009-07-24 03:05:34 +04:00
|
|
|
|
|
|
|
#include <libwapcaplet/libwapcaplet.h>
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/content_protected.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "content/fetch.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/hlcache.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "css/css.h"
|
2009-07-24 03:05:34 +04:00
|
|
|
#include "css/internal.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "desktop/gui.h"
|
2009-07-24 03:05:34 +04:00
|
|
|
#include "render/html.h"
|
2010-04-30 20:06:03 +04:00
|
|
|
#include "utils/utils.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "utils/http.h"
|
2010-07-01 23:42:02 +04:00
|
|
|
#include "utils/log.h"
|
2009-07-26 01:42:27 +04:00
|
|
|
#include "utils/messages.h"
|
2011-05-07 00:40:09 +04:00
|
|
|
#include "utils/talloc.h"
|
2005-05-22 03:30:19 +04:00
|
|
|
|
2011-02-27 23:11:39 +03:00
|
|
|
/* Define to trace import fetches */
|
|
|
|
#undef NSCSS_IMPORT_TRACE
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
/**
|
|
|
|
* CSS content data
|
|
|
|
*/
|
|
|
|
typedef struct nscss_content
|
|
|
|
{
|
|
|
|
struct content base; /**< Underlying content object */
|
|
|
|
|
|
|
|
struct content_css_data data; /**< CSS data */
|
|
|
|
} nscss_content;
|
|
|
|
|
2010-04-11 14:52:18 +04:00
|
|
|
/**
|
|
|
|
* Context for import fetches
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
struct content_css_data *css; /**< Object containing import */
|
2011-02-26 03:58:54 +03:00
|
|
|
uint32_t index; /**< Index into parent sheet's
|
|
|
|
* imports array */
|
2010-04-11 14:52:18 +04:00
|
|
|
} nscss_import_ctx;
|
|
|
|
|
2011-09-16 02:47:50 +04:00
|
|
|
static void nscss_fini(void);
|
2011-05-07 00:40:09 +04:00
|
|
|
static nserror nscss_create(const content_handler *handler,
|
|
|
|
lwc_string *imime_type, const http_parameter *params,
|
|
|
|
llcache_handle *llcache, const char *fallback_charset,
|
|
|
|
bool quirks, struct content **c);
|
|
|
|
static bool nscss_process_data(struct content *c, const char *data,
|
|
|
|
unsigned int size);
|
|
|
|
static bool nscss_convert(struct content *c);
|
|
|
|
static void nscss_destroy(struct content *c);
|
|
|
|
static nserror nscss_clone(const struct content *old, struct content **newc);
|
|
|
|
static bool nscss_matches_quirks(const struct content *c, bool quirks);
|
2011-09-03 13:27:42 +04:00
|
|
|
static content_type nscss_content_type(void);
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2010-04-11 14:52:18 +04:00
|
|
|
static void nscss_content_done(struct content_css_data *css, void *pw);
|
2011-02-26 03:58:54 +03:00
|
|
|
static css_error nscss_handle_import(void *pw, css_stylesheet *parent,
|
|
|
|
lwc_string *url, uint64_t media);
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror nscss_import(hlcache_handle *handle,
|
|
|
|
const hlcache_event *event, void *pw);
|
2011-02-26 03:58:54 +03:00
|
|
|
static css_error nscss_import_complete(nscss_import_ctx *ctx);
|
|
|
|
|
|
|
|
static css_error nscss_register_imports(struct content_css_data *c);
|
|
|
|
static css_error nscss_register_import(struct content_css_data *c,
|
|
|
|
const hlcache_handle *import);
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
static const content_handler css_content_handler = {
|
2011-09-16 02:47:50 +04:00
|
|
|
.fini = nscss_fini,
|
2011-05-09 19:32:34 +04:00
|
|
|
.create = nscss_create,
|
|
|
|
.process_data = nscss_process_data,
|
|
|
|
.data_complete = nscss_convert,
|
|
|
|
.destroy = nscss_destroy,
|
|
|
|
.clone = nscss_clone,
|
|
|
|
.matches_quirks = nscss_matches_quirks,
|
|
|
|
.type = nscss_content_type,
|
|
|
|
.no_share = false,
|
2011-05-07 00:40:09 +04:00
|
|
|
};
|
|
|
|
|
2011-07-08 12:38:17 +04:00
|
|
|
static lwc_string *css_charset;
|
2011-02-26 04:19:11 +03:00
|
|
|
static css_stylesheet *blank_import;
|
|
|
|
|
|
|
|
/**
|
2011-05-07 00:40:09 +04:00
|
|
|
* Initialise the CSS content handler
|
2011-02-26 04:19:11 +03:00
|
|
|
*/
|
2011-09-16 02:47:50 +04:00
|
|
|
nserror nscss_init(void)
|
2011-02-26 04:19:11 +03:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
lwc_error lerror;
|
|
|
|
nserror error;
|
|
|
|
|
2011-07-08 12:38:17 +04:00
|
|
|
lerror = lwc_intern_string("charset", SLEN("charset"), &css_charset);
|
|
|
|
if (lerror != lwc_error_ok) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2011-09-16 02:31:16 +04:00
|
|
|
error = content_factory_register_handler("text/css",
|
2011-05-07 00:40:09 +04:00
|
|
|
&css_content_handler);
|
2011-07-08 12:38:17 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
lwc_string_unref(css_charset);
|
|
|
|
}
|
2011-05-07 00:40:09 +04:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up after the CSS content handler
|
|
|
|
*/
|
2011-09-16 02:47:50 +04:00
|
|
|
void nscss_fini(void)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
2011-09-16 02:31:16 +04:00
|
|
|
if (css_charset != NULL) {
|
|
|
|
lwc_string_unref(css_charset);
|
|
|
|
css_charset = NULL;
|
|
|
|
}
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2011-09-16 02:31:16 +04:00
|
|
|
if (blank_import != NULL) {
|
2011-02-26 04:19:11 +03:00
|
|
|
css_stylesheet_destroy(blank_import);
|
2011-09-16 02:31:16 +04:00
|
|
|
blank_import = NULL;
|
|
|
|
}
|
2011-02-26 04:19:11 +03:00
|
|
|
}
|
2005-05-22 03:30:19 +04:00
|
|
|
|
2005-01-16 03:03:45 +03:00
|
|
|
/**
|
2009-07-24 03:05:34 +04:00
|
|
|
* Initialise a CSS content
|
2005-01-16 03:03:45 +03:00
|
|
|
*
|
2009-07-24 03:05:34 +04:00
|
|
|
* \param c Content to initialise
|
|
|
|
* \param params Content-Type parameters
|
|
|
|
* \return true on success, false on failure
|
2005-01-16 03:03:45 +03:00
|
|
|
*/
|
2011-05-07 00:40:09 +04:00
|
|
|
nserror nscss_create(const content_handler *handler,
|
|
|
|
lwc_string *imime_type, const http_parameter *params,
|
|
|
|
llcache_handle *llcache, const char *fallback_charset,
|
|
|
|
bool quirks, struct content **c)
|
2005-01-16 03:03:45 +03:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
nscss_content *result;
|
2009-07-27 17:56:05 +04:00
|
|
|
const char *charset = NULL;
|
2011-07-08 12:38:17 +04:00
|
|
|
lwc_string *charset_value = NULL;
|
2009-07-26 01:42:27 +04:00
|
|
|
union content_msg_data msg_data;
|
2010-03-28 16:56:39 +04:00
|
|
|
nserror error;
|
2005-05-22 03:30:19 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
result = talloc_zero(0, nscss_content);
|
|
|
|
if (result == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
error = content__init(&result->base, handler, imime_type,
|
|
|
|
params, llcache, fallback_charset, quirks);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
talloc_free(result);
|
|
|
|
return error;
|
|
|
|
}
|
2005-01-16 03:48:47 +03:00
|
|
|
|
2009-07-27 17:56:05 +04:00
|
|
|
/* Find charset specified on HTTP layer, if any */
|
2011-07-08 12:38:17 +04:00
|
|
|
error = http_parameter_list_find_item(params, css_charset,
|
|
|
|
&charset_value);
|
|
|
|
if (error != NSERROR_OK || lwc_string_length(charset_value) == 0) {
|
2010-03-28 16:56:39 +04:00
|
|
|
/* No charset specified, use fallback, if any */
|
|
|
|
/** \todo libcss will take this as gospel, which is wrong */
|
2011-05-07 00:40:09 +04:00
|
|
|
charset = fallback_charset;
|
2011-07-08 12:38:17 +04:00
|
|
|
} else {
|
|
|
|
charset = lwc_string_data(charset_value);
|
2009-07-27 17:56:05 +04:00
|
|
|
}
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
error = nscss_create_css_data(&result->data,
|
2011-12-04 18:55:23 +04:00
|
|
|
nsurl_access(content_get_url(&result->base)),
|
2011-05-07 00:40:09 +04:00
|
|
|
charset, result->base.quirks,
|
|
|
|
nscss_content_done, result);
|
|
|
|
if (error != NSERROR_OK) {
|
2010-03-28 16:56:39 +04:00
|
|
|
msg_data.error = messages_get("NoMemory");
|
2011-05-07 00:40:09 +04:00
|
|
|
content_broadcast(&result->base, CONTENT_MSG_ERROR, msg_data);
|
2011-07-08 12:38:17 +04:00
|
|
|
if (charset_value != NULL)
|
|
|
|
lwc_string_unref(charset_value);
|
2011-05-07 00:40:09 +04:00
|
|
|
talloc_free(result);
|
|
|
|
return error;
|
2005-01-17 00:39:21 +03:00
|
|
|
}
|
2005-05-22 03:30:19 +04:00
|
|
|
|
2011-07-08 12:38:17 +04:00
|
|
|
if (charset_value != NULL)
|
|
|
|
lwc_string_unref(charset_value);
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
*c = (struct content *) result;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
2005-01-17 00:39:21 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Create a struct content_css_data, creating a stylesheet object
|
|
|
|
*
|
|
|
|
* \param c Struct to populate
|
|
|
|
* \param url URL of stylesheet
|
|
|
|
* \param charset Stylesheet charset
|
|
|
|
* \param quirks Stylesheet quirks mode
|
2011-02-26 03:58:54 +03:00
|
|
|
* \param done Callback to call when content has completed
|
|
|
|
* \param pw Client data for \a done
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, NSERROR_NOMEM on memory exhaustion
|
|
|
|
*/
|
|
|
|
nserror nscss_create_css_data(struct content_css_data *c,
|
2011-02-26 03:58:54 +03:00
|
|
|
const char *url, const char *charset, bool quirks,
|
|
|
|
nscss_done_callback done, void *pw)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
css_error error;
|
2011-01-29 17:53:18 +03:00
|
|
|
css_stylesheet_params params;
|
2003-04-04 19:19:32 +04:00
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
c->pw = pw;
|
|
|
|
c->done = done;
|
|
|
|
c->next_to_register = (uint32_t) -1;
|
2010-03-28 16:56:39 +04:00
|
|
|
c->import_count = 0;
|
|
|
|
c->imports = NULL;
|
2010-04-08 19:40:17 +04:00
|
|
|
if (charset != NULL)
|
|
|
|
c->charset = strdup(charset);
|
2010-04-08 19:41:36 +04:00
|
|
|
else
|
|
|
|
c->charset = NULL;
|
2009-07-24 03:05:34 +04:00
|
|
|
|
2011-03-13 12:32:13 +03:00
|
|
|
params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1;
|
2011-01-29 17:53:18 +03:00
|
|
|
params.level = CSS_LEVEL_DEFAULT;
|
|
|
|
params.charset = charset;
|
|
|
|
params.url = url;
|
|
|
|
params.title = NULL;
|
|
|
|
params.allow_quirks = quirks;
|
|
|
|
params.inline_style = false;
|
|
|
|
params.resolve = nscss_resolve_url;
|
|
|
|
params.resolve_pw = NULL;
|
2011-02-26 03:58:54 +03:00
|
|
|
params.import = nscss_handle_import;
|
|
|
|
params.import_pw = c;
|
2011-01-30 16:40:47 +03:00
|
|
|
params.color = gui_system_colour;
|
2011-01-29 17:53:18 +03:00
|
|
|
params.color_pw = NULL;
|
2011-02-03 12:31:11 +03:00
|
|
|
params.font = NULL;
|
|
|
|
params.font_pw = NULL;
|
2011-01-29 17:53:18 +03:00
|
|
|
|
|
|
|
error = css_stylesheet_create(¶ms, ns_realloc, NULL, &c->sheet);
|
2009-07-24 03:05:34 +04:00
|
|
|
if (error != CSS_OK) {
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_NOMEM;
|
2009-07-24 03:05:34 +04:00
|
|
|
}
|
2005-01-05 23:22:57 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
2003-04-04 19:19:32 +04:00
|
|
|
}
|
|
|
|
|
2004-05-01 21:48:38 +04:00
|
|
|
/**
|
2009-07-24 03:05:34 +04:00
|
|
|
* Process CSS source data
|
2004-05-01 21:48:38 +04:00
|
|
|
*
|
2009-07-24 03:05:34 +04:00
|
|
|
* \param c Content structure
|
|
|
|
* \param data Data to process
|
|
|
|
* \param size Number of bytes to process
|
|
|
|
* \return true on success, false on failure
|
2004-05-01 21:48:38 +04:00
|
|
|
*/
|
2010-04-03 20:37:36 +04:00
|
|
|
bool nscss_process_data(struct content *c, const char *data, unsigned int size)
|
2003-04-04 19:19:32 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
nscss_content *css = (nscss_content *) c;
|
2009-07-26 01:42:27 +04:00
|
|
|
union content_msg_data msg_data;
|
2009-07-24 03:05:34 +04:00
|
|
|
css_error error;
|
2003-04-04 19:19:32 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
error = nscss_process_css_data(&css->data, data, size);
|
2009-07-26 01:42:27 +04:00
|
|
|
if (error != CSS_OK && error != CSS_NEEDDATA) {
|
|
|
|
msg_data.error = "?";
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
}
|
|
|
|
|
2009-07-24 03:05:34 +04:00
|
|
|
return (error == CSS_OK || error == CSS_NEEDDATA);
|
2004-05-01 21:48:38 +04:00
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Process CSS data
|
|
|
|
*
|
|
|
|
* \param c CSS content object
|
|
|
|
* \param data Data to process
|
|
|
|
* \param size Number of bytes to process
|
|
|
|
* \return CSS_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2010-04-03 20:37:36 +04:00
|
|
|
css_error nscss_process_css_data(struct content_css_data *c, const char *data,
|
2010-03-28 16:56:39 +04:00
|
|
|
unsigned int size)
|
|
|
|
{
|
|
|
|
return css_stylesheet_append_data(c->sheet,
|
|
|
|
(const uint8_t *) data, size);
|
|
|
|
}
|
|
|
|
|
2004-05-01 21:48:38 +04:00
|
|
|
/**
|
2009-07-24 03:05:34 +04:00
|
|
|
* Convert a CSS content ready for use
|
2004-05-01 21:48:38 +04:00
|
|
|
*
|
2009-07-24 03:05:34 +04:00
|
|
|
* \param c Content to convert
|
|
|
|
* \return true on success, false on failure
|
2004-05-01 21:48:38 +04:00
|
|
|
*/
|
2010-03-30 02:33:21 +04:00
|
|
|
bool nscss_convert(struct content *c)
|
2003-04-06 22:09:34 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
nscss_content *css = (nscss_content *) c;
|
2009-07-26 01:42:27 +04:00
|
|
|
union content_msg_data msg_data;
|
2009-07-24 03:05:34 +04:00
|
|
|
css_error error;
|
2003-04-06 22:09:34 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
error = nscss_convert_css_data(&css->data);
|
2010-03-28 16:56:39 +04:00
|
|
|
if (error != CSS_OK) {
|
|
|
|
msg_data.error = "?";
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-11 14:52:18 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Convert CSS data ready for use
|
|
|
|
*
|
2010-04-11 14:52:18 +04:00
|
|
|
* \param c CSS data to convert
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return CSS error
|
|
|
|
*/
|
2011-02-26 03:58:54 +03:00
|
|
|
css_error nscss_convert_css_data(struct content_css_data *c)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
css_error error;
|
|
|
|
|
|
|
|
error = css_stylesheet_data_done(c->sheet);
|
2003-04-06 22:09:34 +04:00
|
|
|
|
2009-07-24 03:05:34 +04:00
|
|
|
/* Process pending imports */
|
2010-04-11 14:52:18 +04:00
|
|
|
if (error == CSS_IMPORTS_PENDING) {
|
2011-02-26 03:58:54 +03:00
|
|
|
/* We must not have registered any imports yet */
|
|
|
|
assert(c->next_to_register == (uint32_t) -1);
|
2004-05-01 21:48:38 +04:00
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
/* Start registering, until we find one that
|
|
|
|
* hasn't finished fetching */
|
|
|
|
c->next_to_register = 0;
|
|
|
|
error = nscss_register_imports(c);
|
2010-07-01 23:42:02 +04:00
|
|
|
} else if (error == CSS_OK) {
|
|
|
|
/* No imports, and no errors, so complete conversion */
|
2011-02-26 03:58:54 +03:00
|
|
|
c->done(c, c->pw);
|
2010-07-01 23:42:02 +04:00
|
|
|
} else {
|
|
|
|
const char *url;
|
|
|
|
|
|
|
|
if (css_stylesheet_get_url(c->sheet, &url) == CSS_OK) {
|
|
|
|
LOG(("Failed converting %p %s (%d)", c, url, error));
|
|
|
|
} else {
|
|
|
|
LOG(("Failed converting %p (%d)", c, error));
|
|
|
|
}
|
2009-07-24 03:05:34 +04:00
|
|
|
}
|
2003-06-17 23:24:21 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return error;
|
2003-04-06 22:09:34 +04:00
|
|
|
}
|
|
|
|
|
2004-08-05 05:57:14 +04:00
|
|
|
/**
|
2009-07-24 03:05:34 +04:00
|
|
|
* Clean up a CSS content
|
2005-05-22 03:30:19 +04:00
|
|
|
*
|
2009-07-24 03:05:34 +04:00
|
|
|
* \param c Content to clean up
|
2004-08-05 05:57:14 +04:00
|
|
|
*/
|
2009-07-24 03:05:34 +04:00
|
|
|
void nscss_destroy(struct content *c)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
nscss_content *css = (nscss_content *) c;
|
|
|
|
|
|
|
|
nscss_destroy_css_data(&css->data);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up CSS data
|
|
|
|
*
|
|
|
|
* \param c CSS data to clean up
|
|
|
|
*/
|
|
|
|
void nscss_destroy_css_data(struct content_css_data *c)
|
2004-08-05 05:57:14 +04:00
|
|
|
{
|
2009-07-24 03:05:34 +04:00
|
|
|
uint32_t i;
|
2005-05-22 03:30:19 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
for (i = 0; i < c->import_count; i++) {
|
|
|
|
if (c->imports[i].c != NULL) {
|
|
|
|
hlcache_handle_release(c->imports[i].c);
|
2005-05-22 03:30:19 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
c->imports[i].c = NULL;
|
2004-08-05 05:57:14 +04:00
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
free(c->imports);
|
2004-08-05 05:57:14 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
if (c->sheet != NULL) {
|
|
|
|
css_stylesheet_destroy(c->sheet);
|
|
|
|
c->sheet = NULL;
|
2005-05-22 03:30:19 +04:00
|
|
|
}
|
2010-04-08 19:40:17 +04:00
|
|
|
|
|
|
|
free(c->charset);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
2005-05-22 03:30:19 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
nserror nscss_clone(const struct content *old, struct content **newc)
|
2010-04-04 16:41:19 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
const nscss_content *old_css = (const nscss_content *) old;
|
|
|
|
nscss_content *new_css;
|
2010-04-04 16:41:19 +04:00
|
|
|
const char *data;
|
|
|
|
unsigned long size;
|
2011-05-07 00:40:09 +04:00
|
|
|
nserror error;
|
|
|
|
|
|
|
|
new_css = talloc_zero(0, nscss_content);
|
|
|
|
if (new_css == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
/* Clone content */
|
|
|
|
error = content__clone(old, &new_css->base);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
content_destroy(&new_css->base);
|
|
|
|
return error;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
|
|
|
|
/* Simply replay create/process/convert */
|
2011-05-07 00:40:09 +04:00
|
|
|
error = nscss_create_css_data(&new_css->data,
|
2011-12-04 18:55:23 +04:00
|
|
|
nsurl_access(content_get_url(&new_css->base)),
|
2011-05-07 00:40:09 +04:00
|
|
|
old_css->data.charset,
|
|
|
|
new_css->base.quirks,
|
|
|
|
nscss_content_done, new_css);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
content_destroy(&new_css->base);
|
|
|
|
return error;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
data = content__get_source_data(&new_css->base, &size);
|
2010-04-04 16:41:19 +04:00
|
|
|
if (size > 0) {
|
2011-05-07 00:40:09 +04:00
|
|
|
if (nscss_process_data(&new_css->base, data, size) == false) {
|
|
|
|
content_destroy(&new_css->base);
|
|
|
|
return NSERROR_CLONE_FAILED;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old->status == CONTENT_STATUS_READY ||
|
|
|
|
old->status == CONTENT_STATUS_DONE) {
|
2011-05-07 00:40:09 +04:00
|
|
|
if (nscss_convert(&new_css->base) == false) {
|
|
|
|
content_destroy(&new_css->base);
|
|
|
|
return NSERROR_CLONE_FAILED;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
*newc = (struct content *) new_css;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nscss_matches_quirks(const struct content *c, bool quirks)
|
|
|
|
{
|
|
|
|
return c->quirks == quirks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the stylesheet object associated with a CSS content
|
|
|
|
*
|
|
|
|
* \param h Stylesheet content
|
|
|
|
* \return Pointer to stylesheet object
|
|
|
|
*/
|
|
|
|
css_stylesheet *nscss_get_stylesheet(struct hlcache_handle *h)
|
|
|
|
{
|
|
|
|
nscss_content *c = (nscss_content *) hlcache_handle_get_content(h);
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
return c->data.sheet;
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Retrieve imported stylesheets
|
|
|
|
*
|
|
|
|
* \param h Stylesheet containing imports
|
|
|
|
* \param n Pointer to location to receive number of imports
|
|
|
|
* \return Pointer to array of imported stylesheets
|
|
|
|
*/
|
|
|
|
struct nscss_import *nscss_get_imports(hlcache_handle *h, uint32_t *n)
|
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
nscss_content *c = (nscss_content *) hlcache_handle_get_content(h);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
assert(n != NULL);
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
*n = c->data.import_count;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
return c->data.imports;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the type of a content
|
|
|
|
*
|
|
|
|
* \return CONTENT_CSS
|
|
|
|
*/
|
2011-09-03 13:27:42 +04:00
|
|
|
content_type nscss_content_type(void)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
|
|
|
return CONTENT_CSS;
|
2004-08-05 05:57:14 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
/*****************************************************************************
|
|
|
|
* Object completion *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2010-04-11 14:52:18 +04:00
|
|
|
/**
|
2011-02-26 03:58:54 +03:00
|
|
|
* Handle notification that a CSS object is done
|
2010-04-11 14:52:18 +04:00
|
|
|
*
|
2011-02-26 03:58:54 +03:00
|
|
|
* \param css CSS object
|
|
|
|
* \param pw Private data
|
|
|
|
*/
|
|
|
|
void nscss_content_done(struct content_css_data *css, void *pw)
|
|
|
|
{
|
|
|
|
union content_msg_data msg_data;
|
|
|
|
struct content *c = pw;
|
|
|
|
uint32_t i;
|
|
|
|
size_t size;
|
|
|
|
css_error error;
|
|
|
|
|
|
|
|
/* Retrieve the size of this sheet */
|
|
|
|
error = css_stylesheet_size(css->sheet, &size);
|
|
|
|
if (error != CSS_OK) {
|
|
|
|
msg_data.error = "?";
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
2011-07-21 01:39:51 +04:00
|
|
|
content_set_error(c);
|
2011-02-26 03:58:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
c->size += size;
|
|
|
|
|
|
|
|
/* Add on the size of the imported sheets */
|
|
|
|
for (i = 0; i < css->import_count; i++) {
|
|
|
|
if (css->imports[i].c != NULL) {
|
|
|
|
struct content *import = hlcache_handle_get_content(
|
|
|
|
css->imports[i].c);
|
|
|
|
|
|
|
|
if (import != NULL) {
|
|
|
|
c->size += import->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, catch the content's users up with reality */
|
2011-02-27 23:11:39 +03:00
|
|
|
content_set_ready(c);
|
|
|
|
content_set_done(c);
|
2011-02-26 03:58:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Import handling *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle notification of the need for an imported stylesheet
|
|
|
|
*
|
|
|
|
* \param pw CSS object requesting the import
|
|
|
|
* \param parent Stylesheet requesting the import
|
|
|
|
* \param url URL of the imported sheet
|
|
|
|
* \param media Applicable media for the imported sheet
|
|
|
|
* \return CSS_OK on success, appropriate error otherwise
|
2010-04-11 14:52:18 +04:00
|
|
|
*/
|
2011-02-26 03:58:54 +03:00
|
|
|
css_error nscss_handle_import(void *pw, css_stylesheet *parent,
|
|
|
|
lwc_string *url, uint64_t media)
|
2010-04-11 14:52:18 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
content_type accept = CONTENT_CSS;
|
2011-02-26 03:58:54 +03:00
|
|
|
struct content_css_data *c = pw;
|
|
|
|
nscss_import_ctx *ctx;
|
2010-04-11 14:52:18 +04:00
|
|
|
hlcache_child_context child;
|
|
|
|
struct nscss_import *imports;
|
2011-02-26 03:58:54 +03:00
|
|
|
const char *referer;
|
2010-04-11 14:52:18 +04:00
|
|
|
css_error error;
|
|
|
|
nserror nerror;
|
|
|
|
|
2011-10-03 19:56:47 +04:00
|
|
|
nsurl *ns_url;
|
|
|
|
nsurl *ns_ref;
|
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
assert(parent == c->sheet);
|
|
|
|
|
|
|
|
error = css_stylesheet_get_url(c->sheet, &referer);
|
2010-04-11 14:52:18 +04:00
|
|
|
if (error != CSS_OK) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
ctx = malloc(sizeof(*ctx));
|
|
|
|
if (ctx == NULL)
|
|
|
|
return CSS_NOMEM;
|
|
|
|
|
|
|
|
ctx->css = c;
|
|
|
|
ctx->index = c->import_count;
|
|
|
|
|
2010-04-11 14:52:18 +04:00
|
|
|
/* Increase space in table */
|
|
|
|
imports = realloc(c->imports, (c->import_count + 1) *
|
|
|
|
sizeof(struct nscss_import));
|
|
|
|
if (imports == NULL) {
|
2011-02-26 03:58:54 +03:00
|
|
|
free(ctx);
|
2010-04-11 14:52:18 +04:00
|
|
|
return CSS_NOMEM;
|
|
|
|
}
|
|
|
|
c->imports = imports;
|
|
|
|
|
|
|
|
/** \todo fallback charset */
|
|
|
|
child.charset = NULL;
|
|
|
|
error = css_stylesheet_quirks_allowed(c->sheet, &child.quirks);
|
|
|
|
if (error != CSS_OK) {
|
2011-02-26 03:58:54 +03:00
|
|
|
free(ctx);
|
2010-04-11 14:52:18 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create content */
|
|
|
|
c->imports[c->import_count].media = media;
|
2011-08-23 23:59:13 +04:00
|
|
|
|
2011-10-03 19:56:47 +04:00
|
|
|
/* TODO: Why aren't we getting a relative url part, to join? */
|
|
|
|
nerror = nsurl_create(lwc_string_data(url), &ns_url);
|
|
|
|
if (nerror != NSERROR_OK) {
|
|
|
|
free(ctx);
|
|
|
|
return CSS_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Constructing nsurl for referer here is silly, avoid */
|
|
|
|
nerror = nsurl_create(referer, &ns_ref);
|
|
|
|
if (nerror != NSERROR_OK) {
|
|
|
|
nsurl_unref(ns_url);
|
|
|
|
free(ctx);
|
|
|
|
return CSS_NOMEM;
|
|
|
|
}
|
|
|
|
|
2011-08-23 23:59:13 +04:00
|
|
|
/* Avoid importing ourself */
|
2011-10-03 19:56:47 +04:00
|
|
|
if (nsurl_compare(ns_url, ns_ref, NSURL_COMPLETE)) {
|
2011-08-23 23:59:13 +04:00
|
|
|
c->imports[c->import_count].c = NULL;
|
|
|
|
/* No longer require context as we're not fetching anything */
|
2011-02-26 03:58:54 +03:00
|
|
|
free(ctx);
|
2011-08-23 23:59:13 +04:00
|
|
|
ctx = NULL;
|
|
|
|
} else {
|
2011-10-03 19:56:47 +04:00
|
|
|
nerror = hlcache_handle_retrieve(ns_url,
|
|
|
|
0, ns_ref, NULL, nscss_import, ctx,
|
2011-08-23 23:59:13 +04:00
|
|
|
&child, accept,
|
|
|
|
&c->imports[c->import_count].c);
|
|
|
|
if (nerror != NSERROR_OK) {
|
|
|
|
free(ctx);
|
|
|
|
return CSS_NOMEM;
|
|
|
|
}
|
2010-04-11 14:52:18 +04:00
|
|
|
}
|
|
|
|
|
2011-10-03 19:56:47 +04:00
|
|
|
nsurl_unref(ns_url);
|
|
|
|
nsurl_unref(ns_ref);
|
|
|
|
|
2011-02-27 23:11:39 +03:00
|
|
|
#ifdef NSCSS_IMPORT_TRACE
|
|
|
|
LOG(("Import %d '%s' -> (handle: %p ctx: %p)",
|
|
|
|
c->import_count, lwc_string_data(url),
|
|
|
|
c->imports[c->import_count].c, ctx));
|
|
|
|
#endif
|
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
c->import_count++;
|
|
|
|
|
|
|
|
return CSS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for imported stylesheet events
|
|
|
|
*
|
|
|
|
* \param handle Handle for stylesheet
|
|
|
|
* \param event Event object
|
|
|
|
* \param pw Callback context
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror nscss_import(hlcache_handle *handle,
|
|
|
|
const hlcache_event *event, void *pw)
|
|
|
|
{
|
|
|
|
nscss_import_ctx *ctx = pw;
|
|
|
|
css_error error = CSS_OK;
|
|
|
|
|
2011-02-27 23:11:39 +03:00
|
|
|
#ifdef NSCSS_IMPORT_TRACE
|
|
|
|
LOG(("Event %d for %p (%p)", event->type, handle, ctx));
|
|
|
|
#endif
|
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
assert(ctx->css->imports[ctx->index].c == handle);
|
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
case CONTENT_MSG_LOADING:
|
|
|
|
break;
|
|
|
|
case CONTENT_MSG_READY:
|
|
|
|
break;
|
|
|
|
case CONTENT_MSG_DONE:
|
|
|
|
error = nscss_import_complete(ctx);
|
|
|
|
break;
|
|
|
|
case CONTENT_MSG_ERROR:
|
|
|
|
hlcache_handle_release(handle);
|
|
|
|
ctx->css->imports[ctx->index].c = NULL;
|
|
|
|
|
|
|
|
error = nscss_import_complete(ctx);
|
|
|
|
/* Already released handle */
|
|
|
|
break;
|
|
|
|
case CONTENT_MSG_STATUS:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Preserve out-of-memory. Anything else is OK */
|
|
|
|
return error == CSS_NOMEM ? NSERROR_NOMEM : NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an imported stylesheet completing
|
|
|
|
*
|
|
|
|
* \param ctx Import context
|
|
|
|
* \return CSS_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
css_error nscss_import_complete(nscss_import_ctx *ctx)
|
|
|
|
{
|
|
|
|
css_error error = CSS_OK;
|
|
|
|
|
|
|
|
/* If this import is the next to be registered, do so */
|
|
|
|
if (ctx->css->next_to_register == ctx->index)
|
|
|
|
error = nscss_register_imports(ctx->css);
|
|
|
|
|
2011-02-27 23:11:39 +03:00
|
|
|
#ifdef NSCSS_IMPORT_TRACE
|
|
|
|
LOG(("Destroying import context %p for %d", ctx, ctx->index));
|
|
|
|
#endif
|
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
/* No longer need import context */
|
|
|
|
free(ctx);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Import registration *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register imports with a stylesheet
|
|
|
|
*
|
|
|
|
* \param c CSS object containing the imports
|
|
|
|
* \return CSS_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
css_error nscss_register_imports(struct content_css_data *c)
|
|
|
|
{
|
|
|
|
uint32_t index;
|
|
|
|
css_error error;
|
|
|
|
|
|
|
|
assert(c->next_to_register != (uint32_t) -1);
|
|
|
|
assert(c->next_to_register < c->import_count);
|
|
|
|
|
|
|
|
/* Register imported sheets */
|
|
|
|
for (index = c->next_to_register; index < c->import_count; index++) {
|
|
|
|
/* Stop registering if we encounter one whose fetch hasn't
|
|
|
|
* completed yet. We'll resume at this point when it has
|
|
|
|
* completed.
|
|
|
|
*/
|
|
|
|
if (c->imports[index].c != NULL &&
|
|
|
|
content_get_status(c->imports[index].c) !=
|
|
|
|
CONTENT_STATUS_DONE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = nscss_register_import(c, c->imports[index].c);
|
|
|
|
if (error != CSS_OK)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Record identity of the next import to register */
|
|
|
|
c->next_to_register = (uint32_t) index;
|
|
|
|
|
|
|
|
if (c->next_to_register == c->import_count) {
|
|
|
|
/* No more imports: notify parent that we're DONE */
|
|
|
|
c->done(c, c->pw);
|
|
|
|
}
|
|
|
|
|
2010-04-11 14:52:18 +04:00
|
|
|
return CSS_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-26 03:58:54 +03:00
|
|
|
|
2010-04-11 14:52:18 +04:00
|
|
|
/**
|
2011-02-26 03:58:54 +03:00
|
|
|
* Register an import with a stylesheet
|
2010-04-11 14:52:18 +04:00
|
|
|
*
|
|
|
|
* \param c CSS object that requested the import
|
2011-02-26 03:58:54 +03:00
|
|
|
* \param import Cache handle of import, or NULL for blank
|
2010-04-11 14:52:18 +04:00
|
|
|
* \return CSS_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2011-02-26 03:58:54 +03:00
|
|
|
css_error nscss_register_import(struct content_css_data *c,
|
2010-04-11 14:52:18 +04:00
|
|
|
const hlcache_handle *import)
|
|
|
|
{
|
|
|
|
css_stylesheet *sheet;
|
|
|
|
css_error error;
|
|
|
|
|
|
|
|
if (import != NULL) {
|
2011-05-07 00:40:09 +04:00
|
|
|
nscss_content *s =
|
|
|
|
(nscss_content *) hlcache_handle_get_content(import);
|
|
|
|
sheet = s->data.sheet;
|
2010-04-11 14:52:18 +04:00
|
|
|
} else {
|
2010-04-11 23:59:59 +04:00
|
|
|
/* Create a blank sheet if needed. */
|
|
|
|
if (blank_import == NULL) {
|
2011-01-29 17:53:18 +03:00
|
|
|
css_stylesheet_params params;
|
|
|
|
|
2011-03-13 12:32:13 +03:00
|
|
|
params.params_version = CSS_STYLESHEET_PARAMS_VERSION_1;
|
2011-01-29 17:53:18 +03:00
|
|
|
params.level = CSS_LEVEL_DEFAULT;
|
|
|
|
params.charset = NULL;
|
|
|
|
params.url = "";
|
|
|
|
params.title = NULL;
|
|
|
|
params.allow_quirks = false;
|
|
|
|
params.inline_style = false;
|
|
|
|
params.resolve = nscss_resolve_url;
|
|
|
|
params.resolve_pw = NULL;
|
|
|
|
params.import = NULL;
|
|
|
|
params.import_pw = NULL;
|
2011-01-30 16:40:47 +03:00
|
|
|
params.color = gui_system_colour;
|
2011-01-29 17:53:18 +03:00
|
|
|
params.color_pw = NULL;
|
2011-02-03 12:31:11 +03:00
|
|
|
params.font = NULL;
|
|
|
|
params.font_pw = NULL;
|
2011-01-29 17:53:18 +03:00
|
|
|
|
|
|
|
error = css_stylesheet_create(¶ms,
|
2010-04-30 20:06:03 +04:00
|
|
|
ns_realloc, NULL,
|
2010-04-11 23:59:59 +04:00
|
|
|
&blank_import);
|
|
|
|
if (error != CSS_OK) {
|
|
|
|
return error;
|
|
|
|
}
|
2011-02-26 03:58:54 +03:00
|
|
|
|
|
|
|
error = css_stylesheet_data_done(blank_import);
|
|
|
|
if (error != CSS_OK) {
|
|
|
|
css_stylesheet_destroy(blank_import);
|
|
|
|
return error;
|
|
|
|
}
|
2010-04-11 14:52:18 +04:00
|
|
|
}
|
2010-04-11 23:59:59 +04:00
|
|
|
|
|
|
|
sheet = blank_import;
|
2010-04-11 14:52:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
error = css_stylesheet_register_import(c->sheet, sheet);
|
|
|
|
if (error != CSS_OK) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|