Skip to content

Commit

Permalink
Fix mounting styles under multiple shadow roots
Browse files Browse the repository at this point in the history
FIX: Fix a regression that broke mounting a set of styles under more than
one shadow root.

Closes #12
  • Loading branch information
marijnh committed Feb 29, 2024
1 parent f5c0f2f commit f961e42
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/style-mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ export class StyleModule {
let set = root[SET], nonce = options && options.nonce
if (!set) set = new StyleSet(root, nonce)
else if (nonce) set.setNonce(nonce)
set.mount(Array.isArray(modules) ? modules : [modules])
set.mount(Array.isArray(modules) ? modules : [modules], root)
}
}

let adoptedSet = new Map //<Document, StyleSet>

class StyleSet {
constructor(root, nonce) {
this.root = root
let doc = root.ownerDocument || root, win = doc.defaultView
if (!root.head && root.adoptedStyleSheets && win.CSSStyleSheet) {
let adopted = adoptedSet.get(doc)
Expand All @@ -105,7 +104,7 @@ class StyleSet {
root[SET] = this
}

mount(modules) {
mount(modules, root) {
let sheet = this.sheet
let pos = 0 /* Current rule offset */, j = 0 /* Index into this.modules */
for (let i = 0; i < modules.length; i++) {
Expand All @@ -127,14 +126,14 @@ class StyleSet {
}

if (sheet) {
if (this.root.adoptedStyleSheets.indexOf(this.sheet) < 0)
this.root.adoptedStyleSheets = [this.sheet, ...this.root.adoptedStyleSheets]
if (root.adoptedStyleSheets.indexOf(this.sheet) < 0)
root.adoptedStyleSheets = [this.sheet, ...root.adoptedStyleSheets]
} else {
let text = ""
for (let i = 0; i < this.modules.length; i++)
text += this.modules[i].getRules() + "\n"
this.styleTag.textContent = text
let target = this.root.head || this.root
let target = root.head || root
if (this.styleTag.parentNode != target)
target.insertBefore(this.styleTag, target.firstChild)
}
Expand Down

0 comments on commit f961e42

Please sign in to comment.