File size: 5,560 Bytes
bbfde3f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #!/usr/bin/env node
// Test document content that simulates the form field from the API response
const testDocumentWithForm = `
<w:document>
<w:body>
<w:p>
<w:r><w:t>Form Example</w:t></w:r>
</w:p>
<w:p>
<w:r><w:t>Please complete the following form:</w:t></w:r>
</w:p>
<w:p>
<w:r>
<w:fldChar w:fldCharType="begin"/>
<w:ffData>
<w:name w:val="Text1"/>
<w:enabled/>
<w:calcOnExit w:val="0"/>
<w:textInput>
<w:type w:val="regular"/>
<w:default w:val=""/>
<w:maxLength w:val="0"/>
<w:format w:val=""/>
</w:textInput>
</w:ffData>
</w:r>
<w:bookmarkStart w:id="1" w:name="Text1"/>
<w:r>
<w:instrText xml:space="preserve"> FORMTEXT </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
<w:bookmarkEnd w:id="1"/>
</w:p>
</w:body>
</w:document>
`;
// Test the improved form detection
function extractTextFromParagraph(paragraph) {
const textMatch = paragraph.match(/<w:t[^>]*>(.*?)<\/w:t>/g);
if (!textMatch) return '';
return textMatch.map(match => match.replace(/<[^>]*>/g, '')).join(' ');
}
function getFormType(formIndex) {
const formTypes = [
'text-field', 'checkbox-field', 'dropdown-field', 'form-data-complete',
'form-data', 'checkbox-control', 'dropdown-control', 'text-input',
'content-control', 'content-control-data', 'field-character',
'formtext-simple', 'formcheckbox-simple', 'formdropdown-simple'
];
return formTypes[formIndex] || 'form-element';
}
function testImprovedFormDetection(documentXml) {
const results = [];
let paragraphCount = 0;
let currentHeading = null;
let approximatePageNumber = 1;
// Updated form detection patterns
const formElements = [
/<w:fldSimple[^>]*FORMTEXT/, // Text form fields
/<w:fldSimple[^>]*FORMCHECKBOX/, // Checkbox form fields
/<w:fldSimple[^>]*FORMDROPDOWN/, // Dropdown form fields
/<w:ffData[\s\S]*?<\/w:ffData>/, // Form field data (complete tags)
/<w:ffData>/, // Form field data (opening tag)
/<w:checkBox/, // Checkbox controls
/<w:dropDownList/, // Dropdown list controls
/<w:textInput/, // Text input controls
/<w:sdt>/, // Structured document tags (content controls)
/<w:sdtContent>/, // Content control content
/<w:fldChar w:fldCharType="begin"\/>/, // Field character begin
/FORMTEXT/, // Simple FORMTEXT detection
/FORMCHECKBOX/, // Simple FORMCHECKBOX detection
/FORMDROPDOWN/ // Simple FORMDROPDOWN detection
];
const paragraphRegex = /<w:p\b[^>]*>[\s\S]*?<\/w:p>/g;
const paragraphs = documentXml.match(paragraphRegex) || [];
paragraphs.forEach((paragraph, index) => {
paragraphCount++;
if (paragraphCount % 15 === 0) {
approximatePageNumber++;
}
if (/<w:pStyle w:val="Heading/.test(paragraph)) {
currentHeading = extractTextFromParagraph(paragraph);
}
formElements.forEach((regex, formIndex) => {
const matches = paragraph.match(regex);
if (matches) {
const formType = getFormType(formIndex);
results.push({
type: formType,
location: `Paragraph ${paragraphCount}`,
approximatePage: approximatePageNumber,
context: currentHeading || 'Document body',
preview: extractTextFromParagraph(paragraph).substring(0, 150),
recommendation: 'Consider using alternative formats like accessible web forms or structured tables instead of Word form fields',
detectedPattern: regex.toString(),
matchedText: matches[0]
});
}
});
});
return results;
}
console.log('π Testing Improved Form Detection');
console.log('==================================\n');
const results = testImprovedFormDetection(testDocumentWithForm);
console.log(`Forms detected: ${results.length}`);
console.log('');
results.forEach((result, index) => {
console.log(`${index + 1}. Form Type: ${result.type}`);
console.log(` Location: ${result.location}`);
console.log(` Context: ${result.context}`);
console.log(` Preview: ${result.preview}`);
console.log(` Pattern: ${result.detectedPattern}`);
console.log(` Matched: ${result.matchedText.substring(0, 50)}...`);
console.log('');
});
if (results.length > 0) {
console.log('β
SUCCESS: Improved form detection is working!');
console.log('The patterns now properly detect form fields in Word documents.');
} else {
console.log('β ISSUE: Form detection still not working');
console.log('Need to further investigate the patterns.');
}
console.log('\nπ Key Improvements Made:');
console.log(' β’ Added complete <w:ffData>...</w:ffData> pattern');
console.log(' β’ Added <w:fldChar w:fldCharType="begin"/> detection');
console.log(' β’ Added simple FORMTEXT/FORMCHECKBOX/FORMDROPDOWN patterns');
console.log(' β’ Removed global flag (g) from regex patterns');
console.log(' β’ Added more comprehensive form element detection'); |