This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 23636 - [Shadow]: selectors in style elements in shadowRoots should match in sibling shadowRoots
Summary: [Shadow]: selectors in style elements in shadowRoots should match in sibling ...
Status: RESOLVED WONTFIX
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: HISTORICAL - Component Model (show other bugs)
Version: unspecified
Hardware: PC All
: P2 normal
Target Milestone: ---
Assignee: Dimitri Glazkov
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 22715
  Show dependency treegraph
 
Reported: 2013-10-25 21:27 UTC by Steve Orvell
Modified: 2014-12-12 05:39 UTC (History)
3 users (show)

See Also:


Attachments

Description Steve Orvell 2013-10-25 21:27:02 UTC
Given an element with a stack of shadowRoot's, it's common to want to write styles in one shadowRoot that apply to sibling shadowRoots. Typically, if one creates an additional shadowRoot on an element and decides to include the older root via <shadow>, one wants to control how this older root is styled. Likewise, it's common to want to inherit styling from an older shadowRoot in a younger one.

When styles are scoped to a specific shadowRoot, this is not possible. With the addition of the ^ combinator this becomes possible but requires a more complicated, cumbersome syntax for the common case (see https://www.w3.org/Bugs/Public/show_bug.cgi?id=23467). Instead, this should be the default behavior. Here's an example:

<host>
  [SR1]
    <style> 
      .foo { background: red; }
    </style>
    <div class="foo"></div>
  [SR2]
    <style> 
      .foo { color: green; }
    </style>
    <div class="foo"></div>

Both [SR1] .foo and [SR2] .foo should have green text with a red background.
Comment 1 Steve Orvell 2013-10-25 21:42:21 UTC
This would also allow us to eliminate the [space] and > combinators to the right of :host. These combinators next to :host are confusing since one would think they match elements in the host's scope, but they in fact, match elements in the shadowRoot. For example, :host means match the host but :host > * means match children inside the host's *shadowRoot*.

Instead, these combinators should match nothing when to the right of :host. The ^ combinators should be used for these types of selectors since it specifically jumps into a shadowRoot.

1. match an element in shadowRoot that matches .foo if a host matches .bar:

   * old: :host(.bar:host)  .foo
   * new: :host(.bar:host) ^ .foo

2. match an element that's a .foo child of a shadowRoot if a host matches.bar:

   * old: :host(.bar:host) > .foo
   * new: :host(.bar:host) ^ :top > .foo 

The proposed selectors are non-trivial, but they avoid applying magical new behavior for the [space] and > combinators.
Comment 2 Elliott Sprehn 2014-01-16 22:59:39 UTC
This means that component authors need to be really careful how they write their CSS and also of how they structure the markup inside their widgets.

Previously you could do:

<x-expander>'s ShadowRoot:

<style>
  div { display: none; }
  .expanded { display: block }
</style>

<button>Expand me</button>
<div>
  <content></content>
</div>
<script>
// onclick of the button add "expanded" to the div.  
</script>

but this component author has just prevented people from inheriting from his component because now they can't use <div> as it'll be display: none so they have to add even more CSS to undo this.

It also means you need to be super careful not to collide with your superclass with class names so even though you can use the same id in both of them like #container and then this.$.container in polymer, you can't style them separately because the CSS ends up applying to both.

This doesn't seem like a good change, it breaks the encapsulation in a way that now authors need to constantly be careful with what CSS they use.
Comment 3 Elliott Sprehn 2014-01-16 23:17:01 UTC
(In reply to Elliott Sprehn from comment #2)
>...
> 
> It also means you need to be super careful not to collide with your
> superclass with class names so even though you can use the same id in both
> of them like #container and then this.$.container in polymer, you can't
> style them separately because the CSS ends up applying to both.
> 

It seems Polymer already has this issue, if you have an id conflict then this.$.container will always be the one from the youngest shadow root. So I can understand why allowing conflicts for div { display: none; } doesn't seem like an issue as Polymer already has an opinion that you just shouldn't do that.

Previously CSS matched the same as querySelector() so if <style> in any ShadowRoot applies to all others on that element why doesn't shadowRoot.querySelector("div") return ones in all the other ShadowRoots? Or why doesn't root.getElementById("x") return things from the older shadow root like this.$ would?

Can you give examples why you want DOM level encapsulation but no style encapsulation?
Comment 4 Scott Miles 2014-01-17 08:38:31 UTC
I generally think of the inheritance relationship as being non-encapsulating; as opposed to the composition relationship which relies on encapsulation.

Polymer uses <shadow>/olderShadowRoot typically to implement inheritance relationships, so by the above it feels natural to remove style encapsulation. By the same token, we would probably favor loosening of the DOM encapsulation too, but I can't say right now what form that would take.

However, here are (at least) two arguments against this ticket that I cannot dispute beyond the above.

1. It's awkward for the ShadowDOM machinery to share styles in this manner, and it lacks parallelism vs querySelector.
2. The notions of inheritance vs encapsulation as described above are certainly my opinion.

I'm willing to concede this on those points if that's desirous.

Last bit: If we kill this issue, I think we need a new (declarative) way to override styling on inherited shadow root. Is there already a syntax for that?
Comment 5 Hayato Ito 2014-09-08 07:49:57 UTC
Is this feature request still valid? Can we close this?
Comment 6 Hayato Ito 2014-12-12 05:39:47 UTC
Let me close this. I think this is obsolete one.