we (web engine): Experimental web browser project to understand the limits of Claude

DOM-JS bridge: document object and element access #107

open opened by pierrelf.com

Create the bridge between the JS engine and the DOM crate. Expose the document global and implement core element access APIs.

Scope#

Architecture#

  • The DOM Document (arena-based, NodeId handles) must be accessible from JS native functions
  • Design a mechanism to associate a Document with a Vm instance — options include:
    • Adding an Option<Document> field to Vm
    • Using a shared reference (e.g., Rc<RefCell<Document>>) stored in a VM field
    • Storing a Document pointer in a dedicated host data slot on the VM
  • DOM nodes should be represented as JS objects wrapping their NodeId
  • Use an internal property (e.g., __node_id__) to store the NodeId on JS wrapper objects
  • Create wrapper objects lazily — same NodeId should return the same JS object (identity)

Document Global#

  • Register document as a global JS object during page initialization
  • Properties:
    • document.documentElement — returns the <html> element wrapper
    • document.head — returns the <head> element wrapper
    • document.body — returns the <body> element wrapper
    • document.title — get/set document title

Query Methods#

  • document.getElementById(id) — walk DOM tree, find element with matching id attribute, return wrapper or null
  • document.querySelector(selector) — match first element using CSS selector (reuse css crate's selector matching)
  • document.querySelectorAll(selector) — return array of all matching elements
  • document.getElementsByTagName(tag) — return array of elements with matching tag name
  • document.getElementsByClassName(name) — return array of elements with matching class

Factory Methods#

  • document.createElement(tagName) — create new element, return wrapper
  • document.createTextNode(text) — create new text node, return wrapper

Element Properties (on wrapper objects)#

  • element.tagName — uppercase tag name
  • element.id — get/set id attribute
  • element.className — get/set class attribute
  • element.nodeType — 1 for Element, 3 for Text, 8 for Comment, 9 for Document
  • element.nodeName — tag name for elements, #text for text nodes

Acceptance Criteria#

  • document is accessible as a global in JS
  • document.getElementById("foo") finds elements by id attribute
  • document.createElement("div") creates a new element
  • document.createTextNode("hello") creates a text node
  • document.body returns the body element
  • Element wrappers have tagName, id, nodeType properties
  • document.querySelector(".class") works with CSS selectors
  • Wrapper identity: same node returns same JS object
  • Null/undefined returned for missing elements

Phase 11 — DOM-JS Bindings (issue 2 of 8). No Phase 11 dependencies.

sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mhyfmgy2qu2i