a
    ¥Q•hyÝ  ã                   @   sü  d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ d	d
lmZ ejejejejejejejejejejejejejejejejejejdœZdd„ eddƒD ƒZ e  !dddœ¡ e "d¡Z#e "d¡Z$e "d¡Z%dd„ Z&dd„ Z'dd„ Z(dTdd„Z)dd„ Z*d d!„ Z+dUd"d#„Z,dVd$d%„Z-d&d'„ Z.d(d)„ Z/d*d+„ Z0d,d-„ Z1e "d.¡j2fd/d0„Z3dWd1d2„Z4d3d4„ Z5d5d6„ Z6d7d8„ Z7d9d:„ Z8d;d<„ Z9d=d>„ Z:d?d@„ Z;dXdBdC„Z<dDdE„ Z=dFdG„ Z>dHdI„ Z?dJdK„ Z@dYdLdM„ZAdNdO„ ZBdPdQ„ ZCdRdS„ ZDdS )Zz’Turn an element tree with style into a "before layout" box tree.

This includes creating anonymous boxes and processing whitespace as necessary.

é    Né   )Úhtml)Ú
propertiesÚtargets)Úcollapse_table_borders)ÚLOGGER)Úget_lang_quotesé   )Úboxes)©ÚblockÚflow©Úinliner   )r   ú	flow-root)r   r   )r   Útable)r   r   )r   Úflex)r   r   )r   Úgrid)r   r   )z	table-row)ztable-row-group©ztable-header-group©ztable-footer-group)ztable-column)ztable-column-group)z
table-cell)ztable-captionc                 C   s   i | ]}|t |d  ƒ“qS )iàþ  )Úchr)Ú.0Úi© r   ú^/var/www/sistema_ama/venv/lib/python3.9/site-packages/weasyprint/formatting_structure/build.pyÚ
<dictcomp>-   ó    r   é!   é   u   ã€€u   âˆ’)é    é-   z
?z[	 ]*
[	 ]*z[	 ]+c                 C   s,   t | ƒ} t| ƒ} t| ƒ} t| ƒ} t| ƒ} | S )zDCreate anonymous boxes in box descendants according to layout rules.)Úanonymous_table_boxesÚ
flex_boxesÚ
grid_boxesÚinline_in_blockÚblock_in_inline©Úboxr   r   r   Úcreate_anonymous_boxes5   s    r(   c           
         sh   t ˆ ˆ|||||ƒ}|r |\}n&d‡ ‡fdd„	}	t ˆ |	|||||ƒ\}| ¡  d|_t|ƒ}t|ƒ}|S )z=Build a formatting structure (box tree) from an element tree.Nc                    s0   ˆ| |ƒ}|d ur,| ˆ kr$d|d< nd|d< |S )Nr   Údisplay©Únoner   )ÚelementÚpseudo_typeÚstyle©Úelement_treeÚ	style_forr   r   Úroot_style_forJ   s    

z2build_formatting_structure.<locals>.root_style_forT)N)Úelement_to_boxZcheck_pending_targetsÚis_for_root_elementr(   Úset_viewport_overflow)
r0   r1   Úget_image_from_uriÚbase_urlÚtarget_collectorÚcounter_styleÚ	footnotesÚbox_listr'   r2   r   r/   r   Úbuild_formatting_structure?   s     þþr<   c                 C   s   t |d d d…  | |||ƒS )Nr)   r   )ÚBOX_TYPE_FROM_DISPLAY)Úelement_tagr.   Úcontentr,   r   r   r   Úmake_box_   s    ÿr@   c              
   C   s`  t | jtƒsg S || ƒ}|d }	|	dkr,g S |d dkrV|d dkrNd|d< nd|d< t| j|g | ƒ}
|d	u r„d
gdd
gidhgf}|\}}}t||ƒ g }| tƒ ¡ || dƒ|
_|| dƒ|
_g }d|d v ròt	t
| ||||||ƒƒ}| |¡ | t| d|||||ƒ¡ |d �r(| |d ||
¡ | j}|�rH| tj |
|¡¡ | D ]ð}t||||||||ƒ}|�rÞ|d
 jd dk�rÞ|d
 }d|jd< | |¡ || dƒ}t| j› d�|g | ƒ}t|||||||ƒ|_||_|g}| |¡ |j}|�rLtj |
|¡}|�r0t |d tjƒ�r0|d  j|j7  _n
| |¡ �qL| t| d|||||ƒ¡ | ¡ D ]&}||  ¡  || �s`| |¡ �q`||
_t|
ƒ t| |
||||ƒ t|
ƒ |�rêt|
jƒdk�rê|d dk�rê|
j tj |
d¡¡ |d dk�rP|d d  d7  < || dƒ}t| j› d�|g | ƒ}t||
|||||ƒ|_|
j d
|¡ t | |
||¡S )a!  Convert an element and its children into a box with children.

    Return a list of boxes. Most of the time the list will have one item but
    may have zero or more than one.

    Eg.::

        <p>Some <em>emphasised</em> text.</p>

    gives (not actual syntax)::

        BlockBox[
            TextBox['Some '],
            InlineBox[
                TextBox['emphasised'],
            ],
            TextBox[' text.'],
        ]

    ``TextBox``es are anonymous inline boxes:
    See https://www.w3.org/TR/CSS21/visuren.html#anonymous

    r)   r*   ÚfloatÚfootnoteÚfootnote_displayr   r   r   Nr   úfirst-letterz
first-lineú	list-itemÚbeforeÚanchorr+   zfootnote-callz::footnote-calléÿÿÿÿÚafterr	   Úlist_style_positionÚoutsideu   â€‹zfootnote-markerz::footnote-marker) Ú
isinstanceÚtagÚstrr@   Úupdate_countersÚappendÚsetZfirst_letter_styleZfirst_line_styleÚlistÚmarker_to_boxÚextendÚbefore_after_to_boxZstore_targetÚtextr
   ÚTextBoxÚanonymous_fromr3   r.   Úcontent_to_boxesÚchildrenrB   ÚtailÚpopÚprocess_whitespaceÚset_content_listsÚprocess_text_transformÚlenÚinsertr   Úhandle_element)r,   r1   r6   r7   r8   r9   r:   Ústater.   r)   r'   Úquote_depthÚcounter_valuesÚcounter_scopesrZ   Úmarker_boxesrV   Zchild_elementZchild_boxesrB   Z
call_styleZfootnote_callÚtext_boxÚnameZmarker_styleÚmarkerr   r   r   r3   d   sº    
û


þ

þ
þ


ÿþ

þ
ÿ
ÿ
þr3   c              
   C   sð   || |ƒ}|r|du rg S |d }|dkr.g S |d }	|	dv rBg S t | j› d|› �|g | ƒ}
|\}}}t||ƒ g }d|v ržtt| ||||||ƒƒ}| |¡ | t||
|||||ƒ¡ ||
_|d d	krê|\}}}t| |
|d
 |||ƒ |
gS )z8Return the boxes for ::before or ::after pseudo-element.Nr)   r*   r?   )ÚnormalÚinhibitr+   ú::rE   Úbookmark_levelr+   Úbookmark_label)	r@   rM   rO   rR   rS   rT   rY   rZ   Úcompute_bookmark_label)r,   r-   rc   r1   r6   r8   r9   r.   r)   r?   r'   rd   re   Ú_counter_scopesrZ   rg   Ú_quote_depthr   r   r   rU     s>    



þ

þ
þrU   c              
   c   s˜  || dƒ}g }|\}	}
}t | j› d�||| ƒ}|d dkr>dS |d \}}|d dvrr| t|||	|
|||ƒ¡ nš|d	krª|||d
 d�}|durªtj ||¡}| |¡ |�s|d dk�r|
 ddg¡d }|d }| 	||¡ }�rtj
 ||¡}d|jd< | |¡ |�sdS |d dk�r€tj ||¡}d|jd< |d dk�rXt dd¡}nt dd¡}tj}d||fff|jd< ntj ||¡}|V  dS )zyYield the box for ::marker pseudo-element if there is one.

    https://drafts.csswg.org/css-lists-3/#marker-pseudo

    rj   ú::markerr)   r*   NÚlist_style_imager?   )rk   rl   ÚurlÚimage_orientation©ru   ÚorientationÚlist_style_typer+   rE   r   rH   zpre-wrapÚwhite_spacerJ   rK   ÚabsoluteÚpositionÚ	directionÚltriœÿÿÿú%éd   Ú	translateÚ	transform)r@   rM   rT   rY   r
   ÚInlineReplacedBoxrX   rP   ÚgetÚrender_markerrW   r.   ÚBlockBoxr   Ú	DimensionÚZERO_PIXELSÚ	InlineBox)r,   rc   Úparent_styler1   r6   r8   r9   r.   rZ   rd   re   rq   r'   Z
image_typeÚimageÚcounter_valueÚcounter_typeZmarker_textZ
marker_boxZtranslate_xZtranslate_yr   r   r   rS   3  sN    


þÿ




ÿrS   c           .         s:  g ‰ t ƒ ‰‡ ‡‡fdd„}g }i }|duo0|du}|jo<| }ˆjdu r\dd„ | ¡ D ƒˆ_| D �]°\}}|dkr|||ƒ q`|dkrÎ|durÎ|\}}|dkržq`||ˆjd	 d
�}|durÌˆ  tj ˆ|¡¡ q`|dkrît	|ˆƒ}|| 
¡ ƒ q`|dk�r*|�st dd |¡¡ q`||j|g|¢R Ž ƒ q`|dv �rÔ|d |d  }‰ˆdk�rRq`|�rt|t|ƒ| v�rt| |¡ |dk�rž| |dg¡d }ˆ |ˆ¡}n,|d }| ‡‡fdd„| |dg¡D ƒ¡}||ƒ q`|dv �rø|dd… |d  \}}‰ˆdk�rq`| |ˆ||¡}|jdk�r& �q|jj}|�rh||v�rht |¡} | | g ¡}||v�rh| |¡ |j ¡ }!|! |¡ |dk�r¦|! |dg¡d }ˆ |ˆ¡}nH|d }|d dk�rÂ �q|d }"|" ‡‡fdd„|! |dg¡D ƒ¡}||ƒ q`|dk�rL|\}}#| |ˆ||¡}|jdk�rD|j}$t	|#|$ƒ}|| 
¡ ƒ n �qq`|dk�rd||	fv�rd|v }%| d¡ �o€|	dk}&|%�sžtd|d d ƒ|d< |&�rð|	d k�r¼t|
ƒ\}'}(n|	\}'}(|%�rÎ|'n|(})||)t|d t|)ƒd ƒ ƒ |%�r|d  d7  < q`|d!k�r¸|�s,t d"d |¡¡ q`|j |g|¢R Ž }*|*du �rJq`|* !¡ }*d#|*jd$< t"|*tj#ƒ�r¬|* $¡ D ]8}+|+jd% d&v �rŠ�qrt%|+j|+||||ˆ||d'�	|+_&�qrˆ  |*¡ q`|d(kr`|d �sÌq`tj' ˆ|d ¡},tj( ˆ|,f¡}-d)|-jd*< d)|,jd*< d+|-_)ˆ  |-¡ q`ˆ�s ˆ �r6| *ˆ||||¡ ˆ S dS ),ae  Compute and return the boxes corresponding to the ``content_list``.

    ``parse_again`` is called to compute the ``content_list`` again when
    ``target_collector.lookup_target()`` detected a pending target.

    ``build_formatting_structure`` calls
    ``target_collector.check_pending_targets()`` after the first pass to do
    required reparsing.

    c                    sN   ˆ  d¡ | rJˆ r6tˆ d tjƒr6ˆ d  j| 7  _nˆ  tj ˆ| ¡¡ d S )NTrH   )ÚaddrL   r
   rW   rV   rP   rX   ©rV   )Úcontent_boxesÚhas_textÚ
parent_boxr   r   Úadd_textŒ  s    
ÿz&compute_content_list.<locals>.add_textNc                 S   s   i | ]\}}||  ¡ “qS r   )Úcopy)r   ÚkeyÚvaluer   r   r   r      s   z(compute_content_list.<locals>.<dictcomp>Ústringru   Úexternalrv   rw   z	content()zstring()z,"string(%s)" is only allowed in page marginsú )ú	counter()z
counters()r   rH   r+   rš   r	   c                 3   s   | ]}ˆ   |ˆ¡V  qd S ©N©Úrender_value©r   rŒ   ©r9   r�   r   r   Ú	<genexpr>É  s   ÿz'compute_content_list.<locals>.<genexpr>)útarget-counter()ztarget-counters()r   z
up-to-dater¡   c                 3   s   | ]}ˆ   |ˆ¡V  qd S r›   rœ   rž   rŸ   r   r   r    è  s   ÿztarget-text()ÚquoteÚopenzno-Úautoz	element()z-"element(%s)" is only allowed in page marginsÚstaticr|   r?   )rk   r+   )ÚcontextÚpagezleader()Úprerz   T)+rQ   Z
collectingÚcached_counter_valuesÚitemsr.   rP   r
   rƒ   rX   Úextract_textÚstripr   ÚwarningÚjoinÚget_string_set_forrR   r„   r�   Úlookup_targetrc   Ú
target_boxr   Zanchor_name_from_tokenÚ
setdefaultZcached_page_counter_valuesr”   ÚupdateÚ
startswithÚmaxr   Úminr`   Úget_running_element_forÚdeepcopyrL   Ú	ParentBoxÚdescendantsrY   rZ   rW   r‰   Ú	is_leaderZcollect_missing_counters).Úcontent_listr’   re   Ú	css_tokenÚparse_againr8   r9   r6   rd   Zquote_styleÚlangr¦   r§   r,   r“   Zmissing_countersZmissing_target_countersZin_page_contextZneed_collect_missingÚtype_r–   ÚoriginÚurir‹   Z
added_textÚcounter_namerŒ   rV   Ú	separatorÚanchor_tokenr°   Ztarget_valuesÚanchor_nameÚlocal_countersZseparator_stringZ
text_styler±   Zis_openra   Zopen_quotesZclose_quotesÚquotesÚnew_boxÚchildrh   Z
leader_boxr   )r�   r9   r�   r‘   r’   r   Úcompute_content_listu  s   	
ÿ

ÿÿ

þ



þ


ÿ
ÿ




þ

ÿ


þ


ý
ÿ

þrË   c	                    sl   d‡ ‡‡‡‡‡fdd„	}	ˆd dkr(g S |dd… ‰d}
t ˆd ˆ||
|	ˆˆ ˆ|ˆd ˆd ||ƒ}|pjg S )	z:Take the value of a ``content`` property and return boxes.Nc              
      sx   | du ri }n|   ¡ }| ˆj¡ g }| tˆˆˆ|ˆˆˆ ƒ¡ tˆjƒdkrntˆjd tj	ƒrn|ˆjd _n|ˆ_dS )z9Closure to parse the ``parent_boxes`` children all again.Nr	   r   )
r”   r³   r©   rT   rY   r`   rZ   rL   r
   ÚLineBox)Úmixin_pagebased_countersrÇ   Zlocal_children©r9   r6   Zorig_quote_depthr’   r.   r8   r   r   r¾   5  s    þÿz%content_to_boxes.<locals>.parse_againr?   rl   rÈ   r¿   )N)rË   )r.   r’   rd   re   r6   r8   r9   r¦   r§   r¾   r½   r;   r   rÎ   r   rY   1  s    ýrY   c              
      s’   d
‡ ‡‡‡‡‡fdd„	}dˆ› �}t ˆˆ |||ˆˆˆd�}	|	durŽd dd„ |	D ƒ¡}
ˆ jD ] }|d	 ˆkr\ˆ j |¡  q~q\ˆ j ˆ|
f¡ dS )zCParse the content-list value of ``string_name`` for ``string-set``.Nc                    s:   | du ri }n|   ¡ }| ˆ j¡ tˆˆ ˆˆ|ˆˆƒ dS )z7Closure to parse the string-set string value all again.N)r”   r³   r©   Úcompute_string_set©rÍ   rÇ   ©r'   r¼   r9   r,   Ústring_namer8   r   r   r¾   ^  s    
þz'compute_string_set.<locals>.parse_againzstring-set::©r,   Ú c                 s   s    | ]}t |tjƒr|jV  qd S r›   ©rL   r
   rW   rV   ©r   r'   r   r   r   r    r  s   z%compute_string_set.<locals>.<genexpr>r   )N)rË   r®   Ú
string_setÚremoverP   )r,   r'   rÒ   r¼   re   r8   r9   r¾   r½   r;   r—   Zstring_set_tupler   rÑ   r   rÏ   [  s    

þ
ÿ
rÏ   c           	   
      sR   i f‡ ‡‡‡‡fdd„	}d}t ˆˆ |||ˆˆˆd�}|rNd dd„ |D ƒ¡ˆ _dS )	z5Parses the content-list value for ``bookmark-label``.c                    s@   | du ri }n|   ¡ }|   ¡ }| ˆ j¡ tˆˆ ˆ|ˆˆƒ dS )z.Closure to parse the bookmark-label all again.N)r”   r³   r©   rp   rÐ   ©r'   r¼   r9   r,   r8   r   r   r¾   €  s    
þz+compute_bookmark_label.<locals>.parse_againzbookmark-labelrÓ   rÔ   c                 s   s   | ]}t |ƒV  qd S r›   )Úbox_textrÖ   r   r   r   r    “  r   z)compute_bookmark_label.<locals>.<genexpr>N)rË   r®   ro   )	r,   r'   r¼   re   r8   r9   r¾   r½   r;   r   rÙ   r   rp   }  s    
þrp   c           	   	   C   sf   g |_ |d dkr@t|d ƒD ] \}\}}t| ||||||ƒ q|d dkrbt| ||d |||ƒ dS )zˆSet the content-lists values.

    These content-lists are used in GCPM properties like ``string-set`` and
    ``bookmark-label``.

    r×   r+   rn   ro   N)r×   Ú	enumeraterÏ   rp   )	r,   r'   r.   re   r8   r9   r   rÒ   Zstring_valuesr   r   r   r^   –  s    
þþr^   c           
      C   s  | \}}}|d }|d D ]:\}}||v r8||   ¡  n
| |¡ | |g ¡ |¡ q|d D ]@\}}| |g ¡}|s–||vs‚J ‚| |¡ | d¡ ||d< q^|d }	|	dkrÈd|d v rÄd	g}	ng }	|	D ]J\}}| |g ¡}|�s||vsòJ ‚| |¡ | d¡ |d  |7  < qÌd
S )z$Handle the ``counter-*`` properties.rH   Úcounter_resetÚcounter_setr   Úcounter_incrementr¤   rE   r)   )rE   r	   N)r\   rŽ   r²   rP   )
rc   r.   rr   re   rf   Zsibling_scopesri   r–   ÚvaluesrÞ   r   r   r   rO   ª  s4    






rO   z\Sc                 C   s   t | tjƒo|| jƒ S )z9Return True if ``box`` is a TextBox with only whitespace.rÕ   )r'   Z_has_non_whitespacer   r   r   Úis_whitespaceÑ  s    rà   c                 #   s€   |du r‡ fdd„}g }|D ]@}||ƒrR|rJˆ j | g d�}t||ƒV  g }|V  q| |¡ q|r|ˆ j | g d�}t||ƒV  dS )z£Wrap consecutive children that do not pass ``test`` in a ``wrapper_type`` box.

    ``test`` defaults to children being of the same type as ``wrapper_type``.

    Nc                    s
   t | ˆ ƒS r›   )rL   ©rÊ   ©Úwrapper_typer   r   ÚtestÝ  s    zwrap_improper.<locals>.test)rZ   )rX   Útable_boxes_childrenrP   )r'   rZ   rã   rä   ZimproperrÊ   Úwrapperr   râ   r   Úwrap_improperÖ  s    rç   c                 C   s2   t | tjƒr|  ¡ r| S dd„ | jD ƒ}t| |ƒS )z Remove and add boxes according to the table model.

    Take and return a ``Box`` object.

    See https://www.w3.org/TR/CSS21/tables.html#anonymous-boxes

    c                 S   s   g | ]}t |ƒ‘qS r   )r!   ©r   rÊ   r   r   r   Ú
<listcomp>ü  r   z)anonymous_table_boxes.<locals>.<listcomp>)rL   r
   r¹   Ú
is_runningrZ   rå   ©r'   rZ   r   r   r   r!   ð  s    r!   c                    sä  t ˆ tjƒrg }nTt ˆ tjƒrfdd„ |D ƒ}|sfˆ jdu sDˆ jdk rJd}nˆ j}‡ fdd„t|ƒD ƒ}ˆ jrÒt|ƒdkrÒ|dd… \}}|jržt	|ƒrž| 
¡  t|ƒdkrÒ|dd… \}}|jrÒt	|ƒrÒ| 
d¡ d	d„ tdg|dd
…  ||dd… dg ƒD ƒ}t ˆ tjƒ�r(tˆ |tjdd„ ƒ}nt ˆ tjƒ�rDtˆ |tjƒ}t ˆ tjƒ�rbtˆ |tjƒ}ntˆ |tjdd„ ƒ}t ˆ tjƒ�rštˆ |tjdd„ ƒ}n tˆ ƒ‰tˆ |tj‡fdd„ƒ}t ˆ tjƒ�rÒtˆ |ƒS t|ƒˆ _ˆ S dS )z3Internal implementation of anonymous_table_boxes().c                 S   s   g | ]}t |tjƒr|‘qS r   )rL   r
   ÚTableColumnBoxrè   r   r   r   ré     s   ÿz(table_boxes_children.<locals>.<listcomp>Nr	   c                    s   g | ]}t j ˆ g ¡‘qS r   )r
   rì   rX   )r   Ú_r&   r   r   ré     s   ÿr   éþÿÿÿr   c                 S   s2   g | ]*\}}}|r*|j r*|r*|j r*t|ƒs|‘qS r   )Úinternal_table_or_captionrà   )r   Z
prev_childrÊ   Z
next_childr   r   r   ré   *  s
   

örH   c                 S   s   | j S r›   ©Úproper_table_childrá   r   r   r   Ú<lambda>=  r   z&table_boxes_children.<locals>.<lambda>c                 S   s   t | tjƒ S r›   )rL   r
   ÚTableCellBoxrá   r   r   r   rò   I  r   c                 S   s   | j  S r›   rð   rá   r   r   r   rò   O  r   c                    s   | j  pˆ | jv S r›   )rñ   Úproper_parentsrá   )Úparent_typer   r   rò   T  s   )rL   r
   rì   ÚTableColumnGroupBoxÚspanÚrangeÚtabular_containerr`   rï   rà   r\   ÚzipÚTableBoxrç   ÚTableRowBoxÚTableRowGroupBoxró   r‰   ÚInlineTableBoxÚtypeÚ
wrap_tablerR   rZ   )r'   rZ   r÷   ÚinternalrV   r   )r'   rõ   r   rå      sn    ÿ
ÿ
ýþþþþ
þ

rå   c           !   
   C   s$  g }g }g }t j|t j|t j|t j|t j|i}|D ]}|t|ƒ  |¡ q2g g dœ}|D ]}||jd   |¡ qXt	t
| |t jƒƒ}	d}
|	D ]6}|
|_|jr¸|jD ]}|
|_|
d7 }
q¢qŒ|
|j7 }
qŒ|
}t
| |t jƒ}g }d}d}|D ]X}|jd }|dk�r|du �rd|_|}qæ|d	k�r4|du �r4d|_|}qæ| |¡ qæ|du�rP|gng | |du�rf|gng  }d}|D ]ò}d
d„ |jD ƒ}|jD ]Æ}| d¡}d}
|jD ]ª}|
|v �rÀ|
d7 }
�qª|
|_|
|j }|jdk�r@t|ƒd }|jdk�r |}||_n t|j|ƒ|_|d|jd … }t|
|ƒ}|D ]}| |¡ �q.|}
t||
ƒ}�q¦�qŽ|t|jƒ7 }�qt|  |¡}|j ¡ |_t|	ƒ|_|jd dk�r¦t|||ƒ|_t| t jƒ�r¼t j }nt j!}| "| |d |g |d  ¡}|j ¡ |_d|_#t$j%D ]&} |j|  |j| < t$j&|  |j| < �qø|S )a•  Take a table box and return it in its table wrapper box.

    Also re-order children and assign grid positions to each column and cell.

    Because of colspan/rowspan works, grid_y is implicitly the index of a row,
    but grid_x is an explicit attribute on cells, columns and column group.

    https://www.w3.org/TR/CSS21/tables.html#model
    https://www.w3.org/TR/CSS21/tables.html#table-layout

    )ÚtopÚbottomÚcaption_sider   r	   Nr)   r   Tr   c                 S   s   g | ]
}t ƒ ‘qS r   )rQ   )r   Úrowr   r   r   ré   «  r   zwrap_table.<locals>.<listcomp>Úborder_collapseÚcollapser  r  )'r
   rì   rö   rü   rý   ÚTableCaptionBoxrÿ   rP   r.   rR   rç   Úgrid_xrZ   r÷   Ú	is_headerÚ	is_footerr\   ÚcolspanÚrowspanr`   r¶   rø   r³   rµ   Úcopy_with_childrenr”   ÚtupleÚcolumn_groupsr   Zcollapsed_border_gridrL   rþ   ÚInlineBlockBoxr†   rX   Úis_table_wrapperr   ÚTABLE_WRAPPER_BOX_PROPERTIESÚINITIAL_VALUES)!r'   rZ   ÚcolumnsÚrowsZall_captionsZby_typerÊ   ZcaptionsÚcaptionr  r	  ÚgroupÚcolumnZ
grid_widthZ
row_groupsZbody_row_groupsÚheaderÚfooterr)   Zgrid_heightZoccupied_cells_by_rowr  Zoccupied_cells_in_this_rowÚcellZ
new_grid_xZmax_rowspanZspanned_rowsZspanned_columnsZoccupied_cellsr   rã   ræ   ri   r   r   r   r   ^  s°    û
ÿ

ÿþÿ







ÿÿ
r   c                 C   s8   t | tjƒr|  ¡ r| S dd„ | jD ƒ}t| |ƒ| _| S )z—Remove and add boxes according to the flex model.

    Take and return a ``Box`` object.

    See https://www.w3.org/TR/css-flexbox-1/#flex-items

    c                 S   s   g | ]}t |ƒ‘qS r   )r"   rè   r   r   r   ré   í  r   zflex_boxes.<locals>.<listcomp>)rL   r
   r¹   rê   rZ   Úflex_childrenrë   r   r   r   r"   á  s
    r"   c                 C   sÎ   t | tjƒrÆg }|D ]¬}dd„ |_| ¡ r0d|_t |tjƒrJ|j d¡sJqt |tj	ƒr€tj
 ||j¡}|j|_d|_| |¡ qt |tjƒr¶tj
 ||g¡}|j|_d|_| |¡ q| |¡ q|S |S d S )Nc                   S   s   dS )NFr   r   r   r   r   rò   ö  r   zflex_children.<locals>.<lambda>Tr™   )rL   r
   ÚFlexContainerBoxÚ
is_floatedÚis_in_normal_flowÚis_flex_itemrW   rV   r¬   r  r†   rX   rZ   r.   rP   ÚInlineLevelBox)r'   rZ   r  rÊ   Ú	anonymousr   r   r   r  ò  s*    
r  c                 C   s8   t | tjƒr|  ¡ r| S dd„ | jD ƒ}t| |ƒ| _| S )z–Remove and add boxes according to the grid model.

    Take and return a ``Box`` object.

    See https://drafts.csswg.org/css-grid-2/#grid-item

    c                 S   s   g | ]}t |ƒ‘qS r   )r#   rè   r   r   r   ré     r   zgrid_boxes.<locals>.<listcomp>)rL   r
   r¹   rê   rZ   Úgrid_childrenrë   r   r   r   r#     s
    r#   c                 C   sÊ   t | tjƒrÂg }|D ]¨}| ¡ r&d|_t |tjƒr@|j d¡s@qt |tjƒrvtj	 
||j¡}|j|_d|_| |¡ qt |tjƒr²tj	 
||g¡}|j|_d|_d|_| |¡ q| |¡ q|S |S d S )NTr™   F)rL   r
   ÚGridContainerBoxr   Úis_grid_itemrW   rV   r¬   r  r†   rX   rZ   r.   rP   r"  )r'   rZ   r$  rÊ   r#  r   r   r   r$     s*    r$  Fc                 C   s  t | tjƒr®| j}|s|S t d|¡}| jd dv }| jd dv }|rRt d|¡}|rb| dd¡}|r¢t	 d|¡ }}|r–| 
d¡r–|dd… }d| _| d¡}nd	}|| _nJ| jD ]B}t |tjtjfƒrêt||ƒ}|  ¡ rö| ¡ rö|}q´| ¡ r´d	}q´|�o|  ¡  S )
zµFirst part of "The 'white-space' processing model".

    See https://www.w3.org/TR/CSS21/text.html#white-space-model
    https://drafts.csswg.org/css-text-3/#white-space-rules

    Ú
rz   )rk   Únowrap©rk   r(  zpre-liner™   r	   NTF)rL   r
   rW   rV   ÚLINE_FEED_REÚsubr.   ÚTAB_REÚreplaceÚSPACE_REr´   Úleading_collapsible_spaceÚendswithrZ   r‰   r]   r   rê   )r'   Zfollowing_collapsible_spacerV   Znew_line_collapseÚspace_collapseZprevious_textrÊ   Zchild_collapsible_spacer   r   r   r]   =  s:    
ÿr]   c                 C   s–   t | tjƒrd| jd }|dkrDdd„ dd„ tdd„ dœ| | jƒ| _| jd dkr’| j d	d
¡| _n.|  ¡ s’| jD ]}t |tjtj	fƒrrt
|ƒ qrd S )NÚtext_transformr+   c                 S   s   |   ¡ S r›   )Úupperr�   r   r   r   rò   x  r   z(process_text_transform.<locals>.<lambda>c                 S   s   |   ¡ S r›   )Úlowerr�   r   r   r   rò   y  r   c                 S   s
   |   t¡S r›   )r�   ÚASCII_TO_WIDEr�   r   r   r   rò   {  r   )Ú	uppercaseÚ	lowercaseÚ
capitalizez
full-widthÚhyphensõ   Â­rÔ   )rL   r
   rW   r.   r8  rV   r-  rê   rZ   r‰   r_   )r'   r2  rÊ   r   r   r   r_   s  s$    
üûû
r_   c                 C   sR   d}d}| D ]@}t  |¡d }|s8|dv r8d}| ¡ }n|dkrDd}||7 }q|S )uC   Capitalize words according to CSSâ€™s "text-transform: capitalize".FrÔ   r   )ÚLÚNTÚZ)ÚunicodedataÚcategoryr3  )rV   Zletter_foundÚoutputÚletterr?  r   r   r   r8  †  s    

r8  c           
      C   s¼  | j r|  ¡ r| S t| j ƒ}|r6| jdu r6|d j| _g }d}|D ]:}|rPd|_t|tjƒrj|jsj|j}qBd}| t	|ƒ¡ qB| j
du rŽ|| _
t| tjƒs¤|| _ | S g }g }|D ]À}t|tjƒrÄJ ‚|rÜ| ¡ rÜ| |¡ q°t|tjƒsø|�r4| ¡ �s4|�s(t|tjƒ�r(|jdk�r(|jd dv �sp| |¡ q°|�rftj | |¡}tj | |g¡}	| |	¡ g }| |¡ q°|�r²tj | |¡}|�r¨tj | |g¡}	| |	¡ n
| |¡ || _ | S )a†  Build the structure of lines inside blocks and return a new box tree.

    Consecutive inline-level boxes in a block container box are wrapped into a
    line box, itself wrapped into an anonymous block box.

    This line box will be broken into multiple lines later.

    This is the first case in
    https://www.w3.org/TR/CSS21/visuren.html#anonymous-block-level

    Eg.::

        BlockBox[
            TextBox['Some '],
            InlineBox[TextBox['text']],
            BlockBox[
                TextBox['More text'],
            ]
        ]

    is turned into::

        BlockBox[
            AnonymousBlockBox[
                LineBox[
                    TextBox['Some '],
                    InlineBox[TextBox['text']],
                ]
            ]
            BlockBox[
                LineBox[
                    TextBox['More text'],
                ]
            ]
        ]

    Fr   Tr™   rz   r)  )rZ   rê   rR   r/  rL   r
   rW   rV   rP   r$   Útrailing_collapsible_spaceÚBlockContainerBoxrÌ   Úis_absolutely_positionedr"  r   r.   rX   r†   )
r'   Zbox_childrenrZ   rB  rÊ   Znew_line_childrenÚnew_childrenZ	child_boxZline_boxr#  r   r   r   r$   •  sj    &
ÿ
ÿÿ
ÿüû

r$   c           	      C   sà   | j r|  ¡ r| S g }d}| j D ]°}t|tjƒr²t| j ƒdksLJ d| j  ƒ‚d}t||d�\}}}|du rlq–tj | |g¡}| 	|¡ | 	t
|ƒ¡ qP|r¬tj | |g¡}qº|}nt
|ƒ}||urÆd}| 	|¡ q |rÜ|| _ | S )aµ  Build the structure of blocks inside lines.

    Inline boxes containing block-level boxes will be broken in two
    boxes on each side on consecutive block-level boxes, each side wrapped
    in an anonymous block-level box.

    This is the second case in
    https://www.w3.org/TR/CSS21/visuren.html#anonymous-block-level

    Eg. if this is given::

        BlockBox[
            LineBox[
                InlineBox[
                    TextBox['Hello.'],
                ],
                InlineBox[
                    TextBox['Some '],
                    InlineBox[
                        TextBox['text']
                        BlockBox[LineBox[TextBox['More text']]],
                        BlockBox[LineBox[TextBox['More text again']]],
                    ],
                    BlockBox[LineBox[TextBox['And again.']]],
                ]
            ]
        ]

    this is returned::

        BlockBox[
            AnonymousBlockBox[
                LineBox[
                    InlineBox[
                        TextBox['Hello.'],
                    ],
                    InlineBox[
                        TextBox['Some '],
                        InlineBox[TextBox['text']],
                    ]
                ]
            ],
            BlockBox[LineBox[TextBox['More text']]],
            BlockBox[LineBox[TextBox['More text again']]],
            AnonymousBlockBox[
                LineBox[
                    InlineBox[
                    ]
                ]
            ],
            BlockBox[LineBox[TextBox['And again.']]],
            AnonymousBlockBox[
                LineBox[
                    InlineBox[
                    ]
                ]
            ],
        ]

    Fr	   z9Line boxes should have no siblings at this stage, got %r.N)Ú
skip_stackT)rZ   rê   rL   r
   rÌ   r`   Ú_inner_block_in_inliner†   rX   rP   r%   )	r'   rE  ÚchangedrÊ   ÚstackZnew_liner   ZanonÚ	new_childr   r   r   r%     s<    =
ÿÿÿ
r%   c                 C   s  g }d}d}d}|du }|r"d}n|  ¡ \\}}t| j|d… ƒD ]®\}}	|| }
t|	tjƒr€|	 ¡ r€|du srJ ‚|	}|
d7 }
nPt|	tjƒr¦t|	|ƒ}d}|\}}}n|du s²J ‚t	|	ƒ}||	urÆd}| 
|¡ |durB|
|i}|  |¡}  �qqB|sü|�r|  |¡} | ||fS )aÑ  Find a block-level box in an inline formatting context.

    If one is found, return ``(new_box, block_level_box, resume_at)``.
    ``new_box`` contains all of ``box`` content before the block-level box.
    ``resume_at`` can be passed as ``skip_stack`` in a new call to
    this function to resume the search just after the block-level box.

    If no block-level box is found after the position marked by
    ``skip_stack``, return ``(new_box, None, None)``

    NFr   r	   T)rª   rÛ   rZ   rL   r
   ÚBlockLevelBoxr   r‰   rG  r%   rP   r  )r'   rF  rE  Zblock_level_boxÚ	resume_atrH  Zis_startÚskipr   rÊ   ÚindexZ	recursionrJ  r   r   r   rG  k  s@    ÿ





rG  c                 C   s\   | }| j  ¡ dkrB| jd dkrB| jD ]}|j  ¡ dkr&|} qBq&|jd | _d|jd< | S )zê
    Set a ``viewport_overflow`` attribute on the box for the root element.

    Like backgrounds, ``overflow`` on the root element must be propagated
    to the viewport.

    See https://www.w3.org/TR/CSS21/visufx.html#overflow
    r   ÚoverflowÚvisibleÚbody)r>   r4  r.   rZ   Zviewport_overflow)Úroot_boxZ
chosen_boxrÊ   r   r   r   r5      s    	ÿ

r5   c                 C   s:   t | tjƒr| jS t | tjƒr6d dd„ |  ¡ D ƒ¡S dS )NrÔ   c                 s   sD   | ]<}|j  d ¡s|j  d¡s|j  d¡st|tjƒr|jV  qdS )z::beforez::afterrs   N)r>   r0  rL   r
   rW   rV   rè   r   r   r   r    º  s   üzbox_text.<locals>.<genexpr>)rL   r
   rW   rV   r¹   r®   rº   r&   r   r   r   rÚ   ¶  s    
ÿrÚ   c                    s”   ˆ dv rt |ƒS ˆ dv rDt|tjƒr@d ‡ fdd„| ¡ D ƒ¡S dS ˆ dkr�d}d}t |ƒ}|D ]*}t |¡}|dvr‚|r~ qŒd	}||7 }q`|S d S )
N)rV   r?   )rF   rI   rÔ   c                 3   s4   | ],}|j  d ˆ › �¡rt|tjƒst|ƒV  qdS )rm   N)r>   r0  rL   r
   r¹   rÚ   rè   ©Ú	text_partr   r   r    È  s   þzextract_text.<locals>.<genexpr>rD   F)ZPsZPeÚPiZPfZPoT)rÚ   rL   r
   r¹   r®   rº   r>  r?  )rT  r'   Zcharacter_foundZfirst_letterrV   rA  r?  r   rS  r   r«   Ã  s(    ÿ

r«   )N)NNNNNNN)NN)N)F)N)EÚ__doc__Úrer>  rÔ   r   Úcssr   r   Zlayout.tabler   Úloggerr   Ztext.constantsr   r
   r†   r‰   r  rû   rþ   ÚFlexBoxÚInlineFlexBoxÚGridBoxÚInlineGridBoxrü   rý   rì   rö   ró   r  r=   rø   r5  r³   Úcompiler*  r,  r.  r(   r<   r@   r3   rU   rS   rË   rY   rÏ   rp   r^   rO   Úsearchrà   rç   r!   rå   r   r"   r  r#   r$  r]   r_   r8  r$   r%   rG  r5   rÚ   r«   r   r   r   r   Ú<module>   sŒ   é



  ÿ
 ".D   ü
 ? þ
*"'
^ 
6pf
5