fix: iframe input hook (#991)

* fix: iframe input hook (#990)

* Update observer.ts

* format code with prettier

* format all code with prettier

Co-authored-by: luocq3 <luocq3@asiainfo.com>
Co-authored-by: Yun Feng <yun.feng@anu.edu.au>
This commit is contained in:
luocongqiu
2026-04-01 12:00:00 +08:00
committed by GitHub
parent 13277213ae
commit c697294451

View File

@@ -428,28 +428,40 @@ function initInputObserver({
const handlers: Array< const handlers: Array<
listenerHandler | hookResetter listenerHandler | hookResetter
> = events.map((eventName) => on(eventName, eventHandler, doc)); > = events.map((eventName) => on(eventName, eventHandler, doc));
const propertyDescriptor = Object.getOwnPropertyDescriptor( const currentWindow = doc.defaultView;
HTMLInputElement.prototype, if (!currentWindow) {
return () => {
handlers.forEach((h) => h());
};
}
const propertyDescriptor = currentWindow.Object.getOwnPropertyDescriptor(
currentWindow.HTMLInputElement.prototype,
'value', 'value',
); );
const hookProperties: Array<[HTMLElement, string]> = [ const hookProperties: Array<[HTMLElement, string]> = [
[HTMLInputElement.prototype, 'value'], [currentWindow.HTMLInputElement.prototype, 'value'],
[HTMLInputElement.prototype, 'checked'], [currentWindow.HTMLInputElement.prototype, 'checked'],
[HTMLSelectElement.prototype, 'value'], [currentWindow.HTMLSelectElement.prototype, 'value'],
[HTMLTextAreaElement.prototype, 'value'], [currentWindow.HTMLTextAreaElement.prototype, 'value'],
// Some UI library use selectedIndex to set select value // Some UI library use selectedIndex to set select value
[HTMLSelectElement.prototype, 'selectedIndex'], [currentWindow.HTMLSelectElement.prototype, 'selectedIndex'],
[HTMLOptionElement.prototype, 'selected'], [currentWindow.HTMLOptionElement.prototype, 'selected'],
]; ];
if (propertyDescriptor && propertyDescriptor.set) { if (propertyDescriptor && propertyDescriptor.set) {
handlers.push( handlers.push(
...hookProperties.map((p) => ...hookProperties.map((p) =>
hookSetter<HTMLElement>(p[0], p[1], { hookSetter<HTMLElement>(
set() { p[0],
// mock to a normal event p[1],
eventHandler({ target: this as EventTarget } as Event); {
set() {
// mock to a normal event
eventHandler({ target: this as EventTarget } as Event);
},
}, },
}), false,
currentWindow,
),
), ),
); );
} }