|
| 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); |
| 42 | + |
| 43 | +// Load and execute repeater.js in global context |
| 44 | +const repeaterPath = join(__dirname, '../../src/assets/repeater.js'); |
| 45 | +const repeaterCode = fs.readFileSync(repeaterPath, 'utf-8'); |
| 46 | +vm.runInThisContext(repeaterCode); |
| 47 | + |
| 48 | +// Fix jsdom select element behavior to match browser |
| 49 | +// In browsers, the first option is selected by default if no selected attribute is set |
| 50 | +const originalInnerHTMLSetter = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML').set; |
| 51 | +Object.defineProperty(Element.prototype, 'innerHTML', { |
| 52 | + set: function(value) { |
| 53 | + originalInnerHTMLSetter.call(this, value); |
| 54 | + // Synchronously fix all select elements in the inserted HTML |
| 55 | + const selects = this.querySelectorAll('select'); |
| 56 | + selects.forEach(select => { |
| 57 | + if (!select.hasAttribute('multiple') && select.options.length > 0 && select.selectedIndex === -1) { |
| 58 | + select.selectedIndex = 0; |
| 59 | + } |
| 60 | + }); |
| 61 | + // Reset all form fields to their default values to fix jsdom's defaultValue tracking |
| 62 | + const inputs = this.querySelectorAll('input, textarea'); |
| 63 | + inputs.forEach(input => { |
| 64 | + if (input.type === 'checkbox' || input.type === 'radio') { |
| 65 | + input.defaultChecked = input.hasAttribute('checked'); |
| 66 | + input.checked = input.defaultChecked; |
| 67 | + } else { |
| 68 | + const attrValue = input.getAttribute('value'); |
| 69 | + input.defaultValue = attrValue !== null ? attrValue : ''; |
| 70 | + if (input.value === '') { |
| 71 | + input.value = input.defaultValue; |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + }, |
| 76 | + configurable: true |
| 77 | +}); |
0 commit comments