o
    %eg                  
   @   s6  d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZ dd	lmZ dd
lmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- i Z.G dd deZ/dd Z0dd Z1dd Z2e1de1de1de1de1de1ddd Z3e1de2ddddd d!d"d# Z4e1d$e2d%d&d'd d!d(d) Z5e1d*d+d, Z6e1d-e1d.e1d/e1d0e1d1e1d2e2d3d4d5d6d7 Z7e1d8d9d: Z8e1d;e2d<d4d5d=d> Z9d?d@ Z:e1dAe2dBdCdD Z;e1dEe2dFdGdH Z<e1dIe2dJdKdL Z=e1dMe2dNdOdPdQ Z>e1dRe2dSdTdUdVdWd&dXdY Z?e1dZe2d5d[d\d]d^d_d`dadb Z@e1dce2dddedf ZAe1dge2dhdidjdkdl ZBe1dme2dndodpdq ZCe1dre2dsdtdudvdw ZDe1dxe2dydzd{d| ZEdS )}zValidate properties expanders.    N)DimensionToken
IdentTokenNumberToken)parse_color   )INITIAL_VALUES)InvalidValuesPendingcheck_var_functionget_keywordget_single_keywordsplit_on_comma   expand_font_variant)background_attachmentbackground_imagebackground_positionbackground_repeatbackground_sizeblock_ellipsisborder_styleborder_widthboxcolumn_countcolumn_width
flex_basisflex_directionflex_grow_shrink	flex_wrapfont_family	font_sizefont_stretch
font_stylefont_weightline_heightlist_style_imagelist_style_positionlist_style_typeother_colorsoverflow_wrapvalidate_non_shorthandc                       s(   e Zd ZdZ fddZdd Z  ZS )PendingExpanderz>Expander with validation done when defining calculated values.c                    s   t  ||jd  || _d S )Nname)super__init__keywords	validator)selftokensr1   	__class__ V/var/www/html/venv/lib/python3.10/site-packages/weasyprint/css/validation/expanders.pyr/      s   
zPendingExpander.__init__c                 C   sF   |  |D ]\}}|dr| j jd  | }||kr |  S qt)N-r-   )r1   
startswithr0   KeyError)r2   r3   
wanted_keykeyvaluer6   r6   r7   validate   s   
zPendingExpander.validate)__name__
__module____qualname____doc__r/   r>   __classcell__r6   r6   r4   r7   r,      s    r,   c                    s6   | D ]}t |rt| |  fdd|D   S qdS )z5Return pending expanders when var is found in tokens.c                       i | ]}| qS r6   r6   .0r-   pendingr6   r7   
<dictcomp>.       z_find_var.<locals>.<dictcomp>N)r
   r,   )r3   expanderexpanded_namestokenr6   rG   r7   	_find_var(   s   
rN   c                    s    fdd}|S )z1Decorator adding a function to the ``EXPANDERS``.c                    s    t vsJ  | t  < | S )z&Add ``function`` to the ``EXPANDERS``.)	EXPANDERS)functionproperty_namer6   r7   expander_decorator3   s   z$expander.<locals>.expander_decoratorr6   )rR   rS   r6   rQ   r7   rK   1   s   rK   c                     s&   | dd|r
J  fdd}|S )zDecorator helping expanders to handle ``inherit`` and ``initial``.

    Wrap an expander so that it does not have to handle the 'inherit' and
    'initial' cases, and can just yield name suffixes. Missing suffixes
    get the initial value.

    wants_base_urlFc                    s    t  fdd  S )z"Decorate the ``wrapped`` expander.c                 3   s$   t j||d}d}t|   dv r fddD }d}n
t| |}|r)d}|s`i }r6| ||}n| |}|D ]"\}}|v sIJ |||v r[td|d d	| d
|||< q=D ]-}|drp| | }	n|}	||v r|| }
|st|
|	|dd\\}	}
nd}
|	|
fV  qbdS )zWrap the expander.r-   base_urlF)inheritinitialc                    rD   r6   r6   rE   keywordr6   r7   rI   Q   rJ   zjgeneric_expander.<locals>.generic_expander_decorator.<locals>.generic_expander_wrapper.<locals>.<dictcomp>Tzgot multiple r8   z values in a z
 shorthandrequiredrX   N)	functoolspartialr   rN   r   stripr9   r+   )r3   r-   rV   rK   skip_validationresultsresultnew_name	new_tokenactual_new_namer=   )rL   generic_expander_wrapperrT   wrappedrY   r7   rf   H   sP   


zVgeneric_expander.<locals>.generic_expander_decorator.<locals>.generic_expander_wrapper)r]   wraps)rg   rL   rT   )rf   rg   r7   generic_expander_decoratorF   s   .z4generic_expander.<locals>.generic_expander_decorator)pop)rL   kwargsrj   r6   ri   r7   generic_expander;   s   2rm   zborder-colorzborder-stylezborder-widthmarginpaddingbleedc           
      c   s&   g }dD ](}| d }dkr|| |  q||d|  | ||d   qtjt||d}t| || }rG| E dH  dS t| dkrR| d9 } n(t| dkr]| d9 } nt| d	krk| | d f7 } nt| dkrztd
t|  t	|| D ]\}}	t
|	g||dd\}|V  qdS )z>Expand properties setting a token for the four sides of a box.z-topz-rightz-bottomz-leftr8   NrU   r      r      %Expected 1 to 4 token components got Tr[   )rfindappendr]   r^   expand_four_sidesrN   itemslenr   zipr+   )
r3   r-   rV   rL   suffixirK   rb   expanded_namerM   r6   r6   r7   rx   {   s8   	(


rx   zborder-radiuszborder-top-left-radiuszborder-top-right-radiuszborder-bottom-right-radiuszborder-bottom-left-radiusT)rT   c           
      c   s,   g  }}g }| D ]&}|j dkr*|jdkr*||u r&|| d kr#td|}q	td|| q	|s8|dd }||fD ]5}t|dkrI|d9 }q<t|d	krT|d	9 }q<t|d
krb||d  q<t|dkrqtdt| q<d}t|t||D ]\}	} d|	 d}t| ||dd || fV  q|dS )z-Validator for the ``border-radius`` property.literal/rr   z"Expected value after "/" separatorzExpected only one "/" separatorNr   rs   r   rt   ru   )ztop-leftz	top-rightzbottom-rightzbottom-leftzborder-z-radiusTr[   )typer=   r   rw   rz   r{   r+   )
r3   r-   rV   current
horizontalverticalrM   valuescornerscornerr6   r6   r7   border_radius   s>   

r   z
list-style-type	-position-imagec           	      c   s    d }}d}| D ]:}t |dkr|d7 }|}q	t|g|dur%d}d}nt|gdur/d}nt|gdur;d	}d}nt||gfV  q	|sR|rRd	|gfV  |d8 }|s`|r`d|gfV  |d8 }|rdtdS )
zyExpand the ``list-style`` shorthand property.

    See https://www.w3.org/TR/CSS21/generate.html#propdef-list-style

    Fr   noner   Nr   Tr   r   )r   r&   r'   r(   r   )	r3   r-   rV   type_specifiedimage_specified
none_countrM   
none_tokenr|   r6   r6   r7   expand_list_style   s6   r   borderc                 c   s,    dD ]}t | || |D ]}|V  qqdS )zlExpand the ``border`` shorthand property.

    See https://www.w3.org/TR/CSS21/box.html#propdef-border

    rq   N)expand_border_side)r3   r-   rV   r|   new_propr6   r6   r7   expand_border   s   r   z
border-topzborder-rightzborder-bottomzborder-leftzcolumn-ruleoutline-width-color-stylec                 c   sZ    | D ]'}t |durd}nt|gdurd}nt|gdur"d}nt||gfV  qdS )ztExpand the ``border-*`` shorthand properties.

    See https://www.w3.org/TR/CSS21/box.html#propdef-border-top

    Nr   r   r   )r   r   r   r   )r3   r-   rM   r|   r6   r6   r7   r     s   r   
backgroundc                 #   s   dt | }|dv rD ]}||fV  qdS tjt| d}t| | }r0| E dH  dS d fdd	}tt| }|t|dd	\}}	d
d |	 D }
|D ]} || \}}| D ]\}}|
| 	| q_qS|
 D ]\}}||ddd fV  qpd|fV  dS )zwExpand the ``background`` shorthand property.

    See https://drafts.csswg.org/css-backgrounds-3/#the-background

    )background-colorzbackground-imagezbackground-repeatzbackground-attachmentzbackground-positionzbackground-sizezbackground-clipzbackground-origin)rX   rW   NrU   Fc                    sV  i   fdd}| d d d } | r|dt | dd  d d d r)| dd = q| dd  }|r=|dt|r=|   q|dt|rK|   q|dt |rX|   q|dt|re|   qd	t|  d  D ]d}| | d  d d d }t|}|d ur|d
|sJ | | d = | r| d jdkr| d j	dkrdt|  d  D ]$}| | d d d d }t
|}|d ur|d|sJ | | d = q nqn|d urq|dt|r|   | dd  }|dt|r|   n|dt| qt dtd }	D ]}
|
 vr%|
dkr%t|
dd d  |
< q|	 fS )Nc                    s.   |d u rdS d|  } |  v rt | | < dS )NFzbackground-T)r   )r-   r=   ra   r6   r7   add9  s   
z3expand_background.<locals>.parse_layer.<locals>.addrr   repeatcolorimage
attachment)rs   rt   r   r   positionr   r   )rt   r   sizeoriginclipr   background_colorr8   _r   )r   single_valuer)   rk   r   r   rz   r   r   r=   r   r   r   r   replace)r3   final_layerr   rM   nn_tokensr   r   
next_tokenr   r-   rV   rL   r   r7   parse_layer6  sr   




z&expand_background.<locals>.parse_layerT)r   c                 S   s   i | ]\}}||gqS r6   r6   )rF   r<   r=   r6   r6   r7   rI   |  s    z%expand_background.<locals>.<dictcomp>rr   r   )F)
r   r]   r^   expand_backgroundrN   ry   reversedr   nextrw   )r3   r-   rV   rZ   rK   rb   r   layersr   
last_layerra   r   layerr=   r   r6   r   r7   r     s2   Dr   ztext-decoration-linec           	      c   s    g }g }g }d}| D ];}t |}|dv r%|| |rt|dkr$d}q|dv r3|r-t|| qt|}|du r=t|rAt|| q|rNd|fV  |rUd|fV  |r^d	|fV  dS dS )
z2Expand the ``text-decoration`` shorthand property.F)r   	underlineoverlinezline-throughblinkr   T)soliddoubledotteddashedwavyNr   r   r   )r   rw   r   r   )	r3   r-   text_decoration_linetext_decoration_colortext_decoration_stylenone_in_linerM   rZ   r   r6   r6   r7   expand_text_decoration  s>   


r   c                 c   sd    t | }|ddd }|dv r|| fV  dS |dkr0t| d j| d jd}||gfV  dS t)zExpand legacy ``page-break-before`` and ``page-break-after`` properties.

    See https://www.w3.org/TR/css-break-3/#page-break-properties

    r8   r   )autoleftrightavoidalwaysr   pageN)r   splitr   source_linesource_columnr   )r3   r-   rZ   rc   rM   r6   r6   r7   expand_page_break_before_after  s   r   zpage-break-afterzbreak-afterc                 C   
   t | |S )ztExpand legacy ``page-break-after`` property.

    See https://www.w3.org/TR/css-break-3/#page-break-properties

    r   r3   r-   r6   r6   r7   expand_page_break_after     
r   zpage-break-beforezbreak-beforec                 C   r   )zuExpand legacy ``page-break-before`` property.

    See https://www.w3.org/TR/css-break-3/#page-break-properties

    r   r   r6   r6   r7   expand_page_break_before  r   r   zpage-break-insidebreak-insidec                 c   s$    t | }|dv rd| fV  dS t)zyExpand the legacy ``page-break-inside`` property.

    See https://www.w3.org/TR/css-break-3/#page-break-properties

    )r   r   r   N)r   r   r3   r-   rZ   r6   r6   r7   expand_page_break_inside  s
   r   columnscolumn-widthcolumn-countc                 c   s    d}t | dkrt| d dkr| ddd } | D ]"}t|gdur*|dkr*d}nt|gdur4d}nt||gfV  qt | dkr_|dkrIdnd}t| d j| d jd}||gfV  dS dS )	z*Expand the ``columns`` shorthand property.Nr   r   r   rr   r   r   r   )rz   r   r   r   r   r   r   r   )r3   r-   rM   r6   r6   r7   expand_columns  s$   r   zfont-variantz-alternatesz-capsz-east-asianz
-ligaturesz-numericc                 C   s   t | S )zoExpand the ``font-variant`` shorthand property.

    https://www.w3.org/TR/css-fonts-3/#font-variant-prop

    r   r   r6   r6   r7   font_variant  s   	r   font-variant-caps-weight-stretch-sizeline-height-familyc                 c   sZ   t | }|dv rtdtt| } tdD ]@}|  }t|dkr$qt|gdur.d}nt|dv r7d}nt|gdurAd	}nt	|gdurKd
}n n||gfV  | sWtq| s\t|  }t
|gdu ritd|gfV  | sst|  }|jdkr|jdkr|  }t|gdu rtd|gfV  n| | |   t| du rtd| fV  dS )z_Expand the ``font`` shorthand property.

    https://www.w3.org/TR/css-fonts-3/#font-prop

    )captioniconmenuzmessage-boxzsmall-captionz
status-barzSystem fonts are not supportedrs   normalNr   )r   z
small-capsr   r   r   r   r   r   r   r   )r   r   listr   rangerk   r   r#   r$   r"   r!   r   r=   r%   rw   reverser    )r3   r-   expand_font_keywordr   rM   r|   r6   r6   r7   expand_font  sR   	
r   z	word-wrapoverflow-wrapc                 c   s$    t | }|du rtd| fV  dS )zhExpand the ``word-wrap`` legacy property.

    See https://www.w3.org/TR/css-text-3/#overflow-wrap

    Nr   )r*   r   r   r6   r6   r7   expand_word_wrapP  s
   r   flex-grow-shrink-basisc                 c   s   t | }|dkr6| d j| d j}}t||ddd}t||d}d|gfV  d|gfV  d|gfV  dS d\}}}	d	\}
}}| D ]J}|jd
koT|jdkoTt|
|f }|sg|sgt|g}|durg|}	d}qB|
syt	|g}|du rtt
|}d}
qB|st	|g}|du rt
|}d}qBt
| d j| d j}}t| rt|nd}t| rt|nd}t||||t|}t||||t|}|st||dddd}	d|gfV  d|gfV  d|	gfV  dS )zExpand the ``flex`` property.r   r   0r   r   r   r   )r   r   N)FFFnumberNTpx)r   r   r   r   r   r   	int_valueallr   r   r   float
is_integerintstrr   )r3   r-   rZ   linecolumn
zero_token
auto_tokengrowshrinkbasis
grow_foundshrink_foundbasis_foundrM   forced_flex_factor	new_basisnew_grow
new_shrinkint_grow
int_shrink
grow_tokenshrink_tokenr6   r6   r7   expand_flex^  s`   




r  z	flex-flowflex-direction	flex-wrapc                 c   s    t | dkr:| | ddd fD ]'}t|d g}t|d g}|r7|r7d|d gfV  d|d gfV   dS qtt | dkrht| d g}|rSd| d gfV  dS t| d g}|rfd| d gfV  dS tt)z"Expand the ``flex-flow`` property.r   Nrr   r   r   r  r  )rz   r   r   r   )r3   r-   sorted_tokens	directionwrapr6   r6   r7   expand_flex_flow  s(   r  z
line-clamp	max-linescontinueblock-ellipsisc           
      c   s   t | dkrwt| }|dkr:| d j| d j}}t||d}t||d}d|gfV  d|gfV  d|gfV  d	S | d jdkru| d jd	uru| d j| d j}}t||d}t||d
}d| d gfV  d|gfV  d|gfV  d	S tt | dkr| d jdkr| d j}t| d g}	|r|	d	ur| d j| d j}}t||d
}d| d gfV  d|gfV  d| d gfV  d	S ttt)z#Expand the ``line-clamp`` property.r   r   r   r   r  r  r  r   Ndiscardr   )	rz   r   r   r   r   r   r   r   r   )
r3   r-   rZ   r   r   r   r   discard_token	max_linesellipsisr6   r6   r7   expand_line_clamp  s>   
r  z
text-align-all-lastc                 c   s    t | dkrQt| }|du rt|dkr'| d j| d j}}t||d}n| d }d|gfV  |dkrG| d j| d j}}t||d}n|}d|gfV  dS t)	z#Expand the ``text-align`` property.r   Nzjustify-allr   justifyr  startr  )rz   r   r   r   r   r   )r3   r-   rZ   r   r   	align_all
align_lastr6   r6   r7   expand_text_align  s    r#  )FrB   r]   tinycss2.astr   r   r   tinycss2.color3r   
propertiesr   utilsr   r	   r
   r   r   r   descriptorsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   rO   r,   rN   rK   rm   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r#  r6   r6   r6   r7   <module>   s     t		
@$%)


f
&		
	F
8
&