当前位置: 首页>>代码示例>>Python>>正文


Python CssCompressor.split_contents方法代码示例

本文整理汇总了Python中compressor.CssCompressor.split_contents方法的典型用法代码示例。如果您正苦于以下问题:Python CssCompressor.split_contents方法的具体用法?Python CssCompressor.split_contents怎么用?Python CssCompressor.split_contents使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在compressor.CssCompressor的用法示例。


在下文中一共展示了CssCompressor.split_contents方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CompressorTestCase

# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import split_contents [as 别名]
class CompressorTestCase(BaseTestCase):
    
    def setUp(self):
        super(CompressorTestCase, self).setUp()
        
        
        self.ccssFile = os.path.join(settings.MEDIA_ROOT, u'css/three.css')
        self.css = dedent("""
        <link rel="stylesheet" href="/media/css/one.css" type="text/css">
        <style type="text/css">p { border:5px solid green;}</style>
        <link rel="stylesheet" href="/media/css/two.css" type="text/css">
        <link rel="stylesheet" href="/media/css/three.ccss" type="text/css">
        <style type="text/ccss">
        """)+ \
        '\n'.join((
        "small:\n  font-size:10px",
        '</style>\n<style type="ccss">',
        "h1:\n  font-weight:bold",
        "</style>"))
        self.css_hash = "1ff892c21b66"
        self.js_hash = "3f33b9146e12"
        self.cssNode = CssCompressor(self.css)

        self.js = """
        <script src="/media/js/one.js" type="text/javascript"></script>
        <script type="text/javascript">obj.value = "value";</script>
        """
        self.jsNode = JsCompressor(self.js)
    
    def test_get_filename(self):
        settings.COMPRESS_URL = '/static_test/'
        settings.COMPRESS_ROOT = os.path.join(os.path.dirname(__file__),'static_test_dir')
        path = os.path.join(settings.MEDIA_URL,'something.css')
        filename = self.cssNode.get_filename(path)
        self.assertEqual(filename,os.path.join(settings.MEDIA_ROOT,'something.css'))
        path = "http://something.com/static/something.css"
        self.assertRaises(UncompressableFileError, self.cssNode.get_filename,path)
        self.assertRaises(UncompressableFileError, self.jsNode.get_filename,path)
    
    def test_css_compiler_exists(self):
        settings.COMPILER_FORMATS = {
            '.ccss': {
                'binary_path': 'python ' + os.path.join(self.TEST_DIR,'clevrcss.py'),
                'arguments': '*.ccss'
            },
        }            
        self.assertRaises(Exception, self.cssNode.output)

    def test_css_split(self):
        out = [
            ('file', os.path.join(settings.MEDIA_ROOT, u'css/one.css'), '<link rel="stylesheet" href="/media/css/one.css" type="text/css" />'),
            ('hunk', u'p { border:5px solid green;}', '<style type="text/css">p { border:5px solid green;}</style>'),
            ('file', os.path.join(settings.MEDIA_ROOT, u'css/two.css'), '<link rel="stylesheet" href="/media/css/two.css" type="text/css" />'),
            ('file', self.ccssFile, '<link rel="stylesheet" href="/media/css/three.css" type="text/css" />'),
            ('hunk', 'small {\n  font-size: 10px;\n}', "<style type='text/css'>\nsmall {\n  font-size: 10px;\n}\n</style>"),
            ('hunk', 'h1 {\n  font-weight: bold;\n}', "<style type='text/css'>\nh1 {\n  font-weight: bold;\n}\n</style>")
        ]
        split = self.cssNode.split_contents()
        split = [(x[0], x[1], str(x[2])) for x in split]
        self.assertEqual(out, split)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_hunks(self):
        out = ['body { background:#990; }', 'p { border:5px solid green;}', 'body { color:#fff; }', 'a {\n  color: #5c4032;\n}', 'small {\n  font-size: 10px;\n}', 'h1 {\n  font-weight: bold;\n}']
        self.assertEqual(out, self.cssNode.hunks)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_output(self):
        out = u'body { background:#990; }\np { border:5px solid green;}\nbody { color:#fff; }\na {\n  color: #5c4032;\n}\nsmall {\n  font-size: 10px;\n}\nh1 {\n  font-weight: bold;\n}'
        self.assertEqual(out, self.cssNode.combined)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_mtimes(self):
        is_date = re.compile(r'^\d{10}\.\d$')
        for date in self.cssNode.mtimes:
            self.assert_(is_date.match(str(date)), "mtimes is returning something that doesn't look like a date")
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_css_return_if_off(self):
        settings.COMPRESS = False
        css = """
<link rel="stylesheet" href="/media/css/one.css" type="text/css">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css">
<link rel="stylesheet" href="/media/css/three.css" type="text/css">
<style type='text/css'>\nsmall {\n  font-size: 10px;\n}
</style>\n<style type='text/css'>\nh1 {\n  font-weight: bold;\n}\n</style>
"""
        self.assertEqual(dedent(css).strip(), dedent(self.cssNode.output()).strip())
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
            
    def test_css_hash(self):
        self.assertEqual(self.css_hash, self.cssNode.hash)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)
