Exemplo n.º 1
0
def lazy_pgettext(context, string, **variables):
    """Like :func:`pgettext` but the string returned is lazy which means
    it will be translated when it is used as an actual string.

    .. versionadded:: 0.7
    """
    return LazyString(pgettext, context, string, **variables)
Exemplo n.º 2
0
    def lazy_ngettext(self, singular, plural, num, **variables):
        """Like :func:`ngettext` but the string returned is lazy which means
        it will be translated when it is used as an actual string.

        Example::

            apples = lazy_ngettext(u'%(num)d Apple', u'%(num)d Apples', num=len(apples))

            @app.route('/')
            def index():
                return unicode(apples)
        """
        return LazyString(self.ngettext, singular, plural, num, **variables)
Exemplo n.º 3
0
def lazy_gettext(string, **variables):
    """Like :func:`gettext` but the string returned is lazy which means
    it will be translated when it is used as an actual string.

    Example::

        hello = lazy_gettext(u'Hello World')

        @app.route('/')
        def index():
            return unicode(hello)
    """
    return LazyString(gettext, string, **variables)
Exemplo n.º 4
0
def lazy_formatter_gettext(string: str, lazy_formatter: Callable[[str], str],
                           **variables: Any) -> LazyString:
    """Formats a lazy_gettext string with a custom function

    Example::

        def custom_formatter(string: str) -> str:
            if current_app.config["CONDITIONAL_KEY"]:
                string += " . Condition key is on"
            return string

        hello = lazy_formatter_gettext(u'Hello World', custom_formatter)

        @app.route('/')
        def index():
            return unicode(hello)
    """
    return LazyString(_wrap_lazy_formatter_gettext, string, lazy_formatter,
                      **variables)
Exemplo n.º 5
0
def _l(string: str, /) -> str:
    """Like _() but the string returned is lazy which means it will be translated when it is used as
    an actual string. Positional-only arguments to simplify additional linting of localized
    strings."""
    return LazyString(_, string)
Exemplo n.º 6
0
def _l(string: str) -> str:
    """Like _() but the string returned is lazy which means it will be translated when it is used as
    an actual string."""
    return LazyString(_, string)
Exemplo n.º 7
0
def lazy_ngettext(*args, **kwargs):
    return LazyString(ngettext, *args, **kwargs)
Exemplo n.º 8
0
def test_htmllib_integration():
    assert escaping.escape_attribute("") == ""
    assert escaping.escape_text("") == ""


@pytest.mark.parametrize(
    "inp,out",
    [
        ('">alert(1)', "">alert(1)"),
        (None, ""),
        (1, "1"),
        (HTML('">alert(1)'), '">alert(1)'),
        (1.1, "1.1"),
        ("<", "&lt;"),
        ("'", "&#x27;"),
        (LazyString(lambda: "'"), "&#x27;"),
    ],
)
def test_escape_attribute(inp, out):
    assert escaping.escape_attribute(inp) == out


@pytest.mark.parametrize(
    "inp,out",
    [
        ("&quot;&gt;alert(1)", '">alert(1)'),
        ("&lt;", "<"),
    ],
)
def test_unescape_attribute(inp, out):
    assert escaping.unescape_attributes(inp) == out