michanismus / cssmin

Automatically exported from code.google.com/p/cssmin
0 stars 0 forks source link

Support for at-rule @import #16

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I already wrote a CSS minifier by myself including this functionality. It loops 
through a filelist, reads the content line by line and recursively reads the 
content of all @import lines. Additionally it corrects all of the relative urls.
Perhaps you can take over my code with the changes you need in order to adjust 
it to your project. The only thing what I want to ask for is to mention my name 
and website (http://danielvogt.info) to the comment area of your file. Here are 
the methods that I use:

protected function parseRecursive($fileName)
{
    $output = '';
    $content = file_get_contents($fileName);
    // all line breaks to \n
    $content = str_replace("\r\n", "\n", $content);
    $content = str_replace("\r", "\n", $content);
    // read line by line and parse the @include calls
    $contentLines = explode("\n", $content);
    foreach ($contentLines as $line) {
        $line = trim($line);
        $line = $this->correctRelativeUrls($fileName, $line);
        if (stristr($line, '@import url(')) {
            $urlToParse = self::stringFromTo($line, 'url(', ')');
            $line = $this->parseRecursive($urlToParse);
        }
        $output .= $line;
    }
    return $output;
}

protected function correctRelativeUrls($fileName, $line)
{
    if (!stristr($line, 'url(')) {
        return $line;
    }
    $relPath = trim($this->stringFromTo($line, 'url(', ')'));
    if (substr($relPath, 0, 1)=='/') {
        $absoluteFilePath = $relPath;
    } else {
        $dir = dirname($fileName);
        $absoluteFilePath = $dir.'/'.$relPath;
    }
    $realPath = str_replace("\\", '/', realpath($absoluteFilePath));
    if (stristr($line, '@import url(')) {
        return str_replace($relPath, $realPath, $line);
    }
    return str_replace($relPath, str_replace($_SERVER['DOCUMENT_ROOT'], '', $realPath), $line);
}

public static function stringFromTo ($string, $from, $to = "")
{
    if ($from == "") {
        $start = 0;
    } else {
        $start = strpos($string, $from);
        if ($start !== false) {
            $start += strlen($from);
        }
    }
    if ($to == "") {
        $end = strlen($string) + 1;
    } else {
        $end = strpos($string, $to, $start);
    }
    if ($end === false) {
        $end = strlen($string) + 1;
    }
    return substr($string, $start, $end - $start);
}

And here is the loop through the files:

$output = '';
foreach ($fileList as $file) {
    $output .= $this->parseRecursive($file);
}

Best,
Daniel Vogt

Original issue reported on code.google.com by danielvogt61@gmail.com on 30 Sep 2010 at 12:41

GoogleCodeExporter commented 8 years ago
This feature is already planned (as listed on 
http://code.google.com/p/cssmin/wiki/Planned) but on low priority.

Original comment by joe.scylla on 30 Sep 2010 at 1:55

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I'm working on this feature now. The implementation is quite difficult. This 
feature will require a new configuration parameter defining the base url for 
all imports. Every path defined in the @import at-rule has to be based on the 
base url (as also @import at-rule in any imported css files).

Most of the business logic will deal with the media type handling. If the 
@import at-rule define one or more media types the included tokens have to get 
wraped into a T_AT_MEDIA_START and T_AT_MEDIA_END but the wrapping have to 
exclude @font-face, @media and @page at-rule blocks and @import at-rules. 
Additional to simulate the browser handling @media at-rule blocks with media 
types not defined in the @import at-rule have to get removed because every 
tested browser (IE, FF, Safari, Chrome and Opera) will ignore these blocks.

Original comment by joe.scylla on 27 Jan 2011 at 2:39

GoogleCodeExporter commented 8 years ago
Implemented with http://code.google.com/p/cssmin/source/detail?r=83

Original comment by joe.scylla on 1 Feb 2011 at 9:46