Complete JavaScript integration Phase 4: Full functionality with error-free execution

Major improvements to JavaScript engine integration:

- Fixed script concatenation: JavaScript now executes as complete scripts instead of line-by-line, resolving syntax errors with multi-line constructs
- Enhanced document.write() integration: Content now injects directly into HTML parsing buffer and displays in browser instead of stderr only
- Added comprehensive DOM element methods: All elements now have addEventListener, removeEventListener, and appendChild methods
- Implemented document.body fallback: Created functional document.body object when DOM body element not available
- Fixed HTML body element creation: Added proper DOM body element creation during HTML_BODY tag processing
- Improved HTML environment context passing: JavaScript execution now receives HTML parsing context for proper content injection
- Resolved all major JavaScript errors: Fixed "unexpected end of string", "expecting semicolon", "not a function", and "appendChild undefined" errors

JavaScript integration is now fully functional with error-free execution of complex scripts including DOM manipulation, event listeners, form handling, and dynamic content generation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Storm Dragon
2025-08-23 03:56:20 -04:00
parent a7e04cf28e
commit 9118766ba4
6 changed files with 131 additions and 16 deletions

View File

@@ -254,7 +254,7 @@ w3m_events_handle_click(Buffer *buf, Anchor *anchor)
if (!js_state || !js_state->ctx || !js_state->event_system) return 0;
/* Check if there are click listeners for this element */
if (!w3m_events_has_listener(js_state->event_system, anchor->element, "click")) {
if (!w3m_events_has_listener((W3MEventSystem*)js_state->event_system, anchor->element, "click")) {
return 0; /* No listeners, continue normal processing */
}
@@ -268,7 +268,7 @@ w3m_events_handle_click(Buffer *buf, Anchor *anchor)
event->data.mouse.clientY = 0;
/* Dispatch the event */
int handled = w3m_events_dispatch_event(js_state->event_system, js_state->ctx, event);
int handled = w3m_events_dispatch_event((W3MEventSystem*)js_state->event_system, js_state->ctx, event);
/* Check if default action was prevented */
int prevent_default = event->defaultPrevented;
@@ -314,7 +314,7 @@ w3m_events_handle_page_load(Buffer *buf)
{
/* Phase 1: Execute pending scripts */
if (buf && buf->js_state) {
w3m_js_execute_pending_scripts((BufferJSState *)buf->js_state);
w3m_js_execute_pending_scripts((BufferJSState *)buf->js_state, NULL);
}
}
@@ -361,7 +361,7 @@ w3m_events_bind_to_js(W3MJSContext *ctx, W3MEventSystem *system)
JS_FreeValue(js_ctx, element_proto);
/* Store event system reference in context for use by functions */
ctx->event_system = system;
ctx->event_system = (struct W3MEventSystem*)system;
/* Also store in global object for JavaScript functions to access */
JSValue system_ref = JS_NewObjectClass(js_ctx, 0);