Skip to content

fix: Text nodes are not what they seem#596

Merged
route merged 3 commits into
mainfrom
fix-handle_response
Jun 30, 2026
Merged

fix: Text nodes are not what they seem#596
route merged 3 commits into
mainfrom
fix-handle_response

Conversation

@route

@route route commented Jun 29, 2026

Copy link
Copy Markdown
Member

Send all nodes from backend to frontend including hidden whitespace nodes which resolve to node id 0 without this setting on

#594

route added 2 commits June 29, 2026 18:53
@Nakilon

Nakilon commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

For context.

Ferrum may throw Could not find node with given id (Ferrum::NodeNotFoundError) on trying to access "html-ignorable whitespace" text nodes like this: .xpath(".//text()").
<div><span> </span></div> -- this is ok
<div> <span></span></div> -- this is exception
You can't the see cause in Elements tab because this whitespace isn't essentially shown in the DOM tree.

Also when you open local html files, Chrome adds \n before </body> to create the issue no matter what, like this:

"<html lang=\"ru\"><head></head><body><div>text1<span>text2</span></div>\n</body></html>"

Workarounds were:

  1. export outerHTML of the node and parse with Nokogiri
  2. force removal of those whitespaces:
    listitem.evaluate <<~JS
      (function(){
        const nodes = [];
        const walker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT);
        let node
        while (node = walker.nextNode())
          if (!node.nodeValue.trim())
            nodes.push(node);
        nodes.forEach(n => n.remove());
      }).call(this)
    JS

Nakilon added a commit to Nakilon/ferrum that referenced this pull request Jun 30, 2026
@route route force-pushed the fix-handle_response branch from 0b892f7 to 3e41109 Compare June 30, 2026 12:48
@route route force-pushed the fix-handle_response branch from 3e41109 to 3b12cbf Compare June 30, 2026 13:28
@route route merged commit adb34b2 into main Jun 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants