
    ,gy                         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)	 dEd)Z*d* Z+d+ Z,	 	 	 dFd,Z-	 dGd-Z.d. Z/d/ Z0d0 Z1d1 Z2 ejF                  d2      jf                  fd3Z4dEd4Z5d5 Z6d6 Z7d7 Z8d8 Z9d9 Z:d: Z;d; Z<dHd<Z=d= Z>d> Z?d? Z@d@ ZAdEdAZBdB ZCdC ZDdD ZEyc c} w )IzTurn 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    }/home/viktor/gitlab-persoonlijk/factuur-applicatie/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                          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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 **,"C
 
%C

$CJr%   c                 0    t        |d   d d    | |||      S )Nr(   r   )BOX_TYPE_FROM_DISPLAY)element_tagr.   contentr,   s       r#   make_boxrB   _   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	gdd	gidhgf}|\  }}}t	        ||       g }|j                  t                       || d
      |
_         || d      |
_        g }d|d   v r+t        t        | ||||||            }|j                  |       |j                  t        | d|||||             |d   r|j                  |d   ||
       | j                  }|r/|j                  t        j                   j#                  |
|             | D ])  }t%        ||||||||      }|r~|d	   j&                  d   dk(  ri|d	   }d|j&                  d<   |j                  |        || d      }t        | j                   d|g |       }t)        |||||||      |_        ||_        |g}|j                  |       |j.                  }|st        j                   j#                  |
|      }|rAt        |d   t        j                         r$|d   xj                  |j                  z  c_        |j                  |       , |j                  t        | d|||||             |j1                         D ],  }||   j1                          ||   r|j1                  |       . ||
_        t3        |
       t5        | |
||||       t7        |
       |rYt9        |
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*                  j;                  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   r   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strrB   update_countersappendsetfirst_letter_stylefirst_line_stylelistmarker_to_boxextendbefore_after_to_boxstore_targettextr   TextBoxanonymous_fromr3   r.   content_to_boxeschildrenrE   tailpopprocess_whitespaceset_content_listsprocess_text_transformleninsertr   handle_element)r,   r0   r7   r8   r9   r:   r;   stater.   r(   r"   quote_depthcounter_valuescounter_scopesr`   marker_boxesr\   child_elementchild_boxesrE   
call_stylefootnote_calltext_boxnamemarker_stylemarkers                             r#   r3   r3   d   sG   2 gkk3'	gE IG)	W~##$/0E)  2E)
7;;r7
3C} C!\N
 38/KE5!H #% &w?C$Wl;CLeI&&MUE9.@m- . 	%OO'5)-?-) * X%%eHo~sK<<D44S$?@  *$9&8(mY? ;q>//8JF"1~H&,HNN7#X&"7O<J$;;-/RJM%5M;"$4m&EM" &.M"(/K$!!}}33C>HJx|U]]C!!X]]2!)3*6 OO'%,>-) *
 ""$ %t  "d#t$%
 CLse^-=}N3CLL)Q. &'94LL < <S% HIW~#z"2&!+& *;<{{m,-|RJ*#{N<N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)   rA   )normalinhibitr*   ::rH   bookmark_levelr*   bookmark_label)	rB   rP   rR   rW   rX   rY   r_   r`   compute_bookmark_label)r,   r-   ri   r0   r7   r9   r:   r.   r(   rA   r"   rj   rk   _counter_scopesr`   rm   _quote_depths                    r#   rZ   rZ     s0    g{+Eu} 	
 IG)	IG//	
gkk]"[M2E2w
GC380KE5!HgMUE9.@m- . 	%OO$sK1C-) * CL &(8=5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(  rt        j                  j                  ||      }d|j                  d<   |d   dk(  rt        j                  dd      }nt        j                  dd      }t        j                  }d||fff|j                  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

    ru   ::markerr(   r)   Nlist_style_imagerA   )rw   rx   urlimage_orientationr   orientationlist_style_typer*   rH   r   rK   zpre-wrapwhite_spacerM   rN   absoluteposition	directionltri%d   	translate	transform)rB   rP   rY   r_   r   InlineReplacedBoxr^   rS   getrender_markerr]   r.   BlockBoxr   	DimensionZERO_PIXELS	InlineBox)r,   ri   parent_styler0   r7   r9   r:   r.   r`   rj   rk   r}   r"   
image_typeimagecounter_valuecounter_typemarker_text
marker_boxtranslate_xtranslate_ys                        r#   rX   rX   3  s     gx(EH
 490K
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
$$-$..tS9K$..sC8K ,,;45)8
% __33CB

s   GGc                 r  /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        |      } ||j                                |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              } ||       |dv r7|dd |d   c\  }}00dk(  r|j'                  |||      }|j(                  dk7  r nY|j*                  j                  } |r@|| vr<t-        j.                  |      }!|j1                  |!g       }||vr|j                  |       |j2                  j	                         }"|"j5                  |        |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(  r[|\  }}$|j'                  |||      }|j(                  dk(  r0|j*                  }%t        |$|%      } ||j                                 n|dk(  rd||	fvrd|v }&|j7                  d       xr |	dk7  }'|&st9        d|d   dz
        |d<   |'rC|	dk(  rt;        |
      \  }(})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| }+|+|+jC                         }+d|+j
                  d <   tE        |+t        jF                        rJ|+jI                         D ]7  },|,j
                  d!   d"v rtK        |,j
                  |,||||||#	      |,_&        9 /j                  |+       |d$k(  s|d   st        jN                  j                  |d         }-t        jP                  j                  |-f      }.d%|.j
                  d&<   d%|-j
                  d&<   d'|._)        /j                  |.        1s/r|jU                  ||||       /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 )NTrK   )addrO   r   r]   r\   rS   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   rK   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   rA   )rw   r*   )contextpagezleader()prer   T)+rT   
collectingcached_counter_valuesitemscopyr.   rS   r   r   r^   extract_textstripr   warningjoinget_string_set_forrW   r   r   lookup_targetri   
target_boxr   anchor_name_from_token
setdefaultcached_page_counter_valuesupdate
startswithmaxr	   minrf   get_running_element_fordeepcopyrO   	ParentBoxdescendantsr_   r`   r]   r   	is_leadercollect_missing_counters)2content_listr   rk   	css_tokenparse_againr9   r:   r7   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childrr   
leader_boxr   r   r   s2    `    `                                        @@@r#   compute_content_listr   u  sP   ( MuHD  T)>d$.>O ,66N;N''/ 1?0D0D0F,H",#uC,H
($ E-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   ,KE-N =11	;0@#	%  !S,Hs   V3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   rY   r_   rf   r`   rO   r   LineBox)	mixin_pagebased_countersr   local_childrenr:   r7   orig_quote_depthr   r.   r9   s	      r#   r   z%content_to_boxes.<locals>.parse_again5  s    
 $+N5::<Nj>>?.:/ 0-A 	B 
##$):..q15==A.<J"+"0Jr%   rA   rx   r   r   r2   )r   )r.   r   rj   rk   r7   r9   r:   r   r   r   r   r<   r   s   ``  ```     @r#   r_   r_   1  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_namer9   s     r#   r   z'compute_string_set.<locals>.parse_again^  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   rO   r   r]   r\   r   r"   s     r#   r   z%compute_string_set.<locals>.<genexpr>r  s(      LJsEMM,JCHHLs   33r   r2   )r   r   
string_setremoverS   )r,   r"   r   r   rk   r9   r:   r   r   r<   r   string_set_tuples   ```` ``     r#   r   r   [  s    - - {m,I#c>9k-:H  L (L L !$ 	"k1%%&67	 	{F34 r%   c           
           i f fd	}d}t        |||       }|rdj                  d |D              _        yy)z5Parses the content-list value for ``bookmark-label``.c                     | i }n| j                         }| 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,   r9   s     r#   r   z+compute_bookmark_label.<locals>.parse_again  sW     $+N5::<N1668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   N)r   r   r{   )	r,   r"   r   rk   r9   r:   r   r   r<   s	   ``` ``   r#   r|   r|   }  sV     .0   !I#c>9k-:H WW$Gh$GG r%   c           
          g |_         |d   dk7  r*t        |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*   rz   r{   N)r  	enumerater   r|   )	r,   r"   r.   rk   r9   r:   ir   string_valuess	            r#   rd   rd     s     CN\f$/8|9L/M 	1+A+]k=. -1	1 &(S% 01>m	- )r%   c                 8   | \  }}}|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.rK   counter_resetcounter_setr   counter_incrementr   rH   r(   )rH   r
   N)rb   r   r   rS   )
ri   r.   r~   rk   rl   sibling_scopesrs   r   valuesr  s
             r#   rR   rR     sd   380L..#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   )rO   )r   wrapper_types    r#   testzwrap_improper.<locals>.test  s    e\22r%   )r`   )r^   table_boxes_childrenrS   )r"   r`   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

    )rO   r   r   
is_runningr`   r   r  r"   r   r`   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 z   ||dd dgz         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   rK   c                     | j                   S r2   proper_table_childr   s    r#   <lambda>z&table_boxes_children.<locals>.<lambda>=  s    %22 r%   c                 8    t        | t        j                         S r2   )rO   r   TableCellBoxr%  s    r#   r&  z&table_boxes_children.<locals>.<lambda>I  s    j0B0BCC r%   c                     | j                    S r2   r#  r%  s    r#   r&  z&table_boxes_children.<locals>.<lambda>O  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>T  s&    u777 ?&%*>*>> r%   )rO   r   TableColumnBoxTableColumnGroupBoxspanranger^   tabular_containerrf   internal_table_or_captionr  rb   zipTableBoxr  TableRowBoxTableRowGroupBoxr(  r   InlineTableBoxtype
wrap_tablerW   r`   )
r"   r`   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FXcr]"QRLD6!.
 )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collapser?  r@  )(r   r-  r.  r5  r6  TableCaptionBoxr8  rS   r.   rW   r  grid_xr`   r/  	is_header	is_footerrT   rb   colspanrowspanrf   r   r0  r   r   copy_with_childrenr   tuplecolumn_groupsr   collapsed_border_gridrO   r7  InlineBlockBoxr   r^   is_table_wrapperr   TABLE_WRAPPER_BOX_PROPERTIESINITIAL_VALUES)!r"   r`   columnsrowsall_captionsby_typer   captionscaptionrL  rE  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  rs   s!                                    r#   r9  r9  ^  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| 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

    )rO   r   r   r  r`   r   flex_childrenr  s      r#   r   r     V     c5??+s~~/?
 03||<e
5!<H< h/CLJ =   A"c                    t        | t        j                        rGg }|D ]=  }d |_        |j	                         rd|_        t        |t        j                        r|j                  j                  d      sYt        |t        j                        rTt        j                  j                  ||j                        }|j                  |_        d|_        |j                  |       t        |t        j                        rLt        j                  j                  ||g      }|j                  |_        d|_        |j                  |       -|j                  |       @ |S |S )Nc                       y)NFr+   r+   r%   r#   r&  zflex_children.<locals>.<lambda>  s    r%   Tr   )rO   r   FlexContainerBox
is_floatedis_in_normal_flowis_flex_itemr]   r\   r   rN  r   r^   r`   r.   rS   InlineLevelBox)r"   r`   rj  r   	anonymouss        r#   rj  rj    s   #u--. 	,E,E&&(%)"%/

8H8H8M %!5!56!NN99%P	"'++	)-	&$$Y/E5#7#78!NN99%%I	"'++	)-	&$$Y/$$U+)	,* 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

    )rO   r   r   r  r`   r   grid_childrenr  s      r#   r   r     rk  rl  c                    t        | t        j                        rFg }|D ]<  }|j                         rd|_        t        |t        j
                        r|j                  j                  d      sQt        |t        j                        rTt        j                  j                  ||j                        }|j                  |_        d|_        |j                  |       t        |t        j                        rSt        j                  j                  ||g      }|j                  |_        d|_        d|_        |j                  |       ,|j                  |       ? |S |S )NTr   F)rO   r   GridContainerBoxrq  is_grid_itemr]   r\   r   rN  r   r^   r`   r.   rS   rs  )r"   r`   rv  r   rt  s        r#   rv  rv     s   #u--. 	,E&&(%)"%/

8H8H8M %!5!56!NN99%P	"'++	)-	&$$Y/E5#7#78!NN99%%I	"'++	%*")-	&$$Y/$$U+)	,* 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	}|| _        n| j                  D ]p  }t        |t        j                  t        j                  f      r1t        ||      }| j!                         sJ|j!                         s[|}^|j!                         sod	}r |xr | j#                          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   )rw   nowraprw   r|  zpre-liner   r
   NTF)rO   r   r]   r\   LINE_FEED_REsubr.   TAB_REreplaceSPACE_REr   leading_collapsible_spaceendswithr`   r   rc   rq  r  )r"   following_collapsible_spacer\   new_line_collapsespace_collapseprevious_textr   child_collapsible_spaces           r#   rc   rc   =  si    #u}}%xx.. d+IIm48LL=1 6, , ::dD)D
 <<c*D#+<<T#::MD*ts/CABx04-*7*@*@*E'*/' \\ 	4E%%--!AB*<6+8'((*u/F/F/H2I/((*.3+	4 '?s~~/?+??r%   c                    t        | t        j                        ro| j                  d   }|dk7  r& d d t        d d|   | j
                        | _        | j                  d   dk(  r"| j
                  j                  dd	      | _        y y | j                         sH| j                  D ]8  }t        |t        j                  t        j                  f      s.t        |       : y y )
Ntext_transformr*   c                 "    | j                         S r2   )upperr\   s    r#   r&  z(process_text_transform.<locals>.<lambda>x      $**, r%   c                 "    | j                         S r2   )lowerr  s    r#   r&  z(process_text_transform.<locals>.<lambda>y  r  r%   c                 ,    | j                  t              S r2   )r   ASCII_TO_WIDEr  s    r#   r&  z(process_text_transform.<locals>.<lambda>{  s    4>>-+H r%   )	uppercase	lowercase
capitalizez
full-widthhyphens   ­r   )rO   r   r]   r.   r  r\   r  r  r`   r   re   )r"   r  r   s      r#   re   re   s  s    #u}}%#34V#66(H	
 
 "hh(CH 99Y6)xx''"5CH * ^^\\ 	.E%%--!AB&u-	. r%   c                     d}d}| D ]?  }t        j                  |      d   }|s|dv rd}|j                         }n|dk(  rd}||z  }A |S )uC   Capitalize words according to CSS’s "text-transform: capitalize".Fr   r   )LNTZ)unicodedatacategoryr  )r\   letter_foundoutputletterr  s        r#   r  r    sg    LF ''/2J 6L\\^F_ 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}  )r`   r  rW   r  rO   r   r]   r\   rS   r   trailing_collapsible_spaceBlockContainerBoxr   is_absolutely_positionedrs  rq  r.   r^   r   )
r"   box_childrenr`   r  r   new_line_childrennew_children	child_boxline_boxrt  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)r`   r  rO   r   r   rf   _inner_block_in_inliner   r^   rS   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	  r`   rO   r   BlockLevelBoxrq  r   r  r    rS   rJ  )r"   r  r  block_level_box	resume_atr  is_startskipr
  r   index	recursionr  s                r#   r  r  k  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)r@   r  r.   r`   viewport_overflow)root_box
chosen_boxr   s      r#   r6   r6     s     J""$.NN:&)3&& 	E  &&(F2"
	
 ",!1!1*!=H#,JZ Or%   c                     t        | t        j                        r| j                  S t        | t        j                        r&dj                  d | j                         D              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)r@   r  rO   r   r]   r\   )r   r   s     r#   r   zbox_text.<locals>.<genexpr>  sf      . $$--j9!!**95!!**:6uemm,	 JJ.s   BB)rO   r   r]   r\   r   r   r   r!   s    r#   r  r    sQ    #u}}%xx	C	)ww .$'OO$5. . 	. 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\   rA   )rI   rL   r   c              3      K   | ]G  }|j                   j                  d        r't        |t        j                        st        |       I yw)ry   N)r@   r  rO   r   r   r  )r   r   	text_parts     r#   r   zextract_text.<locals>.<genexpr>  sF      8$)$$--9+.>?ueoo6 8s   AArG   F)PsPePiPfPoT)r  rO   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)F__doc__rer  r   r   cssr   r   layout.tabler   loggerr   text.constantsr	   r   r   r   rN  r4  r7  FlexBoxInlineFlexBoxGridBoxInlineGridBoxr5  r6  r-  r.  r(  rD  r?   r0  chrr  r   compiler~  r  r  r$   r=   rB   r3   rZ   rX   r   r_   r   r|   rd   rR   searchr  r  r   r  r9  r   rj  r   rv  rc   re   r  r   r    r  r6   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@. FJ^KB+\?H ?CIM!%	y| )-'T5DH2-($N ,62::e+<+C+C P
64/ [|@F":":3@l.&m`cL2+j,
m, @s   )H