[thelist] DOM: previousSibling and LI tags

liorean liorean at gmail.com
Fri Jan 20 17:11:34 CST 2006


On 20/01/06, Howard Jess <howard at dhitechnologies.com> wrote:
> VOLKAN ÖZÇELİK wrote:
> Better to go backwards through the list of childNodes, as it's "live".

Or you can make it position independent. This code is untested, but I
figure it should work.

function sweep(nd,fn){
    var tnd;
    while(nd)
        tnd=nd.nextSibling,
        fn(nd),
        nd=tnd;
}

function mkSweeper(fn){
    return function(nd){
        sweep(nd,fn)};
}

var sweepWhitespaceAway=mkSweep(function(nd){
    if(nd.nodeType!=1&&/^\s*$/.test(nd.nodeValue))
        nd.parentNode.removeChild(nd);});

sweepWhitespaceAway(nodeToSweepChildNodesFrom.firstChild);


On 20/01/06, VOLKAN ÖZÇELİK <volkan.ozcelik at gmail.com> wrote:
> I think PPK should open a "most complicated, mind-crushing,
> brain-teasing  nextSibling wrapper implementation" contest :))

I've got a start for it:) Capable of breadth-first searching too. I'm
thinking it might be better to separate the subtree exclusion
condition function from the processing function so as to allow use for
such cases as removing nodes from the tree, but otherwise it's pretty
fully featured.

/* Copyright (c) 2005, David "liorean" Andersson
    This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
    Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the restriction that this notice may
not be removed or altered from any source distribution. */

// Function traverse:
// > Node nd - The node you want to traverse into
// > Function fn - Filter function. This must follow the structure
detailed below
//
// Function fn must take a node nd as argument.
// This nd is the node to perform any processing or filtering on.
// Don't do things like replace or remove this node from the DOM tree -
// all processing performed by a method on parentNode should be performed
// on that node and not when traversal has reached it's children.
// The return value should be a boolean. This boolean should indicate
// whether to traverse deeper or not.

function traverse(nd,fn,b){
    var
        l=nd,
        e,
        o=e={nx:null,
            ct:nd=nd.firstChild};

    if(b)
        while(o)
            o=(fn(o.ct)?
                climb:
                visit)(o);
    else
        while(nd)
            nd=(fn(nd)?
                dive:
                stay)(nd,l);

    // Breadth first traversal functionality
    function branch(nd){
        e=e.nx={nx:null,ct:nd};
        return(nd);
    }
    function climb(o){
//        if(!confirm('Climb:\n\n'+(o.ct&&o.ct.nodeName)))throw'meaningless';
        return(visit(o,o.ct.firstChild));
    }
    function visit(o,nd){
        nd=typeof nd!=undefined?nd:o.ct;
//        if(!confirm('Visit:\n\n'+(o.ct&&o.ct.nodeName)+'\n\n,\n\n'+(nd&&nd.nodeName)))throw'meaningless';
        while(nd)
            nd=branch(nd).nextSibling;
        return(o.nx);
    }


    // Depth first traversal functionality
    function dive(nd,l){
        return(nd.firstChild||stay(nd,l));
    }
    function stay(nd,l){
        return(nd.nextSibling||rise(nd,l));
    }
    function rise(nd,l){
        return(nd.parentNode!=l?stay(nd.parentNode,l):null);
    }
}
--
David "liorean" Andersson
<uri:http://liorean.web-graphics.com/>


More information about the thelist mailing list