Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ObjectPascal simple but fast and efficient CSS minifier
function MinifyCSS(const aInput:ansistring):ansistring;
var Position,Len,TemporaryPosition:longint;
TemporaryInput:string;
begin
Position:=1;
Len:=length(aInput);
TemporaryInput:='';
while Position<=Len do begin
if ((Position+2)<=Len) and ((aInput[Position]='/') and (aInput[Position+1]='*')) then begin
inc(Position,2);
while ((Position+1)<=Len) and ((aInput[Position]<>'*') or (aInput[Position+1]<>'/')) do begin
inc(Position);
end;
if ((Position+1)<=Len) and (aInput[Position]='*') and (aInput[Position+1]='/') then begin
inc(Position,2);
TemporaryInput:=TemporaryInput+#32;
end;
end else begin
TemporaryInput:=TemporaryInput+aInput[Position];
inc(Position);
end;
end;
Position:=1;
Len:=length(TemporaryInput);
result:='';
while Position<=Len do begin
case TemporaryInput[Position] of
#0..#32:begin
inc(Position);
while (Position<=Len) and (TemporaryInput[Position] in [#0..#32]) do begin
inc(Position);
end;
result:=result+#32;
end;
';':begin
inc(Position);
while (Position<=Len) and (TemporaryInput[Position] in [#0..#32]) do begin
inc(Position);
end;
if (Position<=Len) and (TemporaryInput[Position]='}') then begin
result:=result+'}';
inc(Position);
end else begin
result:=result+';';
end;
end;
'A'..'Z','a'..'z','0','9':begin
result:=result+TemporaryInput[Position];
inc(Position);
TemporaryPosition:=Position;
while (TemporaryPosition<=Len) and (TemporaryInput[TemporaryPosition] in [#0..#32]) do begin
inc(TemporaryPosition);
end;
if (TemporaryPosition=Position) and (TemporaryInput[Position-1]='0') then begin
if ((Position-2)>0) and (TemporaryInput[Position-2] in [#0..#32,':']) then begin
if (Position<=Len) and (TemporaryInput[Position]='%') then begin
inc(Position);
end else if ((Position+2)<=Len) and (TemporaryInput[Position]='p') and (TemporaryInput[Position+1] in ['t','x']) then begin
inc(Position,2);
end else if ((Position+2)<=Len) and (TemporaryInput[Position]='e') and (TemporaryInput[Position+1]='m') then begin
inc(Position,2);
end else begin
end;
end;
end else if (TemporaryPosition<>Position) and (TemporaryInput[TemporaryPosition] in ['{',';','}',',']) then begin
Position:=TemporaryPosition;
end;
end;
':':begin
result:=result+':';
inc(Position);
while (Position<=Len) and (TemporaryInput[Position] in [#0..#32]) do begin
inc(Position);
end;
end;
'{':begin
result:=result+'{';
inc(Position);
while (Position<=Len) and (TemporaryInput[Position] in [#0..#32]) do begin
inc(Position);
end;
end;
else begin
result:=result+TemporaryInput[Position];
inc(Position);
end;
end;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment