3
۸W                 @   sX  d Z ddlmZmZ ddlZddlZddlZddlmZm	Z	m
Z
mZ ddlmZ ddlmZ dd	lmZmZmZ ed
d ejejgejggejejggejejggejejejejejejggej ejejejejejggejejggejejggejej!ggij" D Z#dd Z$dd Z%dd Z&edfddZ'dd Z(edfddZ)dd Z*dd Z+dd Z,e
dd  Z-e
d!d" Z.e
d#d$ Z/e
d%d& Z0e
d'd( Z1e
d)d* Z2e
d+d, Z3e
d-d. Z4e
d/d0 Z5e
d1d2 Z6e
d3d4 Z7e
d5d6 Z8e
d7d8 Z9e
d9d: Z:e
d;d< Z;e
d=d> Z<e
d?d@ Z=e
dAdB Z>e
dCdD Z?e
dEdF Z@dS )Ga  
    weasyprint.tests.test_boxes
    ---------------------------

    Test that the "before layout" box tree is correctly constructed.

    :copyright: Copyright 2011-2014 Simon Sapin and contributors, see AUTHORS.
    :license: BSD, see LICENSE for details.

    )divisionunicode_literalsN   )resource_filenameTestHTMLassert_no_logscapture_logs   )get_all_computed_styles)images)boxesbuildcountersc             c   s$   | ]\}}|t tt |fV  qd S )N)tuplemap).0keyvalue r   [/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/tests/test_boxes.py	<genexpr>   s    r   c             C   s   dd | D S )zDTransform a box list into a structure easier to compare for testing.c          	   S   s~   g | ]v}|j |jjr,t|tjtjfkr,d ndt|jdd  t|tjrR|j	n$t|tj
rbdntt|df |j fqS )ZAnon N   z
<replaced>column_groups)element_tagstyle	anonymoustyper   ZTextBoxLineBox__name__
isinstancetextZReplacedBox	serializegetattrchildren)r   boxr   r   r   
<listcomp>.   s   zserialize.<locals>.<listcomp>r   )Zbox_listr   r   r   r#   +   s    r#   c             C   s^   | j dkstt| tjstt| jdks0t| jd } t| tjsJt| j dksXt| jS )zTest that the box tree starts with a ``<html>`` and a ``<body>`` blocks.

    Remove them to simplify further tests. These are always at the root
    of HTML documents.

    htmlr   r   body)r   AssertionErrorr!   r   BlockBoxlenr%   )r&   r   r   r   unwrap_html_body9   s    
r-   c             C   s   t t| S )z/Serialize and unwrap ``<html>`` and ``<body>``.)r#   r-   )Zbox_treer   r   r   to_listsK   s    r.   z<test>c             C   s2   t | |d}t|}tjtji |j}|j||fS )N)stringbase_url)r   r
   	functoolspartialr   get_image_from_uriZurl_fetcherZroot_element)html_contentr0   Zdocument	style_forr3   r   r   r   _parse_baseP   s
    r6   c             C   s   t jt|  \}|S )z:Parse some HTML, apply stylesheets and transform to boxes.)r   Zelement_to_boxr6   )r4   r&   r   r   r   parse[   s    r7   c             C   s   t jt| | }t| |S )z3Like parse() but also run all corrections on boxes.)r   Zbuild_formatting_structurer6   sanity_checks)r4   r0   r&   r   r   r   	parse_alla   s    
r9   c             C   s$   dd t | tddjddjD S )z8Lay out a document and return a list of PageBox objects.c             S   s   g | ]
}|j qS r   )Z	_page_box)r   pr   r   r   r'   k   s    z render_pages.<locals>.<listcomp>z<test>)r/   r0   T)Zenable_hinting)r   r   Zrenderpages)r4   r   r   r   render_pagesi   s    r<   c             C   sH   t | }||krDtdjtjdd ||gD ddi ||ksDtdS )zCheck the box tree equality.

    The obtained result is prettified in the message in case of failure.

    box: a Box object, starting with <html> and <body> blocks.
    expected: a list of serialized <body> children as returned by to_lists().

    r   c             S   s   g | ]}t j|jd qS )T)pprintpformat
splitlines)r   vr   r   r   r'   |   s    zassert_tree.<locals>.<listcomp>ni'  N)r.   printjoindifflibZunified_diffr*   )r&   expectedlistsr   r   r   assert_treep   s    	
rG   c                s|   t  tjsdS d}x&t j D ]}|tkr"t| }P q"W t fdd|D s`t  jfx jD ]}t	| qhW dS )a  Check that the rules regarding boxes are met.

    This is not required and only helps debugging.

    - A block container can contain either only block-level boxes or
      only line boxes;
    - Line boxes and inline boxes can only contain inline-level boxes.

    Nc             3   s&   | ] t  fd djD V  qdS )c             3   s"   | ]}t | p|j  V  qd S )N)r!   Zis_in_normal_flow)r   child)acceptable_typesr   r   r      s   z*sanity_checks.<locals>.<genexpr>.<genexpr>N)allr%   )r   )r&   )rI   r   r      s   z sanity_checks.<locals>.<genexpr>)
r!   r   Z	ParentBoxr   mroPROPER_CHILDRENanyr*   r%   r8   )r&   Zacceptable_types_listsclass_rH   r   )r&   r   r8      s    

r8   c               C   sJ   t tdddg fg t tdddddddddddgfgfdgfg dS )z-Test the creation of trees from HTML strings.z<p>r:   Blockz
        <style>
            span { display: inline-block }
        </style>
        <p>Hello <em>World <img src="pattern.png"><span>L</span></em>!</p>TextHello emInlineWorld imgInlineReplaced
<replaced>spanInlineBlockL!N)r:   rP   rQ   )rR   rP   rT   )rU   rV   rW   )rX   rP   rZ   )r:   rP   r[   )rG   r7   r   r   r   r   test_box_tree   s    
r\   c              C   s2   x,dD ]$} t tdj| | dddgfg qW d
S )z%Test the management of HTML entities."&quot;&#x22;&#34;z<p>{0}abc{1}r:   rO   rP   "abc"N)r]   r^   r_   r`   )r:   rP   ra   )rG   r7   format)quoter   r   r   test_html_entities   s    
rd   c              C   sj  d} ddddddddd	dgfdgfgfdddddgfgfgfg}t | }tj|}t|| d} dddddddgfgfddddddd	dgfdgfgfgfg}t | }tj|}t|| d} ddddddddddgfgfdgfgfg}t | }tj|}t|| tj|}t|| d} t | }tj|}tj|}t|dddddddddd gfgfd!gfgfg dS )"z3Test the management of inline boxes in block boxes.z0<div>Hello, <em>World</em>!
<p>Lipsum.</p></div>divrO   	AnonBlockLinerP   Hello, rR   rS   World!
r:   Lipsum.z0<div><p>Lipsum.</p>Hello, <em>World</em>!
</div>zj<p>Hello <em style="position:absolute;
                                    display: block">World</em>!</p>Hello r[   z0<p>Hello <em style="float: left">World</em>!</p>N)re   rP   rh   )rR   rP   ri   )re   rP   rj   )r:   rP   rk   )r:   rP   rk   )re   rP   rh   )rR   rP   ri   )re   rP   rj   )r:   rP   rl   )rR   rP   ri   )r:   rP   r[   )r:   rP   rl   )rR   rP   ri   )r:   rP   r[   )r7   r   inline_in_blockrG   block_in_inline)sourcerE   r&   r   r   r   test_inline_in_block   sj    













rp   c              C   s  t d} tj| } t| ddddddddd	ddd	ddddddgfgfddddddgfgfgfdddddd	dddg fgfgfgfgfgfgfgfg tj| } t| ddddddddddd	ddd	dgfgfgfgfddddd gfgfdddddd	dd	d!gfgfgfgfddddd"gfgfdddddd	dd	g fgfgfgfdddddddd	d#gfgfgfddg fdddddd	g fgfgfgfdddddd	g fgfgfgfgfg dS )$z3Test the management of block boxes in inline boxes.z
<style>
    p { display: inline-block; }
    span, i { display: block; }
</style>
<p>Lorem <em>ipsum <strong>dolor <span>sit</span>
    <span>amet,</span></strong><span><em>conse<i></i></em></span></em></p>r)   rg   r:   rY   rP   Lorem rR   rS   ipsum strongdolor rX   rO   sit
    amet,conseirf   N)r:   rP   rq   )rR   rP   rr   )rs   rP   rt   )rX   rP   ru   )rs   rP   rv   )rX   rP   rw   )rR   rP   rx   )r:   rP   rq   )rR   rP   rr   )rs   rP   rt   )rX   rP   ru   )rs   rP   rv   )rX   rP   rw   )rR   rP   rx   )r7   r   rm   rG   rn   )r&   r   r   r   test_block_in_inline   s|    

*


rz   c              C   s   t d} tj| } tj| } t| j }t|dks8t|d | ksHtxF|D ]>}|jj	dksbt|jj
r||jjd	kstqN|jjd
ksNtqNW dS )z$Test the application of CSS to HTML.a	  
        <style>
            span { display: block; }
            * { margin: 42px }
            html { color: blue }
        </style>
        <p>Lorem <em>ipsum <strong>dolor <span>sit</span>
            <span>amet,</span></strong><span>consectetur</span></em></p>   r   r   px*   N)r   r   r   r   )r   r|   )r}   r|   )r7   r   rm   rn   listdescendantsr,   r*   r   colorr   
margin_top)r&   r   rH   r   r   r   test_stylesK  s    


r   c               C   s~   t tdddddddddddddg fdddgfdgfdgfgfdddddgfgfdddddgfgfddddd gfgfg dS )!z$Test the management of white spaces.aO  
        <p>Lorem 	
  ipsum	<strong>  dolor
            <img src=pattern.png> sit
            <span style="position: absolute"></span> <em> amet </em>
            consectetur</strong>.</p>
        <pre>	  foo
</pre>
        <pre style="white-space: pre-wrap">	  foo
</pre>
        <pre style="white-space: pre-line">	  foo
</pre>
    r:   rO   rg   rP   Lorem ipsum rs   rS   dolor rU   rV   
<replaced> sit rX   rR   amet consectetur.pre	  foo
 foo
N)r:   rP   r   )rs   rP   r   )rU   rV   r   )rs   rP   r   )rR   rP   r   )rs   rP   r   )r:   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )rG   r9   r   r   r   r   test_whitespaceg  s.    


r   c                 sf   t tdd  fdd} | dddddd	 | d
ddddd	 | dddddd	 | dddddd	 dS )z#Test the management of page styles.z
        <style>
            @page { margin: 3px }
            @page :first { margin-top: 20px }
            @page :right { margin-right: 10px; margin-top: 10px }
            @page :left { margin-left: 10px; margin-top: 10px }
        </style>
    )r/   c                sT    | }|j |dfkst|j|dfks,t|j|dfks>t|j|dfksPtdS )zCheck the page margin values.r|   N)r   r*   Zmargin_rightZmargin_bottomZmargin_left)Z	page_typetoprightbottomleftr   )r5   r   r   assert_page_margins  s
    z,test_page_style.<locals>.assert_page_marginsZfirst_left_page   r   
   )r   r   r   r   Zfirst_right_pageZ	left_pageZ
right_pageN)r
   r   )r   r   )r5   r   test_page_style  s    
r   c              C   s   t  } td}W dQ R X t| dks*td| d ks:td| d ksJtt|dddd	dd
ddgfd
ddgfgfgfg t  } tddd}W dQ R X t| dkstd| d kstt|dddd	d
ddgfgfgfg dS )z-Test images that may or may not be available.z
            <p><img src=pattern.png
                /><img alt="No src"
                /><img src=inexistent.jpg alt="Inexistent src" /></p>
        Nr   zWARNING: Failed to load imager   zinexistent.jpgr:   rO   rg   rU   rV   
<replaced>rS   rP   No srcInexistent srcz*<p><img src=pattern.png alt="No base_url">)r0   z2WARNING: Relative URI reference without a base URINo base_url)rU   rV   r   )rU   rP   r   )rU   rP   r   )rU   rP   r   )r   r9   r,   r*   rG   )logsresultr   r   r   test_images  s0    r   c               C   s  t tdddddddd2gfgfdd	dd
ddg fgfddddddg fgfgfddddddddd3gfgfddddd4gfgfgfgfddg fddddddddd5gfgfgfgfddg fgfddg fgfg t tdddddddddddddd6gfgfddddd7gfgfgfgfgfgfg t tdddddddd dg fgfd!dd!d"g fgfgfgfg t td#dddd	dddddd$ddddd8gfgfd&d'g fgfgfgfgfgfg t td(dddddddddd$d&d'g fgfddg fgfgfgfgfg t td)dddd*dd+dd,ddddg fgfgfgfgfgfg t td-dddd*d9dd+dd,ddddd dg fd dg fgfgfgfgfd:gfgfg t td/ddddddddg fddg fgfgfgfg t td0dddddd
ddg fgfd1dd1d"g fgfgfgfg d S );Na  
        <x-table>
            <x-tr>
                <x-th>foo</x-th>
                <x-th>bar</x-th>
            </x-tr>
            <x-tfoot></x-tfoot>
            <x-thead><x-th></x-th></x-thead>
            <x-caption style="caption-side: bottom"></x-caption>
            <x-thead></x-thead>
            <x-col></x-col>
            <x-caption>top caption</x-caption>
            <x-tr>
                <x-td>baz</x-td>
            </x-tr>
        </x-table>
    x-tablerf   	x-captionZTableCaptionrg   rP   top captionZTableZAnonTableColumnGroupzx-colZTableColumnzx-theadZTableRowGroupZAnonTableRowx-thZ	TableCellZAnonTableRowGroupzx-trZTableRowfoobarx-tdbazzx-tfootzo
        <span style="display: table-cell">foo</span>
        <span style="display: table-cell">bar</span>
    r)   Z	AnonTablerX   a#  
        <span style="display: table-column-group">
            1
            <em style="display: table-column">
                2
                <strong>3</strong>
            </em>
            <strong>4</strong>
        </span>
        <ins style="display: table-column-group"></ins>
    ZTableColumnGrouprR   ZinsZAnonTableColumnz"<x-table>foo <div></div></x-table>ZAnonTableCellfoo re   rO   zO<x-thead style="display: table-header-group"><div></div><x-td></x-td></x-thead>z<span><x-tr></x-tr></span>rS   ZAnonInlineBlockZAnonInlineTablez
        <span>
            <em style="display: table-cell"></em>
            <em style="display: table-cell"></em>
        </span>
     z<x-tr></x-tr>	<x-tr></x-tr>z)<x-col></x-col>
<x-colgroup></x-colgroup>z
x-colgroup)r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )rX   rP   r   )rX   rP   r   )r   rP   r   )rX   rP   r   )rX   rP   r   )rG   r9   r   r   r   r   test_tables  s    


 

""r   c              C   s   t d} | j\}|j\}|j\}t|tjs0tt|tjs@t|jjdksPt|jj	dks`t|jjdkspt|jj	d	kstd S )
Nz1<table style="margin: 1px; padding: 2px"></table>r   r|   r   r	   )r   r|   )r   r|   )r   r|   )r	   r|   )
r9   r%   r!   r   r+   r*   TableBoxr   r   Zpadding_top)r(   r)   wrappertabler   r   r   test_table_styleg  s    r   c              C   s   t d} | j\}|j\}|j\}|j\}dd |jD }|dddddgksNtdd |jD dd	d
ddgkspt|jd |jd	 k	std S )Nzx
        <table>
            <col span=3 style="width: 10px"></col>
            <col span=2></col>
        </table>
    c             S   s   g | ]}|j jqS r   )r   width)r   colr   r   r   r'     s    z%test_column_style.<locals>.<listcomp>r   r|   autoc             S   s   g | ]
}|j qS r   )grid_x)r   r   r   r   r   r'     s    r   r   r	   r      )r   r|   )r   r|   )r   r|   )r9   r%   r   r*   )r(   r)   r   r   colgroupZwidthsr   r   r   test_column_styleu  s    "r   c              C   sh   t d} | j\}|j\}|j\}dd |jD }|dddgfdddgfddd	d
gfddgfgksdtd S )Nz
        <table>
            <col span=2></col>
            <colgroup span=2></colgroup>
            <colgroup>
                <col></col>
                <col span=2></col>
            </colgroup>
            <col></col>
        </table>
    c             S   s"   g | ]}|j d d |jD fqS )c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r'     s    z1test_nested_grid_x.<locals>.<listcomp>.<listcomp>)r   r%   )r   r   r   r   r   r'     s   z&test_nested_grid_x.<locals>.<listcomp>r   r   r	   r   r            )r9   r%   r   r*   )r(   r)   r   r   gridr   r   r   test_nested_grid_x  s    

r   c              C   sb  t d} | j\}|j\}|j\}|j\}dd |jD dddgddgddgdgddggks^tdd |jD dddgddgddgdgddggkstd	d |jD dddgddgddgdgddggkstt d
} | j\}|j\}|j\}|j\}dd |jD ddgdggkstdd |jD ddgdggks:tdd |jD ddgdggks^tdS )a  
    +---+---+---+
    | A | B | C | #
    +---+---+---+
    | D |     E | #
    +---+---+   +---+
    |  F ...|   |   |   <-- overlap
    +---+---+---+   +
    | H | #   # | G |
    +---+---+   +   +
    | I | J | # |   |
    +---+---+   +---+

    # empty cells

    a  
        <table>
            <tr>
                <td>A <td>B <td>C
            </tr>
            <tr>
                <td>D <td colspan=2 rowspan=2>E
            </tr>
            <tr>
                <td colspan=2>F <td rowspan=0>G
            </tr>
            <tr>
                <td>H
            </tr>
            <tr>
                <td>I <td>J
            </tr>
        </table>
    c             S   s   g | ]}d d |j D qS )c             S   s   g | ]
}|j qS r   )r   )r   cr   r   r   r'     s    z3test_colspan_rowspan.<locals>.<listcomp>.<listcomp>)r%   )r   rowr   r   r   r'     s    z(test_colspan_rowspan.<locals>.<listcomp>r   r   r	   r   c             S   s   g | ]}d d |j D qS )c             S   s   g | ]
}|j qS r   )colspan)r   r   r   r   r   r'     s    z3test_colspan_rowspan.<locals>.<listcomp>.<listcomp>)r%   )r   r   r   r   r   r'     s    c             S   s   g | ]}d d |j D qS )c             S   s   g | ]
}|j qS r   )rowspan)r   r   r   r   r   r'     s    z3test_colspan_rowspan.<locals>.<listcomp>.<listcomp>)r%   )r   r   r   r   r   r'     s    z
        <table>
            <tr>
                <td rowspan=5></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>
    c             S   s   g | ]}d d |j D qS )c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r'     s    z3test_colspan_rowspan.<locals>.<listcomp>.<listcomp>)r%   )r   r   r   r   r   r'     s    c             S   s   g | ]}d d |j D qS )c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r'     s    z3test_colspan_rowspan.<locals>.<listcomp>.<listcomp>)r%   )r   r   r   r   r   r'     s    c             S   s   g | ]}d d |j D qS )c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r'     s    z3test_colspan_rowspan.<locals>.<listcomp>.<listcomp>)r%   )r   r   r   r   r   r'     s    N)r9   r%   r*   )r(   r)   r   r   groupr   r   r   test_colspan_rowspan  sN    
r   c              C   sb  t tdddg fddg fddg fg t tddddddd	d)gfd*dd	d+gfgfgfg t tddddddd	dd	d,gfd-gfgfgfg t tddddddd	dd	d.gfd/dd	dd	d0gfd1dd	d2gfgfd3dd	d4gfgfgfgfg t 4} t tddddddd	d5d6d7gfd8gfgfg W d$Q R X t| d%ks:td&| d' ksLtd(| d' ks^td$S )9z,Test the :before and :after pseudo-elements.z
        <style>
            p:before { content: normal }
            div:before { content: none }
            section:before { color: black }
        </style>
        <p></p>
        <div></div>
        <section></section>
    r:   rO   re   sectionz
        <style>
            p:before { content: 'a' 'b' }
            p:after { content: 'd' 'e' }
        </style>
        <p>
            c
        </p>
    rg   p:beforerS   rP   ab c p:afterdez
        <style>
            a[href]:before { content: '[' attr(href) '] ' }
        </style>
        <p><a href="some url">some text</a></p>
    aa:before[some url] 	some textu   
        <style>
            body { quotes: '«' '»' '“' '”' }
            q:before { content: open-quote ' '}
            q:after { content: ' ' close-quote }
        </style>
        <p><q>Lorem ipsum <q>dolor</q> sit amet</q></p>
    qq:before   « Lorem ipsum    “ dolorq:after    ”	 sit amet    »av  
            <style>
                p:before {
                    content: 'a' url(pattern.png) 'b';

                    /* Invalid, ignored in favor of the one above.
                       Regression test: this used to crash: */
                    content: some-function(nested-function(something));
                }
            </style>
            <p>c</p>
        AnonInlineReplaced
<replaced>br   Nr   znested-function(r   zinvalid value)r   rP   r   )r:   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   r   r   )r   rP   r   )r:   rP   r   )rG   r9   r   r,   r*   )r   r   r   r   test_before_after  sh    	
r   c               C   sf  t tddd dj D  t tddddddd	d*gfgfdddd	d+gfgfdddd	d,gfgfdddddd	d-gfgfdddddd	d.gfgfdddd	d/gfgfdddd	d0gfgfgfgfdddd	d1gfgfgfg t tddddddd	d2gfgfdddd	d3gfgfdddd	d4gfgfgfdddd	d5gfgfg t tddddddd	ddg fgfgfdddd	ddd6gfgfgfdddd	ddd7gfgfgfdddddd	ddg fgfgfdddd	ddd8gfgfgfdddd	ddd9gfgfgfgfdddd	ddd:gfgfgfgfg t td"dddd	d#dd;gfgfgfdddd	d%dd<gfgfgfg t td'dddd	d=gfgfg d)S )>zDTest counter-reset, counter-increment, content: counter() counters()a  
        <style>
            p { counter-increment: p 2 }
            p:before { content: counter(p); }
            p:nth-child(1) { counter-increment: none; }
            p:nth-child(2) { counter-increment: p; }
        </style>
        <p></p>
        <p></p>
        <p></p>
        <p style="counter-reset: p 117 p"></p>
        <p></p>
        <p></p>
        <p style="counter-reset: p -13"></p>
        <p></p>
        <p></p>
        <p style="counter-reset: p 42"></p>
        <p></p>
        <p></p>c             S   s.   g | ]&}d dd ddddd|fgfgfgfqS )r:   rO   rg   zp:beforerS   rP   r   )r   counterr   r   r   r'   p  s   z!test_counters.<locals>.<listcomp>z!0 1 3  2 4 6  -11 -9 -7  44 46 48a1  
        <ol style="list-style-position: inside">
            <li></li>
            <li></li>
            <li></li>
            <li><ol>
                <li></li>
                <li style="counter-increment: none"></li>
                <li></li>
            </ol></li>
            <li></li>
        </ol>ZolrO   Zlirg   
li::markerrP   1.2.3.rf   4.5.z
        <style>
            p { display: list-item; list-style: inside decimal }
        </style>
        <div>
            <p></p>
            <p></p>
            <p style="counter-reset: list-item 7 list-item -56"></p>
        </div>
        <p></p>re   r:   	p::marker-55.a  
        <style>
            section:before { counter-reset: h; content: '' }
            h1:before { counter-increment: h; content: counters(h, '.') }
        </style>
        <body>
            <section><h1></h1>
                <h1></h1>
                <section><h1></h1>
                    <h1></h1>
                </section>
                <h1></h1>
            </section>
        </body>r   zsection:beforerS   Zh1	h1:before122.12.23z
        <style>
            p:before { content: counter(c) }
        </style>
        <div>
            <span style="counter-reset: c">
                Scope created now, deleted after the div
            </span>
        </div>
        <p></p>rX   )Scope created now, deleted after the div p:before0zk
        <p style="counter-increment: c;
                  display: list-item; list-style: inside decimal">0.N)r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )r   rP   r   )rX   rP   r   )r   rP   r   )r   rP   r   )rG   r9   splitr   r   r   r   test_countersZ  s    





	

	 r   c            /   C   s4  t tddd dj D  t tddd dj D  dd dbD dj ksRtdd dfD d)j ksltd*d djD d+j k std,d dnD d4j k std5d drD d6j k std7d dvD d4j k std8d dzD d6j k std9d d~D dUj kstdVd dD dYj ks0tdZS )z Test the various counter styles.a  
        <style>
            body { counter-reset: p -12 }
            p { counter-increment: p }
            p:nth-child(1):before { content: '-' counter(p, none) '-'; }
            p:nth-child(2):before { content: counter(p, disc); }
            p:nth-child(3):before { content: counter(p, circle); }
            p:nth-child(4):before { content: counter(p, square); }
            p:nth-child(5):before { content: counter(p); }
        </style>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
    c             S   s.   g | ]&}d dd ddddd|fgfgfgfqS )r:   rO   rg   zp:beforerS   rP   r   )r   r   r   r   r   r'     s   z'test_counter_styles.<locals>.<listcomp>u   --  •  ◦  ▪  -7a^  
        <style>
            p { counter-increment: p }
            p:before { content: counter(p, decimal-leading-zero); }
        </style>
        <p style="counter-reset: p -1987"></p>
        <p></p>
        <p style="counter-reset: p -12"></p>
        <p></p>
        <p></p>
        <p></p>
        <p style="counter-reset: p -2"></p>
        <p></p>
        <p></p>
        <p></p>
        <p style="counter-reset: p 8"></p>
        <p></p>
        <p></p>
        <p style="counter-reset: p 98"></p>
        <p></p>
        <p></p>
        <p style="counter-reset: p 4134"></p>
        <p></p>
    c             S   s.   g | ]&}d dd ddddd|fgfgfgfqS )r:   rO   rg   zp:beforerS   rP   r   )r   r   r   r   r   r'   /  s   zf-1986 -1985  -11 -10 -09 -08  -01 00 01 02  09 10 11
                            99 100 101  4135 4136c             S   s   g | ]}t j|d qS )zdecimal-leading-zero)r   rb   )r   r   r   r   r   r'   8  s    i  i     r   	      r   r   r	   c   d   e   '  (  z`
        -1986 -1985  -11 -10 -09 -08  -01 00 01 02  09 10 11
        99 100 101  4135 4136
    c             S   s   g | ]}t j|d qS )zlower-roman)r   rb   )r   r   r   r   r   r'   C  s    r   r   r   r   r      1   2                   z
        -1986 -1985  -1 0 i ii iii iv v vi vii viii ix x xi xii
        xlix l  ccclxxxix cccxc  mmmcdlxxxix mmmcdxc mmmcdxci
        mmmmcmxcix  5000 5001
    c             S   s   g | ]}t j|d qS )zupper-roman)r   rb   )r   r   r   r   r   r'   M  s    z
        -1986 -1985  -1 0 I II III IV V VI VII VIII IX X XI XII
        XLIX L  CCCLXXXIX CCCXC  MMMCDLXXXIX MMMCDXC MMMCDXCI
        MMMMCMXCIX 5000 5001
    c             S   s   g | ]}t j|d qS )zlower-alpha)r   rb   )r   r   r   r   r   r'   V  s                       z=
        -1986 -1985  -1 0 a b c d  y z aa ab ac bxz bya
    c             S   s   g | ]}t j|d qS )zupper-alpha)r   rb   )r   r   r   r   r   r'   \  s    z=
        -1986 -1985  -1 0 A B C D  Y Z AA AB AC BXZ BYA
    c             S   s   g | ]}t j|d qS )zlower-latin)r   rb   )r   r   r   r   r   r'   b  s    c             S   s   g | ]}t j|d qS )zupper-latin)r   rb   )r   r   r   r   r   r'   h  s    c             S   s   g | ]}t j|d qS )Zgeorgian)r   rb   )r   r   r   r   r   r'   o  s    r      (   <   F   P   Z      ,      X                 p  X  @  (#  '  N   N  !N  u  
        -1986 -1985  -1 0 ა
        ბ გ დ ე ვ ზ ჱ თ ი ია იბ
        კ ლ მ ნ ჲ ო პ ჟ რ
        ს ტ ჳ ფ ქ ღ ყ შ ჩ
        ც ძ წ ჭ ხ ჴ ჯ ჰ ჵ
        ჵჰშჟთ 20000 20001
    c             S   s   g | ]}t j|d qS )Zarmenian)r   rb   )r   r   r   r   r   r'     s    '  '  u   
        -1986 -1985  -1 0 Ա
        Բ Գ Դ Ե Զ Է Ը Թ Ժ ԺԱ ԺԲ
        Ի Լ Խ Ծ Կ Հ Ձ Ղ Ճ
        Մ Յ Ն Շ Ո Չ Պ Ջ Ռ
        Ս Վ Տ Ր Ց Ւ Փ Ք
        ՔՋՂԹ 10000 10001
    N>?)r!  r"  r#  r$  r%  r&  r'  r   r   r	   r   r   r   r   r   r   r   r   >?r'  )r(  r)  r'  r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   >?r'  )r*  r+  r'  r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   >?r'  )r,  r-  r'  r   r   r	   r   r   r   r   r   r  r  r  r  >?r'  )r.  r/  r'  r   r   r	   r   r   r   r   r   r  r  r  r  >?r'  )r0  r1  r'  r   r   r	   r   r   r   r   r   r  r  r  r  >?r'  )r2  r3  r'  r   r   r	   r   r   r   r   r   r  r  r  r  >?r'  ).r4  r5  r'  r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r	  r
  r   r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  >?r'  )-r6  r7  r'  r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r	  r
  r   r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r   )rG   r9   r   r*   r   r   r   r   test_counter_styles  s`                 r8  c              C   s   t d\} }| jd jdks t|jd jdks4tdd | jdd D }dd |jdd D }|d	d
dgksvt|d	gkst|j\}}|j\}|j\}|jdkstdS )z9
    Test that the correct margin boxes are created.
    a  
        <style>
            @page {
                /* Make the page content area only 10px high and wide,
                   so every word in <p> end up on a page of its own. */
                size: 30px;
                margin: 10px;
                @top-center { content: "Title" }
            }
            @page :first {
                @bottom-left { content: "foo" }
                @bottom-left-corner { content: "baz" }
            }
        </style>
        <p>lorem ipsum
    r   r(   c             S   s   g | ]
}|j qS r   )
at_keyword)r   r&   r   r   r   r'     s    z%test_margin_boxes.<locals>.<listcomp>r   Nc             S   s   g | ]
}|j qS r   )r9  )r   r&   r   r   r   r'     s    z@top-centerz@bottom-leftz@bottom-left-cornerZTitle)r<   r%   r   r*   r"   )page_1page_2Zmargin_boxes_1Zmargin_boxes_2r(   
top_centerline_boxtext_boxr   r   r   test_margin_boxes  s    

r?  c              C   s  t d\} }|j\}}|j\}|j\}|jdks4t| j\}}|j\}|j\}|jdks\tddd}x0dD ](}|dkrd	| }||| ql|| qlW t d
\} | j\}}	|	j\}|j\}|jdkstt d\} }| j\}}	t|	jdk st|j\}}	|	j\}|j\}|jdkstt d\} | jdd \}}	|	j\}|j\}|jdksZtt d\} | j\}}	}|	j\}
|
j\}|jdkst|j\}|j\}|jdkstdS )z+Test string-set / string() in margin boxes.am  
        <style>
            @page {
                @bottom-center { content: string(text_header); }
            }
            p{
                -weasy-string-set: text_header content();
            }
            .page{
                page-break-before: always;
            }
        </style>
        <p>first assignment</p>
        <div class="page"></div>
    zfirst assignmentr   c             S   sZ   t dt| |d \}|j\}}|j\}|j\}| dkrH|jdksVtn|jdksVtd S )NaT  
            <style>
                @page {
                    @top-center { content: string(text_header); }
                }
                p{
                    -weasy-string-set: text_header content(%(content_val)s);
                }
                %(extra_style)s
            </style>
            <p>first assignment</p>
        )content_valextra_stylebeforeafterZpseudozfirst assignment)rB  rC  )r<   dictr%   r"   r*   )r@  rA  r:  r(   r<  r=  r>  r   r   r   simple_string_set_test  s    
z:test_margin_box_string_set.<locals>.simple_string_set_testr"   rB  rC  zp:%s{content: 'pseudo'}a%  
        <style>
            @page {
                @top-center { content: string(text_header, first); }
            }
            p{
                -weasy-string-set: text_header content();
            }
        </style>
        <p>first assignment</p>
        <p>Second assignment</p>
    a|  
        <style>
            @page {
                @top-center { content: string(header_nofirst, first-except); }
            }
            p{
                -weasy-string-set: header_nofirst content();
            }
            .page{
                page-break-before: always;
            }
        </style>
        <p>first_excepted</p>
        <div class="page"></div>
    r   Zfirst_excepteda  
        <style>
            @page {
                @top-center { content: string(header_last, last); }
            }
            p{
                -weasy-string-set: header_last content();
            }
        </style>
        <p>String set</p>
        <p>Second assignment</p>
    Nr	   zSecond assignmenta  
        <style>
            @page {
              @top-center { content: string(text_header, first); }
              @bottom-center { content: string(text_footer, last); }
            }
            html { counter-reset: a }
            body { counter-increment: a }
            ul { counter-reset: b }
            li {
              counter-increment: b;
              -weasy-string-set:
                text_header content(before) "-" content() "-" content(after)
                            counter(a, upper-roman) '.' counters(b, '|'),
                text_footer content(before) '-' attr(class)
                            counters(b, '|') "/" counter(a, upper-roman);
            }
            li:before { content: 'before!' }
            li:after { content: 'after!' }
            li:last-child:before { content: 'before!last' }
            li:last-child:after { content: 'after!last' }
        </style>
        <ul>
          <li class="firstclass">first
          <li>
            <ul>
              <li class="secondclass">second
    zbefore!-first-after!I.1zbefore!last-secondclass2|1/I)r   )r   r"   rB  rC  )rB  rC  )r<   r%   r"   r*   r,   )r:  r;  r(   bottom_centerr=  r>  rE  r   rA  r<  Ztop_line_boxZtop_text_boxZbottom_line_boxZbottom_text_boxr   r   r   test_margin_box_string_set  sZ    








rG  c              C   sR   t d} xDt| dD ]6\}}|j\}}|j\}|j\}|jdj|kstqW dS )zTest page-based counters.u  
        <style>
            @page {
                /* Make the page content area only 10px high and wide,
                   so every word in <p> end up on a page of its own. */
                size: 30px;
                margin: 10px;
                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages) ".";
                }
            }
        </style>
        <p>lorem ipsum dolor
    r   u   Page {0} of 3.N)r<   	enumerater%   r"   rb   r*   )r;   Zpage_numberZpager(   rF  r=  r>  r   r   r   test_page_countersW  s    
rI  c              C   s  t d} | j\}|j\}|j\}t|tjs0tt|d s@tdd }|d}|g g fks`td}d}d}d}	d}
|d\}}d	d
|f}d	d|f}||||g|||ggkst|||g||g||ggkst|d\}}||d d g|||ggk st||d g|d g||ggkstd	d|
f}d	d|f}dd|	f}|d\}}|||||g||||g||||g||||ggksxt||||g|||g|||g|||g|||ggkst|d\}}|||||g||d |ggkst||||gd ||g|||ggkstd S )Nz<table></table>collapsed_border_gridc             S   s4   t | } | j\}|j\}|j\}tdd |jD S )Nc             s   s   | ]}d d |D V  qdS )c             S   s   g | ]}d d |D qS )c             S   s(   g | ] \}\}}}|r |||fnd qS )Nr   )r   Z_scorer   r   r   r   r   r   r'   ~  s   zOtest_border_collapse.<locals>.get_grid.<locals>.<genexpr>.<listcomp>.<listcomp>r   )r   columnr   r   r   r'   ~  s   zDtest_border_collapse.<locals>.get_grid.<locals>.<genexpr>.<listcomp>Nr   )r   r   r   r   r   r   ~  s   z9test_border_collapse.<locals>.get_grid.<locals>.<genexpr>)r9   r%   r   rJ  )r(   r)   table_wrapperr   r   r   r   get_gridx  s    z&test_border_collapse.<locals>.get_gridz1<table style="border-collapse: collapse"></table>r   r   z
        <style>td { border: 1px solid red }</style>
        <table style="border-collapse: collapse; border: 3px solid black">
            <tr> <td>A</td> <td>B</td> </tr>
            <tr> <td>C</td> <td>D</td> </tr>
        </table>
    Zsolidr   a  
        <style>table, td { border: 3px solid }</style>
        <table style="border-collapse: collapse">
            <tr> <td>A</td> <td style="border-style: hidden">B</td> </tr>
            <tr> <td>C</td> <td style="border-style: none">D</td> </tr>
        </table>
    r   Zdasheda  
        <style>td { border: 1px solid red }</style>
        <table style="border-collapse: collapse; border: 5px solid yellow">
            <col style="border: 3px solid black" />
            <tr> <td></td> <td></td> <td></td> </tr>
            <tr> <td></td> <td style="border: 5px dashed blue"></td>
                 <td style="border: 5px solid lime"></td> </tr>
            <tr> <td></td> <td></td> <td></td> </tr>
            <tr> <td></td> <td></td> <td></td> </tr>
        </table>
    a  
        <style>col, tr { border: 3px solid }</style>
        <table style="border-collapse: collapse">
            <col /><col /><col />
            <tr> <td rowspan=2></td> <td></td> <td></td> </tr>
            <tr>                     <td colspan=2></td> </tr>
        </table>
    )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r9   r%   r!   r   r   r*   hasattr)r(   r)   rL  r   rM  r   ZblackZredZgreenZblueZyellowZvertical_bordersZhorizontal_bordersZblack_3Zred_1Zyellow_5Zgreen_5Zdashed_blue_5r   r   r   test_border_collapseo  sv    













rO  )A__doc__
__future__r   r   r1   r=   rD   Ztesting_utilsr   r   r   r   cssr
   r   r   Zformatting_structurer   r   r   rD  ZBlockContainerBoxZBlockLevelBoxr   ZInlineLevelBoxZ	InlineBoxr   ZTableCaptionBoxZTableColumnGroupBoxZTableColumnBoxZTableRowGroupBoxZTableRowBoxZInlineTableBoxZTableCellBoxitemsrL   r#   r-   r.   r6   r7   r9   r<   rG   r8   r\   rd   rp   rz   r   r   r   r   r   r   r   r   r   r   r   r8  r?  rG  rI  rO  r   r   r   r   <module>   sl   	CL(! ]` % $ #