0

I am getting an infinite loop for a function that was previously working -- I actually got this from a stackoverflow post (I'll link to it below). I don't understand why the slight changes I've made to it would cause an infinite loop.

Infinite loop version

  function walk(node, filter) {
        console.log("value of filter in walk "+ filter + " node: " + node);
        var child, next;
        if(node !== null && filter !== undefined) {

            switch (node.nodeType) {
                case Node.ELEMENT_NODE:  // Element
                case Node.DOCUMENT_NODE:  // Document
                case Node.DOCUMENT_FRAGMENT_NODE: // Document fragment
                    child = node.firstChild;


                while (child) {
                    next = child.nextSibling;
                    walk(child, filter);
                    child = next;
                }

                break;

                case Node.TEXT_NODE: // Text node
                hide(node, filter);
                break;
        }
        }//if statement
    }

Working version

function walk(node) {

    var child, next;

    switch (node.nodeType) {
        case Node.ELEMENT_NODE:  // Element
        case Node.DOCUMENT_NODE:  // Document
        case Node.DOCUMENT_FRAGMENT_NODE: // Document fragment
            child = node.firstChild;
            while (child) {
                next = child.nextSibling;
                walk(child);
                child = next;
            }
            break;

        case Node.TEXT_NODE: // Text node
            hide(node);
            break;
    }
}

There is also a difference in how I'm calling the functions -- for the working version, it was simply:

walk(document.body);

For the new version, I have a function that gets the value for a filter from chrome.storage (this is only here to be passed into another function later) --

function walkWithFilter(){
    chrome.storage.sync.get('theFilter', function(e){
            var word = e.theFilter;
            console.log(word);
            if(word !== null){
                walk(document.body, word);
            }
        });
}

however I've also tried getting rid of this and hard coding a value for filter and then passing it in like:

walk(document.body, filter);

This still gives the same infinite loop in walk, so I've narrowed the issue to the walk function.

Here's a link to the stackoverflow post I got the function from walkfunction

**** EDIT ****

here is the hide() function, it finds the 'filter' word in the DOM and hides its parent elements:

function hide(textNode, filter) {
    var nodeValue = textNode.nodeValue;

    for (var i=0; i < filter.length; i++) {
        if(-1 != nodeValue.indexOf(filter[i])) {
            textNode.parentNode.classList.add('filter');

            //how to change the element by class name (test)
            var x = document.getElementsByClassName('filter');
            var i;
            for (i = 0; i < x.length; i++) {
                console.log("parent length loop");
                x[i].style.backgroundColor = "red";
                 $('.filter').parent().parent().parent().parent().addClass("parent-filter");

                    $('.parent-filter').hide();
                    $('.parent-filter').css("background-color", "black");

                }
             $('.filter').parent().css("background-color", "red");

        }
    }
    textNode.nodeValue = nodeValue;
} 

I believe I am getting an infinite loop because when I run the script it won't stop printing the console.log statement in the beginning of the walk function thousands of times and crashes/becomes unresponsive. This is for a chrome extension if that's relevant.

Community
  • 1
  • 1
user146303
  • 437
  • 1
  • 7
  • 20
  • 1
    I don't get an infinite loop. Since `filter` isn't even used except to check if it's `undefined`, then the only mystery lift is what `hide()` does. Do you still get the same issue if you remove `hide(node, filter)`? Does `hide()` actually use `filter()`? –  Jun 04 '16 at 18:03
  • How do you know you got an infinite loop? – Bergi Jun 04 '16 at 19:13
  • Does `hide` mess with the DOM in any way? Please show us the source of that function- – Bergi Jun 04 '16 at 19:13
  • @squint I added the hide function thanks! it finds the elements in the DOM containing the filter and removes their parent elements. (This is for a chrome extension) – user146303 Jun 04 '16 at 19:48
  • @Bergi I added it thanks - I think it's an infinite loop because when I run the script, the console.log statement in walk runs thousands of times and crashes – user146303 Jun 04 '16 at 19:49
  • @user146303: "*removes their parent elements*" would be devastating while you're walking the DOM, but it seems your script only adds a class to hide it. That should be fine. Hm. – Bergi Jun 04 '16 at 19:54
  • @Bergi ah just realized it's not stuck in the loop anymore that was from some code I removed! And yes it's not actually removing, just hiding --- going to update the question. It's not behaving correctly but it's not an infinite loop. sorry! – user146303 Jun 04 '16 at 19:56

0 Answers0