B
    Ի\+                 @   sH  d dl mZ yd dlmZ W n  ek
r<   d dlmZ Y nX d dlmZmZ dZ	d'ddZ
dd	 ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZd&S )(    )unicode_literals)escape)	text_type	iteritems)CheckboxInput	FileInputHiddenInput
ListWidgetPasswordInput
RadioInputSelectSubmitInputTableWidgetTextArea	TextInputOptionTc             C   s(   t | dr|  } ntt| |d} | S )a  
    Replace special characters "&", "<" and ">" to HTML-safe sequences.

    If the optional flag quote is true (the default), the quotation mark
    characters, both double quote (") and single quote (') characters are also
    translated.

    If a `HTMLString` is provied, it's assumed that whatever you give to
    escape_html is a string with any unsafe values already escaped.
    __html__)quote)hasattrr   r   r   )sr    r   Z/Users/stiphol/Desktop/Flask_blog/venv/lib/python3.7/site-packages/wtforms/widgets/core.pyescape_html   s    

r   c           	   K   s   g }xt t| D ]t\}}|dkr0|dd }n|drF|dd}|dkrZ|| q|dkrdq|d	t|tt|dd
f  qW d|S )a=  
    Generate HTML attribute syntax from inputted keyword arguments.

    The output value is sorted by the passed keys, to provide consistent output
    each time this function is called with the same parameters. Because of the
    frequent use of the normally reserved keywords `class` and `for`, suffixing
    these with an underscore will allow them to be used.

    In order to facilitate the use of ``data-`` attributes, the first underscore
    behind the ``data``-element is replaced with a hyphen.

    >>> html_params(data_any_attribute='something')
    'data-any_attribute="something"'

    In addition, the values ``True`` and ``False`` are special:
      * ``attr=True`` generates the HTML compact output of a boolean attribute,
        e.g. ``checked=True`` will generate simply ``checked``
      * ``attr=False`` will be ignored and generate no output.

    >>> html_params(name='text1', id='f', class_='text')
    'class="text" id="f" name="text1"'
    >>> html_params(checked=True, readonly=False, name="text1", abc="hello")
    'abc="hello" checked name="text1"'
    )class_Zclass__Zfor_NZdata__-TFz%s="%s")r    )sortedr   
startswithreplaceappendr   r   join)kwargsparamskvr   r   r   html_params#   s    
&r'   c               @   s   e Zd ZdZdd ZdS )
HTMLStringa  
    This is an "HTML safe string" class that is returned by WTForms widgets.

    For the most part, HTMLString acts like a normal unicode string, except
    in that it has a `__html__` method. This method is invoked by a compatible
    auto-escaping HTML framework to get the HTML-safe version of a string.

    Usage::

        HTMLString('<input type="text" value="hello">')

    c             C   s   | S )a=  
        Give an HTML-safe string.

        This method actually returns itself, because it's assumed that
        whatever you give to HTMLString is a string with any unsafe values
        already escaped. This lets auto-escaping template frameworks
        know that this string is safe for HTML rendering.
        r   )selfr   r   r   r   X   s    	zHTMLString.__html__N)__name__
__module____qualname____doc__r   r   r   r   r   r(   K   s   r(   c               @   s"   e Zd ZdZd	ddZdd ZdS )
r	   a  
    Renders a list of fields as a `ul` or `ol` list.

    This is used for fields which encapsulate many inner fields as subfields.
    The widget will try to iterate the field to get access to the subfields and
    call them to render them.

    If `prefix_label` is set, the subfield's label is printed before the field,
    otherwise afterwards. The latter is useful for iterating radios or
    checkboxes.
    ulTc             C   s   |dkst || _|| _d S )N)Zolr.   )AssertionErrorhtml_tagprefix_label)r)   r0   r1   r   r   r   __init__p   s    zListWidget.__init__c             K   s   | d|j d| jtf |f g}x@|D ]8}| jrL|d|j| f  q*|d| |jf  q*W |d| j  td|S )Nidz<%s %s>z<li>%s %s</li>z</%s> )	
setdefaultr3   r0   r'   r1   r!   labelr(   r"   )r)   fieldr#   htmlsubfieldr   r   r   __call__u   s    
zListWidget.__call__N)r.   T)r*   r+   r,   r-   r2   r:   r   r   r   r   r	   d   s   
r	   c               @   s"   e Zd ZdZdddZdd ZdS )	r   a  
    Renders a list of fields as a set of table rows with th/td pairs.

    If `with_table_tag` is True, then an enclosing <table> is placed around the
    rows.

    Hidden fields will not be displayed with a row, instead the field will be
    pushed into a subsequent table row to ensure XHTML validity. Hidden fields
    at the end of the field list will appear outside the table.
    Tc             C   s
   || _ d S )N)with_table_tag)r)   r;   r   r   r   r2      s    zTableWidget.__init__c             K   s   g }| j r,|d|j |dtf |  d}xF|D ]>}|jdkrR|t|7 }q6|dt|j|t|f  d}q6W | j r|d |r|| td	|S )Nr3   z
<table %s>r4   )HiddenFieldZCSRFTokenFieldz!<tr><th>%s</th><td>%s%s</td></tr>z</table>)
r;   r5   r3   r!   r'   typer   r6   r(   r"   )r)   r7   r#   r8   hiddenr9   r   r   r   r:      s    



zTableWidget.__call__N)T)r*   r+   r,   r-   r2   r:   r   r   r   r   r      s   

r   c               @   s*   e Zd ZdZeeZdddZdd ZdS )Inputz
    Render a basic ``<input>`` field.

    This is used as the basis for most of the other input fields.

    By default, the `_value()` method will be called upon the associated field
    to provide the ``value=`` HTML attribute.
    Nc             C   s   |d k	r|| _ d S )N)
input_type)r)   r@   r   r   r   r2      s    zInput.__init__c             K   sn   | d|j | d| j d|kr0| |d< d|krPdt|dg krPd|d< td| jf d|ji| S )	Nr3   r=   valuerequiredflagsTz
<input %s>name)r5   r3   r@   _valuegetattrr(   r'   rD   )r)   r7   r#   r   r   r   r:      s    zInput.__call__)N)r*   r+   r,   r-   staticmethodr'   r2   r:   r   r   r   r   r?      s   
r?   c               @   s   e Zd ZdZdZdS )r   z*
    Render a single-line text input.
    textN)r*   r+   r,   r-   r@   r   r   r   r   r      s   r   c                   s.   e Zd ZdZdZdddZ fddZ  ZS )	r
   z
    Render a password input.

    For security purposes, this field will not reproduce the value on a form
    submit by default. To have the value filled in, set `hide_value` to
    `False`.
    passwordTc             C   s
   || _ d S )N)
hide_value)r)   rJ   r   r   r   r2      s    zPasswordInput.__init__c                s"   | j rd|d< tt| j|f|S )Nr4   rA   )rJ   superr
   r:   )r)   r7   r#   )	__class__r   r   r:      s    zPasswordInput.__call__)T)r*   r+   r,   r-   r@   r2   r:   __classcell__r   r   )rL   r   r
      s   
