Wednesday 25 January 2017

C# CSS Stylesheet Compression, fast website loading

Hi All, Here's a static class developed in c# to compress Css stylesheets,
It removes all “\r\n” (newline carriage return) , all “\t” (tab) all spaces before and after “:” “;” , it also remove all comment from your css.
Thanks to this function Stylesheet should be more light and fast for web and you can remove additionals spaces manually for complete optimization .
You can copy your css and pass it to class with:
textBox2.Text = LWEBCODE.Get_CSS_RTM(textBox1.Text);
I suggest to make a form o webform with 2 textboxes and paste in textbox1 your css stylesheet ,result can be show in textbox2 , after paste into css_runtime_sheet.css, 
REMEMBER on pagae_load event or in <head> tag to correctly set the right stylesheet: version for development and debug, or runtime version
Please for any optimization or suggestion contact us.
Here the code:


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

/*
Css Runtime Generator, compressor
developed by: lwebcode;
for other script and resource :
http://lwebcode.blogspot.com/
*/
class LWEBCODE
{
    public static string Get_CSS_RTM(string sInpunt)
    {
    string sOut = "";
    char[] c = System.Environment.NewLine.ToCharArray();
    string[] sRows = new string[] { };
    string[] sRow = new string[] { };
    bool Esc1 = false;
    bool Esc2 = false;
    bool Esc3 = false;

    sRows = Regex.Split(sInpunt, "\t");
    for (int i = 0; i 0)
    {
        if (sRows[i].IndexOf(Convert.ToChar("-")) < 0)
        {
        sRows[i] = sRows[i].Trim();
        }
    }
    sRow = SplitChar(sRows[i]);
    for (int j = 0; j < sRow.Length; j++)
    {
        if (sRow[j] == "/")
        {
            if (j < sRow.Length - 1)
            {
            if (sRow[j + 1] == "*")
            {
            Esc1 = true;
            }
        }
    }
    if (sRow[j] == "*")
    {
        if (j < sRow.Length - 1)
        {
            if (sRow[j + 1] == "/")
            {
            Esc1 = false;
            j = j + 2;
            }
        }
    }

    if (j < sRow.Length)
    {
    char[] c2 = sRow[j].ToCharArray();
        if ((c[0] == c2[0]) || (c[1] == c2[0]))
        Esc2 = true;
        else
        Esc2 = false;
    }
    if (j < sRow.Length)
    {
    char[] c2 = sRow[j].ToCharArray();
    if (c2[0] == Convert.ToChar(" "))
    {
        if ((sRow[j - 1] == ":") || (sRow[j - 1] == ";"))
        Esc3 = true;
        else
        Esc3 = false;
        if ((j + 1 < sRow.Length) && (!Esc3))
        {
            if ((sRow[j + 1] == ":") || (sRow[j + 1] == ";"))
            Esc3 = true;
            else
            Esc3 = false;
        }
    }
    else
    {
    Esc3 = false;
    }
    }

    if ((!Esc1) && (!Esc2) && (!Esc3))
    {
        if (j <= sRow.Length - 1)
        {
        sOut += sRow[j];
        }
    }
    }
    return sOut;
    }

    private static string[] SplitChar(string sIn)
    {
        string[] sInSpl;
        sInSpl = new string[sIn.Length];
        for (int i = 0; i < sIn.Length; i++)
        {
        sInSpl[i] = sIn.Substring(i, 1);
        }
        return sInSpl;
    }

}


No comments:

Post a Comment