Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Last active January 25, 2023 04:12
Show Gist options
  • Save jonathantneal/3ff00130407328f3e14179c593e62481 to your computer and use it in GitHub Desktop.
Save jonathantneal/3ff00130407328f3e14179c593e62481 to your computer and use it in GitHub Desktop.
Constructable Stylesheet Polyfill — 330 bytes / 235 bytes gzipped — https://wicg.github.io/construct-stylesheets/
try {
new CSSStyleSheet()
} catch {
CSSStyleSheet = ((
stylePtt = CSSStyleSheet.prototype,
styleDoc = new Document(),
styleDom = styleDoc.appendChild(document.createElement('style')),
sheet
) => Object.assign(stylePtt.constructor = function CSSStyleSheet() {
if (!new.target) {
throw new TypeError("Constructor requires 'new' operator")
}
sheet = styleDom.sheet
styleDoc.removeChild(styleDom)
styleDoc.appendChild(styleDom)
return sheet
}.bind(), { prototype: stylePtt })
)()
}
try{new CSSStyleSheet}catch{CSSStyleSheet=((e=CSSStyleSheet.prototype,t=new Document,r=t.appendChild(document.createElement("style")),n)=>Object.assign(e.constructor=function(){if(!new.target)throw new TypeError("Constructor requires 'new' operator");return n=r.sheet,t.removeChild(r),t.appendChild(r),n}.bind(),{prototype:e}))()}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment