Skip to content

Commit 61d5015

Browse files
committed
Fix for :global(...) styles in custom elements. Close #2969
1 parent 1ef7601 commit 61d5015

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/compiler/compile/render_dom/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ export default function dom(
442442
constructor(options) {
443443
super();
444444
445-
${css.code && b`this.shadowRoot.innerHTML = \`<style>${css.code.replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
445+
${css.code && b`this.shadowRoot.innerHTML = \`<style>${css.code.replace(/\\/g, '\\\\').replace(/:global\(([-.\w]+)\)/g, '$1')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
446446
447447
@init(this, { target: this.shadowRoot }, ${definition}, ${has_create_fragment ? 'create_fragment': 'null'}, ${not_equal}, ${prop_indexes}, ${dirty});
448448
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
dev: true
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<svelte:options tag="custom-element"/>
2+
3+
<style>
4+
:global(p.active) {
5+
color:rgb(128, 128, 128);
6+
}
7+
</style>
8+
9+
<p></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as assert from 'assert';
2+
import './main.svelte';
3+
4+
export default function (target) {
5+
const warnings = [];
6+
const warn = console.warn;
7+
8+
console.warn = warning => {
9+
warnings.push(warning);
10+
};
11+
12+
target.innerHTML = '<custom-element/>';
13+
assert.deepEqual(warnings, []);
14+
15+
const style = target.querySelector('custom-element').shadowRoot.querySelector('style');
16+
assert.equal(style.textContent, 'p.active{color:rgb(128, 128, 128)}');
17+
18+
console.warn = warn;
19+
}

0 commit comments

Comments
 (0)