You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A basic polyfill for CSS Variables/custom-properties
3
+
4
+
This is an attempt at a very basic [CSS variables (custom properties)](https://drafts.csswg.org/css-variables/) polyfil. In reality this is more of a partial polyfill as it will not cover variables inside of variables, DOM scoping or anything else "fancy". Just taking variables declared anywhere in the CSS and then re-parsing the CSS for var() statements and replacing them in browsers that don't natively support CSS variables.
5
+
6
+
According to [caniuse.com](https://caniuse.com/#feat=css-variables), of current browsers only IE, Edge and Opera Mini do not support CSS variables. This polyfil appears to work on all three really well. I don't see why this wouldn't work on older browsers as well, but I haven't been able to test it on them yet.
X Maybe account for defaults: color: var(--header-color, blue);
4
+
- Verify cross domain working or not (it is working from dropbox)
5
+
- Option to wait to apply anything until all <link>s are parsed or inject what we have and update as each <link> returns
6
+
- Need to test on a more complex CSS file
7
+
- Option to save parsed file in local/session storage so there isn't a delay on additional page loads. Could only do it for links (with URLs to use as keys) and style blocks with IDs of some sort
8
+
- Need to test more complex values like rgba(255,0,0,0.5); and something with !important
9
+
- Try multiple links
10
+
- Local links
11
+
- Ajax driven site, or CSS added later the top of the stack
12
+
*/
13
+
letcssVarPoly={
14
+
init(){
15
+
// first lets see if the browser supports CSS variables
16
+
// No version of IE supports window.CSS.supports, so if that isn't supported in the first place we know CSS variables is not supported
17
+
// Edge supports supports, so check for actual variable support
// save off the CSS to parse through again later. the value may be empty for links that are waiting for their ajax return, but this will maintain the order
62
+
cssVarPoly.oldCSS[counter]=theCSS;
63
+
counter++;
64
+
});
65
+
},
66
+
67
+
// find all the "--variable: value" matches in a provided block of CSS and add them to the master list
This is an attempt at a very basic <ahref="https://drafts.csswg.org/css-variables/">CSS variables (custom properties)</a> polyfil. In reality this is more of a <em>partial</em> polyfill as it will not cover variables inside of variables, DOM scoping or anything else "fancy". Just taking variables declared anywhere in the CSS and
17
+
then re-parsing the CSS for var() statements and replacing them in browsers that don't natively support CSS variables.
18
+
</p>
19
+
<p>According to <ahref="http://caniuse.com/#feat=css-variables">caniuse.com</a>, of current browsers only IE, Edge and Opera Mini do not support CSS variables. This polyfil appears to work on all three really well. I don't see why this wouldn't work on older browsers as well, but I haven't been able to test it on them yet.</p>
20
+
21
+
<p>As far as we can tell your browser <spanclass="supports">does <spanclass="no">not</span> support</span> native CSS variables. <spanclass="showforpolyfill">That means if you see green tests results below, it is thanks to the polyfill :).</span><spanclass="hideforpolyfill">All the green test results below are actually native CSS Variable support. Good job using a good browser :)</span></p>
22
+
23
+
<h3>Does this work on externally CSS files?</h3>
24
+
<p>Yes!</p>
25
+
<h3>Even ones loaded from another domain?</h3>
26
+
<p>To go across domain, CSS needs to be served up with <code>Access-Control-Allow-Origin:*</code> headers.</p>
27
+
28
+
29
+
</div>
30
+
<ahref="#d" class="hide-docs">Toggle documentation</a> (for Opera Mini vs Codepen issue)
31
+
<style>
32
+
:root {
33
+
--newcolor:#0f0;
34
+
}
35
+
36
+
.inlineoverlink {
37
+
color:var(--success2);
38
+
}
39
+
</style>
40
+
<h2>Tests</h2>
41
+
<p>On mosts tests (unless otherwise noted) success will be green text. We start with a <code>color:red;</code> and then override it with a <code>color:var(--success);</code> (or similar) which is green.</p>
42
+
<spanclass="samename">declare same variable over and over</span>
43
+
<spanclass="demo1">no whitespace on var() calls</span>
44
+
<spanclass="demo2">whitespace on var() calls</span>
45
+
<spanclass="demo3">Multiple variables in same call. orange means first var worked, green var worked</span>
46
+
<spanclass="inlineoverlink">orange if link won, green if style after link won</span>
<p><strong>Edge</strong> appears to be working well on Edge 13. Edge 12 was having some problems.</p>
54
+
<p><strong>Opera mini</strong> seems to work well too. This demo fails because not all the page is displayed, but I think that is a codepen issue, not a polyfill issue. When the upper documentation is removed, all tests display well.</p>
55
+
<p><strong>IE 11</strong> seems to do fine.</p>
56
+
</div>
57
+
58
+
<spanclass="demo4">Gets stuff from external .css file. Should start red and change to green on LINK load. border proves the CSS loaded, missing colors means script didn't get parsed and reinserted</span>
59
+
<spanclass="externalcolor">--externalcolor: should start red and change to green on LINK load</span>
60
+
<spanclass="externalfallback">uses fallback. should be green</span>
61
+
62
+
<p>Another set of text under the test for Opera Mini testing.</p>
0 commit comments