Skip to content

Commit 156b760

Browse files
authored
feat: add observer for 'selected' setter of HTMLOptionElement and try to fix issue #746 (#810)
* feat: add observer for 'selected' setter of HTMLOptionElement and try to fix issue #746 * style: add description of mechanism
1 parent 0c27ad2 commit 156b760

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

packages/rrweb-snapshot/src/snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ function serializeNode(
491491
}
492492
}
493493
if (tagName === 'option') {
494-
if ((n as HTMLOptionElement).selected) {
494+
if ((n as HTMLOptionElement).selected && !maskInputOptions['select']) {
495495
attributes.selected = true;
496496
} else {
497497
// ignore the html attribute (which corresponds to DOM (n as HTMLOptionElement).defaultSelected)

packages/rrweb/src/record/observer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,14 @@ function initInputObserver(
371371
userTriggeredOnInput: boolean,
372372
): listenerHandler {
373373
function eventHandler(event: Event) {
374-
const target = getEventTarget(event);
374+
let target = getEventTarget(event);
375375
const userTriggered = event.isTrusted;
376+
/**
377+
* 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.
378+
* We can treat this change as a value change of the select element the current target belongs to.
379+
*/
380+
if (target && (target as Element).tagName === 'OPTION')
381+
target = (target as Element).parentElement;
376382
if (
377383
!target ||
378384
!(target as Element).tagName ||
@@ -463,6 +469,7 @@ function initInputObserver(
463469
[HTMLTextAreaElement.prototype, 'value'],
464470
// Some UI library use selectedIndex to set select value
465471
[HTMLSelectElement.prototype, 'selectedIndex'],
472+
[HTMLOptionElement.prototype, 'selected'],
466473
];
467474
if (propertyDescriptor && propertyDescriptor.set) {
468475
handlers.push(

packages/rrweb/test/__snapshots__/integration.test.ts.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6066,8 +6066,7 @@ exports[`record integration tests should not record input values if maskAllInput
60666066
\\"type\\": 2,
60676067
\\"tagName\\": \\"option\\",
60686068
\\"attributes\\": {
6069-
\\"value\\": \\"1\\",
6070-
\\"selected\\": true
6069+
\\"value\\": \\"1\\"
60716070
},
60726071
\\"childNodes\\": [
60736072
{

0 commit comments

Comments
 (0)