Packages
zotonic_stdlib
1.8.0
1.31.2
1.31.1
1.31.0
1.30.1
1.30.0
1.29.1
1.29.0
1.28.1
1.28.0
1.27.0
1.26.1
1.25.0
1.24.0
1.23.1
1.23.0
1.22.0
1.21.0
1.20.3
1.20.2
1.20.1
1.20.0
1.19.0
1.18.0
1.17.0
1.16.0
1.15.1
1.15.0
1.14.0
1.13.0
1.12.0
1.11.2
1.11.1
1.11.0
1.10.0
1.9.0
1.8.0
1.7.0
1.6.0
1.5.11
1.5.10
1.5.9
1.5.8
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.11
1.2.10
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-alpha5
1.0.0-alpha4
1.0.0-alpha3
1.0.0-alpha2
1.0.0-alpha1
Zotonic standard library
Current section
Files
Jump to
Current section
Files
src/z_cssmin.erl
%% @author Maas-Maarten Zeeman <mmzeeman@xs4all.nl>
%% @copyright 2018 Maas-Maarten Zeeman
%% @doc Javascript minifier. Based on cssmin.c
%% Copyright 2018 Maas-Maarten Zeeman
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% cssmin is Copyright (c) 2010 (www.ryanday.org)
%% see https://github.com/soldair/cssmin
-module(z_cssmin).
-export([
minify/1
]).
%% @doc Minify a binary containing CSS.
-spec minify( binary() ) -> binary().
minify( CSS ) ->
machine(CSS, <<>>).
machine(<<>>, Acc) -> Acc;
machine(<<$/, $*, Rest/binary>>, Acc) -> machine(skip_comment(Rest), Acc);
machine(<<C, Rest/binary>>, Acc) when C =:= 32 orelse C =:= 10 -> machine(Rest, Acc);
machine(<<$@, Rest/binary>>, Acc) -> atrule(Rest, <<Acc/binary, $@>>);
machine(Bin, Acc) -> selector(Bin, Acc).
selector(<<>>, Acc) -> Acc;
selector(<<$/, $*, Rest/binary>>, Acc) -> selector(skip_comment(Rest), Acc);
selector(<<${, Rest/binary>>, Acc) -> block(Rest, <<Acc/binary, ${>>);
selector(<<$@, Rest/binary>>, Acc) -> atrule(Rest, <<Acc/binary, $@>>);
selector(<<10, Rest/binary>>, Acc) -> selector(Rest, Acc);
selector(<<32, Rest/binary>>, Acc) -> continue_selector(skip_space(Rest), Acc);
selector(<<C, Rest/binary>>, Acc) -> selector(Rest, <<Acc/binary, C>>).
continue_selector(<<${, _/binary>> = Rest, Acc) -> selector(Rest, Acc);
continue_selector(Rest, Acc) ->
case binary:last(Acc) of
32 -> selector(Rest, Acc);
_ -> selector(Rest, <<Acc/binary, 32>>)
end.
atrule(<<>>, Acc) -> Acc;
atrule(<<$/, $*, Rest/binary>>, Acc) -> atrule(skip_comment(Rest), Acc);
atrule(<<10, Rest/binary>>, Acc) ->
case Rest of
<<10, _/binary>> ->
machine(Rest, <<Acc/binary, $;>>);
_ ->
atrule(Rest, <<Acc/binary, 32>>)
end;
atrule(<<$;, Rest/binary>>, Acc) -> machine(Rest, <<Acc/binary, $;>>);
atrule(<<${, Rest/binary>>, Acc) -> block(Rest, <<Acc/binary, ${>>);
atrule(<<C, Rest/binary>>, Acc) -> atrule(Rest, <<Acc/binary, C>>).
block(<<>>, Acc) -> Acc;
block(<<$/, $*, Rest/binary>>, Acc) -> block(skip_comment(Rest), Acc);
block(<<$}, Rest/binary>>, Acc) -> machine(Rest, <<Acc/binary, $}>>);
block(Bin, Acc) -> declaration(skip_whitespace(Bin), Acc).
declaration(<<>>, Acc) -> Acc;
declaration(<<$/, $*, Rest/binary>>, Acc) -> declaration(skip_comment(Rest), Acc);
declaration(<<$(, Rest/binary>>, Acc) -> declaration_in_paren(Rest, <<Acc/binary, $(>>);
declaration(<<$;, Rest/binary>>, Acc) ->
case skip_whitespace(Rest) of
<<$}, _/binary>> = Rest1 -> block(Rest1, Acc);
<<$;, _/binary>> = Rest1 -> block(Rest1, Acc);
_ -> block(Rest, <<Acc/binary, $;>>)
end;
declaration(<<$}, Rest/binary>>, Acc) -> machine(Rest, <<Acc/binary, $}>>);
declaration(<<32, Rest/binary>>, Acc) ->
case binary:last(Acc) of
32 -> declaration(Rest, Acc);
_ -> declaration(Rest, <<Acc/binary, 32>>)
end;
declaration(<<C, Rest/binary>>, Acc) ->
declaration(Rest, <<Acc/binary, C>>).
declaration_in_paren(<<>>, Acc) -> Acc;
declaration_in_paren(<<$), Rest/binary>>, Acc) -> declaration(Rest, <<Acc/binary, $)>>);
declaration_in_paren(<<C, Rest/binary>>, Acc) -> declaration_in_paren(Rest, <<Acc/binary, C>>).
skip_comment(<<>>) -> <<>>;
skip_comment(<<$*, $/, Rest/binary>>) -> Rest;
skip_comment(<<_C, Rest/binary>>) -> skip_comment(Rest).
skip_space(<<32, Rest/binary>>) -> skip_space(Rest);
skip_space(Bin) -> Bin.
skip_whitespace(<<C, Rest/binary>>) when C =:= 32 orelse C =:= 10 -> skip_whitespace(Rest);
skip_whitespace(Bin) -> Bin.