d3.js selection.data and Browser DOM: bound datum visibility across the integration boundary
0 reputation · 11 Nov 2024, 13:01 UTC
0 reputation · 11 Nov 2024, 13:01 UTC
Integrating d3.js data joins with the Browser DOM attaches each datum to a DOM element via a property that is directly accessible from JavaScript and devtools.
The goal is to assess the interoperability trade-off where application data becomes publicly readable through the DOM without a built-in privacy control. The attachment mechanism has been stable across major releases, but lifecycle handling on exit selections and version differences create uncertainty about reference persistence and encapsulation.
Is there a documented way to keep bound data non-public on DOM nodes without breaking joins, transitions, and event handling? Does d3 guarantee clearing of the data reference on detached nodes after an exit selection, or can references linger until garbage collection? What supported interoperability patterns exist when sensitive data must not be exposed through the d3.js and Browser DOM integration boundary?
29775 reputation · 11 Nov 2024, 14:41 UTC
selection.data() method attaches datums to DOM elements via the __data__ property. Because this property exists directly on the DOM object, it is publicly readable through any JavaScript execution or browser developer tools. There is no built-in privacy control within D3 to make this reference private once it has been bound.
D3 does not guarantee the clearing of the __data__ reference on detached nodes after an exit selection. When an element is removed via .remove(), the reference to the datum persists on the node until the node itself is garbage collected. If the node is cached in a variable or remains in a detached fragment, the sensitive data may linger in memory indefinitely.
To prevent exposing sensitive data through the DOM boundary while maintaining joins, transitions, and event handling, consider the following patterns:
selection.exit().each(function(d) {
d3.select(this, null);
}).remove();
WeakMap or a closure-based store during event handlers or transitions.The primary trade-off is between the convenience of D3's declarative data binding and the requirement for data encapsulation. If the data is truly sensitive, the WeakMap approach is the most robust defense against accidental exposure.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.