上面[我有科德]的问题注释部分中的代码笔代码段链接+1。我发现的一件事是,需要对代码片段进行一些修改,以使功能声明以JSON格式定义,以使IE11不会抱怨。以下是代码笔代码片段的稍作修改的版本:
let cssVarPoly = {
init: function() {
if (window.CSS && window.CSS.supports && window.CSS.supports('(--foo: red)')) {
console.log('your browser supports CSS variables, aborting and letting the native support handle things.');
return;
} else {
console.log('no support for you! polyfill all (some of) the things!!');
document.querySelector('body').classList.add('cssvars-polyfilled');
}
cssVarPoly.ratifiedVars = {};
cssVarPoly.varsByBlock = {};
cssVarPoly.oldCSS = {};
cssVarPoly.findCSS();
cssVarPoly.updateCSS();
},
findCSS: function() {
let styleBlocks = document.querySelectorAll('style:not(.inserted),link[type="text/css"]');
let counter = 1;
[].forEach.call(styleBlocks, function (block) {
let theCSS;
if (block.nodeName === 'STYLE') {
theCSS = block.innerHTML;
cssVarPoly.findSetters(theCSS, counter);
} else if (block.nodeName === 'LINK') {
cssVarPoly.getLink(block.getAttribute('href'), counter, function (counter, request) {
cssVarPoly.findSetters(request.responseText, counter);
cssVarPoly.oldCSS[counter] = request.responseText;
cssVarPoly.updateCSS();
});
theCSS = '';
}
cssVarPoly.oldCSS[counter] = theCSS;
counter++;
});
},
findSetters: function(theCSS, counter) {
cssVarPoly.varsByBlock[counter] = theCSS.match(/(--.+:.+;)/g) || [];
},
updateCSS: function() {
cssVarPoly.ratifySetters(cssVarPoly.varsByBlock);
for (let curCSSID in cssVarPoly.oldCSS) {
let newCSS = cssVarPoly.replaceGetters(cssVarPoly.oldCSS[curCSSID], cssVarPoly.ratifiedVars);
if (document.querySelector('#inserted' + curCSSID)) {
document.querySelector('#inserted' + curCSSID).innerHTML = newCSS;
} else {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = newCSS;
style.classList.add('inserted');
style.id = 'inserted' + curCSSID;
document.getElementsByTagName('head')[0].appendChild(style);
}
};
},
replaceGetters: function(curCSS, varList) {
for (let theVar in varList) {
let getterRegex = new RegExp('var\\(\\s*' + theVar + '\\s*\\)', 'g');
curCSS = curCSS.replace(getterRegex, varList[theVar]);
let getterRegex2 = new RegExp('var\\(\\s*.+\\s*,\\s*(.+)\\)', 'g');
let matches = curCSS.match(getterRegex2);
if (matches) {
matches.forEach(function (match) {
curCSS = curCSS.replace(match, match.match(/var\(.+,\s*(.+)\)/)[1]);
});
}
};
return curCSS;
},
ratifySetters: function(varList) {
for (let curBlock in varList) {
let curVars = varList[curBlock];
curVars.forEach(function (theVar) {
let matches = theVar.split(/:\s*/);
cssVarPoly.ratifiedVars[matches[0]] = matches[1].replace(/;/, '');
});
};
},
getLink: function(url, counter, success) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.overrideMimeType('text/css;');
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
if (typeof success === 'function') {
success(counter, request);
}
} else {
console.warn('an error was returned from:', url);
}
};
request.onerror = function () {
console.warn('we could not get anything from:', url);
};
request.send();
}
};
cssVarPoly.init();