diff --git a/htmlhint-server/src/server.ts b/htmlhint-server/src/server.ts index d755515..abf21d4 100644 --- a/htmlhint-server/src/server.ts +++ b/htmlhint-server/src/server.ts @@ -1966,6 +1966,105 @@ function createAttrNoDuplicationFix( }; } +/** + * Create auto-fix action for form-method-require rule + */ +function createFormMethodRequireFix( + document: TextDocument, + diagnostic: Diagnostic, +): CodeAction | null { + trace( + `[DEBUG] createFormMethodRequireFix called with diagnostic: ${JSON.stringify(diagnostic)}`, + ); + + if (!diagnostic.data || diagnostic.data.ruleId !== "form-method-require") { + trace( + `[DEBUG] createFormMethodRequireFix: Invalid diagnostic data or ruleId`, + ); + return null; + } + + const text = document.getText(); + const diagnosticOffset = document.offsetAt(diagnostic.range.start); + + // Use robust tag boundary detection to find the form tag + const tagBoundaries = findTagBoundaries(text, diagnosticOffset); + if (!tagBoundaries) { + trace(`[DEBUG] createFormMethodRequireFix: Could not find tag boundaries`); + return null; + } + + const { tagStart, tagEnd } = tagBoundaries; + const tagContent = text.substring(tagStart, tagEnd + 1); + trace(`[DEBUG] createFormMethodRequireFix: Found tag: ${tagContent}`); + + // Verify this is a form tag + const formTagMatch = tagContent.match(/^<\s*form\b/i); + if (!formTagMatch) { + trace(`[DEBUG] createFormMethodRequireFix: Not a form tag`); + return null; + } + + // Check if method attribute already exists + const methodAttrMatch = tagContent.match(/\bmethod\s*=/i); + if (methodAttrMatch) { + trace( + `[DEBUG] createFormMethodRequireFix: Method attribute already exists`, + ); + return null; + } + + // Find the best position to insert the method attribute + // We'll add it after the opening form tag name but before the closing > + const formMatch = tagContent.match(/^(<\s*form)(\s+[^>]*?)?(\/?\s*>)$/i); + if (!formMatch) { + trace( + `[DEBUG] createFormMethodRequireFix: Could not parse form tag structure`, + ); + return null; + } + + const beforeAttrs = formMatch[1]; // "