Newbie question - Manipulation DOM tree

Johnny Andersson johnny at johnnys.info
Tue Aug 26 21:33:28 BST 2003


Hi. I'm creating a program that looks at the text in Konq's DOM tree, and 
replaces words that it finds with a link to that word in a dictionary. Only 
"plain text" can be replaced in this way, of course - the problem is that 
it's hard to know if it's OK to manipulate the text this way. Here's what I 
do in my program:  

I iterate throught all the nodes in the tree, using 
document.createNodeIterator and NodeFilter::SHOW_TEXT as filter. To see if I 
can change the text node into a href node, I do this (I'm sure this is an 
insanely ugly thing to do, and that's exactly why I'm asking for help. ;) )

  (n is the node that I want to check)

    n=n.parentNode();

    while(!n.isNull())
    {
        str = n.nodeName().string();

        if(str == "SCRIPT" || str == "SELECT")
            return NoChange;

        if(str == "A")
            return Link;

        if(str == "BODY" || str == "FRAMESET")
            return Plain;

        n = n.parentNode();
    }

Basically walking throught this node's parents and having a look at what we 
might find. Return value NoChange means "if you change this node, Konq will 
crash so don't". Link means that the node is a link, and Plain means that 
it's OK to change it into a link.

Obviously, this works so-so and Konq still crashes on many pages. How can I do 
this differently? Is my approach totally stupid, or is it possible that it 
will work after I add enough tests to this function? I'd be happy to supply 
more info/code if anyone wants to help.





More information about the kfm-devel mailing list