Description
This is regarding the discussion in #17773.
React-DOM currently stringifies DOM attribute values before passing them to Element.setAttribute(NS)
functions. This might be unnecessary, as these functions implicitly stringify attribute values on their own (WebIDL attributes typed as DOMString
). It also makes it difficult to enforce Trusted Types in React applications, as the trusted type objects would be stringified before values reach the DOM sinks.
Currently there is a enableTrustedTypesIntegration
feature flag to disable stringification, but it seems like this behavior can be safely removed for modern browsers with no backwards-compatibility problems. Let me explain:
Attribute stringification was introduced in b0455f4, at that time to workaround a jsdom limitation (jsdom's DOM emulation didn't stringify on its own). IE 8/9 have a similar issue. If an object is passed to a DOM attribute, its value becomes [object]
, ignoring any stringification rules defined in objects' toString
function.
- Jsdom does not have the issue anymore. Since at least 4.0.0 its
setAttribute
function does stringify the values via its IDL layer (runkit demo). - React doesn't support IE 8 anymore.
- The issue still exists for IE9 (contrary to Do we still need to stringify attributes before assigning them? #11735, my tests confirm that the bug still exists, but one needs to try a standard attribute, like
p.title
, and not one with a custom name). - All other browsers, even in their old versions (I tested IEs, Firefox, Chrome, Safari, Opera and a few mobile browsers ) correctly stringify.
I propose to remove the stringification (similar to #17774) unless a browser bug is detected.
That way there is no spurious stringification, and the code branches with the workaround can be removed once buggy browsers stop being supported. My testing shows that only IE9 is affected. The change would be backwards-compatible. I'll send a PR with the proposed change.