mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-02-07 13:16:05 -05:00
This might break things, and in the scale of things, probably doesn't save enough memory, BUT...if you look at the most common text nones on a page, you'll likely see hundreds or thousands of nodes containing just space and newline. On an Amazon product page, for example, there are 80 text nodes containing just a newline + 80 spaces. In fact, there are over 4000 text nodes containing just whitespace. Ideally, we could simply ignore them and not even generate the Node. But that's likely to break some JavaScript (e.g. it would mess up the child count, the firstChild, etc...). So this simply attempts to collapse the leading and trailing whitespace (it doesn't trim them, it merely replaces \s+ with " "). This could _still_ break some JavaScript, but seems safer. The real win is here is that " " gets interned, so doesn't require an dupe/allocation.