const fs = require('fs');
const JSZip = require('jszip');
// Test forms and flashing objects detection
async function testFormsAndFlashingDetection() {
console.log('=== Testing Forms and Flashing Objects Detection ===\n');
try {
// Create test content with forms and animations
const testContent = `
Document with Forms
Enter your name:
I agree to terms
Check this box
Content control field
Animated Content Section
Flashing color animation
Rotating element
Looping video content
`;
console.log('๐งช Testing with Content Containing:');
console.log(' - Text form field (FORMTEXT)');
console.log(' - Checkbox form field (FORMCHECKBOX)');
console.log(' - Checkbox control');
console.log(' - Content control (SDT)');
console.log(' - Color animation (flashing)');
console.log(' - Rotation animation');
console.log(' - Looping video\n');
const results = testFormsAndFlashingAnalysis(testContent);
console.log('๐ Forms Detection Results:');
console.log(` Total forms found: ${results.formsDetected.length}`);
console.log(` Expected: 4 form elements\n`);
results.formsDetected.forEach((form, index) => {
console.log(` ${index + 1}. Form Type: ${form.type}`);
console.log(` Location: ${form.location}`);
console.log(` Context: ${form.context}`);
console.log(` Preview: "${form.preview}"`);
console.log(` Recommendation: ${form.recommendation}`);
console.log('');
});
console.log('๐ Flashing Objects Detection Results:');
console.log(` Total flashing objects found: ${results.flashingObjects.length}`);
console.log(` Expected: 3 animated elements\n`);
results.flashingObjects.forEach((flash, index) => {
console.log(` ${index + 1}. Animation Type: ${flash.type}`);
console.log(` Location: ${flash.location}`);
console.log(` Context: ${flash.context}`);
console.log(` Preview: "${flash.preview}"`);
console.log(` Recommendation: ${flash.recommendation}`);
console.log('');
});
if (results.formsDetected.length >= 4 && results.flashingObjects.length >= 3) {
console.log('โ
Forms and Flashing Objects detection test PASSED!');
console.log(' All expected form fields and animated content detected.');
} else {
console.log('โ Detection test FAILED!');
console.log(` Expected: 4+ forms, 3+ animations`);
console.log(` Got: ${results.formsDetected.length} forms, ${results.flashingObjects.length} animations`);
}
} catch (error) {
console.error('โ Test failed:', error.message);
}
}
// Test function for forms and flashing analysis
function testFormsAndFlashingAnalysis(documentXml) {
const results = {
formsDetected: [],
flashingObjects: []
};
let paragraphCount = 0;
let currentHeading = null;
// Extract text from XML
function extractTextFromParagraph(xml) {
const textMatches = xml.match(/]*>(.*?)<\/w:t>/g);
if (!textMatches) return '';
return textMatches.map(t => t.replace(/]*>|<\/w:t>/g, '')).join('').trim();
}
const paragraphRegex = /]*>[\s\S]*?<\/w:p>/g;
const paragraphs = documentXml.match(paragraphRegex) || [];
paragraphs.forEach((paragraph, index) => {
paragraphCount++;
// Track headings
const headingMatch = paragraph.match(//);
if (headingMatch) {
const headingText = extractTextFromParagraph(paragraph);
currentHeading = `${headingMatch[1]}: ${headingText.substring(0, 50)}`;
}
// Check for form fields
const formElements = [
{ regex: /]*fldChar[^>]*FORMTEXT/, type: 'text-field' },
{ regex: /]*fldChar[^>]*FORMCHECKBOX/, type: 'checkbox-field' },
{ regex: //, type: 'checkbox-control' },
{ regex: //, type: 'content-control' }
];
formElements.forEach(({ regex, type }) => {
if (regex.test(paragraph)) {
results.formsDetected.push({
type: type,
location: `Paragraph ${paragraphCount}`,
approximatePage: 1,
context: currentHeading || 'Document body',
preview: extractTextFromParagraph(paragraph).substring(0, 150),
recommendation: 'Consider using alternative formats like accessible web forms instead of Word form fields'
});
}
});
// Check for flashing/animated content
const flashingElements = [
{ regex: /]*loop/, type: 'looping-video' }
];
flashingElements.forEach(({ regex, type }) => {
if (regex.test(paragraph)) {
results.flashingObjects.push({
type: type,
location: `Paragraph ${paragraphCount}`,
approximatePage: 1,
context: currentHeading || 'Document body',
preview: extractTextFromParagraph(paragraph).substring(0, 150) || 'Animated content detected',
recommendation: 'Remove animated/flashing content to prevent seizures and improve accessibility'
});
}
});
});
return results;
}
testFormsAndFlashingDetection();