本文整理汇总了Python中compressor.css.CssCompressor类的典型用法代码示例。如果您正苦于以下问题:Python CssCompressor类的具体用法?Python CssCompressor怎么用?Python CssCompressor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CssCompressor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_filename_in_debug_mode
def test_filename_in_debug_mode(self):
# In debug mode, compressor should look for files using staticfiles
# finders only, and not look into the global static directory, where
# files can be outdated
css_filename = os.path.join(settings.COMPRESS_ROOT, "css", "one.css")
# Store the hash of the original file's content
with open(css_filename) as f:
css_content = f.read()
hashed = get_hexdigest(css_content, 12)
# Now modify the file in the STATIC_ROOT
test_css_content = "p { font-family: 'test' }"
with open(css_filename, "a") as css:
css.write("\n")
css.write(test_css_content)
# We should generate a link with the hash of the original content, not
# the modified one
expected = '<link rel="stylesheet" href="/static/CACHE/css/%s.css" type="text/css" />' % hashed
compressor = CssCompressor(self.css)
compressor.storage = DefaultStorage()
output = compressor.output()
self.assertEqual(expected, output)
with open(os.path.join(settings.COMPRESS_ROOT, "CACHE", "css",
"%s.css" % hashed), "r") as f:
result = f.read()
self.assertTrue(test_css_content not in result)
示例2: render
def render(self, context, compress=settings.COMPRESS, offline=settings.OFFLINE):
if compress and offline:
key = get_offline_cachekey(self.nodelist)
content = cache.get(key)
if content:
return content
content = self.nodelist.render(context)
if offline or not compress or not len(content.strip()):
return content
if self.kind == 'css':
compressor = CssCompressor(content)
if self.kind == 'js':
compressor = JsCompressor(content)
cachekey = "%s-%s" % (compressor.cachekey, self.mode)
output = self.cache_get(cachekey)
if output is None:
try:
if self.mode == OUTPUT_FILE:
output = compressor.output()
else:
output = compressor.output_inline()
self.cache_set(cachekey, output)
except:
from traceback import format_exc
raise Exception(format_exc())
return output
示例3: test_css_output_with_bom_input
def test_css_output_with_bom_input(self):
out = 'body { background:#990; }\n.compress-test {color: red;}'
css = ("""<link rel="stylesheet" href="/static/css/one.css" type="text/css" />
<link rel="stylesheet" href="/static/css/utf-8_with-BOM.css" type="text/css" />""")
css_node_with_bom = CssCompressor(css)
hunks = '\n'.join([h for h in css_node_with_bom.hunks()])
self.assertEqual(out, hunks)
示例4: test_css_hunks
def test_css_hunks(self):
hash_python_png = self.hashing_func(os.path.join(settings.COMPRESS_ROOT, 'img/python.png'))
hash_add_png = self.hashing_func(os.path.join(settings.COMPRESS_ROOT, 'img/add.png'))
css1 = """\
p { background: url('%(compress_url)simg/python.png?%(hash)s'); }
p { background: url(%(compress_url)simg/python.png?%(hash)s); }
p { background: url(%(compress_url)simg/python.png?%(hash)s); }
p { background: url('%(compress_url)simg/python.png?%(hash)s'); }
p { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='%(compress_url)simg/python.png?%(hash)s'); }
""" % dict(compress_url=self.expected_url_prefix, hash=hash_python_png)
css2 = """\
p { background: url('%(compress_url)simg/add.png?%(hash)s'); }
p { background: url(%(compress_url)simg/add.png?%(hash)s); }
p { background: url(%(compress_url)simg/add.png?%(hash)s); }
p { background: url('%(compress_url)simg/add.png?%(hash)s'); }
p { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='%(compress_url)simg/add.png?%(hash)s'); }
""" % dict(compress_url=self.expected_url_prefix, hash=hash_add_png)
css = """
<link rel="stylesheet" href="/static/css/url/url1.css" type="text/css">
<link rel="stylesheet" href="/static/css/url/2/url2.css" type="text/css">
"""
css_node = CssCompressor(css)
self.assertEqual([css1, css2], list(css_node.hunks()))
示例5: helper
def helper(self, enabled, use_precompiler, use_absolute_filter, expected_output):
precompiler = (('text/css', 'compressor.tests.test_base.PassthroughPrecompiler'),) if use_precompiler else ()
filters = ('compressor.filters.css_default.CssAbsoluteFilter',) if use_absolute_filter else ()
with self.settings(COMPRESS_ENABLED=enabled, COMPRESS_PRECOMPILERS=precompiler, COMPRESS_CSS_FILTERS=filters):
css_node = CssCompressor(self.html_orig)
output = list(css_node.hunks())[0]
self.assertEqual(output, expected_output)
示例6: PostCompressSignalTestCase
class PostCompressSignalTestCase(TestCase):
def setUp(self):
settings.COMPRESS_ENABLED = True
settings.COMPRESS_PRECOMPILERS = {}
settings.COMPRESS_DEBUG_TOGGLE = 'nocompress'
self.css = """\
<link rel="stylesheet" href="/static/css/one.css" type="text/css" />
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css" />"""
self.css_node = CssCompressor(self.css)
self.js = """\
<script src="/static/js/one.js" type="text/javascript"></script>
<script type="text/javascript">obj.value = "value";</script>"""
self.js_node = JsCompressor(self.js)
def tearDown(self):
post_compress.disconnect()
def test_js_signal_sent(self):
def listener(sender, **kwargs):
pass
callback = Mock(wraps=listener)
post_compress.connect(callback)
self.js_node.output()
args, kwargs = callback.call_args
self.assertEquals(JsCompressor, kwargs['sender'])
self.assertEquals('js', kwargs['type'])
self.assertEquals('file', kwargs['mode'])
context = kwargs['context']
assert 'url' in context['compressed']
def test_css_signal_sent(self):
def listener(sender, **kwargs):
pass
callback = Mock(wraps=listener)
post_compress.connect(callback)
self.css_node.output()
args, kwargs = callback.call_args
self.assertEquals(CssCompressor, kwargs['sender'])
self.assertEquals('css', kwargs['type'])
self.assertEquals('file', kwargs['mode'])
context = kwargs['context']
assert 'url' in context['compressed']
def test_css_signal_multiple_media_attributes(self):
css = """\
<link rel="stylesheet" href="/static/css/one.css" media="handheld" type="text/css" />
<style type="text/css" media="print">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css" />"""
css_node = CssCompressor(css)
def listener(sender, **kwargs):
pass
callback = Mock(wraps=listener)
post_compress.connect(callback)
css_node.output()
self.assertEquals(3, callback.call_count)
示例7: test_avoid_reordering_css
def test_avoid_reordering_css(self):
css = self.css + '<style type="text/css" media="print">p { border:10px solid red;}</style>'
css_node = CssCompressor(css)
media = ['screen', 'print', 'all', None, 'print']
if six.PY3:
links = make_soup(css_node.output()).find_all('link')
else:
links = make_soup(css_node.output()).findAll('link')
self.assertEqual(media, [l.get('media', None) for l in links])
示例8: test_css_output
def test_css_output(self):
css_node = CssCompressor(self.css)
if six.PY3:
links = make_soup(css_node.output()).find_all('link')
else:
links = make_soup(css_node.output()).findAll('link')
media = ['screen', 'print', 'all', None]
self.assertEqual(len(links), 4)
self.assertEqual(media, [l.get('media', None) for l in links])
示例9: test_passthough_when_compress_disabled
def test_passthough_when_compress_disabled(self):
css = """\
<link rel="stylesheet" href="/static/css/one.css" type="text/css" media="screen">
<link rel="stylesheet" href="/static/css/two.css" type="text/css" media="screen">
<style type="text/foobar" media="screen">h1 { border:5px solid green;}</style>"""
css_node = CssCompressor(css)
output = make_soup(css_node.output()).find_all(['link', 'style'])
self.assertEqual(['/static/css/one.css', '/static/css/two.css', None],
[l.get('href', None) for l in output])
self.assertEqual(['screen', 'screen', 'screen'],
[l.get('media', None) for l in output])
示例10: test_precompiler_class_used
def test_precompiler_class_used(self):
try:
original_precompilers = settings.COMPRESS_PRECOMPILERS
settings.COMPRESS_ENABLED = True
settings.COMPRESS_PRECOMPILERS = (("text/foobar", "compressor.tests.test_base.TestPrecompiler"),)
css = '<style type="text/foobar">p { border:10px solid red;}</style>'
css_node = CssCompressor(css)
output = BeautifulSoup(css_node.output("inline"))
self.assertEqual(output.text, "OUTPUT")
finally:
settings.COMPRESS_PRECOMPILERS = original_precompilers
示例11: test_css_signal_multiple_media_attributes
def test_css_signal_multiple_media_attributes(self):
css = """\
<link rel="stylesheet" href="/media/css/one.css" media="handheld" type="text/css" />
<style type="text/css" media="print">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css" />"""
css_node = CssCompressor(css)
def listener(sender, **kwargs):
pass
callback = Mock(wraps=listener)
post_compress.connect(callback)
css_node.output()
self.assertEquals(3, callback.call_count)
示例12: test_css_single
def test_css_single(self):
css_node = CssCompressor("""<link rel="stylesheet" href="/media/css/one.css" type="text/css" />""")
css_node.opts = {"group_first": "true"}
out = [
[
SOURCE_FILE,
os.path.join(settings.COMPRESS_ROOT, u"css", u"one.css"),
u"css/one.css",
u'<link rel="stylesheet" href="/media/css/one.css" type="text/css" />',
]
]
split = css_node.group_contents()
split = [[x[0], x[1], x[2], make_elems_str(self.css_node.parser, x[3])] for x in split]
self.assertEqual(out, split)
示例13: test_passthough_when_compress_disabled
def test_passthough_when_compress_disabled(self):
original_precompilers = settings.COMPRESS_PRECOMPILERS
settings.COMPRESS_ENABLED = False
settings.COMPRESS_PRECOMPILERS = (
("text/foobar", "python %s {infile} {outfile}" % os.path.join(test_dir, "precompiler.py")),
)
css = """\
<link rel="stylesheet" href="/media/css/one.css" type="text/css" media="screen">
<link rel="stylesheet" href="/media/css/two.css" type="text/css" media="screen">
<style type="text/foobar" media="screen">h1 { border:5px solid green;}</style>"""
css_node = CssCompressor(css)
output = BeautifulSoup(css_node.output()).findAll(["link", "style"])
self.assertEqual([u"/media/css/one.css", u"/media/css/two.css", None], [l.get("href", None) for l in output])
self.assertEqual([u"screen", u"screen", u"screen"], [l.get("media", None) for l in output])
settings.COMPRESS_PRECOMPILERS = original_precompilers
示例14: Html5LibParserTests
class Html5LibParserTests(ParserTestCase, CompressorTestCase):
parser_cls = 'compressor.parser.Html5LibParser'
def setUp(self):
super(Html5LibParserTests, self).setUp()
# special version of the css since the parser sucks
self.css = """\
<link href="/media/css/one.css" rel="stylesheet" type="text/css">
<style type="text/css">p { border:5px solid green;}</style>
<link href="/media/css/two.css" rel="stylesheet" type="text/css">"""
self.css_node = CssCompressor(self.css)
def test_css_split(self):
out = [
(SOURCE_FILE, os.path.join(settings.COMPRESS_ROOT, u'css', u'one.css'), u'css/one.css', u'<link href="/media/css/one.css" rel="stylesheet" type="text/css">'),
(SOURCE_HUNK, u'p { border:5px solid green;}', None, u'<style type="text/css">p { border:5px solid green;}</style>'),
(SOURCE_FILE, os.path.join(settings.COMPRESS_ROOT, u'css', u'two.css'), u'css/two.css', u'<link href="/media/css/two.css" rel="stylesheet" type="text/css">'),
]
split = self.css_node.split_contents()
split = [(x[0], x[1], x[2], self.css_node.parser.elem_str(x[3])) for x in split]
self.assertEqual(out, split)
def test_js_split(self):
out = [
(SOURCE_FILE, os.path.join(settings.COMPRESS_ROOT, u'js', u'one.js'), u'js/one.js', u'<script src="/media/js/one.js" type="text/javascript"></script>'),
(SOURCE_HUNK, u'obj.value = "value";', None, u'<script type="text/javascript">obj.value = "value";</script>'),
]
split = self.js_node.split_contents()
split = [(x[0], x[1], x[2], self.js_node.parser.elem_str(x[3])) for x in split]
self.assertEqual(out, split)
示例15: setUp
def setUp(self):
self.css = """\
<link rel="stylesheet" href="/media/css/one.css" type="text/css" media="screen">
<style type="text/css" media="print">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/media/css/two.css" type="text/css" media="all">
<style type="text/css">h1 { border:5px solid green;}</style>"""
self.css_node = CssCompressor(self.css)