Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anyone think 【tag:before{&:hover{color:red}}】 should compiles to 【tag:hover:before{color:red}】? #1605

Closed
xieranmaya opened this issue Oct 17, 2013 · 7 comments

Comments

@xieranmaya
Copy link

I'm using LESS today and I wrote a LESS spinet like this:

div:before{
    content:'css generated content';
    color:green;
    &:hover{
        color:red;
    }
}

and it compiles to

div:before {
    content:'css generated content';
    color: green;
}
div:before:hover {
    color: red;
}

and this can't work, but it's intuitive....
you can see this question:
http://stackoverflow.com/questions/5777210/how-to-write-hover-condition-for-abefore-and-aafter

so, do you think that the above LESS code should compiles to

div:before {
    content:'css generated content';
    color: green;
}
div:hover:before {
    color: red;
}

and it's works in browser
which means

div:before{
    &:hover{
    }
}

should compile to

div:hover:before

mind the

:hover and :before
's order

@seven-phases-max
Copy link
Member

but it's intuitive....

Well, perhaps. But (usually) it's not compiler's job to guess user intentions and depending on such guesswork to change its own rules (in other words "automatically fix errors for you"). So in this particular case I'm afraid that if you want a different order you need to write it in this different order (even if it won't look very compact), e.g.:

div {
    &:before {
        content: 'css generated content';
        color:   green;
    }
    &:hover:before {
        color:   red;
    }
}

See also #1075.

@lukeapage
Copy link
Member

Does it make any difference? If not why do people care what its compiled
too?

@matthew-dean
Copy link
Member

@lukeapage A generated block (div:before) doesn't have a hover state. It's a sub-element and created via CSS, so it doesn't receive events (e.g. div:before:hover never occurs). But, of course the main div does, and you can generate a block in that main div based on it's hover state (div:hover:before).

@xieranmaya As a general rule, Less doesn't have awareness of what the CSS is, just how the code is written. So div:before:hover == div:blerg:flooglem to the parser. Changing the rules of parsing based on what you write can get messy quick and adds a heap to the parsing logic.

@lukeapage
Copy link
Member

I think this should be covered by the issue about selecting a parent for & e.g. if you could do

div {
  :before {
     content:'css generated content';
     color:green;
     &(1):hover&(2){
        color:red;
     }
  }
}

it would be one way of solving the issue.(though in this case @seven-phases-max suggestion would be better

I presume it is feasible that although at the moment generated elements don't get hover events, that they could in the future? If so we don't want to make this change as we would then have to then make a breaking change to revert it back.

@radium-v
Copy link
Contributor

If we're going against the W3C spec, there's this:

One pseudo-element may be appended to the last simple selector in a chain, in which case the style information applies to a subpart of each subject. (http://www.w3.org/TR/CSS21/selector.html#selector-syntax)

It's slightly different in the CSS3 proposed spec though:

Only one pseudo-element may appear per selector, and if present it must appear after the sequence of simple selectors that represents the subjects of the selector. Note: A future version of this specification may allow multiple pseudo-elements per selector. (http://www.w3.org/TR/2009/PR-css3-selectors-20091215/#pseudo-elements)

To me, it doesn't make sense to accommodate for this, since the compiler will just do what you tell it to:

div {
  &:before {
    content: 'css generated content';
    color: green;
  }

  &:hover {
    &:before {
      color:red;
    }
  }
}

@lukeapage
Copy link
Member

am I right in thinking this particular bug can be closed, e.g. better handling of & is covered by other bugs?

@matthew-dean
Copy link
Member

Yes, in the way this issue is written, Less doesn't need to reorder psuedo-elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants