File: //usr/lib/python3/dist-packages/twisted/web/__pycache__/_flatten.cpython-310.pyc
o
�b�@ � @ s� d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl m
Z
ddlmZm
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZmZ dd lmZ dd
lmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddl&m'Z'm(Z(m)Z) dd
l*m+Z+m,Z, ed�Z-eZ. ee/e0e$e e"e#ee.df ee. ee.ddf e!ee. eee. e1e.f e+f
Z2 dee/e0f de/fdd�Z3dee/e0f de/fdd�Z4de
e/ge1f de
e/gdf fdd�Z5dee/e0f de/fdd�Z6dee/e0f de/fdd�Z7 d2de0deeee0e2f d ee2 de2fd!d"�Z8d#ee- dee- fd$d%�Z9d&ee, d'e2de
e/ge1f deeee0e2f d(ee+ d)e
ee/e0f ge/f deeeee2 f ddf fd*d+�Z:d&ee, d'e2de
e/ge1f ddfd,d-�Z;d&ee, d'e2de
e/ge1f ded fd.d/�Z<d&ee, d'e2dee/ fd0d1�Z=dS )3zu
Context-free flattener/serializer for rendering Python objects, possibly
complex or arbitrarily nested, as strings.
� )�iscoroutine)�BytesIO)�exc_info)�
extract_tb)�
GeneratorType)�Any�Callable� Coroutine� Generator�List�Mapping�Optional�Sequence�Tuple�TypeVar�Union�cast)�Deferred�ensureDeferred)�nativeString)�Failure)�CDATA�CharRef�Comment�Tag�slot�voidElements)�FlattenerError�UnfilledSlot�UnsupportedType)�IRenderable�IRequest�T.N�data�returnc C s4 t | t�r
| �d�} | �dd��dd��dd�} | S )a�
Escape some character or UTF-8 byte data for inclusion in an HTML or XML
document, by replacing metacharacters (C{&<>}) with their entity
equivalents (C{&<>}).
This is used as an input to L{_flattenElement}'s C{dataEscaper} parameter.
@param data: The string to escape.
@return: The quoted form of C{data}. If C{data} is L{str}, return a utf-8
encoded string.
�utf-8� &s &� <s <� >� >��
isinstance�str�encode�replace�r# � r0 �6/usr/lib/python3/dist-packages/twisted/web/_flatten.py�escapeForContentC s
r2 c C s t | t�r
| �d�S | S )aG
Escape some character or UTF-8 byte data for inclusion in the top level of
an attribute. L{attributeEscapingDoneOutside} actually passes the data
through unchanged, because L{writeWithAttributeEscaping} handles the
quoting of the text within attributes outside the generator returned by
L{_flattenElement}; this is used as the C{dataEscaper} argument to that
L{_flattenElement} call so that that generator does not redundantly escape
its text output.
@param data: The string to escape.
@return: The string, unchanged, except for encoding.
r% )r+ r, r- r/ r0 r0 r1 �attributeEscapingDoneOutsideV s
r3 �writec s dt ddf� fdd�}|S )aU
Decorate a C{write} callable so that all output written is properly quoted
for inclusion within an XML attribute value.
If a L{Tag <twisted.web.template.Tag>} C{x} is flattened within the context
of the contents of another L{Tag <twisted.web.template.Tag>} C{y}, the
metacharacters (C{<>&"}) delimiting C{x} should be passed through
unchanged, but the textual content of C{x} should still be quoted, as
usual. For example: C{<y><x>&</x></y>}. That is the default behavior
of L{_flattenElement} when L{escapeForContent} is passed as the
C{dataEscaper}.
However, when a L{Tag <twisted.web.template.Tag>} C{x} is flattened within
the context of an I{attribute} of another L{Tag <twisted.web.template.Tag>}
C{y}, then the metacharacters delimiting C{x} should be quoted so that it
can be parsed from the attribute's value. In the DOM itself, this is not a
valid thing to do, but given that renderers and slots may be freely moved
around in a L{twisted.web.template} template, it is a condition which may
arise in a document and must be handled in a way which produces valid
output. So, for example, you should be able to get C{<y attr="<x />"
/>}. This should also be true for other XML/HTML meta-constructs such as
comments and CDATA, so if you were to serialize a L{comment
<twisted.web.template.Comment>} in an attribute you should get C{<y
attr="<-- comment -->" />}. Therefore in order to capture these
meta-characters, flattening is done with C{write} callable that is wrapped
with L{writeWithAttributeEscaping}.
The final case, and hopefully the much more common one as compared to
serializing L{Tag <twisted.web.template.Tag>} and arbitrary L{IRenderable}
objects within an attribute, is to serialize a simple string, and those
should be passed through for L{writeWithAttributeEscaping} to quote
without applying a second, redundant level of quoting.
@param write: A callable which will be invoked with the escaped L{bytes}.
@return: A callable that writes data with escaping.
r# r$ Nc s � t | ��dd�� d S )N� "s ")r2 r. r/ �r4 r0 r1 �_write� s z*writeWithAttributeEscaping.<locals>._write)�bytes)r4 r7 r0 r6 r1 �writeWithAttributeEscapingi s )r9 c C s t | t�r
| �d�} | �dd�S )z�
Escape CDATA for inclusion in a document.
@param data: The string to escape.
@return: The quoted form of C{data}. If C{data} is unicode, return a utf-8
encoded string.
r% � ]]>s ]]]]><![CDATA[>r* r/ r0 r0 r1 �escapedCDATA� s
r; c C sH t | t�r
| �d�} | �dd��dd�} | r"| dd� dkr"| d 7 } | S )
z�
Escape a comment for inclusion in a document.
@param data: The string to escape.
@return: The quoted form of C{data}. If C{data} is unicode, return a utf-8
encoded string.
r% s --s - - r( r) ���N� -� r* r/ r0 r0 r1 �escapedComment� s
r? �name�slotData�defaultc C sD |ddd� D ]}|dur| |v r|| S q|dur|S t | ��)zK
Find the value of the named slot in the given stack of slot data.
Nr<