#!/usr/bin/env node /** * Script to update error handling in UI handlers * Adds ARIA attributes for better accessibility */ const fs = require('fs'); const path = require('path'); console.log('š§ Updating Error Handling for Accessibility'); console.log('=============================================\n'); const filePath = path.join(__dirname, 'src/http/ui-handlers.js'); let content = fs.readFileSync(filePath, 'utf8'); // Add helper functions after editorialLabel function const helperFunctions = ` function errorMessage(id, text) { return '
' + escapeHtml(text) + '
'; } function inputWithError(id, name, value, errorId, isReadonly, isDisabled) { const readonlyAttr = isReadonly ? 'readonly' : ''; const disabledAttr = isDisabled ? 'disabled' : ''; const ariaInvalid = errorId ? 'aria-invalid="true"' : ''; const ariaDescribedBy = errorId ? 'aria-describedby="error-' + errorId + '"' : ''; return ''; } function textareaWithError(id, name, value, errorId, isReadonly, isDisabled) { const readonlyAttr = isReadonly ? 'readonly' : ''; const disabledAttr = isDisabled ? 'disabled' : ''; const ariaInvalid = errorId ? 'aria-invalid="true"' : ''; const ariaDescribedBy = errorId ? 'aria-describedby="error-' + errorId + '"' : ''; return ''; }`; // Find the editorialLabel function and add helper functions after it const editorialLabelMatch = content.match(/function editorialLabel\(status\) \{[\s\S]*?\}[\s\S]*?(?=function|\/\*\*|module\.exports)/); if (editorialLabelMatch) { content = content.replace(editorialLabelMatch[0], editorialLabelMatch[0] + helperFunctions); console.log('ā Added helper functions for error handling'); } // Update flash messages to use role="alert" content = content.replace( /\${\s*flash\s*\?\s*`/g, '${flash ? `
' ); // Update error messages in forms content = content.replace( /\${\s*msg\s*\?\s*`
/g, '${msg ? errorMessage(\'form-error\', msg.text)}' ); // Add CSS for error states const cssAddition = ` input[aria-invalid="true"], textarea[aria-invalid="true"], select[aria-invalid="true"] { border-color: #b91c1c; box-shadow: 0 0 0 1px #b91c1c; }`; if (!content.includes('input[aria-invalid="true"]')) { content = content.replace( /(\.err \{[^}]+\})/, `$1${cssAddition}` ); console.log('ā Added CSS for error states'); } fs.writeFileSync(filePath, content, 'utf8'); console.log('\nš Error handling updates complete!'); console.log('⨠Forms now have proper ARIA attributes for accessibility');