vscode icon indicating copy to clipboard operation
vscode copied to clipboard

[css] support /deep/ selector

Open tomwanzek opened this issue 10 years ago • 30 comments

  • VSCode Version: 1.1.1
  • OS Version: Windows 10

When writing Angular 2 components using view encapsulation, it may be necessary to use CSS scoping, i.e. special selectors in accordance with: Angular 2 Guide and the related reference CSS Scoping.

VS Code currently produces and error as follows:

Steps to Reproduce:

  1. Entering the following code into a css file (as for a ng2 component)
:host /deep/ rect.d3chart-background {
  fill: rgba(250,250,250,0.8);
  border-radius: 3px;
}
  1. VS Code creates an error with message "{ expected". The error is triggered on the first backslash of /deep/.

Thanks in advance.

tomwanzek avatar May 29 '16 01:05 tomwanzek

'/' is not a valid character in a selector, at least not in CSS3. I wasn't able to find any details in a newer spec. Is '/' now a valid part of an identifier, or is it an operator or is just /deep/ a new special selector?

aeschli avatar May 30 '16 09:05 aeschli

Did /deep/ ever make it into a spec? I think Polymer backtracked on using it in favor of a shadow DOM shim.

Tyriar avatar May 30 '16 09:05 Tyriar

By way of this some more detail. The issue currently arises in the context of some use cases when styling components in accordance with Angular 2 (RC1). Here a few snippets from the Angular documentation section, I linked in my initial comment:

/deep/

Component styles normally apply only to the HTML in the component's own template.

We can use the /deep/selector to force a style down through the child component tree into all the child component views. The /deep/ selector works to any depth of nested components, and it applies both to the view children and the content children of the component.

...

The/deep/selector also has the alias >>>. We can use either of the two interchangeably.

The /deep/ and >>> selectors should only be used with emulated view encapsulation. This is the default and it is what we use most of the time.

Angular 2 supports the use of /deep/and >>> in emulated view encapsulation, i. e. when there there is no native shadow DOM support.

The >>> alias causes an error in a css stylesheets edited in VS Code as well.

Otherwise, the cross-references would lead to the shadow DOM as the note in Section 2.3 of W3C Shadow DOM and the more specific discussion in Deep Combinator W3C CSS Scoping.

tomwanzek avatar May 30 '16 14:05 tomwanzek

Thanks for the link, which led me to this: https://www.w3.org/TR/2013/WD-selectors4-20130502/#idref-combinators

aeschli avatar May 30 '16 19:05 aeschli

/deep/ no longer exists, so I don't think we should support it. >>> is the new version, which should probably be supported, but I don't know how stable it is right now. These kind of things usually get renamed a lot before being widely supported.

https://drafts.csswg.org/css-scoping-1/#deep-combinator

dstorey avatar Jun 07 '16 05:06 dstorey

Is there any workaround for this issue? Its breaking the syntax checking and syntax highlight.

No matter its been deprecated in the future (who knows when, at least its valid at this point), or change to a new version (shouldn't we support both versions then?), it does supported by the parser (it works find if you ignored the vscode error), and has been recommended (or at lease been mentioned) in the Angular2 official doc.

zhenwenc avatar Jul 19 '16 11:07 zhenwenc

There's no workaround except disabling the syntax checking. Changing the parser and syntax highlighting grammar is considerable work. We have limited resources and want to focus on what's in the standards. We have an extension APIs that allow external developers to contribute advanced language supports, including replacing grammars. Closing this issue

aeschli avatar Jul 19 '16 12:07 aeschli

Thanks! Disable syntax checking would be enough (transpiler will tell me the syntax error anyway).

zhenwenc avatar Jul 19 '16 13:07 zhenwenc

The settings is "css.validate": false

aeschli avatar Jul 19 '16 14:07 aeschli

That is what I ended up doing in the interim. Toggling back and forth manually, when I actually change the css files and then screening the real issues out. Not ideal, but I understand your take. Thanks for all the amazing work on VS Code.

tomwanzek avatar Jul 19 '16 15:07 tomwanzek

does anyone know if an extension exists that supports this /deep/ selector?

DmitryEfimenko avatar Sep 24 '16 00:09 DmitryEfimenko

Not to my knowledge. Although, I would be more interested now than even before, since there were some recent changes on the Angular2 final (and the last couple of RCs before that), which made me have to use /deep/ or >>> again.

Until recently I could avoid it.

tomwanzek avatar Sep 24 '16 00:09 tomwanzek

using >>> alias, vscode shows an error but doesn't break the syntax checking for the rest of the file. It seems like a suitable workaround

canda avatar Dec 28 '16 14:12 canda

I hope you guys will change your mind or at least make possible to disable css validation locally in vscode. Thanks.

szykov avatar Feb 11 '17 19:02 szykov

use "css.validate": false to disable css validation

aeschli avatar Feb 15 '17 09:02 aeschli

@aeschli yeap, I'm aware of it, thanks. But mentioning this in vscode settings file will disable css validation globally. I wold like to see something like:

// disable-css-validation-next-line
:host /deep/ >>> other criminal  css stuff.

it would be a good workaround.

update: Or exclude specific file in vscode settings:

  "css-validation.exclude": {
    "illegal.css": true,
    "criminal/*.css": true
  },

But I believe it's off-topic here. Just thinking out loud. Now I ended up by disabling css encapsulation of my Angular 2 component instead of disabling css validation.

szykov avatar Feb 15 '17 10:02 szykov

Another workaround: instead of disabling css validation, just use #{"/deep/"}. It's more verbose, but it produces the same thing, and the syntax checker ignores it as far as I can tell.

leviathanbadger avatar Mar 24 '17 20:03 leviathanbadger

@aboveyou00 can you please explain that syntax?

andregs avatar Mar 24 '17 21:03 andregs

It's basically just an "insert raw" command. You can use it to make variable contents part of a selector; this is the example that they give in the documentation.

$name: foo;
$attr: border;
p.#{$name} {
  #{$attr}-color: blue;
}

A feature that doesn't seem to be documented is that you can just insert raw text there by wrapping it in a string. I'm not sure where I found out about it, but you can use it (for example) to prevent SCSS from collapsing expressions in calc() values. Example:

div {
    width: calc(#{"100% - 80px"});
}

Without that it would set the width to either 20% or 20px, I don't remember which, but either is invalid.

leviathanbadger avatar Mar 25 '17 02:03 leviathanbadger

Some updates from my side: I ended up switching on again angular encapsulation and moving from standard linter to stylelint. It is more advanced and flexible. If you don't search compromises and use experimental things, I would definitely to stick with this.

szykov avatar Mar 26 '17 07:03 szykov