wikimedia / less.php

Less.js ported to PHP. Mirrored from https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/
Apache License 2.0
111 stars 195 forks source link

Error in Compiling #96

Closed Ramishscrapper closed 9 months ago

Ramishscrapper commented 1 year ago

This is not compiling

@width: 960px;
.nav {
  width: @width / 3;
  color: #001 + #abc;
}
.body {
  width: 2 * @width / 3;
  font-family: "hel" + "vetica";
}

it give me this error

fatal error: ParseError: Unexpected input in anonymous-file-0.less on line 8, column 16
.body {
  width: 2 * @width / 3;
  font-family: \"hel\" + \"vetica\";
}

also getting here in this

.clearfix {
  display: block;
  zoom: 1;

  &:after {
    content: " ";
    display: block;
    font-size: 0;
    height: 0;
    clear: both;
    visibility: hidden;
  }
}

this is the error I get

fatal error: ParseError: Unexpected input in anonymous-file-0.less on line 6, column 14 
   &:after {
     content: \" \";
     display: block;
     font-size: 0;
     height: 0;

this is how I used php

try {
    $parser = new Less_Parser();
    $parser->parse($lessCode);
    return $parser->getCss();
  } catch (exception $e) {
    return "fatal error: " . $e->getMessage();
}
Krinkle commented 1 year ago

This is not compiling

@width: 960px;
.nav {
  width: @width / 3;
  color: #001 + #abc;
}
.body {
  width: 2 * @width / 3;
  font-family: "hel" + "vetica";
}

it give me this error

fatal error: ParseError: Unexpected input in anonymous-file-0.less on line 8, column 16
.body {
  width: 2 * @width / 3;
  font-family: \"hel\" + \"vetica\";
}

That's right. This kind of string concatenation is not supported by the Less standard. If you try the reference implementation in JavaScript, we see much the same error:

Both give the same result:

Parse error: Unrecognised input
Line 8:   font-family: "hel" + "vetica";

I suggest using e() and/or %() instead. Refer to the docs at https://lesscss.org/functions/#string-functions-e.

Example:

div {
  font-family: %("%s%s", "hel", "vetica");
  font-family: e(%("%s%s", "hel", "vetica"));
}
div {
  font-family: "helvetica";
  font-family: helvetica;
}
Krinkle commented 1 year ago

also getting here in this

.clearfix {
  display: block;
  zoom: 1;

  &:after {
    content: " ";
    display: block;
    font-size: 0;
    height: 0;
    clear: both;
    visibility: hidden;
  }
}

this is the error I get

fatal error: ParseError: Unexpected input in anonymous-file-0.less on line 6, column 14 

I'm not able to reproduce this.

$ bin/lessc my-input.less

.clearfix {
  display: block;
  zoom: 1;
}
.clearfix:after {
  content: " ";
  display: block;
  font-size: 0;
  height: 0;
  clear: both;
  visibility: hidden;
}

Similarly, using the programmatic API:


$ php -a
php > require_once 'vendor/autoload.php';
php > $parser = new Less_Parser();
php > $parser->parse('.clearfix {  display: block;  zoom: 1;  &:after {    content: " ";    display: block;    font-size: 0;    height: 0;    clear: both;    visibility: hidden;  }} ');
php > print $parser->getCss();
.clearfix {
  display: block;
  zoom: 1;
}
.clearfix:after {
  content: " ";
  display: block;
  font-size: 0;
  height: 0;
  clear: both;
  visibility: hidden;
}