3
CW7                 @   s  d Z ddlmZmZ ddlm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 dd
lmZmZ ddlmZmZ edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zd"S )#z
    weasyprint.tests.test_css
    -------------------------

    Test the CSS parsing, cascade, inherited and computed values.

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

    )divisionunicode_literals)raises   )resource_filenameassert_no_logscapture_logsTestHTML   )css)get_all_computed_styles)strut_layout)open_data_urlpath2url)CSSdefault_url_fetcherc           	   C   s   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ddd
 | dddd
 | dddd
 | dddd
 | dddd
 | dddd
 t t td W d
Q R X d
S )z"Test URLs with the "data:" scheme.c             S   s    t | t|||| dkstd S )N)stringZ	mime_typeencodingZredirected_url)r   dictAssertionError)urlZexpected_contentZexpected_mime_typeZexpected_charset r   Y/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/tests/test_css.pyparse   s
    ztest_data_url.<locals>.parsez	data:,foos   fooz
text/plainzUS-ASCIIzdata:,foo%22bars   foo"barzdata:text/plain,fooNz"data:text/html;charset=utf8,<body>s   <body>z	text/htmlutf8zdata:text/plain;base64,Zm9vzdata:text/plain;base64,Zm9vbw==s   fooozdata:text/plain;base64,Zm9vb28=s   foooozdata:text/plain;base64,Zm9vb29vs   foooooz#data:text/plain;base64,Zm9vbw%3D%3Dz!data:text/plain;base64,Zm9vb28%3Dzdata:text/plain;base64,Zm9vbwzdata:text/plain;base64,Zm9vb28zdata:foo)r   IOErrorr   )r   r   r   r   test_data_url   s    	
r   c           	   C   sJ   t jddd} | jdkst| jdks,ttt | j W dQ R X dS )zTest a style in a ``dict``.   block)margin_leftdisplayN)r   Z	StyleDictr    r   r   r   KeyErrorposition)styler   r   r   test_style_dict<   s    

r$   c              C   s   t td} ttj| jdt}t|dks0tdd |D ddgksJtdd |D }t|dkshtd	d |D d
dddddddddg
kstdS )z5Test if the stylesheets are found in a HTML document.z	doc1.htmlprintr
   c             S   s*   g | ]"}|j jd dd jddd qS )/r   ,r(   )Zbase_urlrsplit).0sr   r   r   
<listcomp>Q   s    z)test_find_stylesheets.<locals>.<listcomp>za%7Bcolor%3AcurrentColor%7Dc             S   s   g | ]}|j D ]}|qqS r   )rules)r*   sheetruler   r   r   r,   T   s    
   c             S   s(   g | ] \}}}|j r|jn|jj qS r   )Z
at_keywordselectoras_css)r*   r/   Z_selector_listZ_declarationsr   r   r   r,   X   s   alipulza:afterNfirstzbody > h1:first-childzh1 ~ p ~ ul a:after)Nr7   )	r	   r   listr   Zfind_stylesheetsroot_elementr   lenr   )documentZsheetsr-   r   r   r   test_find_stylesheetsH   s    r<   c              C   s   t td} | jjd jj dks&ttdd | jjd jD }|d dksRt|d d	ksbt|d
 dksrtd|ks~ttdd | jD }d|kst|d dkst|d dkst|d dkstd|d dkstddS )zTest the expand shorthands.z
sheet2.cssr   r4   c             s   s   | ]}|j |jj fV  qd S )N)namevaluer2   )r*   dr   r   r   	<genexpr>f   s   z)test_expand_shorthands.<locals>.<genexpr>marginz2em 0zmargin-bottomZ3emzmargin-leftZ4emz
margin-topc             s   s,   | ]$\}}}|D ]\}}}||fV  qqd S )Nr   )r*   Z_ruleZ
_selectorsdeclarationsr=   r>   Z	_priorityr   r   r   r@   n   s   
margin_topr
   emmargin_rightNmargin_bottomz.3em was before the shorthand, should be maskedr      z14em was after the shorthand, should not be masked)r
   rD   )r   N)r
   rD   )rG   rD   )	r   r   Z
stylesheetr-   r1   r2   r   r   rB   )r.   r#   r   r   r   test_expand_shorthands`   s$    rH   c              C   s\  t td} dd | _t| ttdgd}| j\}}|\}}}}|\}}	|\}
|\}|\}||}||}||}||}||}||
d}||
}
||}||}|jdttdfgkst|j	d	kst|j
d
kst|jd!kst|jd"k st|jd#kst|jd$kst|jdks,t|jd%ks<t|jd&ksLt|jd'ks\t|jd(kslt|j	dks|t|jdkst|jdkst|jdkst|jdkst|j	d	kst|j
dkst|jd)kst|jd*kst|jd+kst|jd,kst|
jtdgks2t|
j	dksBt|
j
dksRt|
jd-ksbt|
jd.ksrt|
jd/kst|
jd0kst|
jdkst|
jdkst|
jd1kst|
jdkst|j
d
kst|jd2kst|jd3kst|j
dkst|jd4d5d6gks(t|jd7ks8t|jdksHt|jdksXtd S )8z"Test a document with inline style.z	doc1.htmlc               S   s   t tdgS )Nzmini_ua.css)r   r   r   r   r   r   <lambda>   s    z(test_annotate_document.<locals>.<lambda>zuser.css)user_stylesheetsafterr   zlogo_small.pngi  (      pxr   ZcurrentColor               Z	underlinei  r   r
      rG   *      STRING [	home.html]N)rM   rN   )r   rN   )rM   rN   )r   rN   )rL   rN   )rL   rN   )rL   rN   )rL   rN   )rR   rN   )r   rN   )rR   rN   )rS   rN   )r   rN   )r
   rN   )rT   rN   )rG   rN   )r   r   r   r   )rV   rN   )rO   rN   )rW   rX   )rW   rY   )rW   rZ   )r   r   r   r   )r	   r   Z_ua_stylesheetsr   r   r9   Zbackground_imager   r   Zfont_weight	font_sizerC   rE   rF   r   Zbackground_colorZborder_top_widthZborder_right_widthZborder_bottom_widthZborder_left_widthZtext_decoration	frozensetZpadding_topZpadding_rightZpadding_bottomZpadding_leftcolorZborder_top_colorwidthheightcontent)r;   	style_forZ_headbodyZh1r5   r6   divZli_0Z_li_1r3   Zspan1Zspan2rK   r   r   r   test_annotate_document{   s~    


rd   c              C   s  t td} t| tddgd}|d}|jdks6t|jdksDt|jdksRt|jdks`t|d}|jdksvt|jdkst|jdkst|jdkst|d}|jdkst|jdkst|jdk st|jdk st|d}|jd k st|jd!kst|jd"kst|jd#ks,t|dd}|dksDt|dd}|j	dks^t|j
d$ksnt|dd}|j	dkstdS )%zTest the ``@page`` properties.z	doc1.htmla  
            html {
                color: red;
            }
            @page {
                margin: 10px;
            }
            @page :right {
                color: blue;
                margin-bottom: 12pt;
                font-size: 20px;
                @top-left {
                    width: 10em;
                }
                @top-right {
                    font-size: 10px;
                }
            }
        )r   )rJ   Zfirst_left_pagerP   rN   r0   r   r   Zfirst_right_pagerR   Z	left_pageZ
right_pagez	@top-leftN      z
@top-right)rP   rN   )r0   rN   )r0   rN   )r   r   r   r   )rP   rN   )r0   rN   )rR   rN   )r   r   r   r   )r0   rN   )r0   rN   )r0   rN   )r   r   r   r   )r0   rN   )r0   rN   )rR   rN   )r   r   r   r   )rf   rN   )r	   r   r   r   rC   r   r   rF   r]   r[   r^   )r;   ra   r#   r   r   r   	test_page   s>    


rg   c              C   s   xddgfddgfddgfdddgfd	dd
gfddgfddgfgD ]R\} }t  }t| d W dQ R X t|dksvtx|D ]}||d ks|tq|W qBW d}t  }t|dj  W dQ R X t|dkstd|d kstdS )z+Check that appropriate warnings are logged.z:lipsum { margin: 2cmz(WARNING: Invalid or unsupported selectorz::lipsum { margin: 2cmz@page foo { margin: 2cmz*WARNING: Named pages are not supported yetzfoo { margin-color: redzWARNING: Ignoredzunknown propertyzfoo { margin-top: redzinvalid valuez@import "relative-uri.cssz2WARNING: Relative URI reference without a base URIz(@import "invalid-protocol://absolute-URLz%WARNING: Failed to load stylesheet at)r   Nr   r   z6<link rel=stylesheet href=invalid-protocol://absolute>)r   r   r:   r   r	   render)sourcemessageslogsmessagehtmlr   r   r   test_warnings  s2    
rn   c              C   s   t  f} tdd}|j j\}|jj\}|jjdks8ttdd}|j j\}|jj\}|jjdkshtW d Q R X t	| dkstd S )	NzB
            <style> html { color red; color: blue; color
        )r   r   r   zB
            <html style="color; color: blue; color red">
        rG   )r   r   r   r   )r   r   r   r   )
r   r	   rh   pages	_page_boxchildrenr#   r]   r   r:   )rk   r;   pagerm   r   r   r   test_error_recovery4  s    

rs   c              C   s   t dd} | j j\}|jj\}|j\}|j\}|j\}|j\}|jjdksPt|jjdks`tt|jd dksvt|jj	dkst|jjdkstt|jd dkst|jj	dkstd S )	Na  
        <style>
            html { font-size: 10px; line-height: 140% }
            section { font-size: 10px; line-height: 1.4 }
            div, p { font-size: 20px; vertical-align: 50% }
        </style>
        <body><div><section><p></p></section></div></body>
    )r   r0   re   r            )
