Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2fb4296

Browse files
committed
Added library functions & better string handling
quote() and unquote() format functions can remove and add quotes around the arguments passed to them. eg. say you have some complex css syntax that can't be parsed, you can use string operations and unquote() to do a raw output. @color: #aabbcc; some-property: unquote("[doSomething color: {@color}]"); outputs: some-property: [doSomething color: #aabbcc]; If you subclass lessc then you can add your own functions, prefix them with lib_ to make them visible. Take a look at the implementations of lib_quote and lib_unquote as an example (they are very short). Improved string parsing and concatenation
1 parent 25b17ba commit 2fb4296

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

‎lessc.inc.php‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ function accessor(&$var) {
431431
// a string
432432
function string(&$string, &$d = null) {
433433
$s = $this->seek();
434-
if ($this->literal('"')) {
434+
if ($this->literal('"', false)) {
435435
$delim = '"';
436-
} else if($this->literal("'")) {
436+
} else if($this->literal("'", false)) {
437437
$delim = "'";
438438
} else {
439439
return false;
@@ -754,13 +754,36 @@ function compileValue($value) {
754754
case 'function':
755755
// [1] - function name
756756
// [2] - some value representing arguments
757+
758+
// see if there is a library function for this func
759+
$f = array($this, 'lib_'.$value[1]);
760+
if (is_callable($f)) {
761+
return call_user_func($f, $value[2]);
762+
}
763+
757764
return $value[1].'('.$this->compileValue($value[2]).')';
758765

759766
default: // assumed to be unit
760767
return $value[1].$value[0];
761768
}
762769
}
763770

771+
function lib_quote($arg) {
772+
return '"'.$this->compileValue($arg).'"';
773+
}
774+
775+
function lib_unquote($arg) {
776+
$out = $this->compileValue($arg);
777+
if ($this->quoted($out)) $out = substr($out, 1, -1);
778+
return $out;
779+
}
780+
781+
// is a string surrounded in quotes? returns the quoting char if true
782+
function quoted($s) {
783+
if (preg_match('/^("|\').*?1円$/', $s, $m))
784+
return $m[1];
785+
else return false;
786+
}
764787

765788
// convert rgb, rgba into color type suitable for math
766789
// todo: add hsl
@@ -837,18 +860,12 @@ function evaluate($op, $left, $right) {
837860

838861
// concatenate strings
839862
if ($op == '+' && $left[0] == 'string') {
840-
// todo: normalize string quotes
841863
$append = $this->compileValue($right);
842-
if ($right[0] == 'string' && ($append{0} == '"' || $append{0} == "'")) {
843-
$append = substr($append, 1, -1);
844-
}
864+
if ($this->quoted($append)) $append = substr($append, 1, -1);
845865

846866
$lhs = $this->compileValue($left);
847-
$q = '';
848-
if ($left[0] == 'string' && ($lhs{0} == '"' || $lhs{0} == "'")) {
849-
$q = $lhs{0};
850-
$lhs = substr($lhs, 1, -1);
851-
}
867+
if ($q = $this->quoted($lhs)) $lhs = substr($lhs, 1, -1);
868+
if (!$q) $q = '';
852869

853870
return array('string', $q.$lhs.$append.$q);
854871
}
@@ -1159,7 +1176,7 @@ function parse($str = null) {
11591176
if (count($this->env) > 1)
11601177
throw new exception('parse error: unclosed block');
11611178

1162-
//print_r($this->env);
1179+
//print_r($this->env);
11631180
return $out;
11641181
}
11651182

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /