Omit spaces and comments in user stylesheets

This fixes parsing breaking due to comments.

Further more it noticibly reduces the size of stylesheets.
This commit is contained in:
Paweł Forysiuk 2010-12-07 01:40:26 +01:00 committed by Christian Dywan
parent a38338b165
commit 7bd0683869

View file

@ -1,6 +1,7 @@
/* /*
Copyright (C) 2008 Christian Dywan <christian@twotoasts.de> Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
Copyright (C) 2008-2010 Arno Renevier <arno@renevier.net> Copyright (C) 2008-2010 Arno Renevier <arno@renevier.net>
Copyright (C) 2010 Paweł Forysiuk <tuxator@o2.pl>
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public 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** content)
{ {
gchar* file_content; gchar* file_content;
guint meta; GString* content_chunks;
guint meta, comment;
guint i, n; guint i, n;
g_assert (kind == ADDONS_USER_SCRIPTS || kind == ADDONS_USER_STYLES); 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) else if (kind == ADDONS_USER_STYLES)
{ {
meta = 0; meta = 0;
comment = 0;
n = strlen (file_content); n = strlen (file_content);
content_chunks = g_string_new_len (NULL, n);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
/* Replace line breaks with spaces */ /* Replace line breaks with spaces */
@ -1091,23 +1095,19 @@ addons_get_element_content (gchar* file_path,
/* Change all single quotes to double quotes */ /* Change all single quotes to double quotes */
if (file_content[i] == '\'') if (file_content[i] == '\'')
file_content[i] = '\"'; file_content[i] = '\"';
/* Turn metadata we inspected earlier into comments */
if (!meta && file_content[i] == '@') if (!meta && file_content[i] == '@')
{ {
file_content[i] = '/';
meta++; meta++;
} }
else if (meta == 1 else if (meta == 1
&& (file_content[i] == '-' || file_content[i] == 'n')) && (file_content[i] == '-' || file_content[i] == 'n'))
{ {
file_content[i] = '*';
meta++; meta++;
} }
else if (meta == 2 && file_content[i] == '{') else if (meta == 2 && file_content[i] == '{')
{ {
file_content[i - 1] = '*';
file_content[i] = '/';
meta++; meta++;
continue;
} }
else if (meta == 3 && file_content[i] == '{') else if (meta == 3 && file_content[i] == '{')
meta++; meta++;
@ -1115,9 +1115,32 @@ addons_get_element_content (gchar* file_path,
meta--; meta--;
else if (meta == 3 && file_content[i] == '}') else if (meta == 3 && file_content[i] == '}')
{ {
file_content[i] = ' ';
meta = 0; 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 ( *content = g_strdup_printf (
@ -1131,8 +1154,8 @@ addons_get_element_content (gchar* file_path,
"else document.documentElement.insertBefore" "else document.documentElement.insertBefore"
"(mystyle, document.documentElement.firstChild);" "(mystyle, document.documentElement.firstChild);"
"}, true);", "}, true);",
file_content); content_chunks->str);
g_string_free (content_chunks, TRUE);
} }
g_free (file_content); g_free (file_content);
if (*content) if (*content)