#!/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);
});
}