
    OfG9                     p    d Z dZdZdZdgZddlZddZ e            Zd	 Z	e
d
k    rd Z e             dS dS )u
  
==============
 CSS Minifier
==============

CSS Minifier.

The minifier is based on the semantics of the `YUI compressor`_\, which
itself is based on `the rule list by Isaac Schlueter`_\.

:Copyright:

 Copyright 2011 - 2023
 André Malo or his licensors, as applicable

:License:

 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.

This module is a re-implementation aiming for speed instead of maximum
compression, so it can be used at runtime (rather than during a preprocessing
step). RCSSmin does syntactical compression only (removing spaces, comments
and possibly semicolons). It does not provide semantic compression (like
removing empty blocks, collapsing redundant properties etc). It does, however,
support various CSS hacks (by keeping them working as intended).

Here's a feature list:

- Strings are kept, except that escaped newlines are stripped
- Space/Comments before the very end or before various characters are
  stripped: ``:{});=>],!`` (The colon (``:``) is a special case, a single
  space is kept if it's outside a ruleset.)
- Space/Comments at the very beginning or after various characters are
  stripped: ``{}(=:>[,!``
- Optional space after unicode escapes is kept, resp. replaced by a simple
  space
- whitespaces inside ``url()`` definitions are stripped, except if it's a
  quoted non-base64 data url
- Comments starting with an exclamation mark (``!``) can be kept optionally.
- All other comments and/or whitespace characters are replaced by a single
  space.
- Multiple consecutive semicolons are reduced to one
- The last semicolon within a ruleset is stripped
- CSS Hacks supported:

  - IE7 hack (``>/**/``)
  - Mac-IE5 hack (``/*\*/.../**/``)
  - The boxmodelhack is supported naturally because it relies on valid CSS2
    strings
  - Between ``:first-line`` and the following comma or curly brace a space is
    inserted. (apparently it's needed for IE6)
  - Same for ``:first-letter``

rcssmin.c is a reimplementation of rcssmin.py in C and improves runtime up to
factor 100 or so (depending on the input). docs/BENCHMARKS in the source
distribution contains the details.

Supported python versions are 2.7 and 3.4+.

.. _YUI compressor: https://github.com/yui/yuicompressor/

.. _the rule list by Isaac Schlueter: https://github.com/isaacs/cssmin/
u   André MalozApache License, Version 2.0z1.1.2cssmin    NFc                     | s7	 ddl }t          |dd          t          k    r|j        S n# t          $ r Y nw xY wd}d}d}d}dt                      z  }d	}d
}d}	d}
d}d|
d|d}d}d}d|d|d}d}d}d|d|d}dt                      z  }dt                      z  }d}dt                      z  }t          j        |          j        t          j        d          j	        t          j        dt                      z            j        d  t          j        dt                      z            j        t          j        dt                      z            j        t          j        d          j        t          j        dt                      z            j         fd d#fd"	}|S )$z
    Generate CSS minifier.

    Parameters:
      python_only (bool):
        Use only the python variant. If true, the c extension is not even
        tried to be loaded.

    Returns:
      callable: Minifier
    r   N__version__z(?:[\n\f]|\r\n?)z[\r\n\f\040\t]z'[0-9a-fA-F]{1,6}(?:[\040\n\t\f]|\r\n?)?z[^\n\r\f0-9a-fA-F]z"(?:\\(?:%(unicoded)s|%(escaped)s))z3[^\000-\054\056\057\072-\100\133-\136\140\173-\177]z"(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)z&(?:/\*(!?)[^*]*\*+(?:[^/*][^*]*\*+)*/)z<(?:\047[^\047\\\r\n\f]*(?:\\[^\r\n\f][^\047\\\r\n\f]*)*\047)z0(?:"[^"\\\r\n\f]*(?:\\[^\r\n\f][^"\\\r\n\f]*)*")z(?:|)zB(?:\047[^\047\\\r\n\f]*(?:\\(?:[^\r]|\r\n?)[^\047\\\r\n\f]*)*\047)z6(?:"[^"\\\r\n\f]*(?:\\(?:[^\r]|\r\n?)[^"\\\r\n\f]*)*")z6(?:\047[^\047\\]*(?:\\(?:[^\r]|\r\n?)[^\047\\]*)*\047)z*(?:"[^"\\]*(?:\\(?:[^\r]|\r\n?)[^"\\]*)*")z(?:\\%(nl)s)z(?:%(spacechar)s|%(comment)s)z(?:>/\*\*/)z(?:(?:[^\000-\040"\047()\\\177]*(?:%(escape)s[^\000-\040"\047()\\\177]*)*)(?:(?:%(spacechar)s+|%(nl_escaped)s+)(?:(?:[^\000-\040"\047()\\\177]|%(escape)s|%(nl_escaped)s)[^\000-\040"\047()\\\177]*(?:%(escape)s[^\000-\040"\047()\\\177]*)*)+)*)z0[\047"][dD][aA][tT][aA]:([^\000-\040\\"\047,]*),z,(%(escape)s+)|%(spacechar)s+|%(nl_escaped)s+c                 :    |                                  d         pdS )Nr    )groups)ms    9/var/www/html/env/lib/python3.11/site-packages/rcssmin.py<lambda>z_make_cssmin.<locals>.<lambda>   s    A!4"     z[\r\n\f\040\t;]+|(%(comment)s+)z%[\r\n\f\040\t;]+|(%(_bang_comment)s+)z[\r\n\f\t]+a}  ([^\\"\047u>@\r\n\f\040\t/;:{}+]+)|(?<=[{}(=:>[,!])(%(space)s+)|^(%(space)s+)|(%(space)s+)(?=(([:{});=>\],!])|$)?)|;(%(space)s*(?:;%(space)s*)*)(?=(\})?)|(\{)|(\})|(%(strings)s)|(?<!%(nmchar)s)url\(%(spacechar)s*(%(uri_nl_strings)s|%(uri)s)%(spacechar)s*\)|(@(?:[mM][eE][dD][iI][aA]|[sS][uU][pP][pP][oO][rR][tT][sS]|[dD][oO][cC][uU][mM][eE][nN][tT]|(?:-(?:[wW][eE][bB][kK][iI][tT]|[mM][oO][zZ]|[oO]|[mM][sS])-)?[kK][eE][yY][fF][rR][aA][mM][eE][sS]))(?!%(nmchar)s)|(%(ie7hack)s)(%(space)s*)|(:[fF][iI][rR][sS][tT]-[lL](?:[iI][nN][eE]|[eE][tT][tT][eE][rR]))(%(space)s*)(?=[{,])|(%(nl_strings)s)|(%(escape)s[^\\"\047u>@\r\n\f\040\t/;:{}+]*)c                   
 dgdgdgc
| rfdnfd
fd}fd}fd}
fd}fd}fd	}
fd
}
fd}dddd|||||||d ||d|dfdfdfdffd}	|	S )z Make main subber r   c                     | j         rr|                     dd          \  }}|r"|                    d          rdd<   ndd<   |S |                    d          rd         rdS dd<   dS d         rdd<   dS dS ) Space|Comment subber       \*/r   r	   /*\*//**/	lastindexgroupendswith)matchgroup1group2	in_macie5s      r   space_subberz7_make_cssmin.<locals>.main_subber.<locals>.space_subber   s    ? &%*[[A%6%6NFF &!??622 -+,IaLL+,IaL%v.. &$Q< &#%2'(	!'x"1 &'(	!%vrr   c                     | j         rH|                     d                              d          rd         rdS dd<   dS d         rdd<   dS dS )r   r   r   r   r	   r   r   r   )r   r   s    r   r   z7_make_cssmin.<locals>.main_subber.<locals>.space_subber   sk    ? &{{1~~..v66 &$Q< &#%2'(	!'x"1 &'(	!%vrr   c                      | d           | d          dk    r(d         s d         sd  | d                    z   S   | d                    S )z space with token after    N   :r        )r   at_groupin_rule	space_subr   s    r   fn_space_postz8_make_cssmin.<locals>.main_subber.<locals>.fn_space_post  sk    uQxxE!HHOOGAJOx{OYY|UU1XX>>>>9\5588444r   c                 4    d  | d                    z   S ) ; handler ;   r'   )r   r*   r   s    r   fn_semicolonz7_make_cssmin.<locals>.main_subber.<locals>.fn_semicolon  s"    <q::::r   c                 n    d         r  | d                    S d  | d                    z   S )r-   r   r/   r.   r'   )r   r)   r*   r   s    r   fn_semicolon2z8_make_cssmin.<locals>.main_subber.<locals>.fn_semicolon2  sH    qz 9 yuuQxx888<q::::r   c                 D    d         rdxx         dz  cc<   ndd<   dS )z { handler r   r   {r'   )_r(   r)   s    r   fn_openz2_make_cssmin.<locals>.main_subber.<locals>.fn_open  s5    { q 
3r   c                     dd<   dS )z } handler r   }r'   )r5   r)   s    r   fn_closez3_make_cssmin.<locals>.main_subber.<locals>.fn_close   s    GAJ3r   c                      | d          } |          }|r:|                     d                                                              d          r |          }d|dS )z url() handler    r   z;base64zurl(r   )r   lowerr   )r   uridatauri_data_plainuri_space_suburi_space_subbers      r   fn_urlz1_make_cssmin.<locals>.main_subber.<locals>.fn_url%  sr    %))C!>#&&D ;4::a==..0099)DD ;#m$4c:: #%%r   c                 :    dxx         dz  cc<    | d          S )z @xxx group handler r   r      r'   )r   r(   s    r   fn_at_groupz6_make_cssmin.<locals>.main_subber.<locals>.fn_at_group-  s'    QKKK1KKK599r   c                     d         s.d         s&dd<    | d            | d                    z   S d  | d                    z   S )z IE7 Hack handler r         >r'   )r   r(   r   r)   r*   r   s    r   
fn_ie7hackz5_make_cssmin.<locals>.main_subber.<locals>.fn_ie7hack2  sk    1: Fhqk F 	!uRyy99\5599#E#EEE<r;;;;r   Nc                      | d          S )N   r'   )gs    r   r   z3_make_cssmin.<locals>.main_subber.<locals>.<lambda>G  s    aaee r   c                 L     | d          dz     | d                    z   S )N   r%      r'   )rM   r*   r   s    r   r   z3_make_cssmin.<locals>.main_subber.<locals>.<lambda>M  s,    aaeeckIIlAAbEE$B$BB r   c                 .     d | d                    S )Nr	      r'   )rM   nl_unesc_subs    r   r   z3_make_cssmin.<locals>.main_subber.<locals>.<lambda>Q  s    ll2qquu-- r   c                 .     d | d                    S )Nr%      r'   )rM   post_esc_subs    r   r   z3_make_cssmin.<locals>.main_subber.<locals>.<lambda>R  s    ll3".. r   c                     | j         | j        }}|dk    r |         |          S |dk    r |d          S   ||                    S )z Main subber    r   )r   r   )r   idxr   r*   r   tables      r   funcz/_make_cssmin.<locals>.main_subber.<locals>.funcU  s_    %+CQww!uSz%((( uQxx9\55::666r   r'   )keep_bang_commentsr+   r0   r2   r6   r9   rB   rE   rJ   r[   r(   r   r)   r*   r   rZ   rS   rV   space_sub_bangedspace_sub_simpler?   r@   rA   s             @@@@@@r   main_subberz!_make_cssmin.<locals>.main_subber   s   ()sQC!$	7H %	(I     * )I    	5 	5 	5 	5 	5 	5 	5 	5	; 	; 	; 	; 	; 	;	; 	; 	; 	; 	; 	; 	;	 	 	 	 	 		 	 	 	 	
	& 	& 	& 	& 	& 	& 	&	 	 	 	 	
	< 	< 	< 	< 	< 	< 	< 	< 	< OBBBBB .---....3
8
	7 
	7 
	7 
	7 
	7 
	7 
	7 r   Fc                     t          |           \  }}   |          |           } |r*|                     d          } |dk    rt          |           } | S )a  
        Minify CSS.

        Parameters:
          style (str):
            CSS to minify

          keep_bang_comments (bool):
            Keep comments starting with an exclamation mark? (``/*!...*/``)

        Returns:
          str: Minified style
        latin-1r   )_as_strencode	bytearray)styler\   is_bytesmain_subr_   s      r   r   z_make_cssmin.<locals>.cssminc  sd      "%..%%788%@@ 	)LL++E1}}!%((r   F)
_rcssmingetattrr   r   ImportErrorlocals_recompilesubr   )!python_onlyri   nl	spacecharunicodedescapedescapenmcharcomment_bang_commentstring1string2strings
nl_string1
nl_string2
nl_stringsuri_nl_string1uri_nl_string2uri_nl_strings
nl_escapedspaceie7hackr=   r   rg   r_   rS   rV   r]   r^   r?   r@   rA   s!                           @@@@@@@@@r   _make_cssminr   T   s     '	'OOO
 x55DD& E	  	 	 	D	 
B!I9H#G2VXX=FCF 4G >M 	H AGG%ggwww/G 	N JJJ!+ZZZ8JNNBNN%3^^^^^DN 688+J,vxx7EG
	 	#C& ;z**.L[;   K7!    54{*$    {0$    ;~..2L{	96 	=  < = DE E E E E E E E E E EN      0 Ms   3 
A A c                 p   d}t           t          u r%t          | t                    st	          d          n~t          | t                    rd}|                     d          } nQt          | t                    rd}|                     d          } n$t          | t                     st	          d          || fS )z& Make sure the style is a text string FzUnexpected typeTra   r   )strbytes
isinstance
basestring	TypeErrordecoderd   )scriptrf   s     r   rb   rb     s    H
e||&*-- 	/-...	/	FE	"	" +y))	FI	&	& +y))$$ +)***Vr   __main__c                     ddl } d| j        dd         v p!d| j        dd         v pd| j        dd         v }d| j        dd         v s"d| j        dd         v sd| j        dd         v rt          d	          }nt          }| j                             || j                                        |
                     dS )z Main r   Nz-br   z-bpz-pbz-pT)rp   )r\   )sysargvr   r   stdoutwritestdinread)_sysr\   xcssmins      r   mainr     s     DIabbM! &	!""%&	!""% 	
 49QRR=  ETYqrr]$:$:DIabbM))"t444GGG''JOO2D
 
 
 	 	 	 	 	r   rh   )__doc__
__author____license__r   __all__rerm   r   r   rb   __name__r   r'   r   r   <module>r      s   H HR 
+*    g g g gR	 
  $ z  $ 	DFFFFF' r   