Skip to content

Commit 2c78806

Browse files
authored
Merge pull request #8 from Pilatch/issue-7-check-html-imports-for-css
Issue #7 check HTML imports for styles. Really helpful for web compon…
2 parents 4149dc0 + 3ae9cf0 commit 2c78806

File tree

4 files changed

+247
-6
lines changed

4 files changed

+247
-6
lines changed

css-var-polyfill.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ let cssVarPoly = {
2424
cssVarPoly.ratifiedVars = {};
2525
cssVarPoly.varsByBlock = {};
2626
cssVarPoly.oldCSS = {};
27-
27+
2828
// start things off
2929
cssVarPoly.findCSS();
3030
cssVarPoly.updateCSS();
3131
},
32-
32+
3333
// find all the css blocks, save off the content, and look for variables
3434
findCSS: function() {
35-
let styleBlocks = document.querySelectorAll('style:not(.inserted),link[rel="stylesheet"]');
35+
let styleBlocks = document.querySelectorAll('style:not(.inserted),link[rel="stylesheet"],link[rel="import"]');
3636

3737
// we need to track the order of the style/link elements when we save off the CSS, set a counter
3838
let counter = 1;

test-import.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<style>
2+
.html-import-debug {
3+
color: purple;
4+
font-weight: bold;
5+
}
6+
:root {
7+
--import-purple: purple;
8+
--import-white: white;
9+
}
10+
</style>

test.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta http-equiv="X-UA-Compatible" content="ie=edge">
77
<title>Polyfill test</title>
8+
<script src="webcomponents-lite.js"></script><!-- needed for HTML imports in some browsers -->
89
<link rel="stylesheet" type="text/css" media="all" href="test.css">
910
<link rel="stylesheet" type="text/css" media="all" href="http://aaronbarker.net/cssvars/vars.css?a">
11+
<link rel="import" href="test-import.html">
1012
</head>
1113
<body>
1214
<div class="documentation">
@@ -20,7 +22,7 @@ <h1>CSS Variables Polyfill</h1>
2022
<p>According to <a href="http://caniuse.com/#feat=css-variables">caniuse.com</a>, of current browsers only IE, Edge 12-15 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>
2123

2224
<p>As far as we can tell your browser <span class="supports">does <span class="no">not</span> support</span> native CSS variables. <span class="showforpolyfill">That means if you see green tests results below, it is thanks to the polyfill :).</span> <span class="hideforpolyfill">All the green test results below are actually native CSS Variable support. Good job using a good browser :)</span></p>
23-
25+
2426
<h3>Does this work on externally CSS files?</h3>
2527
<p>Yes!</p>
2628
<h3>Even ones loaded from another domain?</h3>
@@ -33,10 +35,15 @@ <h3>Even ones loaded from another domain?</h3>
3335
:root {
3436
--newcolor: #0f0;
3537
}
36-
38+
3739
.inlineoverlink {
3840
color: var(--success2);
3941
}
42+
43+
.html-import-vars {
44+
color: var(--import-white);
45+
background-color: var(--import-purple);
46+
}
4047
</style>
4148
<h2>Tests</h2>
4249
<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>
@@ -51,7 +58,7 @@ <h2>Tests</h2>
5158
<li><span class="fallback">uses fallback <code>--var(--wrongname, green)</code></span></li>
5259
<li><span class="demo-import">css declared in an <code>@import</code></span> - not polyfilled yet. <a href="https://gist.github.com/stramel/91d05253f801f771da38b3bc7d3c765f#gistcomment-2258818">Identfied with a suggested fix</a>, but will require a bit of a re-write (to use document.styleSheets), so haven't done it yet.</li>
5360
</ul>
54-
61+
5562

5663
<h2>Tests on external, cross-domain file</h2>
5764
<div class="documentation">
@@ -66,6 +73,9 @@ <h2>Tests on external, cross-domain file</h2>
6673
<li><span class="externalfallback">uses fallback. should be green</span></li>
6774
</ul>
6875

76+
<h2>HTML imports</h2>
77+
<p class="html-import-debug">This text should be purple and bold because it was styled by a style tag in an HTML import. If it's not, your browser is not importing HTML.</p>
78+
<p class="html-import-vars">This text should be white on a purple background because of the variables imported from HTML.</p>
6979

7080
<p>Another set of text under the test for Opera Mini testing.</p>
7181
<!-- Copy above for codepen update -->

0 commit comments

Comments
 (0)