+ Reply to Thread
Results 1 to 2 of 2

Thread: JavaScript strip tabs function

  1. #1
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    JavaScript strip tabs function

    ok, so i wrote a function to find all 'pre' tags with the class name 'code' and strip out the first n number of tabs from each line, then put the new text back in the 'pre' tag.

    Code:
    function untabCodePres(tabs)
    {
        if ( ! tabs ) { var tabs = 5; }
        var codeBlocks = document.getElementsByTagName('pre');
        for ( var i in codeBlocks )
        {
            if ( codeBlocks[i].className == 'code' )
            {
                var text = codeBlocks[i].innerHTML.split("\n");
                for ( var line in text )
                {
                    if ( text[line].substring(0,tabs-1) == "\t".repeat(tabs) )
                    {
                        text[line] = text[line].substr(tabs);
                    }
                    if ( text[line] == '' )
                    {
                        delete text[line];
                    }
                }
                codeBlocks[i].innerHTML = text.join("\n");
            }
        }
    }
    but for some reason, it's not working. please help.

    http://portfolio.kbjrweb.com/web.php?id=1 is an example, the dark box is the 'pre' tag.
    Edit:
    nevermind, i got it.
    for some reason, it wasn't getting through
    if ( text[line].substring(0,tabs-1) == "\t".repeat(tabs) )
    structure.

    anyway, it works now.
    Last edited by kbjradmin; 06-24-2009 at 07:24 PM. Reason: Automerged Doublepost

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: JavaScript strip tabs function

    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?
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Javascript - Targeting a window by name from a function?
    By onyxmoron in forum Programming Help
    Replies: 5
    Last Post: 01-12-2009, 05:56 AM
  2. DW error message
    By bunglebrown in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 10-06-2008, 03:11 PM
  3. JavaScript calculation problem!!!!
    By willemvo in forum Programming Help
    Replies: 5
    Last Post: 09-12-2008, 07:19 PM
  4. Replies: 2
    Last Post: 08-17-2008, 08:30 PM
  5. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers