3
?WhS                 @   sj  d Z ddlmZmZ ddlZddlZddlZddlZddl	m
Z
 ddl	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 ejdZd4ZddddgddddgddddgddddgddddddddgdZejdjZG dd deZ dd Z!d d! Z"d"d# Z#d$d% Z$d&d' Z%d5d(d)Z&d6d*d+Z'd7d,d-Z(G d.d/ d/eZ)d0d1 Z*d8d2d3Z+dS )9uZ  
    weasyprint.css
    --------------

    This module takes care of steps 3 and 4 of “CSS 2.1 processing model”:
    Retrieve stylesheets associated with a document and annotate every element
    with a value for every CSS property.

    http://www.w3.org/TR/CSS21/intro.html#processing-model

    This module does this in more than two steps. The
    :func:`get_all_computed_styles` function does everything, but it is itsef
    based on other functions in this module.

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

    )divisionunicode_literalsN   )
properties)computed_values)preprocess_declarations   )element_base_urlget_url_attributeurl_joinURLFetchingError)LOGGER)	iteritems)CSSZpage3beforeafter
first-linefirst-letterZfirst_left_pageZfirst_right_pageZfirst_blank_left_pageZfirst_blank_right_pageZ	left_pageZblank_left_pageZ
right_pageZblank_right_page)firstleftrightZblankNzH^(display|column_gap|(border_[a-z]+|outline|column_rule)_(width|color))$c               @   s^   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	eZ
eZdd Zdd ZdZdS )	StyleDictab  A mapping (dict-like) that allows attribute access to values.

    Allow eg. ``style.font_size`` instead of ``style['font-size']``.

    :param parent: if given, should be a mapping. Values missing from this
                   dict will be looked up in the parent dict. Setting a value
                   in this dict masks any value in the parent.

    Nc             C   sB   |d kri }nt |}|d kr"i }tj| d| tj| d| d S )N_storage_parent)dictobject__setattr__)selfdataparent r    W/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/weasyprint/css/__init__.py__init__T   s    zStyleDict.__init__c             C   s$   | j }||kr|| S | j| S d S )N)r   r   )r   keyZstorager    r    r!   __getitem___   s    zStyleDict.__getitem__c             C   s   || j |< d S )N)r   )r   r#   valuer    r    r!   __setitem__f   s    zStyleDict.__setitem__c             C   s   | | }|dkr|S | j S )NZcurrentColor)Zcolor)r   r#   r%   r    r    r!   	get_colori   s    zStyleDict.get_colorc             C   s   | j  }|jj| |S )N)copyr   update)r   otherr(   r    r    r!   updated_copym   s    zStyleDict.updated_copyc             C   s   || j kp|| jkS )N)r   r   )r   r#   r    r    r!   __contains__r   s    zStyleDict.__contains__c             C   s8   | j r| }n| j}t| |d}| jr4tj|dd |S )a  Copy the ``StyleDict``.

        Create a new StyleDict with this one as the parent. This is a cheap
        "copy-on-write". Modifications in the copy will not affect
        the original, but modifications in the original *may* affect the
        copy.

        )r   	anonymousT)r   r   typer-   r   r   )r   r   styler    r    r!   r(   x   s    	zStyleDict.copyc             C   s    t i | dd}tj|dd |S )zReturn a new StyleDict with inherited properties from this one.

        Non-inherited properties get their initial values.
        This is the styles for an anonymous box.
        N)cascadedparent_styleelementr-   T)computed_from_cascadedr   r   )r   r/   r    r    r!   inherit_from   s
    zStyleDict.inherit_fromF)NN)__name__
__module____qualname____doc__r"   r$   r&   r'   r+   r,   __getattr__r   r(   r4   r-   r    r    r    r!   r   J   s   	
r   c             C   s<   | j r| j gng }x| D ]}|jr|j|j qW dj|S )z9Return the text directly in the element, not descendants. )texttailappendjoin)r2   contentchildr    r    r!   get_child_text   s
    
rA   c             c   s:  ddl m} x&| jddD ]}|jddjddd	 j }|dkrHq|jd
dj pZd}dd |jdD }t||s|q|jdkrt|}t	|t
|||d}	|	V  q|jdko|jdr||d s||drqt|d}
|
dk	ryt	|
|d|dV  W q tk
r0 } ztjd|
| W Y dd}~X qX qW dS )zgYield the stylesheets in ``element_tree``.

    The output order is the same as the source order.

    r   )element_has_link_typer/   linkr.   ztext/css;r   r   mediar:   allc             S   s   g | ]}|j  qS r    )strip).0
media_typer    r    r!   
<listcomp>   s    z$find_stylesheets.<locals>.<listcomp>,)stringbase_urlurl_fetcherrI   href
stylesheetZ	alternateNT)urlrN   Z_check_mime_typerI   z$Failed to load stylesheet at %s : %s)htmlrB   itergetsplitrG   evaluate_media_querytagrA   r   r	   r
   r   r   warning)element_treedevice_media_typerN   rB   r2   Z	mime_typeZ
media_attrrE   r?   cssrO   excr    r    r!   find_stylesheets   s6    





r]   c             c   s^   t }xT| j D ]H}|jd}|r|j|\}}x|D ]}tj| q4W ||t|fV  qW dS )z]
    Yield ``element, declaration, base_url`` for elements with
    a "style" attribute.
    r/   N)PARSERrS   rT   Zparse_style_attrr   rX   r	   )rY   parserr2   Zstyle_attributedeclarationserrorserrorr    r    r!   find_style_attributes   s    

rc   c             C   s   d| kp|| kS )zReturn the boolean evaluation of `query_list` for the given
    `device_media_type`.

    :attr query_list: a cssutilts.stlysheets.MediaList
    :attr device_media_type: a media type string (for now)

    rF   r    )Z
query_listrZ   r    r    r!   rV      s    	rV   c             C   sP   | dkrdS | dkr| rdS | dkr0| r0dS | dkr<dS | dksHt dS d	S )
zReturn the precedence for a declaration.

    Precedence values have no meaning unless compared to each other.

    Acceptable values for ``origin`` are the strings ``'author'``, ``'user'``
    and ``'user agent'``.

    z
user agentr   userr   author         N)AssertionError)origin
importancer    r    r!   declaration_precedence   s    
rl   c       	      C   s@   | j ||fi }|j|d\}}|dks0||kr<||f||< dS )zSet the value for a property on a given element.

    The value is only set if there is no value of greater weight defined yet.

    N)NN)
setdefaultrT   )	cascaded_stylesZ	prop_nameZprop_valuesweightr2   pseudo_typer/   Z_valuesZprevious_weightr    r    r!   add_declaration   s    rq   c       	      C   sf   |dk	r||df nd}||kr.dt jd in
||df }| j||fi }t||||||||f< dS )a  Set the computed values of styles to ``element``.

    Take the properties left by ``apply_style_rule`` on an element or
    pseudo-element and assign computed values with respect to the cascade,
    declaration priority (ie. ``!important``) and selector specificity.

    NZ	font_size)r   INITIAL_VALUESrT   r3   )	rn   computed_stylesr2   r   rootrp   r1   
root_styler0   r    r    r!   set_computed_styles  s
    
"rv   c             C   s  | r\|dk	r\t tjd}xtjD ]}|| ||< q"W xdD ]}d|d| < q<W d|d	< |S t  }t  }xttjD ]\}}	||kr|| \}
}|
}n|tjkrd
}nd}|d
kr|dkrd}|dkr|	}
t|s|
||< n|d
kr|| }
|
||< |
||< qtW tj| |||||S )zCGet a dict of computed style mixed from parent and cascaded styles.N)r   topbottomr   r   r   zborder_%s_widthZoutline_widthZinheritinitial)rw   rx   r   r   )r   r   rr   Z	INHERITEDr   RE_INITIAL_NOT_COMPUTEDr   Zcompute)r2   r0   r1   rp   ru   ZcomputednameZsideZ	specifiedry   r%   Z_precedencekeywordr    r    r!   r3   "  s:    


r3   c               @   s   e Zd Zdd ZdS )Selectorc             C   s   || _ || _|| _d S )N)specificitypseudo_elementmatch)r   r~   r   r   r    r    r!   r"   V  s    zSelector.__init__N)r5   r6   r7   r"   r    r    r    r!   r}   U  s   r}   c             c   s  t j j}x|D ]}|js0tt||j}|r|jj }yg }xxt j	|D ]j}	||	}
yt
jj|
}W n0 tk
r } zt jt|W Y dd}~X nX |jtd|	j  |	j| qRW x&|D ]}	|	jtkrt jd|	j qW W n6 t jk
r  } ztjd|| wW Y dd}~X nX |||fV  q|jdkrt|j| sLqt||jd|j|j}|dk	ryt||| d}W n2 tk
r } ztjd|| W Y dd}~X nX x|j D ]}|V  qW q|jd	krt|j| sqxt!| ||j |D ]}|V  qW q|jd
kr|j\}}|dk	rJtjd||r@d| nd  qtt||j}dd t"| }|j}|rt|d|g}|||fV  xB|j#D ]8}tt||j}|rt||j|g}|||fV  qW qW dS )zZDo the work that can be done early on stylesheet, before they are
    in a document.

    Nr   zUnknown pseudo-element: %sz(Invalid or unsupported selector '%s', %sz@importz@import at %s:%s)rQ   rN   rI   z$Failed to load stylesheet at %s : %sz@mediaz@pagezGNamed pages are not supported yet, the whole @page %s rule was ignored.:r:   c                s    fddS )Nc                s    S )Nr    )Z	_document)
page_typesr    r!   <lambda>  s    z9preprocess_stylesheet.<locals>.<lambda>.<locals>.<lambda>r    )r   r    )r   r!   r     s    z'preprocess_stylesheet.<locals>.<lambda>)r   )$	cssselectZHTMLTranslatorselector_to_xpathZ
at_keywordlistr   r`   selectorZas_cssparselxmletreeZXPath
ValueErrorZSelectorErrorstrr=   r}   r~   r   PSEUDO_ELEMENTSZExpressionErrorr   rX   rV   rE   r   Zurilinecolumnr   r   rulespreprocess_stylesheetPAGE_PSEUDOCLASS_TARGETSZat_rules)rZ   rM   r   rN   r   Zruler`   Zselector_stringselector_listr   ZxpathZ
lxml_xpathr\   rQ   rP   resultZ	page_nameZpseudo_classr   r~   Zmargin_ruler    r    r!   r   \  s    

 








r   c             C   s  | j }| j}| j}| j }tt|||}i }x|p6g df|df|pFg dffD ]\}}	x|D ]}
xz|
jD ]p\}}}xd|D ]\}|j}|j}xJ|j	|D ]<}x6|D ].\}}}t
|	|}||f}t|||||| qW qW qxW qhW q\W qNW d}xTt|D ]H\}}}x<t||D ].\}}}t
d|}||f}t||||| qW qW i }x(|j D ]}t|||||j d qLW x$td D ]}t|||||d qvW x,|D ]$\}}|rt||||||d qW d|jfd	d
}|S )a-  Compute all the computed styles of all elements
    in the given ``html`` document.

    Do everything from finding author stylesheets to parsing and applying them.

    Return a ``style_for`` function that takes an element and an optional
    pseudo-element type, and return a StyleDict object.

    z
user agentre   rd   r   r   )rt   r   N)rp   rt   r   c             S   s   || |fS )zQ
        Convenience function to get the computed styles for an element.
        r    )r2   rp   Z__getr    r    r!   	style_for  s    z*get_all_computed_styles.<locals>.style_for)r   r   r   r   )Zroot_elementrI   rN   Z_ua_stylesheetsr   r]   r   r~   r   r   rl   rq   rc   r   rS   rv   Z	getparentr   rT   )rR   Zuser_stylesheetsrY   rZ   rN   Zua_stylesheetsZauthor_stylesheetsrn   Zsheetsrj   ZsheetZ_ruler   r`   r   r~   rp   r2   r{   valuesrk   
precedencero   rM   rs   Z	page_typer   r    r    r!   get_all_computed_styles  s\    



 

r   )Nr   r   r   r   )N)NN)NN)N),r8   
__future__r   r   reZtinycssr   Z
lxml.etreer   r:   r   r   Z
validationr   Zurlsr	   r
   r   r   loggerr   compatr   r   Zmake_parserr^   r   r   compiler   rz   r   r   rA   r]   rc   rV   rl   rq   rv   r3   r}   r   r   r    r    r    r!   <module>   sP   
Q	)

 
2Z