94 words
1 minute
Working around the size limit for nodeValue in the DOM
Anubhav Gain
2024-04-09

Working around the size limit for nodeValue in the DOM#

TIL that nodeValue in the DOM has a size limit!

I had a table cell element containing HTML-escaped JSON and I was doing this:

data = JSON.parse(td.firstChild.nodeValue);

This was breaking on larger JSON strings. It turns out that beyond a certain length limit browsers break up large chunks of text into multiple DOM text nodes.

The solution, via Stackoverflow, was this:

const getFullNodeText = (el) => {
// https://stackoverflow.com/a/4412151
if (!el) {
return '';
}
if (typeof(el.textContent) != "undefined") {
return el.textContent;
}
return el.firstChild.nodeValue;
};

More details in this issue.

Working around the size limit for nodeValue in the DOM
https://mranv.pages.dev/posts/working-around-the-size-limit-for-nodevalue-in-the-dom/
Author
Anubhav Gain
Published at
2024-04-09
License
CC BY-NC-SA 4.0