o
    /h"                     @   sf   d Z dgZddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZ ddlmZ G dd dejjZdS )	zMediaList implements DOM Level 2 Style Sheets MediaList.

TODO:
    - delete: maybe if deleting from all, replace *all* with all others?
    - is unknown media an exception?
	MediaList    N)	normalize	pushtoken)PreDefProd
ProdParserSequence   )
MediaQueryc                       s   e Zd ZdZd$ fdd	Zdd Zdd	 Zd
d Zedd ddZ	dd Z
dd Zee
eddZdd Zdd Zdd Zdd Zdd Zdd  Zed!d d"dZed#d Z  ZS )%r   a  Provides the abstraction of an ordered collection of media,
    without defining or constraining how this collection is
    implemented.

    A single media in the list is an instance of :class:`MediaQuery`.
    An empty list is the same as a list that contains the medium "all".

    New format with :class:`MediaQuery`::

        : S* [media_query [ ',' S* media_query ]* ]?


    NFc                    s>   t    d| _t|trd|}|| _|r|| _|| _dS )a   
        :param mediaText:
            Unicodestring of parsable comma separared media
            or a (Python) list of media.
        :param parentRule:
            CSSRule this medialist is used in, e.g. an @import or @media.
        :param readonly:
            Not used yet.
        F,N)	super__init___wellformed
isinstancelistjoin_parentRule	mediaText	_readonly)selfr   
parentRulereadonly	__class__ R/var/www/html/myenv/lib/python3.10/site-packages/cssutils/stylesheets/medialist.pyr   #   s   




zMediaList.__init__c                 C   s   d| j j d| jdS )Nzcssutils.stylesheets.z(mediaText=))r   __name__r   r   r   r   r   __repr__:   s   zMediaList.__repr__c                 C   s$   d| j j d| jdt| ddS )Nz<cssutils.stylesheets.z object mediaText=z at 0xx>)r   r   r   idr   r   r   r   __str__=   s   $zMediaList.__str__c                 c   s"    | j D ]
}|jdkr|V  qd S )Nr
   )_seqtype)r   itemr   r   r   __iter__@   s   

zMediaList.__iter__c                 C   s   t t| S N)lenr   r   r   r   r   <lambda>F   s    zMediaList.<lambda>z/The number of media in the list (DOM readonly).)docc                 C   s   t j| S r(   )cssutilsserdo_stylesheets_medialistr   r   r   r   _getMediaTextJ   s   zMediaList._getMediaTextc                 C   sj  |    dd }tttj| ddd d| ttjdd| dd d}t j|d	|d
d\}}}}d}|D ]}	|	j}
t|
t	rJ|
j
sHd} nd}q7|s\d}|| _| jjdtjjd || _|rg }tjjdd}tjjdd}|D ]8}	|	jdkr|	jj}|r|dkr|}||	  n||v rqs|| nt|	jtjjjr||	 ||	 qs| | dS dS )at  
        :param mediaText:
            simple value or comma-separated list of media

        :exceptions:
            - - :exc:`~xml.dom.SyntaxErr`:
              Raised if the specified string value has a syntax error and is
              unparsable.
            - - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this media list is readonly.
        c                   S   s   t ddd dd dS )NMediaQueryStartc                 S   s   | dkp|dkS )NIDENT(r   )tvr   r   r   r*   ]   s    z;MediaList._setMediaText.<locals>.<lambda>.<locals>.<lambda>c                 S   s   dt t| |ddfS )Nr
   T)_partof)r
   r   )r3   tokensr   r   r   r*   ^   s   )namematchtoSeq)r   r   r   r   r   r*   [   s
    z)MediaList._setMediaText.<locals>.<lambda>)parentc                   S      dS N)r   Nr   r   r   r   r   r*   d       )minmaxF)r9   c                   S   r;   r<   r   r   r   r   r   r*   f   r=   r   ml)debugTzMediaQuery: No content.error)r   r
   allN)_checkReadonlyr   r   commentcommar   parsevaluer   r
   
wellformedr   _logrB   xmldom	SyntaxErrr,   utilSeqr%   	mediaTypeappendcss
csscomment
CSSComment_setSeq)r   r   
mediaqueryprodsokseqstoreunused
atleastoner&   r4   
mediaTypesfinalseqcommentseqonlyrP   r   r   r   _setMediaTextM   sZ   




zMediaList._setMediaTextz6The parsable textual representation of the media list.c                 C   s(   |    t|tst|}|jr|S d S r(   )rD   r   r
   rI   r   	newMediumr   r   r   __prepareset   s   
zMediaList.__preparesetc                 C   s(   |  |}|r|dddf| j|< dS dS )z^Overwriting ListSeq.__setitem__

        Any duplicate items are **not yet** removed.
        r
   N)_MediaList__preparesetr$   )r   indexrb   r   r   r   __setitem__   s   
zMediaList.__setitem__c                 C   s   |  |}|rPdd | D }t|j}d| j_d|v r(| jjd| tjj	d n"|r;||v r;| 
| | j|d nd|krC|   | j|d d| j_dS dS )	a`  Add the `newMedium` to the end of the list.
        If the `newMedium` is already used, it is first removed.

        :param newMedium:
            a string or a :class:`~cssutils.stylesheets.MediaQuery`
        :returns: Wellformedness of `newMedium`.
        :exceptions:
            - :exc:`~xml.dom.InvalidCharacterErr`:
              If the medium contains characters that are invalid in the
              underlying style language.
            - :exc:`~xml.dom.InvalidModificationErr`:
              If mediaText is "all" and a new medium is tried to be added.
              Exception is "handheld" which is set in any case (Opera does handle
              "all, handheld" special, this special case might be removed in the
              future).
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this list is readonly.
        c                 S   s   g | ]}t |jjqS r   )r   rH   rP   ).0r&   r   r   r   
<listcomp>   s    z*MediaList.appendMedium.<locals>.<listcomp>FrC   zYMediaList: Ignoring new medium %r as already specified "all" (set ``mediaText`` instead).rA   r
   T)rd   r   rP   r$   r   rJ   inforK   rL   InvalidModificationErrdeleteMediumrQ   	_clearSeq)r   rb   mtsnewmtr   r   r   appendMedium   s*   


zMediaList.appendMediumc                 C   s   |  | dS )zSame as :meth:`appendMedium`.N)ro   ra   r   r   r   rQ      s   zMediaList.appendc                 C   sZ   |    t|}t| D ]\}}t|jj|kr| |=  dS q| jjd| tjj	d dS )aO  Delete a medium from the list.

        :param oldMedium:
            delete this medium from the list.
        :exceptions:
            - :exc:`~xml.dom.NotFoundErr`:
              Raised if `oldMedium` is not in the list.
            - :exc:`~xml.dom.NoModificationAllowedErr`:
              Raised if this list is readonly.
        z"%s" not in this MediaListrA   N)
rD   r   	enumeraterH   rP   rJ   rB   rK   rL   NotFoundErr)r   	oldMediumimqr   r   r   rk      s   
zMediaList.deleteMediumc                 C   s"   z| | j W S  ty   Y dS w )zReturn the mediaType of the `index`'th element in the list.
        If `index` is greater than or equal to the number of media in the
        list, returns ``None``.
        N)rP   
IndexError)r   re   r   r   r   r&     s
   zMediaList.itemc                 C      | j S r(   )r   r   r   r   r   r*         zHThe CSSRule (e.g. an @media or @import rule this list is part of or Nonec                 C   rv   r(   )r   r   r   r   r   r*     rw   )NNF)r   
__module____qualname____doc__r   r   r#   r'   propertylengthr/   r`   r   rd   rf   ro   rQ   rk   r&   r   rI   __classcell__r   r   r   r   r      s8    M

4
)rz   __all__xml.domrK   r,   cssutils.helperr   r   cssutils.prodparserr   r   r   r   rV   r
   rN   _NewListBaser   r   r   r   r   <module>   s    