This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix decorator handling in editOnBeforeInput
Summary: This fixes a pretty widespread bug with decorators introduced by D7941738 and D7977136. ## The problem As far as I can tell, the bug happens in any editor containing a decorator that auto-linkifies content. Here are some repro steps using https://our.intern.facebook.com/intern/editor Diffs: - Type "D12". No link. - Update to "D123". Link appears. - Update to "D1234". Link disappears. - Delete "4". Still no link. - Try to delete "3". Crash. (Typing "D1234a" also causes a crash, as does typing "D1234" and then deleting the "D". Anything that causes the regex to fail does the trick.) URLs: - Type "www.facebook.c". No link. - Update to "www.facebook.co". Link appears. - Update to "www.facebook.com". Link disappears. - Delete "m". Still no link. - Try to delete "o". Crash. You can do the exact same thing in other surfaces as well, including comments on Tasks (although here instead of crashing, the editor just freezes until you click out of and into it again) and the Intern Q&A form. I think this issue has also been reported several times on Github: - draft-js-plugins/draft-js-plugins#1328 - #2133 - #2143 ## Why it happens I don't know enough about DraftJS to know exactly what was causing the problem, but here's what I observed in the DOM when playing around with this: - If your decorator uses a simple `span` or `div` (like in the test plan of D7941738), then native events can update the contents just fine. - However, if your decorator uses anything more complicated (like an `a`), then allowing the native event to go through actually removes the `a` from the DOM. If React later attempts to unmount it, it causes an error. ## The fix The fix is pretty simple. If some text was already decorated and its length changed, always prevent native insertion. This means the decorator React component will always properly re-render. ## Side-effects I assume this could have minor perf implications for simple decorators like the hashtag decorator on the FB composer, because it will prevent native handling for some cases where it wasn't actually causing problems. I'm not sure whether this matters or not, but I don't think it should be worse than what it was before D7941738. Reviewed By: claudiopro Differential Revision: D18259308 fbshipit-source-id: f81b23440f7d9467396f3108a4d940b1ec98ca68
- Loading branch information