fix: track value of inputs and textareas in shadow DOM - #1332
Open
TanbirRamim wants to merge 1 commit into
Open
TanbirRamim wants to merge 1 commit into
TanbirRamim wants to merge 1 commit into
Conversation
The document-level focus and blur listeners used `event.target`, which is retargeted to the shadow host for elements inside a shadow root. So the value interceptor was never installed on those elements, and resetting their value was not picked up by the next `user.type()`. Use the original target from `composedPath()` instead. Fixes testing-library#1314
| e => { | ||
| const el = e.target as Element | ||
| // Focus on elements in a shadow tree is retargeted to the shadow host. | ||
| const el = e.composedPath()[0] as Element |
Contributor
There was a problem hiding this comment.
The first three steps of the composedPath algorithm suggest this could be an empty array
https://dom.spec.whatwg.org/#dom-event-composedpath
one place this can happen is if a synthetic focus or blur is called. Some libraries do this to trigger known side effects, so I think we need to check the length and fall back to target if it's empty
| // In other environments this might be `null` when preparing. | ||
| // istanbul ignore else | ||
| if (document.activeElement) { | ||
| prepareElement(document.activeElement) |
Contributor
There was a problem hiding this comment.
I think these lines need to use getActiveElement instead?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #1314. For an
<input>or<textarea>inside a shadow root, setting.valueafteruser.type()was not picked up, so the nextuser.type()appended to the old text (helloworldinstead ofworld). Thechangeevent on blur was also never dispatched for these elements.Why
The document-level
focusandblurlisteners inprepareDocumentusedevent.target, which is retargeted to the shadow host, so the value interceptor was never installed on the actual element.How
Both listeners now use
event.composedPath()[0]to get the original target. I added tests for value tracking and the blurchangeevent intests/document/index.ts, plus the reporteduser.type()case intests/utility/type.ts; all three fail before the change and pass after, and lint and typecheck are clean. Closed shadow roots still resolve to the host, same as before.Checklist