MCPcopy
hub / github.com/CodeboxIDE/codebox / createCSS

Function createCSS

core/cb.addons/require-tools/less/lessc.js:6336–6396  ·  view source on GitHub ↗
(styles, sheet, lastModified)

Source from the content-addressed store, hash-verified

6334}
6335
6336function createCSS(styles, sheet, lastModified) {
6337 // Strip the query-string
6338 var href = sheet.href || '';
6339
6340 // If there is no title set, use the filename, minus the extension
6341 var id = 'less:' + (sheet.title || extractId(href));
6342
6343 // If this has already been inserted into the DOM, we may need to replace it
6344 var oldCss = document.getElementById(id);
6345 var keepOldCss = false;
6346
6347 // Create a new stylesheet node for insertion or (if necessary) replacement
6348 var css = document.createElement('style');
6349 css.setAttribute('type', 'text/css');
6350 if (sheet.media) {
6351 css.setAttribute('media', sheet.media);
6352 }
6353 css.id = id;
6354
6355 if (css.styleSheet) { // IE
6356 try {
6357 css.styleSheet.cssText = styles;
6358 } catch (e) {
6359 throw new(Error)("Couldn't reassign styleSheet.cssText.");
6360 }
6361 } else {
6362 css.appendChild(document.createTextNode(styles));
6363
6364 // If new contents match contents of oldCss, don't replace oldCss
6365 keepOldCss = (oldCss !== null && oldCss.childNodes.length > 0 && css.childNodes.length > 0 &&
6366 oldCss.firstChild.nodeValue === css.firstChild.nodeValue);
6367 }
6368
6369 var head = document.getElementsByTagName('head')[0];
6370
6371 // If there is no oldCss, just append; otherwise, only append if we need
6372 // to replace oldCss with an updated stylesheet
6373 if (oldCss === null || keepOldCss === false) {
6374 var nextEl = sheet && sheet.nextSibling || null;
6375 if (nextEl) {
6376 nextEl.parentNode.insertBefore(css, nextEl);
6377 } else {
6378 head.appendChild(css);
6379 }
6380 }
6381 if (oldCss && keepOldCss === false) {
6382 oldCss.parentNode.removeChild(oldCss);
6383 }
6384
6385 // Don't update the local store if the file wasn't modified
6386 if (lastModified && cache) {
6387 log('saving ' + href + ' to cache.', logLevel.info);
6388 try {
6389 cache.setItem(href, styles);
6390 cache.setItem(href + ':timestamp', lastModified);
6391 } catch(e) {
6392 //TODO - could do with adding more robust error handling
6393 log('failed to save', logLevel.errors);

Callers 4

errorHTMLFunction · 0.85
loadStyleSheetFunction · 0.85
initRunningModeFunction · 0.85
lessc.jsFile · 0.85

Calls 2

extractIdFunction · 0.85
logFunction · 0.85

Tested by

no test coverage detected