Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add observer for 'selected' setter of HTMLOptionElement and try to fix issue #746 #810

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ function serializeNode(
}
}
if (tagName === 'option') {
if ((n as HTMLOptionElement).selected) {
if ((n as HTMLOptionElement).selected && !maskInputOptions['select']) {
attributes.selected = true;
} else {
// ignore the html attribute (which corresponds to DOM (n as HTMLOptionElement).defaultSelected)
Expand Down
9 changes: 8 additions & 1 deletion packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,14 @@ function initInputObserver(
userTriggeredOnInput: boolean,
): listenerHandler {
function eventHandler(event: Event) {
const target = getEventTarget(event);
let target = getEventTarget(event);
const userTriggered = event.isTrusted;
/**
* If a site changes the value 'selected' of an option element, the value of its parent element, usually a select element, will be changed as well.
* We can treat this change as a value change of the select element the current target belongs to.
*/
if (target && (target as Element).tagName === 'OPTION')
target = (target as Element).parentElement;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please inline the mechanism as comment at here

BTW, is there any doc/spec pointing out the parent element of option should be select?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, I will add it.

is there any doc/spec pointing out the parent element of option should be select
This is a good point, parent element could also be <optgroup> and <datalist> according to
image captured from OptionElement MDN.
But these two elements aren't hooked for now, so this exception could be excluded by this conditionINPUT_TAGS.indexOf((target as Element).tagName) < 0

if (
!target ||
!(target as Element).tagName ||
Expand Down Expand Up @@ -463,6 +469,7 @@ function initInputObserver(
[HTMLTextAreaElement.prototype, 'value'],
// Some UI library use selectedIndex to set select value
[HTMLSelectElement.prototype, 'selectedIndex'],
[HTMLOptionElement.prototype, 'selected'],
];
if (propertyDescriptor && propertyDescriptor.set) {
handlers.push(
Expand Down
3 changes: 1 addition & 2 deletions packages/rrweb/test/__snapshots__/integration.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6066,8 +6066,7 @@ exports[`record integration tests should not record input values if maskAllInput
\\"type\\": 2,
\\"tagName\\": \\"option\\",
\\"attributes\\": {
\\"value\\": \\"1\\",
\\"selected\\": true
\\"value\\": \\"1\\"
},
\\"childNodes\\": [
{
Expand Down