From 7bd06838693352f53f11eab5b480ed2b91a71613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Forysiuk?= Date: Tue, 7 Dec 2010 01:40:26 +0100 Subject: [PATCH] Omit spaces and comments in user stylesheets This fixes parsing breaking due to comments. Further more it noticibly reduces the size of stylesheets. --- extensions/addons.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/extensions/addons.c b/extensions/addons.c index c8fc0173..1ce47b43 100644 --- a/extensions/addons.c +++ b/extensions/addons.c @@ -1,6 +1,7 @@ /* Copyright (C) 2008 Christian Dywan Copyright (C) 2008-2010 Arno Renevier + Copyright (C) 2010 Paweł Forysiuk This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -1066,7 +1067,8 @@ addons_get_element_content (gchar* file_path, gchar** content) { gchar* file_content; - guint meta; + GString* content_chunks; + guint meta, comment; guint i, n; g_assert (kind == ADDONS_USER_SCRIPTS || kind == ADDONS_USER_STYLES); @@ -1082,7 +1084,9 @@ addons_get_element_content (gchar* file_path, else if (kind == ADDONS_USER_STYLES) { meta = 0; + comment = 0; n = strlen (file_content); + content_chunks = g_string_new_len (NULL, n); for (i = 0; i < n; i++) { /* Replace line breaks with spaces */ @@ -1091,23 +1095,19 @@ addons_get_element_content (gchar* file_path, /* Change all single quotes to double quotes */ if (file_content[i] == '\'') file_content[i] = '\"'; - /* Turn metadata we inspected earlier into comments */ if (!meta && file_content[i] == '@') { - file_content[i] = '/'; meta++; } else if (meta == 1 && (file_content[i] == '-' || file_content[i] == 'n')) { - file_content[i] = '*'; meta++; } else if (meta == 2 && file_content[i] == '{') { - file_content[i - 1] = '*'; - file_content[i] = '/'; meta++; + continue; } else if (meta == 3 && file_content[i] == '{') meta++; @@ -1115,9 +1115,32 @@ addons_get_element_content (gchar* file_path, meta--; else if (meta == 3 && file_content[i] == '}') { - file_content[i] = ' '; meta = 0; + continue; } + + if (file_content[i] == '/' && file_content[i+1] == '*') + comment++; + /* Check whether we have comment ending, merely '*' or '/' */ + else if (comment == 2 + && file_content[i] == '*' && file_content[i+1] == '/') + { + comment--; + } + else if (comment == 1 + && file_content[i-1] == '*' && file_content[i] == '/') + { + comment--; + continue; + } + + /* Skip consecutive spaces */ + if (file_content[i] == ' ' && file_content[i - 1] == ' ') + continue; + + /* Append actual data to string */ + if ((meta == 0 || meta >= 3) && !comment) + g_string_append_c (content_chunks, file_content[i]); } *content = g_strdup_printf ( @@ -1131,8 +1154,8 @@ addons_get_element_content (gchar* file_path, "else document.documentElement.insertBefore" "(mystyle, document.documentElement.firstChild);" "}, true);", - file_content); - + content_chunks->str); + g_string_free (content_chunks, TRUE); } g_free (file_content); if (*content)