String.repeat isn't a standard method. One possible reason why the code wasn't working is that the script that defined String.repeat wasn't included with the page.
Another problem with the same line is that String.substring(i, j) doesn't include the character at the latter index j, so the line was comparing a string of length 'tabs-1' and another of length 'tabs'.
For what it's worth, here's a function that does the same thing using REs. To me, it's much clearer.
Code:
function untabCodePres(tabs) {
if ( ! tabs ) {
tabs = 5;
}
var tabRE = new RegExp("^\\t{"+tabs+"}\\n?", "gm");
var codeBlocks = document.getElementsByTagName('pre');
for ( var i in codeBlocks ) {
if ( /\bcode\b/.test(codeBlocks[i].className) ) {
codeBlocks[i].innerHTML = codeBlocks[i].innerHTML.replace(tabRE, '');
}
}
}
Here's one that converts all leading tabs into spaces.
Code:
function untabifyPres(tabSize) {
if ( ! tabSize ) {
tabSize = 4;
}
var spaces=" ".repeat(tabSize);
var codeBlocks = document.getElementsByTagName('pre');
for ( var i in codeBlocks ) {
if ( /\bcode\b/.test(codeBlocks[i].className) ) {
codeBlocks[i].innerHTML = codeBlocks[i].innerHTML.replace(/^\t+/gm,
function(tabs) {return spaces.repeat(tabs.length);});
}
}
}
String.repeat brings an applicable algorithm to mind: have you seen Russian peasant multiplication?