views:

235

answers:

1

I want to detect and replace tab characters in a bit of HTML like this:

<code>
  something
  {    something else
  }
</code

Right now, I am using something like this:

$(this).html(replaceAll(trim($(this).html()), "\t", "&nbsp;&nbsp;&nbsp;"));

But IE, in all its cleverness, changes tab characters into spaces, and so doing the above is useless. Does anyone know how I can detect tab characters in the HTML source with javascript for IE?

A: 

So, jmaglasang gave me a good idea. He said IE respects whitespace in a pre tag. So, I thought why not insert a pre tag with javascript, read the html, then remove the pre tag afterward. It works but theres a catch - you have to use a setTimeout callback. Heres the code:

$("code").each(function()
{   $(this).wrap("<pre></pre>");
    var element = $(this);

    setTimeout(function()   // read the html
    {   var x = element.html().split("");
        for(n in x)
        {    alert(x[n].charCodeAt(0) + " '" + x[n] + "'");
        }
    }, 0);
});

The setTimeout is neccessary because for some reason, IE waits to re-render the html until after all the javascript finishes running. Incidentally, it also waits to execute any callbacks issued by setTimeout. I wish I knew how I could force IE to render the html immediately... If anyone knows I'd definitely appreciate it.

B T
One problem. Apparently this doesn't help detect newlines for some reason. Even though they're detectable inside pre tags, inserting a pretag like here doesn't do the trick for newlines...
B T