
    3vj{                     2   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 i dej                  dej                   dej                  dej"                  dej$                  dej&                  dej(                  dej*                  dej,                  dej.                  dej0                  dej2                  dej2                  dej2                  dej4                  dej6                  dej8                  dej:                  iZ edd      D  ci c]  } |  e | dz          c} Z!e!jE                  d d!d"        ejF                  d#      Z$ ejF                  d$      Z% ejF                  d%      Z&d& Z'd' Z(d( Z)	 dHd)Z*d* Z+d+ Z,	 	 	 dId,Z-	 dJd-Z.d. Z/d/ Z0d0 Z1d1 Z2 ejF                  d2      jf                  fd3Z4dHd4Z5d5 Z6d6 Z7d7 Z8d8 Z9d9 Z:d: Z;d; Z<d< Z=dKd=Z>d> Z?d? Z@d@ ZAdA ZBdB ZCdC ZDdHdDZEdE ZFdF ZGdG ZHyc c} w )LzTurn 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-caption!      i  u   　u   −)    -   z
?z[	 ]*
[	 ]*z[	 ]+c                 t    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boxs    X/root/.hermes/venv/lib/python3.12/site-packages/weasyprint/formatting_structure/build.pycreate_anonymous_boxesr$   5   s7    

$C
S/C
S/C
#
C
#
CJ    c           	           t         |||||      }|r|\  }nd fd	}	t         |	|||||      \  }|j                          t        |       t        |       d|_        t        |      }t        |      }|S )z=Build a formatting structure (box tree) from an element tree.c                 @     | |      }|| k(  rd|d<   |S d|d<   |S )Nr   displaynone )elementpseudo_typestyleelement_tree	style_fors      r#   root_style_forz2build_formatting_structure.<locals>.root_style_forJ   s?    g{3E l*'8E)$ L (1E)$Lr%   TN)element_to_boxcheck_pending_targetsprocess_whitespaceprocess_text_transformis_for_root_elementr$   set_viewport_overflow)
r/   r0   get_image_from_uribase_urltarget_collectorcounter_style	footnotesbox_listr"   r1   s
   ``        r#   build_formatting_structurer?   ?   s     i!3X-4H 	 .*<hmY8 **,s3"C
 
%C

$CJr%   c                 0    t        |d   d d    | |||      S )Nr(   r   )BOX_TYPE_FROM_DISPLAY)element_tagr.   contentr,   s       r#   make_boxrD   a   s)     y!1"1!56UGW. .r%   c                    t        | j                  t              sg S  ||       }|d   }	|	dk(  rg S |d   dk(  r|d   dk(  rd|d<   nd|d<   t        | j                  |g |       }
 || d	      |
_         || d
      |
_        |dgddgidhgg f}|\  }}}}t        ||       g }|j                  t                      g }d|d   v r+t        t        | ||||||            }|j                  |       |j                  t        | d|||||             |d   r|j                  |d   ||
       | j                  }|r/|j                  t        j                   j#                  |
|             | D ]G  }t%        ||||||||      }|r|d   j&                  d   dk(  r|d   }d|j&                  d<   |j                  |        ||j(                  d      }t        |j(                  j                   d|g |j(                        }t+        |||||||      |_        ||_        |g}|j                  |       |j0                  }|st        j                   j#                  |
|      }|rAt        |d   t        j                         r$|d   xj                  |j                  z  c_        7|j                  |       J |j                  t        | d|||||             |j3                         D ],  }||   j3                          ||   r|j3                  |       . ||
_        t5        | |
||||       |rYt7        |
j,                        dk(  rA|d   dk(  r9|
j,                  j                  t        j                   j#                  |
d             |d   dk(  rf|d   dxx   dz  cc<    || d      }t        | j                   d|g |       }t+        |||||||      |_        |
j,                  j9                  d|       t;        j<                  | |
||      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   first-letterz
first-liner   	list-itembeforeanchorr*   zfootnote-callz::footnote-callafterr
   list_style_positionoutsideu   ​zfootnote-markerz::footnote-marker)
isinstancetagstrrD   first_letter_stylefirst_line_styleupdate_countersappendsetlistmarker_to_boxextendbefore_after_to_boxstore_targettextr   TextBoxanonymous_fromr3   r.   r,   content_to_boxeschildrenrG   tailpopset_content_listsleninsertr   handle_element)r,   r0   r9   r:   r;   r<   r=   stater.   r(   r"   quote_depthcounter_valuescounter_scopes_page_groupsrb   marker_boxesr^   child_elementchild_boxesrG   
call_stylefootnote_calltext_boxnamemarker_stylemarkers                              r#   r3   r3   f   sW   2 gkk3'	gE IG)	W~##$/0E)  2E)
7;;r7
3C&w?C$Wl;C} C!\N
 AF=KE5!H #% LeI&&MUE9.@m- . 	%OO'5)-?-) * X%%eHo~sK<<D44S$?@  *$9&8(mY? ;q>//8JF"1~H&,HNN7#X&"8#3#3_EJ$##''(8*b  "M &6M;"$4m&EM" &.M"(/K$!!}}33C>HJx|U]]C!!X]]2!)5*8 OO'%,>-) *
 ""$ %t  "d#t$%
 CLe^-=}N CLL)Q. &'94LL < <S% HIW~#z"2&!+& *;<{{m,-|RJ*&+~?Qm- 	Av& w-?JJr%   c                     || |      }|r|g S |d   }|dk(  rg S |d   }	|	dv rg S t        | j                   d| |g |       }
|\  }}}}t        ||       g }d|v r+t        t	        | ||||||            }|j                  |       |j                  t        ||
|||||             ||
_        |d   dk7  r|\  }}}}t        | |
|d	   |||       |
gS )
z8Return the boxes for ::before or ::after pseudo-element.r(   r)   rC   )normalinhibitr*   ::rJ   bookmark_levelr*   bookmark_label)	rD   rR   rV   rY   rZ   r[   ra   rb   compute_bookmark_label)r,   r-   ri   r0   r9   r;   r<   r.   r(   rC   r"   rj   rk   _counter_scopesrm   rb   rn   _quote_depths                     r#   r\   r\     s4    g{+Eu} 	
 IG)	IG//	
gkk]"[M2E2w
GCAF>K,E5!HgMUE9.@m- . 	%OO$sK1C-) * CL &(FKCno|S% 01>m	- 5Lr%   c              #      K    || d      }g }|\  }	}
}}t        | j                   d|||       }|d   dk(  ry|d   \  }}|d   dvr!|j                  t        |||	|
|||             n|d	k(  r@ |||d
         }|1t        j
                  j                  ||      }|j                  |       |sw|d   dk7  ro|
j                  ddg      d   }|d   }|j                  ||      x}r@t        j                  j                  ||      }d|j                  d<   |j                  |       |sy|d   dk(  r7t        j                  j                  ||      }d|j                  d<   d|_        n t        j                  j                  ||      }| yw)zyYield the box for ::marker pseudo-element if there is one.

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

    rv   ::markerr(   r)   Nlist_style_imagerC   )rx   ry   urlimage_orientationr   orientationlist_style_typer*   rJ   r   rM   zpre-wrapwhite_spacerO   rP   absolutepositionT)rD   rR   r[   ra   r   InlineReplacedBoxr`   rW   getrender_markerr_   r.   BlockBoxis_outside_marker	InlineBox)r,   ri   parent_styler0   r9   r;   r<   r.   rb   rj   rk   r~   rm   r"   
image_typeimagecounter_valuecounter_typemarker_text
marker_boxs                       r#   rZ   rZ   4  s     gx(EH
 BG>K,
gkk](+UHg
FCY9$01JY44(3^5Gm- 	.
 &u-@'ACE --<<S%H$E"34>*..{QC@DM !23L+99,VV{Vmm223D+5		-($)*i7^^223A
 (2
$'+
$__33CB

s   FFc                 :  /01 g /t               1/1fd}g }i }|duxr |du}|j                  xr | }j                  7|j                         D ci c]  \  }}||j	                          c}}_        | D ]  \  }}|dk(  r	 ||       |dk(  rW|U|\  }}|dk7  r' ||j
                  d         }|A/j                  t        j                  j                  |             q|dk(  rt        |      } ||       |d	k(  rB|s&t        j                  d
dj                  |              | |j                  |g|        |dv r|d   |d   c}00dk(  r|r!|t        |      |z   vr|j                  |       |dk(  r)|j!                  |dg      d   }j#                  |0      }n1|d   }|j                  0fd|j!                  |dg      D              } ||       s|dv r7|dd |d   c\  }}00dk(  r|j%                  |||      }|j&                  dk7  r nK|j(                  j                  } |r@|| vr<t+        j,                  |      }!|j/                  |!g       }||vr|j                  |       |j0                  j	                         }"|"j3                  |        |dk(  r)|"j!                  |dg      d   }j#                  |0      }nA|d   }|d   dk7  r n|d   }#|#j                  0fd|"j!                  |dg      D              } ||       |dk(  rM|\  }}$|j%                  |||      }|j&                  dk(  r"|j(                  }%t        |$|%      } ||        n|dk(  rd||	fvrd|v }&|j5                  d       xr |	dk7  }'|&st7        d|d   dz
        |d<   |'rC|	dk(  rt9        |
      \  }(})n|	\  }(})|&r|(n|)}* ||*t;        |d   t=        |*      dz
                  |&s|dxx   dz  cc<   |dk(  r|s't        j                  ddj                  |              |j>                  |g| }+|+|+jA                         }+d|+j
                  d <   tC        |+t        jD                        rJ|+jG                         D ]7  },|,j
                  d!   d"v rtI        |,j
                  |,||||||#	      |,_%        9 /j                  |+       s|d$k(  sz|d   st        jL                  j                  |d         }-t        jN                  j                  |-f      }.d%|.j
                  d&<   d%|-j
                  d&<   d'|._(        /j                  |.        1s/r|jS                  ||||       /S yc c}}w )(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                     j                  d       | rhr6t        d   t        j                        rd   xj                  | z  c_        y j                  t        j                  j                  |              y y )NTrM   )addrQ   r   r_   r^   rW   r`   )r^   content_boxeshas_text
parent_boxs    r#   add_textz&compute_content_list.<locals>.add_text  se    TM",=u}}!Mb!&&$.&$$MM00TBD	 r%   Nstringr   externalr   r   z	content()zstring()z,"string(%s)" is only allowed in page margins )	counter()z
counters()r   rM   r*   r   r
   c              3   B   K   | ]  }j                  |        y wr2   render_value.0r   r<   r   s     r#   	<genexpr>z'compute_content_list.<locals>.<genexpr>  s(      &P% "..}lK&P   )target-counter()ztarget-counters()r   z
up-to-dater   c              3   B   K   | ]  }j                  |        y wr2   r   r   s     r#   r   z'compute_content_list.<locals>.<genexpr>  s(      -P% "..}lK-Pr   ztarget-text()quoteopenzno-autoz	element()z-"element(%s)" is only allowed in page marginsstaticr   rC   )rx   r*   )contextpagezleader()prer   T)*rX   
collectingcached_counter_valuesitemscopyr.   rW   r   r   r`   extract_textr   warningjoinget_string_set_forrY   r   r   lookup_targetri   
target_boxr   anchor_name_from_token
setdefaultcached_page_counter_valuesupdate
startswithmaxr	   minrf   get_running_element_fordeepcopyrQ   	ParentBoxdescendantsra   rb   r_   r   	is_leadercollect_missing_counters)2content_listr   rk   	css_tokenparse_againr;   r<   r9   rj   quote_stylelangr   r   r,   r   missing_countersmissing_target_countersin_page_contextneed_collect_missingkeyvaluetype_originurir   
added_textcounter_namer   r^   	separatoranchor_tokenr   target_valuesanchor_namelocal_countersseparator_string
text_styler   is_openrg   open_quotesclose_quotesquotesnew_boxchildrs   
leader_boxr   r   r   s2    `    `                                        @@@r#   compute_content_listr   o  s<   ( MuHD  T)>d$.>O ,66N;N''/ 1?0D0D0F,H",#uC,H
($ A-uHUOe^ 2 >KFC#&Z%5%56I%JLE $$++:::uMOk!%eZ8JZ j " BHHUO% /W//=u=>11).q59&L,v%#tN';>N'NN$++L9# . 2 2<! Eb I$11-N!!H	 ~~ &P)7););L1#)N&P P TN??9>rE"I6(\<,v%,::j)[BM""l2)44JJM#M(I%<<\J#:#E#E$% '77$++L9 +EEJJLN!!-0** . 2 2<! Eb I$11-N!!H	Q<8+#,Q< ',, -P)7););L1#)N-P P TNo%',$L*,::j)[BM""l2*55
 $J
;g${K.H"HoG))%00J[F5JF!$QA(:!;A&(0?0E-K0;-K(/\KNCK!O DEFA!#k!"CHHUO% 5g55dCUCG&&(G(0GMM*%'5??3$002 4E{{9-1CC %5UK*,<m 'd&4EN4   )j 8}}33JaIH77XK)J /4J]+,1HNN=)#'J   ,CA-F =11	;0@#	%  !K,Hs   Vc	                      d fd	}	 d   dk(  rg S |dd d}
t         d   ||
|	| d    d   ||      }|xs g S )z:Take the value of a ``content`` property and return boxes.Nc                 `   | i }n| j                         }|j                  j                         g }|j                  t	        |             t        j                        dk(  r<t        j                  d   t        j                        r|j                  d   _        y|_        y)z9Closure to parse the ``parent_boxes`` children all again.Nr
   r   )
r   r   r   r[   ra   rf   rb   rQ   r   LineBox)	mixin_pagebased_countersr   local_childrenr<   r9   orig_quote_depthr   r.   r;   s	      r#   r   z%content_to_boxes.<locals>.parse_again+  s    
 $+N5::<Nj>>?.:/ 0-A 	B 
##$):..q15==A.<J"+"0Jr%   rC   ry   r   r   r2   )r   )r.   r   rj   rk   r9   r;   r<   r   r   r   r   r>   r   s   ``  ```     @r#   ra   ra   '  sq    1 14 Y9$	"1~I#i*ni-);[hv7H >rr%   c           
      2    d fd	}d }t        |||       }	|	ldj                  d |	D              }
j                  D ]'  }|d   k(  sj                  j                  |        n j                  j	                  |
f       yy)	zCParse the content-list value of ``string_name`` for ``string-set``.Nc           	          | i }n| j                         }|j                  j                         t        |       y)z7Closure to parse the string-set string value all again.N)r   r   r   compute_string_set)r   r   r"   r   r<   r,   string_namer;   s     r#   r   z'compute_string_set.<locals>.parse_againT  sJ     $+N5::<Nc778S+|^m	-r%   zstring-set::r,    c              3   j   K   | ]+  }t        |t        j                        s|j                   - y wr2   rQ   r   r_   r^   r   r"   s     r#   r   z%compute_string_set.<locals>.<genexpr>f  s(      LJsEMM,JCHHLs   33r   r2   )r   r   
string_setremoverW   )r,   r"   r   r   rk   r;   r<   r   r   r>   r   string_set_tuples   ```` ``     r#   r   r   Q  s    - - {m,I#c>9k-:H  L (L L !$ 	"k1%%&67	 	{F34 r%   c           
           d fd	}d}t        |||       }|rdj                  d |D              _        yy)z5Parses the content-list value for ``bookmark-label``.Nc                     | i }n| j                         }|j                  j                         t        |       y)z.Closure to parse the bookmark-label all again.N)r   r   r   r}   )r   r   r"   r   r<   r,   r;   s     r#   r   z+compute_bookmark_label.<locals>.parse_againt  sI     $+N5::<Nc778S,8H	r%   zbookmark-labelr   r   c              3   2   K   | ]  }t        |        y wr2   )box_textr   s     r#   r   z)compute_bookmark_label.<locals>.<genexpr>  s     $GsXc]$Gs   r2   )r   r   r|   )	r,   r"   r   rk   r;   r<   r   r   r>   s	   ``` ``   r#   r}   r}   q  sQ      !I#c>9k-:H WW$Gh$GG r%   c           
          g |_         |d   dk7  r|d   D ]  \  }}t        | ||||||        |d   dk7  rt        | ||d   |||       yy)zSet the content-lists values.

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

    r   r*   r{   r|   N)r   r   r}   )r,   r"   r.   rk   r;   r<   r   string_valuess           r#   re   re     sz     CN\f$*/*= 	1&Kk=. -1	1 &(S% 01>m	- )r%   c                 :   | \  }}}}|d   }|d   D ]O  \  }}||v r||   j                          n|j                  |       |j                  |g       j                  |       Q |d   D ]F  \  }}|j                  |g       }	|	s(||vsJ |j                  |       |	j                  d       ||	d<   H |d   }
|
dk(  rd|d   v rd	g}
ng }
|
D ]N  \  }}|j                  |g       }	|	s(||vsJ |j                  |       |	j                  d       |	dxx   |z  cc<   P y
)z$Handle the ``counter-*`` properties.rM   counter_resetcounter_setr   counter_incrementr   rJ   r(   )rJ   r
   N)rd   r   r   rW   )ri   r.   r   rk   rl   rm   sibling_scopesrt   r   valuesr  s              r#   rV   rV     sf   AF>L..,#B'N_- :e>!4 $$&t$!!$+2259: ]+ e**44~---t$MM!r
 12F"
 %	**!1 2 "( e**44~---t$MM!r
e
r%   z\Sc                 `    t        | t        j                        xr  || j                         S )z9Return True if ``box`` is a TextBox with only whitespace.r   )r"   _has_non_whitespaces     r#   is_whitespacer	    s%    c5==)O2Echh2O.OOr%   c              #      K   |fd}g }|D ]E  } ||      r*|r#j                  | g       }t        ||       g }| 5|j                  |       G |r"j                  | g       }t        ||       yyw)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                     t        |       S r2   )rQ   )r   wrapper_types    r#   testzwrap_improper.<locals>.test  s    e\22r%   )rb   )r`   table_boxes_childrenrW   )r"   rb   r  r  improperr   wrappers     `    r#   wrap_improperr    s      |	3H 	#;&55cB5G*7H==KOOE"	# --cB-?"7H55 s   A9A<c                     t        | t        j                        r| j                         r| S | j                  D cg c]  }t        |       }}t        | |      S c c}w )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

    )rQ   r   r   
is_runningrb   r   r  r"   r   rb   s      r#   r   r     sR     c5??+s~~/?
 ;>,,G%e,GHGX.. Hs   Ac                   	 t        | t        j                        rg }nt        | t        j                        r|D cg c]  }t        |t        j                        r|  }}|s`| j                  | j                  dk  rd}n| j                  }t        |      D cg c]"  }t        j                  j                  | g       $ }}| j                  r{t        |      dk\  rm|dd \  }}|j                  rt        |      r|j                          t        |      dk\  r0|dd \  }}|j                  rt        |      r|j                  d       t        dg|dd |g |dd d      D cg c]/  \  }}}|r%|j                  r|r|j                  rt        |      s|1 }}}}t        | t        j                        rt        | |t        j                  d       }n5t        | t        j                         rt        | |t        j                        }t        | t        j                        rt        | |t        j"                        }nt        | |t        j                  d       }t        | t        j$                        rt        | |t        j&                  d	       }n*t)        |       	t        | |t        j                  	fd
      }t        | t        j                        rt+        | |      S t-        |      | _        | S c c}w c c}w c c}}}w )z3Internal implementation of anonymous_table_boxes().Nr
   r   r   rM   c                     | j                   S r2   proper_table_childr   s    r#   <lambda>z&table_boxes_children.<locals>.<lambda>0  s    %22 r%   c                 8    t        | t        j                         S r2   )rQ   r   TableCellBoxr  s    r#   r  z&table_boxes_children.<locals>.<lambda><  s    j0B0BCC r%   c                     | j                    S r2   r  r  s    r#   r  z&table_boxes_children.<locals>.<lambda>B  s    e666 r%   c                 >    | j                    xs | j                  v S r2   )r  proper_parents)r   parent_types    r#   r  z&table_boxes_children.<locals>.<lambda>G  s&    u777 ?&%*>*>> r%   )rQ   r   TableColumnBoxTableColumnGroupBoxspanranger`   tabular_containerrf   internal_table_or_captionr	  rd   zipTableBoxr  TableRowBoxTableRowGroupBoxr  r   InlineTableBoxtype
wrap_tablerY   rb   )
r"   rb   r   r$  _internalr^   
prev_child
next_childr!  s
            @r#   r  r    s   #u++,	C22	3  (
%!5!56 
 
 xx388a<xx!&t. ,,;;CD .H . X!!3 ""#$..=3FLLN x=A%bq\ND(22}T7JQ .1"HSbM"!hqrl!D!.
 )Jz :??:??%  	H  #u~~& 5,,24 
C//	0 h0A0AB#u(() h0B0BC !5,,CE
 #u' 5//68 3i 5>>@A
 #u~~&#x((H~
i
.0s   #K;'L )4Lc           
         g }g }g }t         j                  |t         j                  |t         j                  |t         j                  |t         j
                  |i}|D ]  }|t        |         j                  |       ! g g d}|D ]#  }||j                  d      j                  |       % t        t        | |t         j                              }	d}
|	D ]B  }|
|_        |j                  r|j                  D ]  }|
|_        |
dz  }
 4|
|j                  z  }
D |
}t        | |t         j                        }g }d}d}|D ]D  }|j                  d   }|dk(  r|
d|_        |}#|d	k(  r|
d|_        |}4|j                  |       F ||gng |z   ||gng z   }d}|D ]*  }|j                  D cg c]  }t!                }}|j                  D ]  }|j#                  d      }d}
|j                  D ]  }|
|v r
|
dz  }
|
|v r
|
|_        |
|j$                  z   }|j&                  dk7  rxt)        |      dz   }|j&                  dk(  r
|}||_        n-t+        |j&                  |      |_        |d|j&                  dz
   }t-        |
|      }|D ]  }|j/                  |        |}
t1        ||
      }  |t)        |j                        z  }- | j3                  |      }|j                  j5                         |_        t7        |	      |_        |j                  d
   dk(  rt;        |||      |_        t?        | t         j@                        rt         jB                  }nt         jD                  }|jG                  | |d   |gz   |d   z         }|j                  j5                         |_        d|_$        tJ        jL                  D ]>  } |j                  |    |j                  | <   tJ        jN                  |    |j                  | <   @ |S c c}w )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   border_collapsecollapser4  r5  )(r   r"  r#  r*  r+  TableCaptionBoxr-  rW   r.   rY   r  grid_xrb   r$  	is_header	is_footerrX   rd   colspanrowspanrf   r   r%  r   r   copy_with_childrenr   tuplecolumn_groupsr   collapsed_border_gridrQ   r,  InlineBlockBoxr   r`   is_table_wrapperr   TABLE_WRAPPER_BOX_PROPERTIESINITIAL_VALUES)!r"   rb   columnsrowsall_captionsby_typer   captionscaptionrA  r:  groupcolumn
grid_width
row_groupsbody_row_groupsheaderfooterr(   grid_heightrowoccupied_cells_by_rowoccupied_cells_in_this_rowcell
new_grid_xmax_rowspanspanned_rowsspanned_columnsoccupied_cellsr   r  r  rt   s!                                    r#   r.  r.  Q  sA    GDLg!!74|G  +U##E*+ R(H @~./66w?@ We//1 2MF 	!>>..  !'!	 ejj F	! JsD%*@*@AJOFF 	*++i(--&."EOF//FN"EOF""5)	* '&R	'&R	1  K + 38.. A3 A A>> 	5C)>)B)B1)E&F 5 ::aKF  ::$#dll2
<<1$"%&;"<q"@K||q('<'2'*4<<'E'<=NdllQ>N'O&+FJ&?O*6 ?&--o>?# V4
)5		52 	s5>>**;+> "":.E++""$EK.E{{$%3&<:{',# #u++,++~~))Xe_w&();;=GMM&&(GM#G 77 <#kk$/d&55d;D< Ng !Bs   +O>c                     t        | t        j                        r+t        j                  j	                  | | j
                        }nt        | t        j                        r-| j                  }t        j                  j	                  | |      }nOt        | t        j                        r3t        j                  j	                  | | g      }t        | d| dd       n| S | j                  |_        t        |d| dd       |S )z$Turn an inline box into a block box.is__itemFT)rQ   r   rC  r   r`   rb   r   replacementBlockReplacedBoxInlineLevelBoxsetattrr.   )r"   layout	anonymousra  s       r#   blockifyrg    s     #u++,NN11#s||D		C00	1oo**99#{K		C--	.NN11#u=	s6(%(%0
iiIOIVHE*D1r%   c                     t        | t        j                        r| j                         r| S | j                  D cg c]  }t        |       }}t        | |      | _        | S c c}w )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

    )rQ   r   r   r  rb   r   flex_childrenr  s      r#   r   r     V     c5??+s~~/?
 03||<e
5!<H< h/CLJ =   A"c                 0   t        | t        j                        r{g }|D ]r  }d |_        |j	                         rd|_        t        |t        j                        r|j                  j                  d      sX|j                  t        |d             t |S |S )Nc                       y)NFr+   r+   r%   r#   r  zflex_children.<locals>.<lambda>  s    r%   Tr   r   )rQ   r   FlexContainerBox
is_floatedis_in_normal_flowis_flex_itemr_   r^   striprW   rg  )r"   rb   ri  r   s       r#   ri  ri    s    #u--. 		:E,E&&(%)"%/

8H8H8M   %!89		: r%   c                     t        | t        j                        r| j                         r| S | j                  D cg c]  }t        |       }}t        | |      | _        | S c c}w )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

    )rQ   r   r   r  rb   r   grid_childrenr  s      r#   r   r   	  rj  rk  c                     t        | t        j                        rsg }|D ]j  }|j                         rd|_        t        |t        j
                        r|j                  j                  d      sP|j                  t        |d             l |S |S )NTr   r   )
rQ   r   GridContainerBoxrp  is_grid_itemr_   r^   rr  rW   rg  )r"   rb   rt  r   s       r#   rt  rt    s    #u--. 	:E&&(%)"%/

8H8H8M   %!89	: r%   c                    t        | t        j                        r| j                  }|s|S t        j                  d|      }| j                  d   dv }| j                  d   dv }|rt        j                  d|      }|r|j                  dd      }|rIt        j                  d|      x}}|r|j                  d      r|dd }d| _        |j                  d      }nd	}|| _        |S | j                  D ]N  }t        ||      }t        |t        j                  t        j                  f      r|}<|j!                         sMd	}P |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

    
r   )rx   nowraprx   rz  zpre-liner   r
   NTF)rQ   r   r_   r^   LINE_FEED_REsubr.   TAB_REreplaceSPACE_REr   leading_collapsible_spaceendswithrb   r5   r   rp  )r"   following_collapsible_spacer^   new_line_collapsespace_collapseprevious_textr   child_collapsible_spaces           r#   r5   r5   +  sK    #u}}%xx.. d+IIm48LL=1 6, , ::dD)D
 <<c*D#+<<T#::MD*ts/CABx04-*7*@*@*E'*/' '& \\ 	4E&82'4#%%--!AB.E+((*.3+	4 '&r%   c                    t        | t        j                        r| j                  d   }| j                  d   xs dj	                  d      d   j                         }|dk7  r-	 t        t        t        d d|   | j                  |      | _	        | j                  d	   dk(  r"| j                  j                  d
d      | _	        y y | j                         s| j                  D ]  }t        |        y y )Ntext_transformr   r   -r   r*   c                 ,    | j                  t              S r2   )	translateASCII_TO_WIDE)r^   	lang_codes     r#   r  z(process_text_transform.<locals>.<lambda>p  s    dnn]6S r%   )	uppercase	lowercase
capitalizez
full-widthhyphens   ­)rQ   r   r_   r.   splitlowerr  r  r  r^   r  r  rb   r6   )r"   r  r  r   s       r#   r6   r6   `  s     #u}}%#34YYv&,"33C8;AAC	V#&&(S	
 
 "hh	3CH 99Y6)xx''"5CH * ^^\\ 	*E"5)	* r%   c                 :   i }|dk(  r	dddddd}n|dv rd	d
i}|j                         D ]  \  }}| j                  ||      }  |dk(  rEt        j                  d|       } dD ]  }| j                  |d      }  t        j                  d|       } | j	                         S )Nelu   ΑΪu   ΑΫu   ΟΪu   ΟΫu   ΗΫ)u   άιu   άυu   όιu   όυu   έυtrazi   İNFDu   ̓̔́̀̆͂̄ͅr   NFC)r   r  unicodedata	normalizeupper)r^   r  mapperr   r   chars         r#   r  r  y  s    FD
 
 
l	" 
 lln (
U||C'( D $$UD1F 	*D<<b)D	*$$UD1::<r%   c                     i }|dv rddd}n|dk(  rdddd	}|j                         D ]  \  }}| j                  ||      }  | j                         S )
Nr  u   ır  )Ir  ltu   i̇̀u   i̇́u   i̇̃)   Ì   Íu   Ĩ)r   r  r  )r^   r  r  r   r   s        r#   r  r    ss    FL  
 
d	 
 lln (
U||C'( ::<r%   c                    d}d}d}t        |       D ]m  \  }}|rd}t        j                  |      d   }|s=|dv r9d}|dk(  r| ||dz    dk(  rd}d	}n)|d
v rt        ||      }n|j	                         }n|dk(  rd}||z  }o |S )uC   Capitalize words according to CSS’s "text-transform: capitalize".Fr   r   )LNTnlr   ijIJr  Z)	enumerater  categoryr  r  )r^   r  letter_foundskip_next_letteroutputr  letterr  s           r#   r  r    s    LFt_ 	6$''/2J 6LD T!AaC[D%8#' l*"695_ L&!" Mr%   c                 &   | j                   r| j                         r| S t        | j                         }|r"| j                  du r|d   j                  | _        g }d}|D ]Z  }|rd|_        t	        |t
        j                        r|j                  s|j                  }?d}|j                  t        |             \ | j                  du r|| _
        t	        | t
        j                        s	|| _         | S g }g }|D ]%  }t	        |t
        j                        rJ |r"|j                         r|j                  |       Dt	        |t
        j                        s|r_|j                         sO|s;t	        |t
        j                        r!|j                  dk(  r|j                   d   dv r|j                  |       |rTt
        j                  j#                  | |      }t
        j$                  j#                  | |g      }	|j                  |	       g }|j                  |       ( |rft
        j                  j#                  | |      }|r3t
        j$                  j#                  | |g      }	|j                  |	       n|j                  |       || _         | 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   r   r{  )rb   r  rY   r  rQ   r   r_   r^   rW   r   trailing_collapsible_spaceBlockContainerBoxr   is_absolutely_positionedrc  rp  r.   r`   r   )
r"   box_childrenrb   r  r   new_line_childrennew_children	child_boxline_boxrf  s
             r#   r   r     sE   L <<3>>+
%L55>O55 	% H!& 4
 &.2E+eU]]+EJJ).)H)H&).&OOOE234 %%.)C&c5223
L +	i777!C!C!E$$Y/	5#7#78!)*E*E*G !y%--8 NNc)OOM2 78 8!((3  !==77=NO!NN99#zJ	##I.$&!	*1+2 ==//5FG55cH:FI	* )CLJr%   c                 d   | j                   r| j                         r| S g }d}| j                   D ]  }t        |t        j                        rt        | j                         dk(  sJ d| j                   z         d}	 t        ||      \  }}}|nMt        j                  j                  | |g      }|j                  |       |j                  t        |             a|r"t        j                  j                  | |g      }n|}nt        |      }||urd}|j                  |        |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.NT)
skip_stack)rb   r  rQ   r   r   rf   _inner_block_in_inliner   r`   rW   r    )	r"   r  changedr   stacknew_liner   anon	new_childs	            r#   r    r    >  s;   z <<3>>+
LG 'eU]]+s||$) B247LLAB) E)?e*-&%=~~44S8*E##D)##OE$:;   "NN99#zJ	 %	 (.IE!GI&9'< #Jr%   c                    g }d}d}d}|du }|rd}n|j                         \  \  }}t        | j                  |d       D ]  \  }}	||z   }
t        |	t        j
                        r|	j                         r|J |	}|
dz  }
nUt        |	t        j                        rt        |	|      }d}|\  }}}n|J t        |	      }||	urd}|j                  |       ||
|i}| j                  |      }  n |s|r| j                  |      } | ||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  rb   rQ   r   BlockLevelBoxrp  r   r  r    rW   r?  )r"   r  r  block_level_box	resume_atr  is_startskipr  r   index	recursionr  s                r#   r  r    sE    LOIGT!H(..0zcll4512 75Due112'')%%%#OQJE%125*E	!
8A5	?I!)))+E2	%	*&	*I((6C-70 d((6C**r%   c                    | }| j                   j                         dk(  rD| j                  d   dk(  r2| j                  D ]#  }|j                   j                         dk(  s!|} n |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)rB   r  r.   rb   viewport_overflow)root_box
chosen_boxr   s      r#   r8   r8     s     J""$.NN:&)3&& 	E  &&(F2"
	
 ",!1!1*!=H#,JZ Or%   c                 >   | j                         } t        |        t        | t        j                        r| j
                  j                         S t        | t        j                        r4dj                  d | j                         D              j                         S y)Nr   c              3   
  K   | ]{  }|j                   j                  d       s^|j                   j                  d      sC|j                   j                  d      s(t        |t        j                        r|j
                   } yw)z::beforez::afterr   N)rB   r  rQ   r   r_   r^   )r   r   s     r#   r   zbox_text.<locals>.<genexpr>  sf      . $$--j9!!**95!!**:6uemm,	 JJ.s   BB)
r   r5   rQ   r   r_   r^   rr  r   r   r   r!   s    r#   r   r     sw     ,,.Cs#u}}%xx~~	C	)ww .$'OO$5. .
 /4eg	6 r%   c                 6     dv rt        |      S  dv rCt        |t        j                        r(dj	                   fd|j                         D              S y dk(  r=d}d}t        |      }|D ]'  }t        j                  |      }|dvr|r |S d}||z  }) |S y )	N)r^   rC   )rK   rN   r   c              3      K   | ]G  }|j                   j                  d        r't        |t        j                        st        |       I yw)rz   N)rB   r  rQ   r   r   r   )r   r   	text_parts     r#   r   zextract_text.<locals>.<genexpr>  sF      8$)$$--9+.>?ueoo6 8s   AArI   F)PsPePiPfPoT)r   rQ   r   r   r   r   r  r  )r  r"   character_foundfirst_letterr^   r  r  s   `      r#   r   r     s    ''}	)	)c5??+77 8-0__->8 8 8 	n	$} 	#F"++F3H=="  #'F"L	#  
%r%   r2   )NNNNNNN)NN)F)I__doc__rer  r   r   cssr   r   layout.tabler   loggerr   text.constantsr	   r   r   r   rC  r)  r,  FlexBoxInlineFlexBoxGridBoxInlineGridBoxr*  r+  r"  r#  r  r9  rA   r%  chrr  r   compiler|  r~  r  r$   r?   rD   r3   r\   rZ   r   ra   r   r}   re   rV   searchr	  r  r   r  r.  rg  r   ri  r   rt  r5   r6   r  r  r  r   r    r  r8   r   r   )r  s   0r#   <module>r     s   
   % 1  , u~~ ENN	
 U11  -- u}} ++ u}} ++  E%%!" %00#$ U33%& U33'( u++)* U66+, U''-. --/ 6 .34->?CF
O#?   HH5 6rzz'"	$	%2::hD. FJ]K@+\8z ?CIM!%	ut )-'T5@H0-($N ,62::e+<+C+C P
64/ [|@F$"$""2'j*2"J.2m`cL2+j,$i. @s   )H