Skip to content

Commit

Permalink
trying to merge stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Nov 19, 2024
1 parent ab46fb0 commit 8bf342a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 48 deletions.
100 changes: 55 additions & 45 deletions crates/gosub_html5/src/html5parser.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use gosub_shared::document::DocumentHandle;
use gosub_shared::location::Location;
use gosub_shared::node_id::NodeId;
use gosub_shared::traits::css_system::{CssParser, HasCssParser};
use gosub_shared::traits::document::Document;
use gosub_shared::traits::document::HasDocument;
use gosub_shared::traits::html5_parser::HtmlParser;
use gosub_shared::document::task_queue::{DocumentTask, DocumentTaskQueue};

/// The HTML parser implementation will parse an input string (or stream reader) and generate a
/// DOM tree based on the input. Instead of generating the tree directly, it will use the Document
/// Task Queue to store tasks that will be executed during a flush of the queue. This will basically
/// defer the actual creation until a later time.
use crate::document::task_queue::{DocumentTask, DocumentTaskQueue, TaskDestination};

pub struct MyHtmlParser<C: HasDocument + HasCssParser> {
doc_handle: DocumentHandle<C>,
Expand Down Expand Up @@ -43,96 +37,112 @@ impl<C: HasDocument + HasCssParser> HtmlParser<C> for MyHtmlParser<C> {

let mut task_queue = DocumentTaskQueue::new(self.doc_handle.clone());

let node_id_1 = task_queue.add_task(DocumentTask::CreateElement {
let tid_1 = task_queue.add_task(DocumentTask::CreateElement {
name: "html".to_string(),
namespace: "html".to_string(),
location: Location::default(),
parent_id: NodeId::root(),
position: None
});
}, TaskDestination::DocumentRoot(None));

let _ = task_queue.add_task(DocumentTask::CreateElement {
name: "head".to_string(),
namespace: "html".to_string(),
location: Default::default(),
parent_id: node_id_1,
position: None
});
}, TaskDestination::Task(tid_1, None));

let node_id_3 = task_queue.add_task(DocumentTask::CreateElement {
let tid_3 = task_queue.add_task(DocumentTask::CreateElement {
name: "body".to_string(),
namespace: "html".to_string(),
location: Default::default(),
parent_id: node_id_1,
position: None
});
}, TaskDestination::Task(tid_1, None));

let node_id_4_1 = task_queue.add_task(DocumentTask::CreateElement {
let tid_41 = task_queue.add_task(DocumentTask::CreateElement {
name: "h1".to_string(),
namespace: "html".to_string(),
location: Default::default(),
parent_id: node_id_3,
position: None
});
}, TaskDestination::Task(tid_3, None));

let _ = task_queue.add_task(DocumentTask::CreateText {
content: "This is a header".to_string(),
location: Default::default(),
parent_id: node_id_4_1,
position: None
});
}, TaskDestination::Task(tid_41, None));

let node_id_4 = task_queue.add_task(DocumentTask::CreateElement {
let tid_4 = task_queue.add_task(DocumentTask::CreateElement {
name: "p".to_string(),
namespace: "html".to_string(),
location: Default::default(),
parent_id: node_id_3,
position: None
});
}, TaskDestination::Task(tid_3, None));

let _ = task_queue.add_task(DocumentTask::CreateText {
content: "hello world!".to_string(),
location: Default::default(),
parent_id: node_id_4,
position: None
});
}, TaskDestination::Task(tid_4, None));

let _ = task_queue.add_task(DocumentTask::CreateText {
content: "prefix".to_string(),
location: Default::default(),
parent_id: node_id_1,
position: Some(0)
});
}, TaskDestination::Task(tid_4, Some(0)));

let _ = task_queue.add_task(DocumentTask::InsertAttribute {
key: "class".to_string(),
value: "a b c".to_string(),
location: Default::default(),
node_id: node_id_3,
});

}, TaskDestination::Task(tid_3, None));
let _ = task_queue.add_task(DocumentTask::InsertAttribute {
key: "id".to_string(),
value: "myid".to_string(),
location: Default::default(),
node_id: node_id_3,
});

}, TaskDestination::Task(tid_3, None));
let _ = task_queue.add_task(DocumentTask::InsertAttribute {
key: "foo".to_string(),
value: "bar".to_string(),
location: Default::default(),
node_id: node_id_3,
});

}, TaskDestination::Task(tid_3, None));

let errors = task_queue.flush();
if !errors.is_empty() {
for error in errors {
println!("Parse Error: {}", error);
}
}
*/

/*
#[allow(type_alias_bounds)]
type BuilderType<C: HasDocument> = NodeBuilder<C::Node>;
let node1 = BuilderType::<C>::new_element_node("html", "html");
let node1_id = binding.register_node_at(node1, NodeId::root(), None);
let node2 = BuilderType::<C>::new_element_node("head", "html");
let _node2_id = binding.register_node_at(node2, node1_id, None);
let node3 = BuilderType::<C>::new_element_node("body", "html");
let node3_id = binding.register_node_at(node3, node1_id, None);
let node41 = BuilderType::<C>::new_element_node("h1", "html");
let node41_id = binding.register_node_at(node41, node3_id, None);
let node42 = BuilderType::<C>::new_text_node("This is a header");
let _node42_id = binding.register_node_at(node42, node41_id, None);
let node4 = BuilderType::<C>::new_element_node("p", "html");
let node4_id = binding.register_node_at(node4, node3_id, None);
let node5 = BuilderType::<C>::new_text_node("hello world!");
let _node5_id = binding.register_node_at(node5, node4_id, None);
// Add some attributes to the P element
if let Some(mut node) = binding.detach_node(node4_id) {
// Get the mutable data and add some attributes
if let Some(data) = node.get_element_data_mut() {
data.add_attribute("class", "a b c");
data.add_attribute("id", "myid");
data.add_attribute("foo", "bar");
}
// Finally, reattach the node back into the document/arena
binding.update_node(node4_id, node);
}
*/

// We also mimic some CSS style parsing here.
let mut parser = C::CssParser::new();
Expand Down
4 changes: 2 additions & 2 deletions crates/gosub_html5/src/node/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl<N: Node> NodeArena<N> {
}

pub fn add_node(&mut self, mut node: N) -> NodeId {
/// If a node is already registered, we should use that node-id instead of generating a new
/// one.
// If a node is already registered, we should use that node-id instead of generating a new
// one.
let node_id = if node.id().is_some() {
node.id().unwrap()
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_html5/src/node/node_data/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl gosub_shared::traits::node::ElementData for ElementData {
fn add_attribute(&mut self, name: &str, value: &str) {
self.attributes.insert(name.into(), value.into());

/// When we add a "class" attribute, we actually store it in the classes map as well.
// When we add a "class" attribute, we actually store it in the classes map as well.
if name == "class" {
for class in value.split_whitespace() {
self.classes.insert(class.to_string(), true);
Expand Down
4 changes: 4 additions & 0 deletions crates/gosub_shared/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ impl<C: HasDocument> Document<C> for MockDocument<C> {
todo!()
}

fn get_next_node_id(&mut self) -> NodeId {
todo!()
}

fn get_handle(&self) -> DocumentHandle<C> {
self.handle.clone().unwrap()
}
Expand Down

0 comments on commit 8bf342a

Please sign in to comment.