Permalink
Browse files

Workaround for less compiler issue #49 (duplicate lines)

  • Loading branch information...
1 parent 63e487a commit 6e44c2a77dc74dc888792ab606a2ac63f2fcf290 @vicb vicb committed with Aug 11, 2010
View
@@ -165,6 +165,12 @@ Last but not least, you can enable CSS compression (remove of whitespaces, tabs
sf_less_plugin:
use_compression: true
+In order to workaround [a flaw in the less compiler](http://github.com/cloudhead/less.js/issues#issue/49) you can use the following option:
+
+ all:
+ sf_less_plugin:
+ fix_duplicate: true
+
## Tasks ##
sfLESSPlugin server side compiler provides a set of CLI tasks to help compiling your LESS styles.
@@ -40,6 +40,15 @@ class LESSConfig
protected $compileClientSide = true;
/**
+ * Fix duplicate lines
+ *
+ * @see http://github.com/cloudhead/less.js/issues#issue/49
+ *
+ * @var boolean
+ */
+ protected $fixDuplicate = false;
+
+ /**
* Creates config instance
*
* @param boolean $checkDates whether we check dates before compile
@@ -140,6 +149,24 @@ public function setIsClientSideCompilation($clientSide)
}
/**
+ * @return boolean Wether to fix duplicate lines
+ */
+ public function getFixDuplicate()
+ {
+ return $this->fixDuplicate;
+ }
+
+ /**
+ * Enable fixing of duplicate lines
+ *
+ * @param boolean $clientSide Wether to fix duplicate line
+ */
+ public function setFixDuplicate($fix)
+ {
+ $this->fixDuplicate = $fix;
+ }
+
+ /**
* Returns debug info of the current state
*
* @return array state
@@ -153,4 +180,5 @@ public function getDebugInfo()
'css' => $this->getCssPaths()
);
}
+
}
@@ -65,4 +65,12 @@ public function isClientSideCompilation()
{
return sfConfig::get('app_sf_less_plugin_use_js', parent::isClientSideCompilation());
}
+
+ /**
+ * @see LESSConfig
+ */
+ public function getFixDuplicate()
+ {
+ return sfConfig::get('app_sf_less_plugin_fix_duplicate', parent::getFixDuplicate());
+ }
}
@@ -198,6 +198,12 @@ public function compile($lessFile)
*/
public function writeCssFile($cssFile, $buffer)
{
+ // Fix duplicate lines
+ if (self::getConfig()->getFixDuplicate())
+ {
+ $buffer = sfLESSUtils::fixDuplicateLines($buffer);
+ }
+
// Compress CSS if we use compression
if (self::getConfig()->isUseCompression())
{
@@ -140,5 +140,72 @@ static public function createFolderIfNeeded($file)
chmod(dirname($file), 0777);
}
}
-
+
+ /**
+ * Remove duplicate lines introduce by a flaw in the current less compiler.
+ * The CSS format is assumed to be non compressed lessc output
+ *
+ * @see http://github.com/cloudhead/less.js/issues#issue/49
+ *
+ * @param string $css CSS to be fixed
+ *
+ * @return string fixed CSS
+ */
+ static public function fixDuplicateLines($css)
+ {
+ $directives = array();
+ $inBlock = false;
+ $inComment = false;
+ $output = array();
+
+ preg_match_all('/.+/', $css, $lines);
+ foreach ($lines[0] as $line)
+ {
+ $active = '';
+ if (!$inComment)
+ {
+ $inComment = preg_match("#/\*#", $line);
+ $active = preg_replace("#/\*.*#", '', $line);
+ }
+
+ if ($inComment)
+ {
+ if (preg_match("#\*/#", $line))
+ {
+ $inComment = false;
+ $active .= preg_replace("#.*?\*/#", '', $line);
+ }
+ }
+
+ if (!$inBlock)
+ {
+ $output[] = $line;
+
+ if (preg_match('/{/', $active))
+ {
+ $inBlock = true;
+ $directives = array();
+ }
+ }
+ else
+ {
+ if ($inComment)
+ {
+ $output[] = $line;
+ }
+ elseif (!in_array($line, $directives))
+ {
+ $output[] = $line;
+ $directives[] = $line;
+ }
+
+ if (preg_match('/}/', $active))
+ {
+ $inBlock = false;
+ }
+ }
+ }
+ return join("\n", $output);
+ }
+
}

0 comments on commit 6e44c2a

Please sign in to comment.