@coren: When implemented as proposed here, Parsoid and VisualEditor both would need to reimplement the same logic independently. Additionally, any application composing content on the client would need to implement similar aggregation passes, which would make our content a lot less modular than desirable.

There are several alternatives that don't suffer from this issue:

## Inline stylesheets

All modern browsers [support <link rel="stylesheet"> in the body](https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/ZAPP8aTnyn0). While this was not sanctioned by the HTML4 spec, [it has recently been allowed in the HTML5 spec](https://github.com/whatwg/html/commit/179983e9eb99efe417349a40ebb664bd11668ddd). Chrome [is explicitly working on further improvements in how it handles rendering of such styles](https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/ZAPP8aTnyn0). Among other things, the technique is used [to speed up time to first paint](https://jakearchibald.com/2016/link-in-body/) by deferring the loading of below-fold CSS until it is needed.

While this could be used to pull in external styles, that would add some network requests. However, the styles we are talking about here should be fairly small, so inlining them would be preferable. This can be done using data URLs like this (tested in Chrome and Firefox):

```lang=html
    <body>
        <div id="mw12345">
            <link rel="stylesheet"
            href="data:text/css;base64,I213MTIzNDUgeyBjb2xvcjogcmVkIH0=">
            This text is styled red.
        </div>
    </body>
```

This solves the primary issue of avoiding global state, but still requires styles to be explicitly prefixed to enforce scoping, for example by prefixing CSS selectors as in the example: `#mw12345 { color: red }`.

## Shadow DOM

The [Shadow DOM spec](https://www.w3.org/TR/shadow-dom/) is aiming to fully encapsulate styling and DOM behavior of components. It is pushed by Google & others as the successor to <style scoped>. In recent months the spec has been shaping up quickly, but I expect it to still take some time until implementations are consistent enough & polyfills have caught up.

In summary, we should be able to leverage Shadow DOM for cleaner style isolation in the future.