Not critical to change now but this could this be simplified as follows, as we only need to call entities.encode which the actual value

    entities.encode(nodes[0].text || nodes[0].textContent);