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 17, 2019
1 parent 1ef7601 commit 61d5015
Show file tree
Hide file tree
Showing 4 changed files with 32 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
3 changes: 3 additions & 0 deletions test/custom-elements/samples/global-styles/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
dev: true
};
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>
19 changes: 19 additions & 0 deletions test/custom-elements/samples/global-styles/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as assert from 'assert';
import './main.svelte';

export default function (target) {
const warnings = [];
const warn = console.warn;

console.warn = warning => {
warnings.push(warning);
};

target.innerHTML = '<custom-element/>';
assert.deepEqual(warnings, []);

const style = target.querySelector('custom-element').shadowRoot.querySelector('style');
assert.equal(style.textContent, 'p.active{color:rgb(128, 128, 128)}');

console.warn = warn;
}

0 comments on commit 61d5015

Please sign in to comment.