Skip to content

Commit c895169

Browse files
committed
feat: add observer for 'selected' setter of HTMLOptionElement and try to fix issue #746
1 parent 9ca3fdc commit c895169

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

packages/rrweb-snapshot/src/snapshot.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ let canvasCtx: CanvasRenderingContext2D | null;
8080

8181
function initCanvasService(doc: Document) {
8282
if (!canvasService) {
83-
canvasService = doc.createElement('canvas');
83+
canvasService = doc.createElement('canvas');
8484
}
8585
if (!canvasCtx) {
86-
canvasCtx = canvasService.getContext('2d');
86+
canvasCtx = canvasService.getContext('2d');
8787
}
8888
canvasService.width = 0;
8989
canvasService.height = 0;
@@ -502,7 +502,7 @@ function serializeNode(
502502
}
503503
}
504504
if (tagName === 'option') {
505-
if ((n as HTMLOptionElement).selected) {
505+
if ((n as HTMLOptionElement).selected && !maskInputOptions['select']) {
506506
attributes.selected = true;
507507
} else {
508508
// ignore the html attribute (which corresponds to DOM (n as HTMLOptionElement).defaultSelected)
@@ -516,7 +516,7 @@ function serializeNode(
516516
}
517517
// save image offline
518518
if (tagName === 'img' && inlineImages && canvasService && canvasCtx) {
519-
const image = (n as HTMLImageElement);
519+
const image = n as HTMLImageElement;
520520
image.crossOrigin = 'anonymous';
521521
try {
522522
canvasService.width = image.naturalWidth;

packages/rrweb/src/record/observer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,10 @@ function initInputObserver(
369369
userTriggeredOnInput: boolean,
370370
): listenerHandler {
371371
function eventHandler(event: Event) {
372-
const target = getEventTarget(event);
372+
let target = getEventTarget(event);
373373
const userTriggered = event.isTrusted;
374+
if (target && (target as Element).tagName === 'OPTION')
375+
target = (target as Element).parentElement;
374376
if (
375377
!target ||
376378
!(target as Element).tagName ||
@@ -461,6 +463,7 @@ function initInputObserver(
461463
[HTMLTextAreaElement.prototype, 'value'],
462464
// Some UI library use selectedIndex to set select value
463465
[HTMLSelectElement.prototype, 'selectedIndex'],
466+
[HTMLOptionElement.prototype, 'selected'],
464467
];
465468
if (propertyDescriptor && propertyDescriptor.set) {
466469
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)