#.........这里部分代码省略.........
开发者ID:wil,项目名称:django-css,代码行数:103,代码来源:tests.py

示例2: CompressorTestCase

# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import split_contents [as 别名]
class CompressorTestCase(TestCase):

    def setUp(self):
        settings.COMPRESS = True
        self.css = """
        <link rel="stylesheet" href="/media/css/one.css" type="text/css" charset="utf-8">
        <style type="text/css">p { border:5px solid green;}</style>
        <link rel="stylesheet" href="/media/css/two.css" type="text/css" charset="utf-8">
        """
        self.cssNode = CssCompressor(self.css)

        self.js = """
        <script src="/media/js/one.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" charset="utf-8">obj.value = "value";</script>
        """
        self.jsNode = JsCompressor(self.js)

    def test_css_split(self):
        out = [
            ('file', os.path.join(settings.MEDIA_ROOT, u'css/one.css'), '<link rel="stylesheet" href="/media/css/one.css" type="text/css" charset="utf-8" />'),
            ('hunk', u'p { border:5px solid green;}', '<style type="text/css">p { border:5px solid green;}</style>'),
            ('file', os.path.join(settings.MEDIA_ROOT, u'css/two.css'), '<link rel="stylesheet" href="/media/css/two.css" type="text/css" charset="utf-8" />'),
        ]
        split = self.cssNode.split_contents()
        split = [(x[0], x[1], str(x[2])) for x in split]
        self.assertEqual(out, split)

    def test_css_hunks(self):
        out = ['body { background:#990; }', u'p { border:5px solid green;}', 'body { color:#fff; }']
        self.assertEqual(out, self.cssNode.hunks)

    def test_css_output(self):
        out = u'body { background:#990; }\np { border:5px solid green;}\nbody { color:#fff; }'
        self.assertEqual(out, self.cssNode.combined)

    def test_css_mtimes(self):
        is_date = re.compile(r'^\d{10}\.\d$')
        for date in self.cssNode.mtimes:
            self.assert_(is_date.match(str(date)), "mtimes is returning something that doesn't look like a date")

    def test_css_return_if_off(self):
        settings.COMPRESS = False
        self.assertEqual(self.css, self.cssNode.output())

    def test_cachekey(self):
        is_cachekey = re.compile(r'django_compressor\.\w{12}')
        self.assert_(is_cachekey.match(self.cssNode.cachekey), "cachekey is returning something that doesn't look like r'django_compressor\.\w{12}'")

    def test_css_hash(self):
        self.assertEqual('f7c661b7a124', self.cssNode.hash)

    def test_css_return_if_on(self):
        output = u'<link rel="stylesheet" href="/media/CACHE/css/f7c661b7a124.css" type="text/css" media="all" charset="utf-8">'
        self.assertEqual(output, self.cssNode.output())


    def test_js_split(self):
        out = [('file', os.path.join(settings.MEDIA_ROOT, u'js/one.js'), '<script src="/media/js/one.js" type="text/javascript" charset="utf-8"></script>'),
         ('hunk', u'obj.value = "value";', '<script type="text/javascript" charset="utf-8">obj.value = "value";</script>')
         ]
        split = self.jsNode.split_contents()
        split = [(x[0], x[1], str(x[2])) for x in split]
        self.assertEqual(out, split)

    def test_js_hunks(self):
        out = ['obj = {};', u'obj.value = "value";']
        self.assertEqual(out, self.jsNode.hunks)

    def test_js_concat(self):
        out = u'obj = {};\nobj.value = "value";'
        self.assertEqual(out, self.jsNode.concat())

    def test_js_output(self):
        out = u'obj={};obj.value="value";'
        self.assertEqual(out, self.jsNode.combined)

    def test_js_return_if_off(self):
        settings.COMPRESS = False
        self.assertEqual(self.js, self.jsNode.output())

    def test_js_return_if_on(self):
        output = u'<script type="text/javascript" src="/media/CACHE/js/3f33b9146e12.js" charset="utf-8"></script>'
        self.assertEqual(output, self.jsNode.output())
开发者ID:fgallina,项目名称:wethemen,代码行数:85,代码来源:tests.py

示例3: CompressorTestCase

# 需要导入模块: from compressor import CssCompressor [as 别名]
# 或者: from compressor.CssCompressor import split_contents [as 别名]
class CompressorTestCase(TestCase):

    def setUp(self):
        settings.COMPRESS = True
        settings.COMPILER_FORMATS = {
            '.ccss': {
                'binary_path': 'python ' + os.path.join(django_settings.TEST_DIR,'clevercss.py'),
                'arguments': '*.ccss'
            },
            '.xcss': {
                'python':'clevercss.convert',
            },            
        }
        self.ccssFile = os.path.join(settings.MEDIA_ROOT, u'css/three.css')
        self.xcssFile = os.path.join(settings.MEDIA_ROOT, u'css/four.xcss')        
        self.css = dedent("""
        <link rel="stylesheet" href="/media/css/one.css" type="text/css">
        <style type="text/css">p { border:5px solid green;}</style>
        <link rel="stylesheet" href="/media/css/two.css" type="text/css">
        <link rel="stylesheet" href="/media/css/three.ccss" type="text/css">
        <link rel="stylesheet" href="/media/css/four.xcss" type="text/css">
        <style type="text/xcss">
        """)+ \
        '\n'.join((
        "small:\n  font-size:10px",
        '</style>\n<style type="xcss">',
        "h1:\n  font-weight:bold",
        "</style>"))

        self.cssNode = CssCompressor(self.css)

        self.js = """
        <script src="/media/js/one.js" type="text/javascript"></script>
        <script type="text/javascript">obj.value = "value";</script>
        """
        self.jsNode = JsCompressor(self.js)
    
    def test_get_filename(self):
        settings.COMPRESS_URL = '/static_test/'
        settings.COMPRESS_ROOT = os.path.join(os.path.dirname(__file__),'static_test_dir')
        path = os.path.join(settings.MEDIA_URL,'something.css')
        filename = self.cssNode.get_filename(path)
        self.assertEqual(filename,os.path.join(settings.MEDIA_ROOT,'something.css'))
        path = "http://something.com/static/something.css"
        self.assertRaises(UncompressableFileError, self.cssNode.get_filename,path)
        self.assertRaises(UncompressableFileError, self.jsNode.get_filename,path)
    
    def test_css_compiler_exists(self):
        settings.COMPILER_FORMATS = {
            '.ccss': {
                'binary_path': 'python ' + os.path.join(django_settings.TEST_DIR,'clevrcss.py'),
                'arguments': '*.ccss'
            },
        }            
        self.assertRaises(Exception, self.cssNode.output)

    def test_css_split(self):
        out = [
            {'elem': '<link rel="stylesheet" href="/media/css/one.css" type="text/css" />',
                'filename': os.path.join(settings.MEDIA_ROOT, u'css/one.css') },
            {'elem': '<style type="text/css">p { border:5px solid green;}</style>',
                'data': u'p { border:5px solid green;}' },
            {'elem': '<link rel="stylesheet" href="/media/css/two.css" type="text/css" />',
                'filename': os.path.join(settings.MEDIA_ROOT, u'css/two.css') },
            {'elem': '<link rel="stylesheet" href="/media/css/three.css" type="text/css" />',
                'filename': self.ccssFile, },
            {'elem': '<link rel="stylesheet" href="/media/css/four.xcss" type="text/css" />',
                'filename': self.xcssFile, 
                'data': u'p {\n  color: black;\n}' },
            {'elem': '<style type="text/xcss">\nsmall:\n  font-size:10px\n</style>',
                'data': u'small {\n  font-size: 10px;\n}' },
            {'elem': '<style type="xcss">\nh1:\n  font-weight:bold\n</style>',
                'data': u'h1 {\n  font-weight: bold;\n}' }
        ]
        split = self.cssNode.split_contents()
        for item in split:
            item['elem'] = str(item['elem'])        
        self.assertEqual(out, split)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_hunks(self):
        out = ['body { background:#990; }',
               'p { border:5px solid green;}', 
               'body { color:#fff; }', 
               'a {\n  color: #5c4032;\n}',
               'p {\n  color: black;\n}',
               'small {\n  font-size: 10px;\n}', 
               'h1 {\n  font-weight: bold;\n}'               
               ]
        self.assertEqual(out, self.cssNode.hunks)
        if os.path.exists(self.ccssFile):
            os.remove(self.ccssFile)

    def test_css_output(self):
        out = '\n'.join((
            'body { background:#990; }',
            'p { border:5px solid green;}',
            'body { color:#fff; }',
            'a {\n  color: #5c4032;\n}',
#.........这里部分代码省略.........
开发者ID:tymofij,项目名称:django-css,代码行数:103,代码来源:tests.py


注:本文中的compressor.CssCompressor.split_contents方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。