xref: /aosp_15_r20/external/libchrome/third_party/markupsafe/_native.py (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker# -*- coding: utf-8 -*-
2*635a8641SAndroid Build Coastguard Worker"""
3*635a8641SAndroid Build Coastguard Worker    markupsafe._native
4*635a8641SAndroid Build Coastguard Worker    ~~~~~~~~~~~~~~~~~~
5*635a8641SAndroid Build Coastguard Worker
6*635a8641SAndroid Build Coastguard Worker    Native Python implementation the C module is not compiled.
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker    :copyright: (c) 2010 by Armin Ronacher.
9*635a8641SAndroid Build Coastguard Worker    :license: BSD, see LICENSE for more details.
10*635a8641SAndroid Build Coastguard Worker"""
11*635a8641SAndroid Build Coastguard Workerfrom markupsafe import Markup
12*635a8641SAndroid Build Coastguard Workerfrom markupsafe._compat import text_type
13*635a8641SAndroid Build Coastguard Worker
14*635a8641SAndroid Build Coastguard Worker
15*635a8641SAndroid Build Coastguard Workerdef escape(s):
16*635a8641SAndroid Build Coastguard Worker    """Convert the characters &, <, >, ' and " in string s to HTML-safe
17*635a8641SAndroid Build Coastguard Worker    sequences.  Use this if you need to display text that might contain
18*635a8641SAndroid Build Coastguard Worker    such characters in HTML.  Marks return value as markup string.
19*635a8641SAndroid Build Coastguard Worker    """
20*635a8641SAndroid Build Coastguard Worker    if hasattr(s, '__html__'):
21*635a8641SAndroid Build Coastguard Worker        return s.__html__()
22*635a8641SAndroid Build Coastguard Worker    return Markup(text_type(s)
23*635a8641SAndroid Build Coastguard Worker        .replace('&', '&amp;')
24*635a8641SAndroid Build Coastguard Worker        .replace('>', '&gt;')
25*635a8641SAndroid Build Coastguard Worker        .replace('<', '&lt;')
26*635a8641SAndroid Build Coastguard Worker        .replace("'", '&#39;')
27*635a8641SAndroid Build Coastguard Worker        .replace('"', '&#34;')
28*635a8641SAndroid Build Coastguard Worker    )
29*635a8641SAndroid Build Coastguard Worker
30*635a8641SAndroid Build Coastguard Worker
31*635a8641SAndroid Build Coastguard Workerdef escape_silent(s):
32*635a8641SAndroid Build Coastguard Worker    """Like :func:`escape` but converts `None` into an empty
33*635a8641SAndroid Build Coastguard Worker    markup string.
34*635a8641SAndroid Build Coastguard Worker    """
35*635a8641SAndroid Build Coastguard Worker    if s is None:
36*635a8641SAndroid Build Coastguard Worker        return Markup()
37*635a8641SAndroid Build Coastguard Worker    return escape(s)
38*635a8641SAndroid Build Coastguard Worker
39*635a8641SAndroid Build Coastguard Worker
40*635a8641SAndroid Build Coastguard Workerdef soft_unicode(s):
41*635a8641SAndroid Build Coastguard Worker    """Make a string unicode if it isn't already.  That way a markup
42*635a8641SAndroid Build Coastguard Worker    string is not converted back to unicode.
43*635a8641SAndroid Build Coastguard Worker    """
44*635a8641SAndroid Build Coastguard Worker    if not isinstance(s, text_type):
45*635a8641SAndroid Build Coastguard Worker        s = text_type(s)
46*635a8641SAndroid Build Coastguard Worker    return s
47