3 ms·
Very nice! And thank you for the correction about innerText. Still, I think that let getText = n => n.text + n.getchildren().map(getText).join('') + n.ta
by kragen 5y ago
Very nice! And thank you for the correction about innerText.
Still, I think that
let getText = n => n.text + n.getchildren().map(getText).join('') + n.tail
is nicer than
let getText=(n,f=(n,a)=>[].map.call(n.childNodes,e=>e.nodeType==3?e.data:f(e,1)))=>f(n,f).flat(1e333).join("")
and not just because it's less strokes.
- easrng 5y agoMost of the reason it's more complicated is because I made it return arrays when recursing and only join at the end because that probably doesn't need as many allocations. The rest of the complication is because the DOM has separate text nodes and the library you're using doesn't seem to. (Oh, and also NodeLists don't have .map so we use the .map from the array prototype) Here's a non-optimized version: let getText = n =>[].map.call(n.childNodes,e => e.nodeType == 3 ? e.data : getText(e)).join("")
- kragen 5y agoRight, I'm saying I think the statically-typed ElementTree approach is superior to the dynamically-typed approach used by the W3C DOM.