123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- # -*- coding: utf-8 -*-
- import pytest
- from markupsafe import escape
- from markupsafe import escape_silent
- from markupsafe import Markup
- from markupsafe._compat import PY2
- from markupsafe._compat import text_type
- def test_adding():
- unsafe = '<script type="application/x-some-script">alert("foo");</script>'
- safe = Markup("<em>username</em>")
- assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe)
- @pytest.mark.parametrize(
- ("template", "data", "expect"),
- (
- ("<em>%s</em>", "<bad user>", "<em><bad user></em>"),
- (
- "<em>%(username)s</em>",
- {"username": "<bad user>"},
- "<em><bad user></em>",
- ),
- ("%i", 3.14, "3"),
- ("%.2f", 3.14, "3.14"),
- ),
- )
- def test_string_interpolation(template, data, expect):
- assert Markup(template) % data == expect
- def test_type_behavior():
- assert type(Markup("foo") + "bar") is Markup
- x = Markup("foo")
- assert x.__html__() is x
- def test_html_interop():
- class Foo(object):
- def __html__(self):
- return "<em>awesome</em>"
- def __unicode__(self):
- return "awesome"
- __str__ = __unicode__
- assert Markup(Foo()) == "<em>awesome</em>"
- result = Markup("<strong>%s</strong>") % Foo()
- assert result == "<strong><em>awesome</em></strong>"
- def test_tuple_interpol():
- result = Markup("<em>%s:%s</em>") % ("<foo>", "<bar>")
- expect = Markup(u"<em><foo>:<bar></em>")
- assert result == expect
- def test_dict_interpol():
- result = Markup("<em>%(foo)s</em>") % {"foo": "<foo>"}
- expect = Markup(u"<em><foo></em>")
- assert result == expect
- result = Markup("<em>%(foo)s:%(bar)s</em>") % {"foo": "<foo>", "bar": "<bar>"}
- expect = Markup(u"<em><foo>:<bar></em>")
- assert result == expect
- def test_escaping():
- assert escape("\"<>&'") == ""<>&'"
- assert Markup("<em>Foo & Bar</em>").striptags() == "Foo & Bar"
- def test_unescape():
- assert Markup("<test>").unescape() == "<test>"
- result = Markup("jack & tavi are cooler than mike & russ").unescape()
- expect = "jack & tavi are cooler than mike & russ"
- assert result == expect
- original = "&foo;"
- once = Markup(original).unescape()
- twice = Markup(once).unescape()
- expect = "&foo;"
- assert once == expect
- assert twice == expect
- def test_format():
- result = Markup("<em>{awesome}</em>").format(awesome="<awesome>")
- assert result == "<em><awesome></em>"
- result = Markup("{0[1][bar]}").format([0, {"bar": "<bar/>"}])
- assert result == "<bar/>"
- result = Markup("{0[1][bar]}").format([0, {"bar": Markup("<bar/>")}])
- assert result == "<bar/>"
- def test_formatting_empty():
- formatted = Markup("{}").format(0)
- assert formatted == Markup("0")
- def test_custom_formatting():
- class HasHTMLOnly(object):
- def __html__(self):
- return Markup("<foo>")
- class HasHTMLAndFormat(object):
- def __html__(self):
- return Markup("<foo>")
- def __html_format__(self, spec):
- return Markup("<FORMAT>")
- assert Markup("{0}").format(HasHTMLOnly()) == Markup("<foo>")
- assert Markup("{0}").format(HasHTMLAndFormat()) == Markup("<FORMAT>")
- def test_complex_custom_formatting():
- class User(object):
- def __init__(self, id, username):
- self.id = id
- self.username = username
- def __html_format__(self, format_spec):
- if format_spec == "link":
- return Markup('<a href="/user/{0}">{1}</a>').format(
- self.id, self.__html__()
- )
- elif format_spec:
- raise ValueError("Invalid format spec")
- return self.__html__()
- def __html__(self):
- return Markup("<span class=user>{0}</span>").format(self.username)
- user = User(1, "foo")
- result = Markup("<p>User: {0:link}").format(user)
- expect = Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
- assert result == expect
- def test_formatting_with_objects():
- class Stringable(object):
- def __unicode__(self):
- return u"строка"
- if PY2:
- def __str__(self):
- return "some other value"
- else:
- __str__ = __unicode__
- assert Markup("{s}").format(s=Stringable()) == Markup(u"строка")
- def test_all_set():
- import markupsafe as markup
- for item in markup.__all__:
- getattr(markup, item)
- def test_escape_silent():
- assert escape_silent(None) == Markup()
- assert escape(None) == Markup(None)
- assert escape_silent("<foo>") == Markup(u"<foo>")
- def test_splitting():
- expect = [Markup("a"), Markup("b")]
- assert Markup("a b").split() == expect
- assert Markup("a b").rsplit() == expect
- assert Markup("a\nb").splitlines() == expect
- def test_mul():
- assert Markup("a") * 3 == Markup("aaa")
- def test_escape_return_type():
- assert isinstance(escape("a"), Markup)
- assert isinstance(escape(Markup("a")), Markup)
- class Foo:
- def __html__(self):
- return "<strong>Foo</strong>"
- assert isinstance(escape(Foo()), Markup)
|