-
-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ponyfill doesn't work with CSSStylesheet.insertRule #19
Comments
Hi @vladbicu. This is a relatively easy fix, but it comes with caveats. Currently, CSS data is collected from a stylesheet's The fix requires iterating over the
With those caveats understood, I think it makes sense to add an option for this scenario. I'm not familiar enough with the various CSS-in-JS libraries to know if this is a common issue, but it seems anyone modifying CSS using the The ponyfill will continue to work as it does today by default (reading CSS data from the cssVars({
// Only process emotion.js <style> nodes
include: 'style[data-emotion]' //
// Get <style> CSS from runtime values
parseRuntime: true
}) ; I'll put something together on a separate branch later on tonight. You can give it a test, see how it works, and if all is well I'll publish the changes to npm. |
I've created a runtime branch with the fix described above. To install from this branch on GitHub using NPM: npm i -D jhildenbiddle/css-vars-ponyfill#runtime You can view the |
Well, that didn't go as planned. Turns out the fix doesn't work in legacy browsers. I was testing in the latest version of Chrome which worked fine. Once I ran all automated tests in legacy browers I noticed that the new The reason for the failure is due to the browser behavior I described above:
Legacy browsers don't understand CSS custom properties. When a rule is inserted that contains either a custom property declaration or // Insert two rules into an empty stylesheet (like emotions.js)
styleElm.sheet.insertRule(':root { --color: red; }', 0); // 1
styleElm.sheet.insertRule('p { color: var(--color); }', 0); // 2
// Modern browser CSSRuleList
// 1: ':root { --color: red; }'
// 2: 'p { color: var(--color); }'
// Legacy browser CSSRuleList (e.g. IE)
// 1: ':root { }'
// 2: 'p { }' Given this, I don't think it is possible to have the ponyfill work with stylesheets that use One last thought... You mentioned that everything was working fine until you did a production build. I assume this means emotion.js behaves differently in development mode vs production mode by modifying the Hope this helps! |
Hi @jhildenbiddle, Thanks for the effort! Maybe will help some people that use insertRule/deleteRule API.
|
Sorry we couldn't find a solution, @vladbicu. Best of luck with your project and finding a solution for IE11. |
Can proxy insetRule method? // Object.keys(CSSStyleSheet.prototype)
// (9) ['ownerRule', 'cssRules', 'rules', 'addRule', 'deleteRule', 'insertRule', 'removeRule', 'replace', 'replaceSync']
// Some methods are supported only in higher versions and can be done without a proxy
var insertRuleFn = CSSStyleSheet.prototype.insertRule
CSSStyleSheet.prototype.insertRule =function(ruleStr,index) {
// console.log(ruleStr);
var legacyRuleStr = window.cssVarsSync ? window.cssVarsSync(ruleStr) : ruleStr;
// console.log(legacyRuleStr);
insertRuleFn.call(this, legacyRuleStr, index) } |
@chenyulun -- The challenge with an If rules are inserted in a very specific order, this is not an issue: styleElm.sheet.insertRule(':root { --color: red; }', 0);
styleElm.sheet.insertRule('p { color: var(--color); }', 0); p { color: red; } But if rules are inserted out of order... styleElm.sheet.insertRule('p { color: var(--color); }', 0);
styleElm.sheet.insertRule(':root { --color: red; }', 0); ... or override previous values... styleElm.sheet.insertRule(':root { --color: red; }', 0);
styleElm.sheet.insertRule('p { color: var(--color); }', 0);
styleElm.sheet.insertRule(':root { --color: blue; }', 0); ... or invalidate previous values ... styleElm.sheet.insertRule(':root { --color: red; }', 0);
styleElm.sheet.insertRule(':root { --color: blue; }', 0);
styleElm.sheet.insertRule('p { color: var(--color); }', 0);
styleElm.sheet.deleteRule(1); ... things get complicated very quickly. The ponyfill doesn't suffer from these problems because it doesn't begin its work until the
Could CSSOM method proxies work? Possibly, but it would be a significant amount of work and has the likely potential to perform poorly if/when previously processed rules need to be re-processed (see examples above). If someone were motivated, they could attempt it using the individual libraries in the |
Hi,
I'm working on a project that use emotion-css library + CSS vars. Because we had to support IE11, I tried to use this ponyfill. All things were great until we did a production build and the ponyfill didn't work.
After some research I discovered that emotion library use insertRule API, which means that <style> tag is empty and the ponyfill has nothing to parse and replace.
There's someone with this issue? Any ideas?
The text was updated successfully, but these errors were encountered: