a
    ¥Q•hF-  ã                   @   sž  d Z ddlZddlmZ ddl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 dd
lmZ ddlmZ e
ƒ Zeeƒd  d¡Zeeƒd  d¡Zeeƒd  d¡Zeeed�Zeeed�Zeed�ZdZe de› d�¡Zdd„ Zdd„ Zi Z dd„ Z!dd„ Z"dd„ Z#e"dƒd d!„ ƒZ$e"d"ƒd#d$„ ƒZ%e"d%ƒd&d'„ ƒZ&e"d(ƒd)d*„ ƒZ'e"d+ƒd,d-„ ƒZ(e"d.ƒd/d0„ ƒZ)d1d2„ Z*d3d4„ Z+e d5ej,¡Z-d6d7„ Z.dS )8aV  Specific handling for some HTML elements, especially replaced elements.

Replaced elements (eg. <img> elements) are rendered externally and behave as an
atomic opaque box in CSS. In general, they may or may not have intrinsic
dimensions. But the only replaced elements currently supported in WeasyPrint
are images with intrinsic dimensions.

é    N)Úfilesé   )ÚCSSÚ
AttachmentÚcss)Úget_child_text)ÚCounterStyle)Úboxes)ÚSVGImage)ÚLOGGER)Úget_url_attributezhtml5_ua.csszutf-8zhtml5_ua_form.csszhtml5_ph.css)ÚstringÚcounter_style©r   z 	
z[^z]+c                 C   s   |   ¡  ¡  ¡ S )a4  Transform (only) ASCII letters to lower case: A-Z is mapped to a-z.

    This is used for `ASCII case-insensitive
    <https://whatwg.org/C#ascii-case-insensitive>`_ matching.

    This is different from the :meth:`str.lower` method of Unicode strings
    which also affect non-ASCII characters,
    sometimes mapping them into the ASCII range:

    >>> keyword = 'Bac\N{KELVIN SIGN}ground'
    >>> assert keyword.lower() == 'background'
    >>> assert ascii_lower(keyword) != keyword.lower()
    >>> assert ascii_lower(keyword) == 'bac\N{KELVIN SIGN}ground'

    )ÚencodeÚlowerÚdecoder   © r   úH/var/www/sistema_ama/venv/lib/python3.9/site-packages/weasyprint/html.pyÚascii_lower$   s    r   c                    s(   t  |  dd¡¡}t‡ fdd„|D ƒƒS )zDReturn whether element has a ``rel`` attribute with given link type.ÚrelÚ c                 3   s   | ]}t |ƒˆ kV  qd S )N)r   )Ú.0Útoken©Ú	link_typer   r   Ú	<genexpr>;   ó    z(element_has_link_type.<locals>.<genexpr>)ÚHTML_SPACE_SEPARATED_TOKENS_REÚfindallÚgetÚany)Úelementr   Útokensr   r   r   Úelement_has_link_type8   s    r$   c                 C   s(   |j tv rt| j | |||ƒS |gS dS )zbHandle HTML elements that need special care.

    :returns: a (possibly empty) list of boxes.
    N)Zelement_tagÚHTML_HANDLERSÚtag)r"   ÚboxÚget_image_from_uriÚbase_urlr   r   r   Úhandle_elementB   s
    
ÿr*   c                    s   ‡ fdd„}|S )zDReturn a decorator registering a function handling ``tag`` elements.c                    s   | t ˆ < | S )z;Decorator registering a function handling ``tag`` elements.)r%   )Úfunction©r&   r   r   Ú	decoratorP   s    zhandler.<locals>.decoratorr   )r&   r-   r   r,   r   ÚhandlerN   s    r.   c                 C   s@   d|j d v rtjntj}|| j|j | |ƒ}|j|_|j|_|S )zˆWrap an image in a replaced box.

    That box is either block-level or inline-level, depending on what the
    element should be.

    ÚblockÚdisplay)Ústyler	   ZBlockReplacedBoxZInlineReplacedBoxr&   Ú
string_setÚbookmark_label)r"   r'   ÚimageÚtype_Znew_boxr   r   r   Úmake_replaced_boxW   s    þr6   Úimgc                 C   s    t | d|ƒ}|  d¡}|r||||jd d�}|durBt| ||ƒgS |r^tj ||¡g|_|gS |dkrjg S |du svJ ‚g S n |r˜tj ||¡g|_|gS g S dS )z›Handle ``<img>`` elements.

    Return either an image or the alt-text.

    See: https://www.w3.org/TR/html5/embedded-content-1.html#the-img-element

    ÚsrcÚaltÚimage_orientation)ÚurlÚorientationNr   )r   r    r1   r6   r	   ZTextBoxÚanonymous_fromÚchildren)r"   r'   r(   r)   r8   r9   r4   r   r   r   Ú
handle_imgi   s&    	

ÿr?   Úembedc                 C   sN   t | d|ƒ}|  dd¡ ¡ }|rJ||||jd d�}|durJt| ||ƒgS g S )z•Handle ``<embed>`` elements, return either an image or nothing.

    See: https://www.w3.org/TR/html5/embedded-content-0.html#the-embed-element

    r8   Útyper   r:   ©r;   Zforced_mime_typer<   N©r   r    Ústripr1   r6   )r"   r'   r(   r)   r8   r5   r4   r   r   r   Úhandle_embedŽ   s    þrE   Úobjectc                 C   sP   t | d|ƒ}|  dd¡ ¡ }|rJ||||jd d�}|durJt| ||ƒgS |gS )zœHandle ``<object>`` elements, return either an image or the fallback.

    See: https://www.w3.org/TR/html5/embedded-content-0.html#the-object-element

    ÚdatarA   r   r:   rB   NrC   )r"   r'   r(   r)   rG   r5   r4   r   r   r   Úhandle_object¡   s    þrH   Úcolgroupc                    s>   t ˆ tjƒr8tdd„ | D ƒƒs8‡ fdd„tˆ jƒD ƒˆ _ˆ gS )úHandle the ``span`` attribute.c                 s   s   | ]}|j d kV  qdS )ÚcolNr,   )r   Úchildr   r   r   r   ¸   r   z"handle_colgroup.<locals>.<genexpr>c                    s   g | ]}t j ˆ g ¡‘qS r   )r	   ÚTableColumnBoxr=   )r   Ú_©r'   r   r   Ú
<listcomp>¹   s   ÿz#handle_colgroup.<locals>.<listcomp>)Ú
isinstancer	   ZTableColumnGroupBoxr!   ÚrangeÚspanr>   ©r"   r'   Z_get_image_from_uriÚ	_base_urlr   rO   r   Úhandle_colgroup´   s    
þrV   rK   c                    s4   t ˆ tjƒr.ˆ jdkr.‡ fdd„tˆ jƒD ƒS ˆ gS )rJ   r   c                    s   g | ]}ˆ   ¡ ‘qS r   )Úcopy)r   Z_irO   r   r   rP   Å   r   zhandle_col.<locals>.<listcomp>)rQ   r	   rM   rS   rR   rT   r   rO   r   Ú
handle_col¿   s    rX   z{http://www.w3.org/2000/svg}svgc              
   C   s|   |j d }|j d }zt| |||ƒ}W nB tyh } z*t d|¡ tjd|d� g W  Y d}~S d}~0 0 t| ||ƒgS dS )zUHandle ``<svg>`` elements.

    Return either an image or the fallback content.

    Úurl_fetcherÚcontextzFailed to load inline SVG: %szError while loading inline SVG:)Úexc_infoN)Úkeywordsr
   Ú	Exceptionr   ÚerrorÚdebugr6   )r"   r'   r(   r)   rY   rZ   r4   Ú	exceptionr   r   r   Ú
handle_svgÉ   s    

ra   c                 C   sÞ  d}d}d}g }g }d}d}g }i }	| j j dd¡}
| j ddd¡D �]|}|j }|jdkrl|du rlt|ƒ}qD|jdk�rdt| dd¡ƒ}| dd¡}|d	krÆtt	| 
d
¡ƒD ]}||vr¬| |¡ q¬nœ|dkrÚ| |¡ nˆ|dkrð|du rî|}nr|dk�r
|du �rb|}nX|dk�r*|du �rbt||ƒ}n8|dk�rJ|du �rbt||ƒ}n|�rÂ||	v�rÂ||	|< qD|jdkrDt|dƒrDt|d| jƒ}| dd¡}|du �r¨t d¡ qDt||| jd�}| |¡ qD|||||||||
|	dœ
S )a2  Get metadata dictionary out of HTML object.

    Relevant specs:

    https://www.whatwg.org/html#the-title-element
    https://www.whatwg.org/html#standard-metadata-names
    https://wiki.whatwg.org/wiki/MetaExtensions
    https://microformats.org/wiki/existing-rel-values#HTML5_link_type_extensions

    NÚlangÚtitleÚmetaÚlinkÚnamer   Úcontentr\   ú,ÚauthorÚdescriptionÚ	generatorzdcterms.createdzdcterms.modifiedÚ
attachmentÚhrefz'Missing href in <link rel="attachment">)r;   rj   rY   )
rc   rj   rk   r\   ÚauthorsÚcreatedÚmodifiedÚattachmentsrb   Úcustom)Úetree_elementÚattribr    Úwrapper_elementÚ	query_allr&   r   r   ÚmapÚstrip_whitespaceÚsplitÚappendÚparse_w3c_dater$   r   r)   r   r^   r   rY   )Úhtmlrc   rj   rk   r\   rn   ro   rp   rq   rr   rb   r"   rf   rg   Úkeywordr;   Zattachment_titlerl   r   r   r   Úget_html_metadataÝ   sx    







ÿ
þör~   c                 C   s
   |   t¡S )zÊUse the HTML definition of "space character",
    not all Unicode Whitespace.

    https://www.whatwg.org/html#strip-leading-and-trailing-whitespace
    https://www.whatwg.org/html#space-character

    )rD   ÚHTML_WHITESPACEr   r   r   r   rx   &  s    rx   aG  
    ^
    [ 	
]*
    (?P<year>\d\d\d\d)
    (?:
        -(?P<month>0\d|1[012])
        (?:
            -(?P<day>[012]\d|3[01])
            (?:
                T(?P<hour>[01]\d|2[0-3])
                :(?P<minute>[0-5]\d)
                (?:
                    :(?P<second>[0-5]\d)
                    (?:\.\d+)?  # Second fraction, ignored
                )?
                (?:
                    Z |  # UTC
                    (?P<tz_hour>[+-](?:[01]\d|2[0-3]))
                    :(?P<tz_minute>[0-5]\d)
                )
            )?
        )?
    )?
    [ 	
]*
    $
c                 C   s    t  |¡r|S t d| |¡ dS )zYParse datetimes as defined by the W3C.

    See https://www.w3.org/TR/NOTE-datetime

    z#Invalid date in <meta name="%s"> %rN)ÚW3C_DATE_REÚmatchr   Úwarning)Z	meta_namer   r   r   r   r{   T  s
    
ÿr{   )/Ú__doc__ÚreÚimportlib.resourcesr   r   r   r   r   r   Zcss.countersr   Zformatting_structurer	   Úimagesr
   Úloggerr   Úurlsr   ÚHTML5_UA_COUNTER_STYLEÚ	read_textZHTML5_UAZHTML5_UA_FORMZHTML5_PHÚHTML5_UA_STYLESHEETÚHTML5_UA_FORM_STYLESHEETÚHTML5_PH_STYLESHEETr   Úcompiler   r   r$   r%   r*   r.   r6   r?   rE   rH   rV   rX   ra   r~   rx   ÚVERBOSEr€   r{   r   r   r   r   Ú<module>   s\   	ÿÿ
	
$




	
Iç