|
| 1 | +import fs from 'fs'; |
| 2 | +import { fileURLToPath } from 'url'; |
| 3 | +import { dirname, join } from 'path'; |
| 4 | +import vm from 'vm'; |
| 5 | + |
| 6 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 7 | + |
| 8 | +// Patch HTMLFormElement.prototype to allow direct access to form fields by name (form.fieldName) |
| 9 | +// This matches browser behavior that jsdom doesn't implement |
| 10 | +const formElementGetter = new Proxy({}, { |
| 11 | + get(target, prop) { |
| 12 | + // Return a getter function for each property |
| 13 | + return function() { |
| 14 | + if (prop in HTMLFormElement.prototype || typeof prop === 'symbol') { |
| 15 | + return undefined; // Let normal prototype chain handle it |
| 16 | + } |
| 17 | + return this.elements.namedItem(prop); |
| 18 | + }; |
| 19 | + } |
| 20 | +}); |
| 21 | + |
| 22 | +// Add a fallback getter for unknown properties |
| 23 | +const originalFormProto = Object.getPrototypeOf(HTMLFormElement.prototype); |
| 24 | +Object.setPrototypeOf(HTMLFormElement.prototype, new Proxy(originalFormProto, { |
| 25 | + get(target, prop, receiver) { |
| 26 | + const value = Reflect.get(target, prop, receiver); |
| 27 | + if (value !== undefined) { |
| 28 | + return value; |
| 29 | + } |
| 30 | + // If property doesn't exist on prototype, try to get it from elements |
| 31 | + if (receiver instanceof HTMLFormElement && typeof prop === 'string') { |
| 32 | + return receiver.elements.namedItem(prop); |
| 33 | + } |
| 34 | + return undefined; |
| 35 | + } |
| 36 | +})); |
| 37 | + |
| 38 | +// Load and execute netteForms.js in global context |
| 39 | +const netteFormsPath = join(__dirname, '../../src/assets/netteForms.js'); |
| 40 | +const netteFormsCode = fs.readFileSync(netteFormsPath, 'utf-8'); |
| 41 | +vm.runInThisContext(netteFormsCode); |
0 commit comments