CSS Pre Processor in SQL
Build an SQL Server function to minimise CSS. It can condense your CSS by over 20%, or use our free tool
Context
CSS is very often formatted in a way that makes it easy for humans to read, but this involves tabbing, spacing and new lines. This is a fairly stripped down version of what we built in the CSS Optimiser (link below), and is used as part of our Content Management System.
The code loops through the various elements that get inserted and strips them out, resulting in CSS files a lot smaller than usual.
It also does some basic reformatting of colours and removes comments.
You can trial the tool on the link below.
SQL
ALTER FUNCTION CSSProcessor(@CSS NVARCHAR(MAX)) RETURNS NVARCHAR(MAX) AS BEGINDECLARE @CSSReturn NVARCHAR(MAX)=@CSSWHILE CHARINDEX(' ',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,' ',' ')WHILE CHARINDEX('; ',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'; ',';')WHILE CHARINDEX(' ;',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,' ;',';')WHILE CHARINDEX(': ',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,': ',':')WHILE CHARINDEX(' :',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,' :',':')WHILE CHARINDEX(', ',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,', ',',')WHILE CHARINDEX(' ,',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,' ,',',')WHILE CHARINDEX('} ',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'} ','}')WHILE CHARINDEX(' }',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,' }','}')WHILE CHARINDEX(' {',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,' {','{')WHILE CHARINDEX('{ ',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'{ ','{')WHILE CHARINDEX(';}',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,';}','}')WHILE CHARINDEX('ffffff',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'ffffff','fff')WHILE CHARINDEX('eeeeee',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'eeeeee','eee')WHILE CHARINDEX('dddddd',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'dddddd','ddd')WHILE CHARINDEX('cccccc',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'cccccc','ccc')WHILE CHARINDEX('bbbbbb',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'bbbbbb','bbb')WHILE CHARINDEX('aaaaaa',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'aaaaaa','aaa')WHILE CHARINDEX('999999',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'999999','999')WHILE CHARINDEX('888888',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'888888','888')WHILE CHARINDEX('777777',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'777777','777')WHILE CHARINDEX('666666',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'666666','666')WHILE CHARINDEX('555555',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'555555','555')WHILE CHARINDEX('444444',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'444444','444')WHILE CHARINDEX('333333',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'333333','333')WHILE CHARINDEX('222222',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'222222','222')WHILE CHARINDEX('111111',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'111111','111')WHILE CHARINDEX('000000',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'000000','000')WHILE CHARINDEX('auto auto auto auto',@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,'auto auto auto auto','auto')WHILE CHARINDEX(CHAR(13)+CHAR(10),@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,CHAR(13)+CHAR(10),'')WHILE CHARINDEX(CHAR(10),@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,CHAR(10),'')WHILE CHARINDEX(CHAR(13),@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,CHAR(13),'')WHILE CHARINDEX(CHAR(9),@CSSReturn,1)>0 SET @CSSReturn=REPLACE(@CSSReturn,CHAR(9),'')DECLARE @St INT,@En INTWHILE CHARINDEX('/*',@CSSReturn,1)>0 AND CHARINDEX('*/',@CSSReturn,CHARINDEX('*/',@CSSReturn,CHARINDEX('/*',@CSSReturn,1)))>0 BEGINSET @St=CHARINDEX('/*',@CSSReturn,1)SET @En=CHARINDEX('*/',@CSSReturn,@St)SET @CSSReturn=STUFF(@CSSReturn,@St,@En-@St+2,'')ENDRETURN @CSSReturnENDGO