简体   繁体   English

在LESS CSS中使用通用选择器作为混合

[英]Using a general purpose selector as a mixin in LESS CSS

I know about mixins and parametric mixins. 我知道mixin和参数mixin。 What we are looking for is a way to make any general purpose selectors in CSS / LESS into a mixin. 我们正在寻找一种将CSS / LESS中的通用选择器转换为mixin的方法。

Eg in Twitter BootStrap , we have here 例如在Twitter BootStrap中 ,我们在这里

.navbar .nav > li {
  float: left;
} 

If I have to use it in a class say .mynavbar I want to be able to do this 如果我必须在课堂上说.mynavbar,我希望能够做到这一点

INPUT->
.mynavbar {
  .navbar .nav >li;
}

OUTPUT->
.mynavbar {
  float:left;
}

Now I know this can't be done with the current version of LESS as the compiler flags a parser error. 现在,我知道使用LESS的当前版本无法完成此操作,因为编译器会标记解析器错误。 I wanted someone to help me out on changing the source code of less.js a little so that this is workable. 我希望有人能帮助我稍微更改less.js的源代码,以便可行。

I've managed to reach the source code for the mixin parser . 我已经设法获得mixin解析器的源代码 I've tried changing the RegExp there, but it interferes with other parts of the parser. 我尝试过在那里更改RegExp,但是它会干扰解析器的其他部分。 I know we have to make only a few changes because, instead of just accepting .mixin and #mixin we have to accept any mixin like tags / attribute selectors like input[type=text] . 我知道我们只需要进行一些更改,因为我们不仅要接受.mixin#mixin ,还必须接受任何mixin,如标签/ input[type=text] /属性选择器,如input[type=text]

It is currently needed for the development of a UI framework that uses Bootstrap. 当前需要开发使用Bootstrap的UI框架。 Unfortunately many places in bootstrap are littered with direct tag selectors instead of ids and classes. 不幸的是,引导程序中的许多地方到处都是直接标签选择器,而不是id和class。

This is possible since version 1.4.0 (2013-06-05) of LESS that include the extend feature. LESS 1.4.0(2013-06-05)版本开始,版本包含extend功能。 Based on the original example 基于原始示例

.navbar .nav > li {
  float: left;
}

.mynavbar:extend(.navbar .nav > li) {}

compiles to 编译为

.navbar .nav > li,
.mynavbar {
  float: left;
}

Documentation here and discussion & example use for the original question here 此处的文档以及此处原始问题的讨论和示例用法

EDIT: Added Code Sample 编辑:添加了代码示例

First off, I would strongly discourage doing such things. 首先,我强烈不鼓励这样做。 Instead, try to use the power of CSS and build your HTML such that the bootstrap rules apply, for example. 相反,请尝试使用CSS的功能并构建HTML,以使引导规则适用。 Anyway, since you asked for it, here is the solution. 无论如何,既然您要了,这里就是解决方案。

The problem is not the complexity of the selector, or the child rule, but the tag name selector part (ie the li ). 问题不是选择器或子规则的复杂性,而是标签名称选择器部分(即li )。 So, what we have to fix is the mixin parser only matching classes and ids. 因此,我们要修复的是mixin解析器仅匹配类和ID。 I guess we would not want to tamper with the first class or id test , since that is probably needed to distinguish mixins from normal CSS rules (although the tests run fine with that check commented out). 我猜我们不想篡改一流或id测试因为可能需要用它来将mixins与普通CSS规则区分开(尽管在注释掉该检查的情况下测试运行良好)。 (Actually, there is a parser preference in action, and the only thing tried after mixins are comments and directives, so we can safely remove that check as well). (实际上,实际上有一个解析器首选项,并且在混合之后尝试的唯一操作是注释和指令,因此我们也可以安全地删除该检查)。 However, we can easily allow tag names in later parts of the mixin selector by adding a question mark after [#.] in the matching regular expression. 但是,通过在匹配的正则表达式[#.]之后添加问号,我们可以轻松地在mixin选择器的后面部分中允许标记名称。 So 所以

while (e = $(/^[#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/)) {

– ie line 825 – becomes –即第825行 –变为

while (e = $(/^[#.]?(?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/)) {

The test cases still run through fine, afterwards, but subtle breakage my occur, so be cautious. 之后,测试用例仍然运行良好,但是会发生细微的损坏,因此请谨慎。

Edit: There is a GitHub Issue for the same problem. 编辑:有一个GitHub问题针对相同的问题。 Apparently the less folks rather want the mixin feature to be more narrow and function-like, instead of allowing a more flexible … well … mixing in of rules. 显然,少一些人希望混合功能更狭窄,更像功能,而不是允许规则更灵活……好……混合。 With regard to the CSS output, that's probably reasonable. 关于CSS输出,这可能是合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM