DotLess LessCss编译器与WebEssentials LessCss编译器不同

时间:2014-10-28 18:16:21

标签: c# css less dotless

我使用dotless在运行时解析LessCss。这大部分都是成功的,但我有一个场景,它没有按预期工作。

鉴于以下LessCss:

@tileWidth: 50px;
@tileMarginX: 5px;
@gridWidth: 2;

// recursive less to generate all x positions down to 1
.position-x(@maxIndex) when (@maxIndex > 0) {
    [data-col="@{maxIndex}"] {
        left: ((@maxIndex - 1) * @tileWidth) + (@tileMarginX * ((@maxIndex * 2) - 1));
    }

    .position-x(@maxIndex - 1);
}

.position-x(@gridWidth);

WebEssentials 2013 Update 3将编译为:

[data-col="2"] {
  left: 65px;
}
[data-col="1"] {
  left: 5px;
}

LessEngine.TransformToCss将输出:

[data-col="@{maxIndex}"] {
    left: 65px
}    
[data-col="@{maxIndex}"] {
    left: 5px
}

DotLess不支持此语法吗?
如何更改Less代码以获得预期的输出?

1 个答案:

答案 0 :(得分:2)

根据https://github.com/dotless/dotless/issues/395 dotless,不支持在属性选择器中进行插值,因此您只需要在变量中“隐藏”该属性,例如:

@tileWidth: 50px;
@tileMarginX: 5px;
@gridWidth: 2;

.position-x(@n) when (@n > 0) {
    @attr: ~'[data-col="@{n}"]';
    @{attr} {
        left: (@n - 1) * @tileWidth + (2 * @n - 1) * @tileMarginX;
    }

    .position-x(@n - 1);
}

.position-x(@gridWidth);