a
    [Xhy                     @   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 )ZzTurn 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   j/var/www/viveiro_mudafortebrasil/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_targetsZ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 rd
gdd
gidhgf}|\}}}t|| g }|t  || d|
_|| d|
_g }d|d v rt	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|
 |rt|
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footnoteZ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childrenrA   tailpopprocess_whitespaceset_content_listsprocess_text_transformleninsertr   Zhandle_element)r,   r1   r5   r6   r7   r8   r9   stater.   r)   r'   quote_depthcounter_valuescounter_scopesrX   marker_boxesrT   Zchild_elementZchild_boxesrA   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 rtt| ||||||}|| |t||
||||| ||
_|d d	kr|\}}}t| |
|d
 ||| |
gS )z8Return the boxes for ::before or ::after pseudo-element.Nr)   r*   r>   )normalinhibitr+   ::rC   bookmark_levelr+   bookmark_label)	r?   rK   rM   rP   rQ   rR   rW   rX   compute_bookmark_label)r,   r-   r`   r1   r5   r7   r8   r.   r)   r>   r'   ra   rb   _counter_scopesrX   rd   _quote_depthr   r   r   rS     s>    






rS   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rtj||}|| |s|d dkr|
ddgd }|d }|	|| }rtj
||}d|jd< || |sdS |d dkrt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

    rg   ::markerr)   r*   NZlist_style_imager>   )rh   ri   urlimage_orientationrq   ZorientationZlist_style_typer+   rC   r   rF   zpre-wrapwhite_spacerH   rI   absoluteposition	directionZltri%d   	translateZ	transform)r?   rK   rR   rW   r
   InlineReplacedBoxrV   rN   getZrender_markerrU   r.   BlockBoxr   Z	DimensionZZERO_PIXELS	InlineBox)r,   r`   Zparent_styler1   r5   r7   r8   r.   rX   ra   rb   rn   r'   Z
image_typeimagecounter_valuecounter_typeZmarker_textZ
marker_boxZtranslate_xZtranslate_yr   r   r   rQ   3  sN    







rQ   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rq`||jd	 d
}|dur̈ tj| q`|dkrt	|}||
  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}&|%std|d d |d< |&r|	d krt|
\}'}(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 sq`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 )NTrF   )addrJ   r
   rU   rT   rN   rV   rT   )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>stringrq   Zexternalrr   rs   z	content()zstring()z,"string(%s)" is only allowed in page margins )	counter()z
counters()r   rF   r+   r   r	   c                 3   s   | ]}  |V  qd S Nrender_valuer   r   r8   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 marginsZstaticrv   r>   )rh   r+   )contextpagezleader()prert   T)+rO   Z
collectingcached_counter_valuesitemsr.   rN   r
   r{   rV   extract_textstripr   warningjoinZget_string_set_forrP   r|   r   lookup_targetr`   
target_boxr   Zanchor_name_from_token
setdefaultZcached_page_counter_valuesr   update
startswithmaxr   minr^   Zget_running_element_fordeepcopyrJ   	ParentBoxdescendantsrW   rX   rU   r~   Z	is_leaderZcollect_missing_counters).content_listr   rb   	css_tokenparse_againr7   r8   r5   ra   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_textZcounter_namer   rT   	separatorZanchor_tokenr   Ztarget_valuesZanchor_namelocal_countersZseparator_stringZ
text_styler   Zis_openr_   Zopen_quotesZclose_quotesquotesZnew_boxchildre   Z
leader_boxr   )r   r8   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   rR   rW   r^   rX   rJ   r
   LineBox)mixin_pagebased_countersr   Zlocal_childrenr8   r5   Zorig_quote_depthr   r.   r7   r   r   r   5  s    z%content_to_boxes.<locals>.parse_againr>   ri   r   r   )N)r   )r.   r   ra   rb   r5   r7   r8   r   r   r   r   r:   r   r   r   rW   1  s    rW   c              
      s   d
 fdd	}d }t  |||d}	|	durd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   r8   r,   string_namer7   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   rJ   r
   rU   rT   r   r'   r   r   r   r   r  s   z%compute_string_set.<locals>.<genexpr>r   )N)r   r   
string_setremoverN   )r,   r'   r   r   rb   r7   r8   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   rm   r   r'   r   r8   r,   r7   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   rl   )	r,   r'   r   rb   r7   r8   r   r   r:   r   r   r   rm   }  s    
rm   c           	   	   C   sf   g |_ |d dkr@t|d D ] \}\}}t| |||||| q|d dkrbt| ||d ||| dS )zSet the content-lists values.

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

    r   r+   rk   rl   N)r   	enumerater   rm   )	r,   r'   r.   rb   r7   r8   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sJ || |d ||d< q^|d }	|	dkrd|d v rd	g}	ng }	|	D ]J\}}||g }|s||vsJ || |d |d  |7  < qd
S )z$Handle the ``counter-*`` properties.rF   Zcounter_resetZcounter_setr   counter_incrementr   rC   r)   )rC   r	   N)rZ   r   r   rN   )
r`   r.   ro   rb   rc   Zsibling_scopesrf   r   valuesr   r   r   r   rM     s4    






rM   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 )zWrap 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   )rJ   r   wrapper_typer   r   test  s    zwrap_improper.<locals>.test)rX   )rV   table_boxes_childrenrN   )r'   rX   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 )zRemove 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>)rJ   r
   r   
is_runningrX   r   r'   rX   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rt|dkr|dd \}}|jrt	|r|
  t|dkr|dd \}}|jrt	|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rt |tjdd }n t t |tjfdd}t  tjrt |S t| _ S dS )z3Internal implementation of anonymous_table_boxes().c                 S   s   g | ]}t |tjr|qS r   )rJ   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   rV   )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
   

rF   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   )rJ   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   Zproper_parentsr   )parent_typer   r   r   T  s   )rJ   r
   r   TableColumnGroupBoxspanrangeZtabular_containerr^   r   r   rZ   zipTableBoxr   TableRowBoxTableRowGroupBoxr   r~   InlineTableBoxtype
wrap_tablerP   rX   )r'   rX   r   ZinternalrT   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 }
qq|
|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rt||||_t| t jrt 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bottomZcaption_sider   r	   Nr)   r   Tr   c                 S   s   g | ]
}t  qS r   )rO   )r   rowr   r   r   r     r   zwrap_table.<locals>.<listcomp>Zborder_collapseZcollapser   r   )'r
   r   r   r   r   TableCaptionBoxr   rN   r.   rP   r   grid_xrX   r   Z	is_headerZ	is_footerrZ   ZcolspanZrowspanr^   r   r   r   r   copy_with_childrenr   tuplecolumn_groupsr   Zcollapsed_border_gridrJ   r   InlineBlockBoxr}   rV   Zis_table_wrapperr   ZTABLE_WRAPPER_BOX_PROPERTIESZINITIAL_VALUES)!r'   rX   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   rf   r   r   r   r   ^  s    











r   c                 C   s8   t | tjr|  r| S dd | jD }t| || _| S )zRemove 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>)rJ   r
   r   r   rX   flex_childrenr   r   r   r   r"     s
    r"   c                 C   s   t | tjrg }|D ]}dd |_| r0d|_t |tjrJ|jdsJqt |tj	rtj
||j}|j|_d|_|| qt |tjrt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   )rJ   r
   ZFlexContainerBoxZ
is_floatedis_in_normal_flowZis_flex_itemrU   rT   r   r   r}   rV   rX   r.   rN   InlineLevelBox)r'   rX   r   r   	anonymousr   r   r   r     s*    
r   c                 C   s8   t | tjr|  r| S dd | jD }t| || _| S )zRemove 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>)rJ   r
   r   r   rX   grid_childrenr   r   r   r   r#     s
    r#   c                 C   s   t | tjrg }|D ]}| r&d|_t |tjr@|jds@qt |tjrvtj	
||j}|j|_d|_|| qt |tjrtj	
||g}|j|_d|_d|_|| q|| q|S |S d S )NTr   F)rJ   r
   ZGridContainerBoxr   Zis_grid_itemrU   rT   r   r   r}   rV   rX   r.   rN   r   )r'   rX   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}|rt	d| }}|r|
dr|dd }d| _|d}nd	}|| _nJ| jD ]B}t |tjtjfrt||}|  r| r|}q| rd	}q|o|   S )
zFirst 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

    
rt   )rh   nowraprh   r   zpre-liner   r	   NTF)rJ   r
   rU   rT   LINE_FEED_REsubr.   TAB_REreplaceSPACE_REr   leading_collapsible_spaceendswithrX   r~   r[   r   r   )r'   Zfollowing_collapsible_spacerT   Znew_line_collapseZ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   )rz   ASCII_TO_WIDEr   r   r   r   r   {  r   )Z	uppercaseZ	lowercase
capitalizez
full-widthZhyphens   ­r   )rJ   r
   rU   r.   r  rT   r  r   rX   r~   r]   )r'   r  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categoryr	  )rT   Zletter_foundoutputletterr  r   r   r   r    s    

r  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rJ |r| r|| qt|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|rtj| |}|rt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   rt   r   )rX   r   rP   r  rJ   r
   rU   rT   rN   r$   trailing_collapsible_spaceZBlockContainerBoxr   Zis_absolutely_positionedr   r   r.   rV   r}   )
r'   Zbox_childrenrX   r  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rt| j dksLJ d| j  d}t||d\}}}|du rlqtj| |g}|	| |	t
| qP|rtj| |g}q|}nt
|}||urd}|	| 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)rX   r   rJ   r
   r   r^   _inner_block_in_inliner}   rV   rN   r%   )	r'   r  changedr   stack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rt|	|}d}|\}}}n|du sJ t	|	}||	urd}|
| |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   rX   rJ   r
   ZBlockLevelBoxr   r~   r  r%   rN   r   )r'   r  r  Zblock_level_boxZ	resume_atr  Zis_startskipr   r   index	recursionr  r   r   r   r  k  s@    





r  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   ZoverflowZvisiblebody)r=   r
  r.   rX   Zviewport_overflow)Zroot_boxZ
chosen_boxr   r   r   r   r4     s    	

r4   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::afterrp   N)r=   r  rJ   r
   rU   rT   r   r   r   r   r     s   zbox_text.<locals>.<genexpr>)rJ   r
   rU   rT   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rd}d}t |}|D ]*}t|}|dvr|r~ qd	}||7 }q`|S d S )
N)rT   r>   )rD   rG   r   c                 3   s4   | ],}|j d   rt|tjst|V  qdS )rj   N)r=   r  rJ   r
   r   r   r   	text_partr   r   r     s   zextract_text.<locals>.<genexpr>rB   F)ZPsZPePiZPfZPoT)r   rJ   r
   r   r   r   r  r  )r"  r'   Zcharacter_foundZfirst_letterrT   r  r  r   r!  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   ZFlexBoxZInlineFlexBoxZGridBoxZInlineGridBoxr   r   r   r   r   r   r<   r   r  r   compiler  r  r  r(   r;   r?   r3   rS   rQ   r   rW   r   rm   r\   rM   searchr   r   r!   r   r   r"   r   r#   r   r[   r]   r  r$   r%   r  r4   r   r   r   r   r   r   <module>   s   



  
 ".D   
 ? 
*"'
^ 
6pf
5