#!/usr/bin/env node /** * Button Functionality Debug Tool * * Specifically tests button creation and event binding */ const fs = require('fs'); const { JSDOM } = require('jsdom'); function analyzeButtonCode(htmlFile) { const html = fs.readFileSync(htmlFile, 'utf8'); console.log('๐Ÿ”ง Button Functionality Analysis'); console.log('โ”'.repeat(50)); // Extract the showImageEditor method const showImageEditorMatch = html.match(/showImageEditor\([\s\S]*?\n \}/); if (showImageEditorMatch) { const method = showImageEditorMatch[0]; console.log('\n๐Ÿ“‹ showImageEditor Method Analysis:'); // Check button creation pattern const buttonCreationPattern = /buttons\.forEach\([\s\S]*?\}\);/; const hasForEach = buttonCreationPattern.test(method); console.log(` Button forEach loop: ${hasForEach ? 'โœ…' : 'โŒ'}`); // Check arrow function binding const arrowFunctionPattern = /action: \(\) => this\.\w+\(sectionId\)/; const hasArrowBinding = arrowFunctionPattern.test(method); console.log(` Arrow function binding: ${hasArrowBinding ? 'โœ…' : 'โŒ'}`); // Check createButton calls const createButtonPattern = /this\.createButton\(/; const hasCreateButton = createButtonPattern.test(method); console.log(` createButton calls: ${hasCreateButton ? 'โœ…' : 'โŒ'}`); // Check if sectionId is in scope const sectionIdPattern = /sectionId/g; const sectionIdCount = (method.match(sectionIdPattern) || []).length; console.log(` sectionId references: ${sectionIdCount} times`); console.log('\n๐Ÿ” Potential Issues:'); if (!hasArrowBinding) { console.log(' โŒ Arrow function binding missing - buttons may not work'); } if (sectionIdCount < 4) { console.log(' โš ๏ธ Low sectionId usage - may not be passed to all handlers'); } // Extract button definitions const buttonDefsMatch = method.match(/const buttons = \[[\s\S]*?\];/); if (buttonDefsMatch) { console.log('\n๐Ÿ“‹ Button Definitions Found:'); const buttonDefs = buttonDefsMatch[0]; const buttonNames = buttonDefs.match(/'([^']+)'/g) || []; buttonNames.forEach(name => { console.log(` โ€ข ${name.replace(/'/g, '')}`); }); } } else { console.log('โŒ showImageEditor method not found'); } // Check createButton method const createButtonMatch = html.match(/createButton\([\s\S]*?\n \}/); if (createButtonMatch) { const method = createButtonMatch[0]; console.log('\n๐Ÿ“‹ createButton Method Analysis:'); const hasEventListener = method.includes('addEventListener'); console.log(` Event listener attachment: ${hasEventListener ? 'โœ…' : 'โŒ'}`); const hasHandlerParam = method.includes('handler'); console.log(` Handler parameter: ${hasHandlerParam ? 'โœ…' : 'โŒ'}`); if (!hasEventListener || !hasHandlerParam) { console.log(' โŒ createButton method may be broken'); } } } async function testButtonCreation(htmlFile) { console.log('\n๐Ÿงช Testing Button Creation in DOM Environment'); console.log('โ”'.repeat(50)); try { const html = fs.readFileSync(htmlFile, 'utf8'); const dom = new JSDOM(html, { runScripts: "dangerously", resources: "usable", pretendToBeVisual: true }); const { window } = dom; const { document } = window; // Wait for load await new Promise(resolve => { if (document.readyState === 'complete') { resolve(); } else { window.addEventListener('load', resolve); } }); // Wait a bit more for initialization await new Promise(resolve => setTimeout(resolve, 500)); console.log('\n๐Ÿ“Š DOM State after initialization:'); // Check if MarkitectEditor is available const editorAvailable = window.MarkitectEditor !== undefined; console.log(` MarkitectEditor global: ${editorAvailable ? 'โœ…' : 'โŒ'}`); if (editorAvailable) { const editorClasses = Object.keys(window.MarkitectEditor); console.log(` Available classes: ${editorClasses.join(', ')}`); } // Check if container has sections const container = document.getElementById('markdown-content'); if (container) { const sections = container.querySelectorAll('[data-section-id]'); console.log(` Sections created: ${sections.length}`); // Look for image sections let imageCount = 0; sections.forEach(section => { if (section.innerHTML.includes(' 0) { console.log('\n๐Ÿ–ฑ๏ธ Simulating click on image section...'); for (const section of sections) { if (section.innerHTML.includes(' { const imageEditor = document.querySelector('.ui-edit-image-editor-container'); console.log(` Image editor created: ${imageEditor ? 'โœ…' : 'โŒ'}`); if (imageEditor) { const buttons = imageEditor.querySelectorAll('button'); console.log(` Buttons in editor: ${buttons.length}`); buttons.forEach((btn, i) => { console.log(` Button ${i + 1}: "${btn.textContent}"`); // Check if button has click handler const hasHandler = btn.onclick || btn.addEventListener; console.log(` Has handler: ${hasHandler ? 'โœ…' : 'โŒ'}`); }); } }, 100); break; } } } } } catch (error) { console.log(`โŒ DOM testing failed: ${error.message}`); } } // Main execution if (require.main === module) { const htmlFile = process.argv[2] || '/tmp/test_complete_functionality.html'; if (!fs.existsSync(htmlFile)) { console.error(`โŒ File not found: ${htmlFile}`); process.exit(1); } // Analyze the code first analyzeButtonCode(htmlFile); // Test in DOM environment testButtonCreation(htmlFile).then(() => { console.log('\nโœ… Analysis complete'); }).catch(error => { console.error('โŒ Testing failed:', error); }); }