From fc44f8301a5e7634e1b5c88dfd2a80be92509851 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Sun, 1 Nov 2026 11:21:06 -0500 Subject: [PATCH 01/86] docs: plan the Phase 12 test split and characterization sweep, and define form member --- GLOSSARY.md | 4 ++++ docs/plans/modernization.md | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/GLOSSARY.md b/GLOSSARY.md index e72d2b0..24b6c92 100644 --- a/GLOSSARY.md +++ b/GLOSSARY.md @@ -11,3 +11,7 @@ _Avoid_: master, develop, next (as standing integration branches), trunk (as a b **v4 line**: The 4.x compatibility release line (legacy jQuery and shim support retained). After 5.0 ships, patches still belong to this line. _Avoid_: old major, legacy branch (when meaning the release line rather than a git ref) + +**form member**: +A property or method of a `
` element (such as `elements`, `ownerDocument`, `getAttribute`, `submit`, `reset`) as the DOM defines it, read or called through the DOM prototypes so that a control with the same name cannot shadow it. +_Avoid_: form property (a control named like it replaces `form.name`), form attribute (the HTML attribute is separate) diff --git a/docs/plans/modernization.md b/docs/plans/modernization.md index 2d59fc3..cbce32d 100644 --- a/docs/plans/modernization.md +++ b/docs/plans/modernization.md @@ -357,6 +357,10 @@ Release day, in one sitting: ## Phase 12: 4.x cleanup and rearchitecture (no breaking change) +- First, split `test/test.js` into one file per area under `test/suite/`. Move tests only: names and + bodies stay the same, and the test-name list before and after must match. +- Then pin current behavior with characterization tests, area by area, before any refactor. Bugs + found this way become `it.fails` records that describe the wanted behavior. - Split `src/jquery.form.js` into source modules: serialization, field helpers, XHR transport, iframe transport, `ajaxForm` binding. Rollup emits the same `dist` files, and the surface tests prove ADR-0001 still holds. - Lower the complexity hot spots that `npm run metrics` reports. - Additive options and docs: From 8164d6c05e2e8132fb622a09a39ad06d583c5a56 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Sun, 1 Nov 2026 11:24:01 -0500 Subject: [PATCH 02/86] test: split test/test.js into one file per area under test/suite Moves tests only; the full test names match on jQuery 1.7.2, 3.7.1 and 4.0.0. --- eslint.config.mjs | 2 +- test/suite/ajax-form.test.js | 428 +++ test/suite/diagnostics.test.js | 72 + test/suite/field-helpers.test.js | 401 +++ test/suite/helpers.js | 110 + test/suite/iframe-transport.test.js | 1135 ++++++++ test/suite/legacy-jquery.test.js | 79 + test/suite/serialization.test.js | 516 ++++ test/suite/submission.test.js | 759 ++++++ test/suite/xhr-transport.test.js | 315 +++ test/test.js | 3755 --------------------------- vitest.config.mjs | 4 +- 12 files changed, 3818 insertions(+), 3758 deletions(-) create mode 100644 test/suite/ajax-form.test.js create mode 100644 test/suite/diagnostics.test.js create mode 100644 test/suite/field-helpers.test.js create mode 100644 test/suite/helpers.js create mode 100644 test/suite/iframe-transport.test.js create mode 100644 test/suite/legacy-jquery.test.js create mode 100644 test/suite/serialization.test.js create mode 100644 test/suite/submission.test.js create mode 100644 test/suite/xhr-transport.test.js delete mode 100644 test/test.js diff --git a/eslint.config.mjs b/eslint.config.mjs index 74b6fe9..3059bb5 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -90,7 +90,7 @@ export default [ rules: sourceRules, }, { - files: ["test/test.js"], + files: ["test/suite/**/*.js"], languageOptions: { sourceType: "module", globals: { ...globals.browser, $: "readonly", jQuery: "readonly" }, diff --git a/test/suite/ajax-form.test.js b/test/suite/ajax-form.test.js new file mode 100644 index 0000000..d860d4d --- /dev/null +++ b/test/suite/ajax-form.test.js @@ -0,0 +1,428 @@ +import { assert, describe, it } from "vitest"; +import { resetFixture, arrayValue, submitAndSettle } from "./helpers.js"; + +describe("form", function () { + resetFixture(); + + describe("ajaxForm", function () { + it("should pass URL shorthand options to ajaxSubmit", function () { + const $form = $('').appendTo("#main"); + const ajaxSubmit = $.fn.ajaxSubmit; + const success = function () {}; + let submittedOptions; + $.fn.ajaxSubmit = function (options) { + submittedOptions = options; + return this; + }; + + try { + $form.ajaxForm("/shorthand", { source: "test" }, "json", success); + $form.trigger("submit"); + + assert.strictEqual(submittedOptions.url, "/shorthand"); + assert.deepEqual(submittedOptions.data, { source: "test" }); + assert.strictEqual(submittedOptions.dataType, "json"); + assert.strictEqual(submittedOptions.success, success); + } finally { + $.fn.ajaxSubmit = ajaxSubmit; + } + }); + + it("should accept false as the shorthand URL without a success callback", function () { + const $form = $("
").appendTo("#main"); + const ajaxSubmit = $.fn.ajaxSubmit; + let submittedOptions; + $.fn.ajaxSubmit = function (options) { + submittedOptions = options; + return this; + }; + + try { + $form.ajaxForm(false); + $form.trigger("submit"); + + assert.strictEqual(submittedOptions.url, false); + assert.strictEqual(submittedOptions.success, undefined); + } finally { + $.fn.ajaxSubmit = ajaxSubmit; + } + }); + + it("should return an empty selection without binding handlers", function () { + const $missing = $(".missing-ajax-form"); + + const result = $missing.ajaxForm(); + + assert.strictEqual(result, $missing); + assert.strictEqual($._data(document, "events"), undefined); + }); + + describe("before the DOM is ready", function () { + // These tests fake `$.isReady`. jQuery 1.7.2 then re-fires its ready handlers, which closes + // the test page, so they only run where the collection has no native selector metadata. + const it = globalThis.it.skipIf(Number($.fn.jquery.split(".")[0]) < 3); + + function withDomNotReady(run) { + const isReady = $.isReady; + $.isReady = false; + + try { + return run(); + } finally { + $.isReady = isReady; + } + } + + it("should bind once ready when old jQuery selector metadata is present", async function () { + const $pending = $(".queued-ajax-form"); + $pending.selector = ".queued-ajax-form"; + + const result = withDomNotReady(() => $pending.ajaxForm()); + const $form = $('
').appendTo("#main"); + await new Promise((resolve) => setTimeout(resolve, 0)); + + assert.strictEqual(result, $pending); + assert.strictEqual($._data($form[0], "events").submit.length, 1); + }); + + it("should not queue an empty selection without selector metadata", async function () { + const $pending = $(".unqueued-ajax-form"); + + const result = withDomNotReady(() => $pending.ajaxForm()); + const $form = $('
').appendTo("#main"); + await new Promise((resolve) => setTimeout(resolve, 0)); + + assert.strictEqual(result, $pending); + assert.strictEqual($._data($form[0], "events"), undefined); + }); + }); + + it("should call beforeFormUnbind before replacing form handlers", function () { + const $form = $('
').appendTo("#main"); + const order = []; + $form.on("submit.form-plugin", function () { + order.push("old handler"); + }); + + $form.ajaxForm({ + beforeFormUnbind: function (forms, options) { + order.push("before unbind"); + assert.strictEqual(forms, $form); + assert.strictEqual(options.beforeFormUnbind instanceof Function, true); + assert.strictEqual($._data($form[0], "events").submit.length, 1); + }, + }); + + assert.deepEqual(order, ["before unbind"]); + assert.strictEqual($._data($form[0], "events").submit.length, 1); + }); + + it("should leave a canceled submit untouched", function () { + const $form = $('
').appendTo("#main"); + const ajaxSubmit = $.fn.ajaxSubmit; + let submissions = 0; + $.fn.ajaxSubmit = function () { + submissions++; + return this; + }; + $form.on("submit", function (event) { + event.preventDefault(); + }); + + try { + const event = $.Event("submit"); + $form.ajaxForm(); + $form.trigger(event); + + assert.strictEqual(event.isDefaultPrevented(), true); + assert.strictEqual(submissions, 0); + } finally { + $.fn.ajaxSubmit = ajaxSubmit; + } + }); + + it("should capture a submit button when nested content is clicked", function () { + const $form = $( + '
' + ).appendTo("#main"); + const button = $form.find("button")[0]; + + $form.ajaxForm(); + $form.find("span").trigger("click"); + + assert.strictEqual($form[0].clk, button); + }); + + // #588: a button without a type attribute is a submit button. + it("should capture a button without a type attribute", function () { + const $form = $( + '
' + ).appendTo("#main"); + const button = $form.find("button")[0]; + + $form.ajaxForm(); + $form.find("span").trigger("click"); + + assert.strictEqual($form[0].clk, button); + }); + + it("should ignore clicks outside submit controls", function () { + const $form = $("
Not a submit control
").appendTo("#main"); + + $form.ajaxForm(); + $form.find("span").trigger("click"); + + assert.strictEqual($form[0].clk, undefined); + }); + + [ + ["a text input", ''], + ["an element with a submit type attribute", 'Send'], + [ + "a submit button that belongs to no form", + '', + ], + ].forEach(function ([label, html]) { + it("should ignore clicks on " + label, function () { + const $form = $("
").append(html).appendTo("#main"); + $form.ajaxForm(); + + const click = () => $form.children().trigger("click"); + + assert.doesNotThrow(click); + assert.strictEqual($form[0].clk, undefined); + }); + }); + + // jQuery 3.7 removed the selection selector needed to scope delegated handlers. + it.fails("should scope delegated handlers to the selected forms", function () { + const $selected = $('
').appendTo("#main"); + const $unselected = $("
").appendTo("#main"); + const ajaxSubmit = $.fn.ajaxSubmit; + let submissions = 0; + $.fn.ajaxSubmit = function () { + submissions++; + return this; + }; + + try { + $selected.ajaxForm({ delegation: true }); + $unselected.trigger("submit"); + + assert.strictEqual(submissions, 0); + } finally { + $(document).off(".form-plugin"); + $.fn.ajaxSubmit = ajaxSubmit; + } + }); + + it("should capture image coordinates supplied by the click event", function () { + const $form = $('
').appendTo("#main"); + const $image = $form.find("input"); + const event = $.Event("click"); + event.offsetX = 14; + event.offsetY = 27; + + $form.ajaxForm(); + $image.trigger(event); + + assert.strictEqual($form[0].clk, $image[0]); + assert.strictEqual($form[0].clk_x, 14); + assert.strictEqual($form[0].clk_y, 27); + }); + + it("should derive image coordinates from its document offset", function () { + const $form = $('
').appendTo("#main"); + const $image = $form.find("input"); + const offset = $.fn.offset; + $.fn.offset = function () { + return { left: 10, top: 20 }; + }; + const event = $.Event("click"); + event.pageX = 31; + event.pageY = 58; + + try { + $form.ajaxForm(); + $image.trigger(event); + + assert.strictEqual($form[0].clk_x, 21); + assert.strictEqual($form[0].clk_y, 38); + } finally { + $.fn.offset = offset; + } + }); + + it("should derive image coordinates from element offsets without the offset helper", function () { + const $form = $('
').appendTo("#main"); + const $image = $form.find("input"); + const offset = $.fn.offset; + $.fn.offset = undefined; + Object.defineProperties($image[0], { + offsetLeft: { configurable: true, value: 7 }, + offsetTop: { configurable: true, value: 9 }, + }); + const event = $.Event("click"); + event.pageX = 19; + event.pageY = 24; + + try { + $form.ajaxForm(); + $image.trigger(event); + + assert.strictEqual($form[0].clk_x, 12); + assert.strictEqual($form[0].clk_y, 15); + } finally { + $.fn.offset = offset; + } + }); + + it("should clear captured submit details after the click window", async function () { + const $form = $('
').appendTo("#main"); + const $image = $form.find("input"); + const event = $.Event("click"); + event.offsetX = 4; + event.offsetY = 6; + $form.ajaxForm(); + $image.trigger(event); + + await new Promise((resolve) => setTimeout(resolve, 120)); + + assert.strictEqual($form[0].clk, null); + assert.strictEqual($form[0].clk_x, null); + assert.strictEqual($form[0].clk_y, null); + }); + + it("should call beforeSubmit and then success when the submit button is clicked", async function () { + const opts = {}; + + const seen = await submitAndSettle(opts, () => { + $("#form4").ajaxForm(opts); + $("#submitForm4")[0].click(); + }); + + assert.ok(seen.beforeSubmit, "pre-submit callback"); + assert.ok(seen.beforeSubmit.data.constructor == Array, "type check"); + assert.ok(seen.beforeSubmit.$form.jquery, "type check jQuery"); + assert.ok(seen.success, "post-submit callback"); + }); + + it("should include the value of the clicked submit button", async function () { + const opts = {}; + + const seen = await submitAndSettle(opts, () => { + $("#form4").ajaxForm(opts); + $("#submitForm4withName")[0].click(); + }); + + assert.ok(seen.beforeSubmit, "pre-callback"); + assert.ok(seen.beforeSubmit.data.constructor == Array, "type check"); + assert.ok(seen.beforeSubmit.$form.jquery, "type check jQuery"); + assert.ok( + arrayValue(seen.beforeSubmit.data, "form4inputName") !== undefined, + "submit button" + ); + }); + + it("should include the coordinates of a clicked image submit", async function () { + const opts = {}; + + const seen = await submitAndSettle(opts, () => { + $("#form4").ajaxForm(opts); + $("#form4imageSubmit")[0].click(); + }); + + assert.ok(seen.beforeSubmit, "pre-callback"); + assert.ok(seen.beforeSubmit.data.constructor == Array, "type check"); + assert.ok(seen.beforeSubmit.$form.jquery, "type check jQuery"); + assert.ok(arrayValue(seen.beforeSubmit.data, "myImage.x") !== undefined, "x coord"); + assert.ok(arrayValue(seen.beforeSubmit.data, "myImage.y") !== undefined, "y coord"); + }); + + it("should include image coordinates when semantic is true", async function () { + const opts = { semantic: true }; + + const seen = await submitAndSettle(opts, () => { + $("#form4").ajaxForm(opts); + $("#form4imageSubmit")[0].click(); + }); + + assert.ok(seen.beforeSubmit, "pre-callback"); + assert.ok(seen.beforeSubmit.data.constructor == Array, "type check"); + assert.ok(seen.beforeSubmit.$form.jquery, "type check jQuery"); + assert.ok(arrayValue(seen.beforeSubmit.data, "myImage.x") !== undefined, "x coord"); + assert.ok(arrayValue(seen.beforeSubmit.data, "myImage.y") !== undefined, "y coord"); + }); + + it("should load the response into the target div", async function () { + $("#targetDiv").empty(); + const opts = { target: "#targetDiv" }; + + const seen = await submitAndSettle(opts, () => { + $("#form4").ajaxForm(opts); + $("#submitForm4")[0].click(); + }); + + assert.ok(seen.beforeSubmit, "pre-callback"); + assert.ok(seen.beforeSubmit.data.constructor == Array, "type check"); + assert.ok(seen.beforeSubmit.$form.jquery, "type check jQuery"); + assert.ok(seen.success, "post-callback"); + assert.ok($("#targetDiv").text().match("Lorem ipsum"), "targetDiv updated"); + }); + + it("should submit once when ajaxForm is bound several times", async function () { + $("#targetDiv").empty(); + let beforeSubmitCalls = 0; + let successCalls = 0; + const opts = { + target: "#targetDiv", + beforeSubmit: function () { + beforeSubmitCalls++; + }, + success: function () { + successCalls++; + }, + }; + const settled = new Promise((resolve) => { + const success = opts.success; + opts.success = function () { + success.apply(this, arguments); + // Let any duplicate submissions from extra bindings land before asserting. + setTimeout(resolve, 100); + }; + }); + + $("#form8").ajaxForm(opts).ajaxForm(opts).ajaxForm(opts); + $("#submitForm8")[0].click(); + await settled; + + assert.strictEqual(beforeSubmitCalls, 1, "pre-callback"); + assert.strictEqual(successCalls, 1, "post-callback"); + }); + + it("should not submit after ajaxFormUnbind", async function () { + $("#targetDiv").empty(); + let callbacks = 0; + const opts = { + target: "#targetDiv", + beforeSubmit: function () { + callbacks++; + }, + success: function () { + callbacks++; + }, + }; + $("#form9") + .ajaxForm(opts) + .submit(function () { + return false; + }); + + $("#form9").ajaxFormUnbind(opts); + $("#submitForm9")[0].click(); + await new Promise((resolve) => setTimeout(resolve, 500)); + + assert.strictEqual(callbacks, 0, "no callbacks after unbind"); + }); + }); +}); diff --git a/test/suite/diagnostics.test.js b/test/suite/diagnostics.test.js new file mode 100644 index 0000000..b2b288a --- /dev/null +++ b/test/suite/diagnostics.test.js @@ -0,0 +1,72 @@ +import { assert, describe, it } from "vitest"; +import { resetFixture } from "./helpers.js"; + +describe("form", function () { + resetFixture(); + + describe("debug logging", function () { + function logWithDebug(globals) { + const saved = {}; + Object.keys(globals).forEach(function (name) { + saved[name] = Object.getOwnPropertyDescriptor(window, name); + Object.defineProperty(window, name, { + configurable: true, + writable: true, + value: globals[name], + }); + }); + $.fn.ajaxSubmit.debug = true; + + try { + $(".missing-form").ajaxSubmit(); + } finally { + $.fn.ajaxSubmit.debug = false; + Object.keys(globals).forEach(function (name) { + if (saved[name]) { + Object.defineProperty(window, name, saved[name]); + } else { + delete window[name]; + } + }); + } + } + + const message = "[jquery.form] ajaxSubmit: skipping submit process - no element selected"; + + it("should write to console.log when debug is enabled", function () { + const logged = []; + + logWithDebug({ console: { log: (text) => logged.push(text) } }); + + assert.deepEqual(logged, [message]); + }); + + it("should fall back to opera.postError without console.log", function () { + const posted = []; + + logWithDebug({ console: {}, opera: { postError: (text) => posted.push(text) } }); + + assert.deepEqual(posted, [message]); + }); + + it("should stay silent without any logging target", function () { + const run = () => logWithDebug({ console: undefined, opera: {} }); + + assert.doesNotThrow(run); + }); + + it("should stay silent when debug is disabled", function () { + const logged = []; + const log = console.log; + console.log = (text) => logged.push(text); + + try { + $(".missing-form").ajaxSubmit(); + } finally { + console.log = log; + } + + assert.deepEqual(logged, []); + }); + }); +}); diff --git a/test/suite/field-helpers.test.js b/test/suite/field-helpers.test.js new file mode 100644 index 0000000..88fe5dd --- /dev/null +++ b/test/suite/field-helpers.test.js @@ -0,0 +1,401 @@ +import { assert, describe, it } from "vitest"; +import { resetFixture } from "./helpers.js"; + +describe("form", function () { + resetFixture(); + + describe("resetForm", function () { + it("should reset the control a label targets and its nested fields", function () { + const $target = $('').appendTo("#main"); + const $label = $('').appendTo( + "#main" + ); + $target.val("changed"); + $label.find("input").val("changed"); + + $label.resetForm(); + + assert.strictEqual($target.val(), "default"); + assert.strictEqual($label.find("input").val(), "nested"); + }); + + // #600: the document and text nodes have no tag name. + it("should reset the fields of a document and skip text nodes", function () { + const $input = $('').appendTo("#main").val("changed"); + const $nodes = $([document, document.createTextNode("text")]); + + const reset = () => $nodes.resetForm(); + + assert.doesNotThrow(reset); + assert.strictEqual($input.val(), "default"); + }); + + [ + ["starts with markup", ''], + ["contains markup", 'x '], + ].forEach(function ([label, value]) { + it("should not parse a for attribute that " + label + " as HTML", async function () { + window.labelPayload = 0; + const $label = $("