示例#1
0
 def test_set(self):
     "settings.set()"
     cssutils.ser.prefs.useMinified()
     text = 'a {filter: progid:DXImageTransform.Microsoft.BasicImage( rotation = 90 )}'
     
     self.assertEqual(cssutils.parseString(text).cssText, '')
     
     from cssutils import settings
     settings.set('DXImageTransform.Microsoft', True)
     self.assertEqual(cssutils.parseString(text).cssText,
                      'a{filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=90)}')
     
     cssutils.ser.prefs.useDefaults()
示例#2
0
    def filter_file_content(self, file_content, path):
        """URLs are made relative to the target and the CSS is minified."""
        if self.rewrite_urls:
            src_dir = os.path.dirname(path)
            relative_src_dir = relative_path(self.target_file, src_dir)
            if relative_src_dir == ".":
                relative_parts = []
            else:
                relative_parts = relative_src_dir.split(os.path.sep)

            def fix_relative_url(match):
                url = match.group(1)
                # Don't modify absolute URLs or 'data:' urls.
                if (url.startswith("http") or url.startswith("/")
                        or url.startswith("data:")):
                    return match.group(0)
                parts = relative_parts + url.split("/")
                result = []
                for part in parts:
                    if part == ".." and result and result[-1] != "..":
                        result.pop(-1)
                        continue
                    result.append(part)
                return "url(%s)" % "/".join(
                    filter(None, [self.resource_prefix] + result))

            file_content = URL_RE.sub(fix_relative_url, file_content)

        if self.minify:
            old_serializer = cssutils.ser
            cssutils.setSerializer(cssutils.serialize.CSSSerializer())
            try:
                cssutils.ser.prefs.useMinified()

                stylesheet = ESCAPE_STAR_PROPERTY_RE.sub(
                    r'\1_ie_star_hack:', file_content)
                settings.set('DXImageTransform.Microsoft', True)
                parser = cssutils.CSSParser(raiseExceptions=True)
                css = parser.parseString(stylesheet)
                stylesheet = UNESCAPE_STAR_PROPERTY_RE.sub(
                    r'*\1:', css.cssText)
                return stylesheet + "\n"
            finally:
                cssutils.setSerializer(old_serializer)
        return file_content + "\n"
示例#3
0
    def filter_file_content(self, file_content, path):
        """URLs are made relative to the target and the CSS is minified."""
        if self.rewrite_urls:
            src_dir = os.path.dirname(path)
            relative_parts = relative_path(self.target_file, src_dir).split(
                os.path.sep)

            def fix_relative_url(match):
                url = match.group(1)
                # Don't modify absolute URLs or 'data:' urls.
                if (url.startswith("http") or
                    url.startswith("/") or
                    url.startswith("data:")):
                    return match.group(0)
                parts = relative_parts + url.split("/")
                result = []
                for part in parts:
                    if part == ".." and result and result[-1] != "..":
                        result.pop(-1)
                        continue
                    result.append(part)
                return "url(%s)" % "/".join(
                    filter(None, [self.resource_prefix] + result))
            file_content = URL_RE.sub(fix_relative_url, file_content)

        if self.minify:
            old_serializer = cssutils.ser
            cssutils.setSerializer(cssutils.serialize.CSSSerializer())
            try:
                cssutils.ser.prefs.useMinified()

                stylesheet = ESCAPE_STAR_PROPERTY_RE.sub(
                    r'\1_ie_star_hack:', file_content)
                settings.set('DXImageTransform.Microsoft', True)
                parser = cssutils.CSSParser(raiseExceptions=True)
                css = parser.parseString(stylesheet)
                stylesheet = UNESCAPE_STAR_PROPERTY_RE.sub(
                    r'*\1:', css.cssText)
                return stylesheet + "\n"
            finally:
                cssutils.setSerializer(old_serializer)
        return file_content + "\n"
示例#4
0
import os
import sys
import logging
import optparse
import re

url_regex = re.compile(r'url\s*\(\s*([\'\"]?)([^\)\'\"]+)(\1)\s*\)')

LIB_HOME = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'lib')
sys.path.insert(0, os.path.join(LIB_HOME, 'google-closure-linter'))
sys.path.insert(0, os.path.join(LIB_HOME, 'cssutils'))

import cssutils
cssutils.log.setLevel(logging.ERROR)
from cssutils import settings
settings.set('DXImageTransform.Microsoft', True)
__author__ = 'leeight <*****@*****.**>'
__date__ = '2012/01/26 11:05:14'
__revision = '$Revision$'

class CssRewriter(object):
  def __init__(self, root, assets_manager, ignore_depth = False):
    """
    root是服务器的根目录路径,用来处理background:url(/assets/img/a.png)的情况
    assets_manager是资源管理器,用开拷贝文件.
    ignore_depth忽略资源URI前面的../和./
    """
    self._root = root
    self._am = assets_manager
    self._ignore_depth = ignore_depth