r	   rh   ro   rp   rq   r#   r[   r   r   Zvertical_align)r;   rr   rm   rb   rc   section	paragraphr   r   r   test_line_height_inheritanceG  s    
ry   c              C   sX   t dd} | jtddgdj\}|jj\}|j\}x|jD ]}|jjdks<tq<W d S )Na  
        <style>
            p:nth-child(1) { color: lime }
            body p:nth-child(2) { color: red }

            p:nth-child(3) { color: lime !important }
            body p:nth-child(3) { color: red }

            body p:nth-child(5) { color: lime }
            p:nth-child(5) { color: red }

            p:nth-child(6) { color: red }
            p:nth-child(6) { color: lime }
        </style>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
    )r   z
        body p:nth-child(1) { color: red }
        p:nth-child(2) { color: lime !important }

        p:nth-child(4) { color: lime !important }
        body p:nth-child(4) { color: red }
    )Zstylesheetsr   r   )r   r   r   r   )	r	   rh   r   ro   rp   rq   r#   r]   r   )r;   rr   rm   rb   rx   r   r   r   test_importantc  s    
rz   c              C   s   t dd} | j j\}|jj\}|j\}dd |jD }|j }|dddddddddddgksbtd	|  k otdk n  s~td S )
Na)  
        <p style="margin-left: 96px"></p>
        <p style="margin-left: 1in"></p>
        <p style="margin-left: 72pt"></p>
        <p style="margin-left: 6pc"></p>
        <p style="margin-left: 2.54cm"></p>
        <p style="margin-left: 25.4mm"></p>
        <p style="margin-left: 101.6q"></p>
        <p style="margin-left: 1.1em"></p>
        <p style="margin-left: 1.1rem"></p>
        <p style="margin-left: 1.1ch; font: 14px Ahem"></p>
        <p style="margin-left: 1.5ex; font: 10px Ahem"></p>
        <p style="margin-left: 1.1ch"></p>
    )r   c             S   s   g | ]}t |jd qS )   )roundr   )r*   r5   r   r   r   r,     s    ztest_units.<locals>.<listcomp>`   g1@g.@r   rG   )r	   rh   ro   rp   rq   popr   )r;   rr   rm   rb   ZmarginsZdefault_font_chr   r   r   
test_units  s    
"r   N) __doc__
__future__r   r   Zpytestr   Ztesting_utilsr   r   r   r	    r   r   Zcss.computed_valuesr   Zurlsr   r   r   r   r   r$   r<   rH   rd   rg   rn   rs   ry   rz   r   r   r   r   r   <module>   s&   "]< $