Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How this recursive treewalker works?

Post

How this recursive treewalker works?

+1
−4

Credit for User:Meriton for developing the following code (first published here).

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    for (const child of e.childNodes) {
      replaceIn(child);
    }
  }
}

replaceIn(document.body);

How this recursive treewalker works?

As a side question which I grasp as important, can there be an even simpler and more direct version without the else?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

As explained [here](https://software.codidact.com/posts/286333), there are different types of nodes. ... (8 comments)
What is the goal that you want to achieve? Why do you want to avoid using an if-else? (6 comments)
As explained [here](https://software.codidact.com/posts/286333), there are different types of nodes. ...
hkotsubo‭ wrote almost 2 years ago

As explained here, there are different types of nodes. And this code traverses through all document's nodes, but the replace only makes sense in text nodes (here there's an explanation of why only text nodes should be changed - to not destroy HTML's structure).

So the if is needed to know the node's type, and replace only text nodes. If you don't want to test it in the function, the alternative is to first get a list that contains only text nodes, such as an actual tree walker - AKA the code that I suggested here.

deleted user wrote almost 2 years ago

hkotsubo‭ I know that we use the condition to pick the node type but what I meant to ask is if there is a way to do it without else.

Just "pick the node type, run on everything and change where needed". "No elses and no buts" :)

hkotsubo‭ wrote almost 2 years ago

Well, there is: https://software.codidact.com/posts/286304/286334#answer-286334 :-)

If I understood correctly, the piece of code that uses createTreeWalker does that.

deleted user wrote almost 2 years ago · edited almost 2 years ago

hkotsubo‭

I guess you meant

const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
let node;
while ((node = walker.nextNode())) {
    node.textContent = node.textContent.replace(/x/g, '*');
}

This also contains syntax which I generally don't bump into in JavaScript, such as

let node;

As can be read It's not let node = x; which is more "regular".

I am looking for a treewalker with more "regular" JavaScript.

Hopefully you understand what I mean.

hkotsubo‭ wrote almost 2 years ago · edited almost 2 years ago

let node; is "regular JavaScript". It's just declaring a variable without assigning an initial value, which is not uncommon. I didn't assign a value because it wasn't needed, as this will be made in the loop. Of course you could do:

let node = 1;
while ((node = walker.nextNode())) {
   etc...

But why assign a value that would not be used and overwritten right in the next line?

deleted user wrote almost 2 years ago

hkotsubo‭ from my experience with elementary and basic JavaScript tutorials it's not common; that's just my experience as someone which is not a JavaScript expert and just try to learn here and there.

Why? maybe readability (or accessibility, if you will).

I'd prefer to declare the variable inside the loop and with an initial value.

deleted user wrote almost 2 years ago

Oh and perhaps instead more regular I should have said "more common".

hkotsubo‭ wrote almost 2 years ago

deleted user

"from my experience with elementary and basic JavaScript tutorials it's not common"

Well, from my experience (I'm not bragging about it, but it goes beyond basic), it's not uncommon. Regarding readability, well, that's very subjective, and setting an initial value is not always necessary.

By the way, have you tried while ((let node = walker.nextNode()))? This code gives a syntax error, one more reason to declare the variable before the loop. And in this case, there's no real gain in doing this:

let node = someValue;
while ((node = walker.nextNode()))
    ...

Why set someValue to the variable, if this value will be overwritten right in the next line? This value won't be used at all for anything, then setting it to the variable is useless and not necessary.