Skip to content

Instantly share code, notes, and snippets.

@maticrivo
Forked from ranbena/CSS units parser
Created June 30, 2013 09:19
Show Gist options
  • Save maticrivo/5894478 to your computer and use it in GitHub Desktop.
Save maticrivo/5894478 to your computer and use it in GitHub Desktop.
//Open the file directly in the browser and run this script from the console
//Assumptions: The browser uses a <pre/> tag for showing the CSS code
var file = document.querySelector("pre");
var fileContent = file.innerHTML;
var newContent = fileContent.replace(/([\d.]+)(px)/g, function(m){
var measure = parseFloat(m.split("px")[0]);
var newMeasure = measure / 10;
newMeasure = newMeasure.toFixed(2);
newMeasure = newMeasure.replace(/(\.[0-9]*?)0+$/, "$1"); // remove trailing zeros
newMeasure = newMeasure.replace(/\.$/, ""); // remove trailing dot
return newMeasure+"rem";
});
file.parentNode.innerHTML = "<pre>"+newContent+"</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment