Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 902 Bytes

working-around-nodevalue-size-limit.md

File metadata and controls

27 lines (21 loc) · 902 Bytes

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.