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

Variable defined with "replace()" result does weird things when interpolated into a selector

Open Pointy opened this issue 12 years ago • 2 comments

Here's the test case:

.foo(@s) {
  @p: replace(@s, "^(.).*$", "$1");

  .{@p}-foo { content: "@{p}"; }
}
.foo(abcdefg);

The result of that call to the .foo() mixin is (via 1.7.4 lessc):

.aaa-foo {
    content: "a";
}

If, however, I change (slightly) the definition of the @p, things get better:

.foo(@s) {
  @p: e(replace(@s, "^(.).*$", "$1"));

  .{@p}-foo { content: "@{p}"; }
}
.foo(abcdefg);

That generates the less-surprising

.a-foo {
    content: "a";
}

I don't know why e() fixes things, but it doesn't seem to be necessary in order to get @{p} to produce the right value when it's used in the rule body.

Pointy avatar Nov 24 '14 17:11 Pointy

Confirmed. Though technically by now replace only officially supports real strings as first argument (e.g. "string", 'string', ~"string" etc.), just string with no quotes is a different non-string type so it may result in some weirdness (in this particular example the value returned by replace is just constructed incorrectly, you could see it if you use just @p as a value, but additional interpolation within another string hides this weirdness). A workaround for your example is to use ~"abcdefg" instead of abcdefg.

Either way I guess this can be considered like a semi-bug/semi-feature-request ("replace improvement") and it's better to be fixed eventually.

seven-phases-max avatar Nov 24 '14 18:11 seven-phases-max

Btw. curiously - the tests do include Keyword as an input type and it passes... I.e. bogus comes to effect only when replace is inside a mixin... e.g.:

.1 {
    1: replace(bar-1, "1", "2"); // OK:  bar-2
    .2();                        // Err: bbar-2b
}

.2 {
    1: replace(bar-1, "1", "2");
}

seven-phases-max avatar Nov 24 '14 23:11 seven-phases-max