From 0388f2fa39666a2228d20ab7a1867bf3caa0d9f6 Mon Sep 17 00:00:00 2001 From: Peter Hatina Date: Wed, 26 Oct 2011 20:36:16 +0200 Subject: [PATCH] Introduce midori_web_settings_add/remove_style API The CSS stylesheets added by ID are globally applied. Related: https://bugs.launchpad.net/midori/+bug/826441 --- midori/midori-websettings.c | 76 +++++++++++++++++++++++++++++++++++++ midori/midori-websettings.h | 9 +++++ 2 files changed, 85 insertions(+) diff --git a/midori/midori-websettings.c b/midori/midori-websettings.c index b2e29a11..7c04da1b 100644 --- a/midori/midori-websettings.c +++ b/midori/midori-websettings.c @@ -1,5 +1,6 @@ /* Copyright (C) 2008-2010 Christian Dywan + Copyright (C) 2011 Peter Hatina This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -92,6 +93,7 @@ struct _MidoriWebSettings #endif gboolean strip_referer; gboolean flash_window_on_bg_tabs; + GHashTable* user_stylesheets; }; struct _MidoriWebSettingsClass @@ -1090,6 +1092,7 @@ midori_web_settings_init (MidoriWebSettings* web_settings) web_settings->http_proxy = NULL; web_settings->open_popups_in_tabs = TRUE; web_settings->kinetic_scrolling = TRUE; + web_settings->user_stylesheets = NULL; g_signal_connect (web_settings, "notify::default-encoding", G_CALLBACK (notify_default_encoding_cb), NULL); @@ -1110,6 +1113,8 @@ midori_web_settings_finalize (GObject* object) katze_assign (web_settings->location_entry_search, NULL); katze_assign (web_settings->http_proxy, NULL); katze_assign (web_settings->ident_string, NULL); + if (web_settings->user_stylesheets != NULL) + g_hash_table_destroy (web_settings->user_stylesheets); G_OBJECT_CLASS (midori_web_settings_parent_class)->finalize (object); } @@ -1732,3 +1737,74 @@ midori_web_settings_new (void) return web_settings; } + +static void +midori_web_settings_process_stylesheets (MidoriWebSettings* settings) +{ + GHashTableIter it; + GString* css = g_string_new (""); + gchar* base64; + gchar* encoded; + gpointer key; + gpointer value; + + g_hash_table_iter_init (&it, settings->user_stylesheets); + while (g_hash_table_iter_next (&it, &key, &value)) + g_string_append_printf (css, "%s\n", (const gchar*) value); + + /* data: uri prefix from Source/WebCore/page/Page.cpp:700 in WebKit */ + encoded = g_base64_encode ((guchar*)css->str, css->len); + base64 = g_strdup_printf ("data:text/css;charset=utf-8;base64,%s", encoded); + g_object_set (settings, "user-stylesheet-uri", base64, NULL); + + g_free (encoded); + g_free (base64); + g_string_free (css, TRUE); +} + +/** + * midori_web_settings_add_style: + * @rule_id: a string identifier + * @style: a CSS stylesheet + * + * Adds or replaces a custom stylesheet. + * + * Since: 0.4.2 + **/ +void +midori_web_settings_add_style (MidoriWebSettings* settings, + gchar* rule_id, + gchar* style) +{ + g_return_if_fail (MIDORI_IS_WEB_SETTINGS (settings)); + g_return_if_fail (rule_id != NULL); + g_return_if_fail (style != NULL); + + if (settings->user_stylesheets == NULL) + settings->user_stylesheets = g_hash_table_new (g_str_hash, NULL); + g_hash_table_insert (settings->user_stylesheets, rule_id, style); + midori_web_settings_process_stylesheets (settings); +} + +/** + * midori_web_settings_remove_style: + * @rule_id: the string identifier used previously + * + * Removes a stylesheet from midori settings. + * + * Since: 0.4.2 + **/ +void +midori_web_settings_remove_style (MidoriWebSettings* settings, + const gchar* rule_id) +{ + g_return_if_fail (MIDORI_IS_WEB_SETTINGS (settings)); + g_return_if_fail (rule_id != NULL); + + if (settings->user_stylesheets != NULL) + { + g_hash_table_remove (settings->user_stylesheets, rule_id); + midori_web_settings_process_stylesheets (settings); + } +} + diff --git a/midori/midori-websettings.h b/midori/midori-websettings.h index 8e7e3605..80bf00b8 100644 --- a/midori/midori-websettings.h +++ b/midori/midori-websettings.h @@ -158,6 +158,15 @@ midori_web_settings_get_type (void) G_GNUC_CONST; MidoriWebSettings* midori_web_settings_new (void); +void +midori_web_settings_add_style (MidoriWebSettings* settings, + gchar* rule_id, + gchar* style); + +void +midori_web_settings_remove_style (MidoriWebSettings* settings, + const gchar* rule_id); + G_END_DECLS #endif /* __MIDORI_WEB_SETTINGS_H__ */ -- 2.39.5