Repository navigation
Closed
Labels
Description
Given the following, I would expect both loops to render similar output. However, the version where the each() is inside the block and & is used produces strange results. Specifically, the styles immediately under the & are rendered at the top of the loop's generated output. Oddly, a child of the & block is rendered in the expected spot.
Given
@infixes: a, b;
/*! LOOP IN BLOCK */
.amp {
each(@infixes, #(@_infix, @i) {
// Each infix actually generates the _next_ infix's block, with the last
// one generating styles with no infix.
@infix: e(if(
((@i + 1) <= length(@infixes)),
%('-%s', extract(@infixes, (@i + 1))),
''
));
&@{infix} {
display: block;
.child {
display: block;
}
}
});
}
/*! BLOCK IN LOOP */
each(@infixes, #(@_infix, @i) {
// Each infix actually generates the _next_ infix's block, with the last
// one generating styles with no infix.
@infix: e(if(
((@i + 1) <= length(@infixes)),
%('-%s', extract(@infixes, (@i + 1))),
''
));
.plain@{infix} {
display: block;
.child {
display: block;
}
}
});Expected
/*! LOOP IN BLOCK */
.amp-b {
display: block;
}
.amp-b .child {
display: block;
}
.amp {
display: block;
}
.amp .child {
display: block;
}
/*! BLOCK IN LOOP */
.plain-b {
display: block;
}
.plain-b .child {
display: block;
}
.plain {
display: block;
}
.plain .child {
display: block;
}Actual
/*! LOOP IN BLOCK */
.amp {
display: block;
}
.amp-b {
display: block;
}
.amp-b .child {
display: block;
}
.amp .child {
display: block;
}
/*! BLOCK IN LOOP */
.plain-b {
display: block;
}
.plain-b .child {
display: block;
}
.plain {
display: block;
}
.plain .child {
display: block;
}Note that .amp is floated to the top.
Activity
calvinjuarez commentedon Dec 5, 2018
Detached rulesets also behave unexpectedly.
_tables – fix .table-responsive output order
_navbar – fix .navbar-expand output order
matthew-dean commentedon Dec 6, 2018
Thanks @calvinjuarez. Do you think you could boil that down to the simplest example that demonstrates the bug? Or are
%(),extract(),e()andlength()essential to trigger the issue? Right now the example is hard to follow, as to what is doing what.calvinjuarez commentedon Dec 6, 2018
@matthew-dean all those functions are just to say, "whatever the current item is, use the next one, unless this is the last one, in which case, use
''". It's important to loop that way to see the bug. Otherwise, the bug is invisible because the un-infixed version is supposed to be at the top. Does that make sense?So in the loop, when
ais the current value, it uses the next value,-bas the actual infix. Whenbis the current value, since it's the last value, the infix is an empty string (escaped).So, to answer the question, no, they're not essential to trigger it (I expect it happens always), they're just a way of looping in the wonky way that exposes the issue.
I ran into the issue while fixing the output order in seanCodes/bootstrap-less-port, where the same behavior is accomplished with
breakpoint-next()andbreakpoint-infix()functions, but the result is the same.I'll see if there's a less obtuse test case that produces a similar error.
calvinjuarez commentedon Dec 6, 2018
Here's something a tiny bit more concise, and hopefully easier to read. The key is that, in the CSS, when
@infixis an empty value, that output is above output when@infixis a non-empty value, even when the empty value should come afterward. This doesn't happen when the class is inside the loop.It looks like it has something to do with how
&works when the selector ends up matching its parent.calvinjuarez commentedon Dec 6, 2018
I'm getting the feeling this is expected behavior out of
&, actually. Consider:That's probably expected. This looks like it'd be a hairy one to sort out...unless we're okay making this output as 2 rules with the same selector. :/
seven-phases-max commentedon Dec 6, 2018
At quick glance I suppose
.ampis floated to the top because it is at the top. E.g. consider:this should result in:
And the same order is expected if you remove
/* 0 */.I.e. it's two instances of
.ampthat simply collapse into the first of them (the surrounding "block" itself) which is always there and always at the top even if empty (like in the initial example).matthew-dean commentedon Dec 6, 2018
Yeah, I never really thought about placement order of nested blocks. But it definitely seems like Less doesn't try to keep things in order. To expand on @seven-phases-max's example:
Outputs:
Sass's output is slightly different, opting to keep nested selectors in order, even if one is just
&Oddly, Sass doesn't treat the
is: fivedeclaration as coming "after" the other nested declarations. But I guess that does allow you to "force" subsequent declarations to come in a certain order by wrapping them in&{}.So, if you're migrating Bootstrap and comparing output, you're likely seeing that Less collapses
&as being part of the parent ruleset, and Sass treats it as a new selector and generates a sequence of rules to follow the parent.The
each()function doesn't do anything special in terms of rulesets. It basically generates a sequence of anonymous rulesets (injecting@value,@keyetc variables at the top of each one) which then get evaluated.As far as which behavior makes more sense? 🤷♂️ I really don't know how to answer that. They're just different. I think the rationale in Less's behavior is to create fewer selectors. But Sass's rationale is probably to keep nested selector order intact, but it probably comes down to the merging algorithms are just different.
Now, I were to put my CSS hat on, and thinking about things like the
@nestproposal, probably BOTH Less and Sass have this wrong (with Sass maybe being "more" wrong, because it placesthreeas the "final" value in the.ampclass?). What the output probably SHOULD be to reflect the way it was written is this:That is, declarations should maintain their order in the stylesheet, if they're broken up by other nested selectors. If nesting were ever added to CSS natively, that's likely what it would mean. It's more verbose, but it seems more accurate.
calvinjuarez commentedon Dec 6, 2018
Yeah, I think we're all seeing eye-to-eye. What we've got is reliable and expected in nearly all cases (the examples shared in the previous 2 comments, for instance). You kind of have to work to find how to "break" it. Even in those situations, though, the workaround doesn't suck, since
each()can run at the root level without anything in the way, so it's probably usually gonna be easy to write out the need for the "naked"&entirely.Given all that, I'm closing this for now (won't fix, intended behavior). If we really wanted to allow forcing output the way sass does, it would need to be a new feature, and it'd need a SUPER compelling use case.
And for posterity:
Workaround
Refactor so that
&doesn't appear alone (even conditionally) at the root of the loop's iterator mixin.BECOMES
_tables – fix .table-responsive output order
_navbar – fix .navbar-expand output order
_tables – fix .table-responsive output order
_navbar – fix .navbar-expand output order
matthew-dean commentedon Dec 7, 2018
@calvinjuarez Btw, you can simplify your code slightly like this:
That is,
booleanandifno longer require outer parens around first argument (as of 3.6...?), and anif()statement doesn't necessarily need an else condition._tables – fix .table-responsive output order
_navbar – fix .navbar-expand output order
_tables – fix .table-responsive output order
_navbar – fix .navbar-expand output order
calvinjuarez commentedon Dec 8, 2018
Interesting. I tried dropping the parens, but it didn't compile for me at the time. I'll double-check. The bootstrap-less-port handles that logic with plugin functions (to be more familiar to folks coming from the Sass stuff), but I'll have to keep that in mind in case we decide to go native-er with it at some point.
Less 3.8 (#14)
Less 3.8 (#14)