douguangxiang0363 2011-10-27 20:40
浏览 52
已采纳

如何将css url路径移动到其他文件夹时重新计算

What is the best way to translate css so that it works in a different path. I've looked at a few different libraries (CrushCSS, minify) and they seem to only over this service along with compressing the files (which I don't want to do).

Is my best a regex? If so what are all the possible cases that I will need to handle?

For example, input: /base/my.css

@import url "../another/file.css"
#container {
    background: url('example/relative/path.png');
}
#container2 {
    background: url('/example/absolute/path.png');
}

Expected output: /base/example/path/my.css

@import url "../../../another/file.css"
#container {
    background: url('../relative/path.png');
}
#container2 {
    background: url('/example/absolute/path.png');
}

EDIT:

I'm not interested in "best practice" or ways to avoid this type of problem. I'm only interested in how to do the transformation so that the css remains correct in the new path.

  • 写回答

2条回答 默认 最新

  • dsfsdfsdfsdf6455 2011-10-28 18:04
    关注

    I decided that I would refactor some code from Minify to allow for the abstract css transformation I needed.

    <?php
    
    //This class is heavy borrowed from Minify_ImportProcessor
    
    class CSSImporter
    {
    
        static function changePaths( $content, $current_path, $target_path )
        {
            $current_path = rtrim( $current_path, "/" );
            $target_path = rtrim( $target_path, "/" );
            $current_path_slugs = explode( "/", $current_path );
            $target_path_slugs = explode( "/", $target_path );
            $smallest_count = min( count( $current_path_slugs ), count( $target_path_slugs ) );
            for( $i = 0; $i < $smallest_count && $current_path_slugs[$i] === $target_path_slugs[$i]; $i++ );
            $change_prefix = implode( "/", array_merge( array_fill( 0, count( $target_path_slugs ) - $i, ".." ), array_slice( $current_path_slugs, $i ) ) );
            if( strlen( $change_prefix ) > 0 ) $change_prefix .= "/";
    
            $content = preg_replace_callback(
                '/
                @import\\s+
                (?:url\\(\\s*)?     # maybe url(
                [\'"]?              # maybe quote
                (.*?)               # 1 = URI
                [\'"]?              # maybe end quote
                (?:\\s*\\))?        # maybe )
                ([a-zA-Z,\\s]*)?    # 2 = media list
                ;                   # end token
                /x',
                function( $m ) use ( $change_prefix ) {
                    $url = $change_prefix.$m[1];
                    $url = str_replace('/./', '/', $url);
                    do {
                        $url = preg_replace('@/(?!\\.\\.?)[^/]+/\\.\\.@', '/', $url, 1, $changed);
                    } while( $changed );
                    return "@import url('$url'){$m[2]};";
                },
                $content
            );
            $content = preg_replace_callback(
                '/url\\(\\s*([^\\)\\s]+)\\s*\\)/',
                function( $m ) use ( $change_prefix ) {
                    // $m[1] is either quoted or not
                    $quote = ($m[1][0] === "'" || $m[1][0] === '"')
                        ? $m[1][0]
                        : '';
                    $url = ($quote === '')
                        ? $m[1]
                        : substr($m[1], 1, strlen($m[1]) - 2);
    
                    if( '/' !== $url[0] && strpos( $url, '//') === FALSE ) {
                        $url = $change_prefix.$url;
                        $url = str_replace('/./', '/', $url);
                        do {
                            $url = preg_replace('@/(?!\\.\\.?)[^/]+/\\.\\.@', '/', $url, 1, $changed);
                        } while( $changed );
                    }
                    return "url({$quote}{$url}{$quote})";
                },
                $content
            );
            return $content;
        }
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多