r
   c               @   s   e Zd ZdZdZdZdS )r   z 
    Render a hidden input.
    )r>   r>   N)r*   r+   r,   r-   field_flagsr@   r   r   r   r   r      s   r   c                   s$   e Zd ZdZdZ fddZ  ZS )r   zq
    Render a checkbox.

    The ``checked`` HTML attribute is set if the field's data is a non-false value.
    Zcheckboxc                s*   t |d|jrd|d< tt| j|f|S )NcheckedT)rF   datarK   r   r:   )r)   r7   r#   )rL   r   r   r:      s    zCheckboxInput.__call__)r*   r+   r,   r-   r@   r:   rM   r   r   )rL   r   r      s   r   c                   s$   e Zd ZdZdZ fddZ  ZS )r   z
    Render a single radio button.

    This widget is most commonly used in conjunction with ListWidget or some
    other listing, as singular radio buttons are not very useful.
    Zradioc                s"   |j rd|d< tt| j|f|S )NTrO   )rO   rK   r   r:   )r)   r7   r#   )rL   r   r   r:      s    zRadioInput.__call__)r*   r+   r,   r-   r@   r:   rM   r   r   )rL   r   r      s   r   c                   s2   e Zd ZdZdZd fdd	Z fddZ  ZS )	r   zURender a file chooser input.

    :param multiple: allow choosing multiple files
    fileFc                s   t t|   || _d S )N)rK   r   r2   multiple)r)   rR   )rL   r   r   r2     s    zFileInput.__init__c                s*   d|d< | j rd|d< tt| j|f|S )NFrA   TrR   )rR   rK   r   r:   )r)   r7   r#   )rL   r   r   r:     s    zFileInput.__call__)F)r*   r+   r,   r-   r@   r2   r:   rM   r   r   )rL   r   r      s   r   c                   s$   e Zd ZdZdZ fddZ  ZS )r   z
    Renders a submit button.

    The field's label is used as the text of the submit button instead of the
    data on the field.
    submitc                s$   | d|jj tt| j|f|S )NrA   )r5   r6   rH   rK   r   r:   )r)   r7   r#   )rL   r   r   r:     s    zSubmitInput.__call__)r*   r+   r,   r-   r@   r:   rM   r   r   )rL   r   r     s   r   c               @   s   e Zd ZdZdd ZdS )r   zs
    Renders a multi-line text area.

    `rows` and `cols` ought to be passed as keyword args when rendering.
    c             K   s^   | d|j d|kr.dt|dg kr.d|d< tdtf d|ji|tt| ddf S )	Nr3   rB   rC   Tz<textarea %s>%s</textarea>rD   F)r   )	r5   r3   rF   r(   r'   rD   r   r   rE   )r)   r7   r#   r   r   r   r:   $  s    zTextArea.__call__N)r*   r+   r,   r-   r:   r   r   r   r   r     s   r   c               @   s.   e Zd ZdZd
ddZdd Zedd Zd	S )r   a:  
    Renders a select field.

    If `multiple` is True, then the `size` property should be specified on
    rendering to make the field useful.

    The field must provide an `iter_choices()` method which the widget will
    call on rendering; this method must yield tuples of
    `(value, label, selected)`.
    Fc             C   s
   || _ d S )N)rR   )r)   rR   r   r   r   r2   9  s    zSelect.__init__c             K   s   | d|j | jrd|d< d|kr<dt|dg kr<d|d< dtf d|ji| g}x*| D ]\}}}|| ||| q`W |d t	d	
|S )
Nr3   TrR   rB   rC   z<select %s>rD   z	</select>r4   )r5   r3   rR   rF   r'   rD   Ziter_choicesr!   render_optionr(   r"   )r)   r7   r#   r8   valr6   selectedr   r   r   r:   <  s    
zSelect.__call__c             K   sF   |dkrt |}t||d}|r(d|d< tdtf |t|ddf S )NT)rA   rV   z<option %s>%s</option>F)r   )r   dictr(   r'   r   )clsrA   r6   rV   r#   optionsr   r   r   rT   H  s    zSelect.render_optionN)F)r*   r+   r,   r-   r2   r:   classmethodrT   r   r   r   r   r   .  s   

r   c               @   s   e Zd ZdZdd ZdS )r   z
    Renders the individual option from a select field.

    This is just a convenience for various custom rendering situations, and an
    option by itself does not constitute an entire field.
    c             K   s   t j| |jj|jf|S )N)r   rT   rE   r6   rH   rO   )r)   r7   r#   r   r   r   r:   [  s    zOption.__call__N)r*   r+   r,   r-   r:   r   r   r   r   r   T  s   r   N)T)
__future__r   r8   r   ImportErrorZcgiwtforms.compatr   r   __all__r   r'   r(   objectr	   r   r?   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s,   
(!&