Initial experiment in adding js support to w3m.
This commit is contained in:
127
js/w3m_dom.h
Normal file
127
js/w3m_dom.h
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* w3m DOM Implementation Header
|
||||
*
|
||||
* This file defines the Document Object Model structures and interfaces
|
||||
* for JavaScript integration in w3m. It provides a minimal but functional
|
||||
* DOM representation that maps to w3m's internal buffer structures.
|
||||
*/
|
||||
|
||||
#ifndef W3M_DOM_H
|
||||
#define W3M_DOM_H
|
||||
|
||||
#ifdef USE_JAVASCRIPT
|
||||
|
||||
#include "quickjs/quickjs.h"
|
||||
#include "w3m_javascript.h"
|
||||
|
||||
/* Forward declarations */
|
||||
struct _Buffer;
|
||||
struct Line;
|
||||
struct Anchor;
|
||||
struct FormItem;
|
||||
|
||||
/* Simplified DOM Element Structure */
|
||||
typedef struct W3MElement {
|
||||
char *tagName; /* Element tag name (uppercase) */
|
||||
char *id; /* Element ID attribute */
|
||||
char *className; /* CSS class names */
|
||||
|
||||
/* Tree structure */
|
||||
struct W3MElement *parent;
|
||||
struct W3MElement *firstChild;
|
||||
struct W3MElement *lastChild;
|
||||
struct W3MElement *nextSibling;
|
||||
struct W3MElement *previousSibling;
|
||||
|
||||
/* w3m mappings */
|
||||
struct Line *line; /* Associated line in buffer */
|
||||
int line_pos; /* Position within line */
|
||||
struct Anchor *anchor; /* If element is interactive */
|
||||
struct FormItem *form_item; /* If element is form control */
|
||||
|
||||
/* Attributes and content */
|
||||
struct {
|
||||
char **names;
|
||||
char **values;
|
||||
int count;
|
||||
int capacity;
|
||||
} attributes;
|
||||
|
||||
char *textContent; /* Text content of element */
|
||||
char *innerHTML; /* HTML content (basic) */
|
||||
|
||||
/* JavaScript object reference */
|
||||
JSValue js_object;
|
||||
int js_object_valid;
|
||||
} W3MElement;
|
||||
|
||||
/* Document structure */
|
||||
typedef struct W3MDocument {
|
||||
W3MElement *documentElement; /* <html> element */
|
||||
W3MElement *body; /* <body> element */
|
||||
W3MElement *head; /* <head> element */
|
||||
Buffer *buffer; /* Associated w3m buffer */
|
||||
|
||||
char *title; /* Document title */
|
||||
char *URL; /* Document URL */
|
||||
char *domain; /* Document domain */
|
||||
|
||||
/* Element collections */
|
||||
W3MElement **all_elements; /* All elements in document */
|
||||
int element_count;
|
||||
int element_capacity;
|
||||
|
||||
/* JavaScript object reference */
|
||||
JSValue js_document;
|
||||
} W3MDocument;
|
||||
|
||||
/* DOM Creation and Management */
|
||||
W3MDocument *w3m_dom_create_document(Buffer *buf);
|
||||
void w3m_dom_destroy_document(W3MDocument *doc);
|
||||
W3MElement *w3m_dom_create_element(const char *tagName);
|
||||
void w3m_dom_destroy_element(W3MElement *elem);
|
||||
|
||||
/* DOM Tree Operations */
|
||||
void w3m_dom_append_child(W3MElement *parent, W3MElement *child);
|
||||
void w3m_dom_remove_child(W3MElement *parent, W3MElement *child);
|
||||
void w3m_dom_insert_before(W3MElement *parent, W3MElement *newChild, W3MElement *referenceChild);
|
||||
|
||||
/* Element Search and Access */
|
||||
W3MElement *w3m_dom_get_element_by_id(W3MDocument *doc, const char *id);
|
||||
W3MElement **w3m_dom_get_elements_by_tag_name(W3MDocument *doc, const char *tagName, int *count);
|
||||
W3MElement **w3m_dom_get_elements_by_class_name(W3MDocument *doc, const char *className, int *count);
|
||||
|
||||
/* Attribute Management */
|
||||
const char *w3m_dom_get_attribute(W3MElement *elem, const char *name);
|
||||
void w3m_dom_set_attribute(W3MElement *elem, const char *name, const char *value);
|
||||
void w3m_dom_remove_attribute(W3MElement *elem, const char *name);
|
||||
int w3m_dom_has_attribute(W3MElement *elem, const char *name);
|
||||
|
||||
/* Content Access and Modification */
|
||||
char *w3m_dom_get_text_content(W3MElement *elem);
|
||||
void w3m_dom_set_text_content(W3MElement *elem, const char *text);
|
||||
char *w3m_dom_get_inner_html(W3MElement *elem);
|
||||
void w3m_dom_set_inner_html(W3MElement *elem, const char *html);
|
||||
|
||||
/* Buffer Integration */
|
||||
void w3m_dom_build_from_buffer(W3MDocument *doc, Buffer *buf);
|
||||
void w3m_dom_update_buffer(W3MDocument *doc);
|
||||
W3MElement *w3m_dom_find_element_at_position(W3MDocument *doc, int line_num, int col);
|
||||
|
||||
/* JavaScript Binding */
|
||||
void w3m_dom_bind_to_js(W3MJSContext *ctx, W3MDocument *doc);
|
||||
JSValue w3m_dom_element_to_js(W3MJSContext *ctx, W3MElement *elem);
|
||||
W3MElement *w3m_dom_js_to_element(W3MJSContext *ctx, JSValue val);
|
||||
|
||||
/* JavaScript DOM API Functions */
|
||||
JSValue js_getElementById(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
|
||||
JSValue js_getElementsByTagName(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
|
||||
JSValue js_createElement(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
|
||||
JSValue js_element_getAttribute(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
|
||||
JSValue js_element_setAttribute(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
|
||||
JSValue js_element_get_textContent(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
|
||||
JSValue js_element_set_textContent(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
|
||||
|
||||
#endif /* USE_JAVASCRIPT */
|
||||
|
||||
#endif /* W3M_DOM_H */
|
Reference in New Issue
Block a user