Search code examples
javascriptprototypejs

prototype cleanWhitespace() is not working for inserted items


I'm adding a new element into OL list using prototype. In order to use .firstChild property I'm using a cleanWhitespace() function before doing anything. This is working quite nice and if I'm tracing innerHTML of my OL after it is just a plain string as expected.

Then I'm adding a new LI item and it is added with whitespaces. Applying cleanWhitespace() function after adding new LI is not making any effect. Any ideas why?

Here is my bit of code:

$$('div.pagination > ol').each(function(item){
    item.cleanWhitespace(); // Working
    if(!item.firstChild.childElementCount) {
        var prevLi = document.createElement('li');
        prevLi.update('<a href="#">&laquo;</a>');
        prevLi.addClassName('disabled');
        item.insertBefore(prevLi, item.firstChild);
    }else{
        item.firstChild.children[0].update('&laquo;');
    }
    item.cleanWhitespace(); // Not working
});

Thanks in advance.


Solution

  • I'm not quite sure I understand why you're using cleanWhitespace here, but if it's only to use firstChild to not hit any text nodes, I'd definitely forego all that and just use Element#firstDescendant as it skips text and comment nodes. All of Prototype's DOM traversal methods (descendants, immediateDescendants, etc) skip the garbage you don't want.