Skip to content

Commit

Permalink
Fix for :global(...) styles in custom elements. Close sveltejs#2969
Browse files Browse the repository at this point in the history
  • Loading branch information
kmmbvnr committed Dec 18, 2019
1 parent 1ef7601 commit ec12b99
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ export default function dom(
constructor(options) {
super();
${css.code && b`this.shadowRoot.innerHTML = \`<style>${css.code.replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
${css.code && b`this.shadowRoot.innerHTML = \`<style>${css.code.replace(/\\/g, '\\\\').replace(/:global\(([-.\w]+)\)/g, '$1')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
@init(this, { target: this.shadowRoot }, ${definition}, ${has_create_fragment ? 'create_fragment': 'null'}, ${not_equal}, ${prop_indexes}, ${dirty});
Expand Down
9 changes: 9 additions & 0 deletions test/custom-elements/samples/global-styles/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<svelte:options tag="custom-element"/>

<style>
:global(p.active) {
color:rgb(128, 128, 128);
}
</style>

<p></p>
12 changes: 12 additions & 0 deletions test/custom-elements/samples/global-styles/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as assert from 'assert';
import CustomElement from './main.svelte';

export default function (target) {
new CustomElement({
target
});

const style = target.querySelector('custom-element').shadowRoot.querySelector('style');

assert.equal(style.textContent, 'p.active{color:rgb(128, 128, 128)}');
}

0 comments on commit ec12b99

Please sign in to comment.