當前位置: 首頁>>代碼示例>>Python>>正文


Python Markup.__html__方法代碼示例

本文整理匯總了Python中markupsafe.Markup.__html__方法的典型用法代碼示例。如果您正苦於以下問題:Python Markup.__html__方法的具體用法?Python Markup.__html__怎麽用?Python Markup.__html__使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在markupsafe.Markup的用法示例。


在下文中一共展示了Markup.__html__方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_type_behavior

# 需要導入模塊: from markupsafe import Markup [as 別名]
# 或者: from markupsafe.Markup import __html__ [as 別名]
    def test_type_behavior(self):
        # an escaped object is markup too
        assert type(Markup('foo') + 'bar') is Markup

        # and it implements __html__ by returning itself
        x = Markup("foo")
        assert x.__html__() is x
開發者ID:Mondego,項目名稱:pyreco,代碼行數:9,代碼來源:allPythonContent.py

示例2: test_markup_operations

# 需要導入模塊: from markupsafe import Markup [as 別名]
# 或者: from markupsafe.Markup import __html__ [as 別名]
    def test_markup_operations(self):
        # adding two strings should escape the unsafe one
        unsafe = '<script type="application/x-some-script">alert("foo");</script>'
        safe = Markup('<em>username</em>')
        assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)

        # string interpolations are safe to use too
        assert Markup('<em>%s</em>') % '<bad user>' == \
               '<em>&lt;bad user&gt;</em>'
        assert Markup('<em>%(username)s</em>') % {
            'username': '<bad user>'
        } == '<em>&lt;bad user&gt;</em>'

        # an escaped object is markup too
        assert type(Markup('foo') + 'bar') is Markup

        # and it implements __html__ by returning itself
        x = Markup("foo")
        assert x.__html__() is x

        # it also knows how to treat __html__ objects
        class Foo(object):
            def __html__(self):
                return '<em>awesome</em>'
            def __unicode__(self):
                return 'awesome'
        assert Markup(Foo()) == '<em>awesome</em>'
        assert Markup('<strong>%s</strong>') % Foo() == \
               '<strong><em>awesome</em></strong>'

        # escaping and unescaping
        assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'
        assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
        assert Markup("&lt;test&gt;").unescape() == "<test>"
開發者ID:10sr,項目名稱:hue,代碼行數:36,代碼來源:tests.py


注:本文中的markupsafe.Markup.__html__方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。