Phase 3, Issue 1: DOM Tree#
Implement the core DOM tree data structure in the dom crate.
Requirements#
Node types:
Document— root of the DOM treeElement— has tag name, attributes (Vec of name/value pairs), namespace (optional)Text— character dataComment— comment text
Tree structure:
- Arena-based or
Rc<RefCell<>>tree (choose arena for performance) - Each node has: parent, first_child, last_child, next_sibling, prev_sibling
NodeIdhandle type for referring to nodes
Tree manipulation API:
Document::new() -> DocumentDocument::create_element(tag_name) -> NodeIdDocument::create_text(data) -> NodeIdDocument::create_comment(data) -> NodeIdDocument::append_child(parent, child)Document::insert_before(parent, new_child, reference_child)Document::remove_child(parent, child)Document::parent(node) -> Option<NodeId>Document::children(node) -> impl Iterator<Item = NodeId>Document::node_data(node) -> &NodeData(to inspect type, tag name, text, etc.)
Element-specific API:
Document::tag_name(node) -> Option<&str>Document::get_attribute(node, name) -> Option<&str>Document::set_attribute(node, name, value)
Acceptance criteria#
- All node types implemented
- Tree manipulation works correctly (append, insert, remove)
- Can build a simple tree:
<html><head><title>Test</title></head><body><p>Hello</p></body></html> -
cargo clippy -p we-dom -- -D warningspasses -
cargo test -p we-dompasses with comprehensive unit tests - No unsafe code
- No external dependencies