less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Embedded JavaScript and strings

Open SomMeri opened this issue 11 years ago • 2 comments

Strings returned from embedded JavaScript are handled differently when they are inside lists and out of them.

  • Escaped js removes quotes from returned strings whether they are inside the list or out of it.
  • Embedded js removes quotes from top level strings, but not from strings nested inside a list.
.strings {
  string-embedded: `'aaa'` `"bbb"`; 
  string-escaped: ~`'aaa'` ~`"bbb"`;
  strings-list-embedded: `['a', 'b', "c"]`;
  strings-list-escaped: ~`['a', 'b', "c"]`;
}

compiles into:

.strings {
  string-embedded: "aaa" "bbb";
  string-escaped: aaa bbb;
  strings-list-embedded: a, b, c;
  strings-list-escaped: a, b, c;
}

Is this how it is supposed to be? I am not sure how embedded js should really work. It make sense to strip them, because it is possible to add them from inside js and it would be impossible to return list of idents.

Then again, maybe 'aaa' "bbb" should not keep them there?

SomMeri avatar Apr 01 '14 09:04 SomMeri

I guess the escape. Is only working when you return a string, not a list. Is a list of strings ever going to occur in less? Ive no strong opinions though I wouldn't want to change strings list embedded as it would be a big breaking change. Guess that narrows down the options ...

lukeapage avatar May 04 '14 19:05 lukeapage

Strings returned from embedded JavaScript are handled differently when they are inside lists and out of them.

  • Escaped js removes quotes from returned strings whether they are inside the list or out of it.
  • Embedded js removes quotes from top level strings, but not from strings nested inside a list.
.strings {
  string-embedded: `'aaa'` `"bbb"`; 
  string-escaped: ~`'aaa'` ~`"bbb"`;
  strings-list-embedded: `['a', 'b', "c"]`;
  strings-list-escaped: ~`['a', 'b', "c"]`;
}

compiles into:

.strings {
  string-embedded: "aaa" "bbb";
  string-escaped: aaa bbb;
  strings-list-embedded: a, b, c;
  strings-list-escaped: a, b, c;
}

Is this how it is supposed to be? I am not sure how embedded js should really work. It make sense to strip them, because it is possible to add them from inside js and it would be impossible to return list of idents.

Then again, maybe 'aaa' "bbb" should not keep them there?

  • For the first point, the ~ operator needs to keep the output, remove the quotes. I think there is no problem with the uniform behavior.

  • For the second point, I thought of this situation,

font-family: `["Microsoft YaHei", "Arial Narrow", "sans-serif"]`;

This requires the quotes to be preserved. So I think our final output should be something like this.

.strings {
  string-embedded: "aaa" "bbb";
  string-escaped: aaa bbb;
  strings-list-embedded: "a", "b", "c";
  strings-list-escaped: a, b, c;
}

I have looked at the code and here is the minimal change to fix. @iChenLei @matthew-dean

lumburr avatar Jun 16 '22 07:06 lumburr