Sign in to follow this  

[web] Javascript: removing consecutive elements

This topic is 3566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm writing a GreaseMonkey script to clean up some HTML and it goes fairly well. I can remove any elements I don't need and mark some important ones. The problem I have is removing multiple consecutive BR tags, since the removing of elements leaves them behind. If I use something like:
var brs = document.evaluate("//br", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
then the nextSibling property is always null so I cannot check if two BR's follow eachother. Does anybody have an idea on how to go about this? tia, CipherCraft

Share this post


Link to post
Share on other sites
Im not too sure of what GreaseMoney is but I do see a potential issue with what you are describing.

If you remove the BR and then try to access its nextSibling, then I would expect it to be null because it (the BR you just removed) no longer exists, which I also expect is what is happening in your case.

You would need to either store BR's next sibling or delete from the bottom up using recursion:

func remove(curObj)
{
if(curObj.hasSibling)
remove(curObj.sibling);
end if

delete curObj;
}

Using the above pseudo, you would need to check if each object is of type "BR" accordingly. Your BR's will then be removed starting with the last one in the consecutive sequence of BR's.

Share this post


Link to post
Share on other sites
Quote:
Original post by ju2wheels
Im not too sure of what GreaseMoney is but I do see a potential issue with what you are describing.


Clicky

Quote:
Original post by ju2wheels
If you remove the BR and then try to access its nextSibling, then I would expect it to be null because it (the BR you just removed) no longer exists, which I also expect is what is happening in your case.


I would expect that I'd thought of that [wink] but I'll check tomorrow at work to see if that's my oversight...

Quote:
Original post by ju2wheels
You would need to either store BR's next sibling or delete from the bottom up using recursion:

func remove(curObj)
{
if(curObj.hasSibling)
remove(curObj.sibling);
end if

delete curObj;
}

Using the above pseudo, you would need to check if each object is of type "BR" accordingly. Your BR's will then be removed starting with the last one in the consecutive sequence of BR's.


This would make sense if the above problem was the cause, yes. Again, I'll check to see if my bug wasn't hiding in plain sight.

Thanks so far,
CipherCraft

Share this post


Link to post
Share on other sites
Quote:
Original post by CipherCraft
Quote:
Original post by ju2wheels
If you remove the BR and then try to access its nextSibling, then I would expect it to be null because it (the BR you just removed) no longer exists, which I also expect is what is happening in your case.


I would expect that I'd thought of that [wink] but I'll check tomorrow at work to see if that's my oversight...


Looks like I'm only removing BRs from their parent if their nextSibling is also a BR; thus reducing a stretch of BR's into just one.


var brs = document.evaluate("//br", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < brs.snapshotLength; i++)
{
var br = brs.snapshotItem(i);
if (br.nextSibling != null && br.nextSibling.nodeName == "br")
{
br.parentNode.removeChild(br);
}
}


But maybe the 'snapshot' request removes all sibling refs? Would be weird since the parent links are still in place...

javascript is not my full time job as you might have gathered, so any help is greatly appreciated. [wink]

tia,
CipherCraft

Share this post


Link to post
Share on other sites
1. Did you try traversing the snapshot backwards? Im not sure how the internal of all this works and how references are kept, but if you remove from front->back you may invalidate the position of everything after the first one you remove where as removing back->front may not invalidate its preceding siblings in your result set (at least not the ones you care about removing).

2. Are the parameters for "//br" and "br" case sensitive? For example, does "//BR" and "BR" produce a different result?

3. Also, Im not sure you if saw it in the Wiki you linked me to, but they prefer to copy the snapshot into a separate array for doing work on, it doesnt say why however... link

Ive never used this but seeing how the documentation describes this as a tree im guessing something might be going wrong with node referencing after a remove is done.

Hope that helps point you in the right direction.

Share this post


Link to post
Share on other sites

This topic is 3566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this