Skip to content

Commit

Permalink
added location to each node
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Aug 26, 2024
1 parent 6dcd26c commit 7fa0c14
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 312 deletions.
55 changes: 32 additions & 23 deletions crates/gosub_html5/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use derive_more::Display;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Weak;
use gosub_shared::byte_stream::Location;

pub const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml";
pub const MATHML_NAMESPACE: &str = "http://www.w3.org/1998/Math/MathML";
Expand Down Expand Up @@ -142,6 +143,8 @@ pub struct Node {
pub document: Weak<RefCell<Document>>,
// Returns true when the given node is registered into an arena
pub is_registered: bool,
// Location of the node in the source code
pub location: Location
}

impl Node {
Expand Down Expand Up @@ -189,14 +192,15 @@ impl Clone for Node {
data: self.data.clone(),
document: Weak::clone(&self.document),
is_registered: self.is_registered,
location: self.location.clone()
}
}
}

impl Node {
/// create a new `Node`
#[must_use]
pub fn new(data: NodeData, document: &DocumentHandle) -> Self {
pub fn new(data: NodeData, document: &DocumentHandle, location: Location) -> Self {
let (id, parent, children, name, namespace, is_registered) = <_>::default();
Self {
id,
Expand All @@ -207,13 +211,14 @@ impl Node {
namespace,
document: document.to_weak(),
is_registered,
location
}
}

/// Create a new document node
#[must_use]
pub fn new_document(document: &DocumentHandle) -> Self {
Self::new(NodeData::Document(DocumentData::new()), document)
pub fn new_document(document: &DocumentHandle, location: Location) -> Self {
Self::new(NodeData::Document(DocumentData::new()), document, location)
}

#[must_use]
Expand All @@ -222,10 +227,12 @@ impl Node {
name: &str,
pub_identifier: &str,
sys_identifier: &str,
loc: Location,
) -> Self {
Self::new(
NodeData::DocType(DocTypeData::new(name, pub_identifier, sys_identifier)),
document,
loc
)
}

Expand All @@ -236,6 +243,7 @@ impl Node {
name: &str,
attributes: HashMap<String, String>,
namespace: &str,
location: Location,
) -> Self {
Self {
name: name.to_owned(),
Expand All @@ -247,20 +255,21 @@ impl Node {
attributes,
))),
document,
location,
)
}
}

/// Creates a new comment node
#[must_use]
pub fn new_comment(document: &DocumentHandle, value: &str) -> Self {
Self::new(NodeData::Comment(CommentData::with_value(value)), document)
pub fn new_comment(document: &DocumentHandle, location: Location, value: &str) -> Self {
Self::new(NodeData::Comment(CommentData::with_value(value)), document, location)
}

/// Creates a new text node
#[must_use]
pub fn new_text(document: &DocumentHandle, value: &str) -> Self {
Self::new(NodeData::Text(TextData::with_value(value)), document)
pub fn new_text(document: &DocumentHandle, location: Location, value: &str) -> Self {
Self::new(NodeData::Text(TextData::with_value(value)), document, location)
}

/// Returns true if the given node is a "formatting" node
Expand Down Expand Up @@ -544,7 +553,7 @@ mod tests {
#[test]
fn new_document() {
let document = Document::shared(None);
let node = Node::new_document(&document);
let node = Node::new_document(&document, Location::default());
assert_eq!(node.id, NodeId::default());
assert_eq!(node.parent, None);
assert!(node.children.is_empty());
Expand All @@ -561,7 +570,7 @@ mod tests {
let mut attributes = HashMap::new();
attributes.insert("id".to_string(), "test".to_string());
let document = Document::shared(None);
let node = Node::new_element(&document, "div", attributes.clone(), HTML_NAMESPACE);
let node = Node::new_element(&document, "div", attributes.clone(), HTML_NAMESPACE, Location::default());
assert_eq!(node.id, NodeId::default());
assert_eq!(node.parent, None);
assert!(node.children.is_empty());
Expand All @@ -578,7 +587,7 @@ mod tests {
#[test]
fn new_comment() {
let document = Document::shared(None);
let node = Node::new_comment(&document, "test");
let node = Node::new_comment(&document, Location::default(), "test");
assert_eq!(node.id, NodeId::default());
assert_eq!(node.parent, None);
assert!(node.children.is_empty());
Expand All @@ -593,7 +602,7 @@ mod tests {
#[test]
fn new_text() {
let document = Document::shared(None);
let node = Node::new_text(&document, "test");
let node = Node::new_text(&document, Location::default(), "test");
assert_eq!(node.id, NodeId::default());
assert_eq!(node.parent, None);
assert!(node.children.is_empty());
Expand All @@ -610,22 +619,22 @@ mod tests {
let mut attributes = HashMap::new();
attributes.insert("id".to_string(), "test".to_string());
let document = Document::shared(None);
let node = Node::new_element(&document, "div", attributes, HTML_NAMESPACE);
let node = Node::new_element(&document, "div", attributes, HTML_NAMESPACE, Location::default());
assert!(node.is_special());
}

#[test]
fn type_of() {
let document = Document::shared(None);
let node = Node::new_document(&document);
let node = Node::new_document(&document, Location::default());
assert_eq!(node.type_of(), NodeType::Document);
let node = Node::new_text(&document, "test");
let node = Node::new_text(&document, Location::default(), "test");
assert_eq!(node.type_of(), NodeType::Text);
let node = Node::new_comment(&document, "test");
let node = Node::new_comment(&document, Location::default(), "test");
assert_eq!(node.type_of(), NodeType::Comment);
let mut attributes = HashMap::new();
attributes.insert("id".to_string(), "test".to_string());
let node = Node::new_element(&document, "div", attributes, HTML_NAMESPACE);
let node = Node::new_element(&document, "div", attributes, HTML_NAMESPACE, Location::default());
assert_eq!(node.type_of(), NodeType::Element);
}

Expand All @@ -635,7 +644,7 @@ mod tests {
for element in SPECIAL_HTML_ELEMENTS.iter() {
let mut attributes = HashMap::new();
attributes.insert("id".to_string(), "test".to_string());
let node = Node::new_element(&document, element, attributes, HTML_NAMESPACE);
let node = Node::new_element(&document, element, attributes, HTML_NAMESPACE, Location::default());
assert!(node.is_special());
}
}
Expand All @@ -646,7 +655,7 @@ mod tests {
for element in SPECIAL_MATHML_ELEMENTS.iter() {
let mut attributes = HashMap::new();
attributes.insert("id".to_string(), "test".to_string());
let node = Node::new_element(&document, element, attributes, MATHML_NAMESPACE);
let node = Node::new_element(&document, element, attributes, MATHML_NAMESPACE, Location::default());
assert!(node.is_special());
}
}
Expand All @@ -657,23 +666,23 @@ mod tests {
for element in SPECIAL_SVG_ELEMENTS.iter() {
let mut attributes = HashMap::new();
attributes.insert("id".to_string(), "test".to_string());
let node = Node::new_element(&document, element, attributes, SVG_NAMESPACE);
let node = Node::new_element(&document, element, attributes, SVG_NAMESPACE, Location::default());
assert!(node.is_special());
}
}

#[test]
fn type_of_node() {
let document = Document::shared(None);
let node = Node::new_document(&document);
let node = Node::new_document(&document, Location::default());
assert_eq!(node.type_of(), NodeType::Document);
let node = Node::new_text(&document, "test");
let node = Node::new_text(&document, Location::default(), "test");
assert_eq!(node.type_of(), NodeType::Text);
let node = Node::new_comment(&document, "test");
let node = Node::new_comment(&document, Location::default(), "test");
assert_eq!(node.type_of(), NodeType::Comment);
let mut attributes = HashMap::new();
attributes.insert("id".to_string(), "test".to_string());
let node = Node::new_element(&document, "div", attributes, HTML_NAMESPACE);
let node = Node::new_element(&document, "div", attributes, HTML_NAMESPACE, Location::default());
assert_eq!(node.type_of(), NodeType::Element);
}
}
19 changes: 13 additions & 6 deletions crates/gosub_html5/src/node/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,19 @@ mod tests {
use super::*;
use crate::node::HTML_NAMESPACE;
use crate::parser::document::Document;
use gosub_shared::byte_stream::Location;

#[test]
fn register_node() {
let mut doc = Document::shared(None);

let node = Node::new_element(&doc, "test", HashMap::new(), HTML_NAMESPACE);
let node = Node::new_element(
&doc,
"test",
HashMap::new(),
HTML_NAMESPACE,
Location::default()
);
let mut document = doc.get_mut();
let id = document.arena.register_node(node);

Expand All @@ -97,7 +104,7 @@ mod tests {
fn register_node_twice() {
let mut doc = Document::shared(None);

let node = Node::new_element(&doc, "test", HashMap::new(), HTML_NAMESPACE);
let node = Node::new_element(&doc, "test", HashMap::new(), HTML_NAMESPACE, Location::default());
let mut document = doc.get_mut();
document.arena.register_node(node);

Expand All @@ -108,7 +115,7 @@ mod tests {
#[test]
fn get_node() {
let mut doc = Document::shared(None);
let node = Node::new_element(&doc, "test", HashMap::new(), HTML_NAMESPACE);
let node = Node::new_element(&doc, "test", HashMap::new(), HTML_NAMESPACE, Location::default());

let mut document = doc.get_mut();
let id = document.arena.register_node(node);
Expand All @@ -120,7 +127,7 @@ mod tests {
#[test]
fn get_node_mut() {
let mut doc = Document::shared(None);
let node = Node::new_element(&doc, "test", HashMap::new(), HTML_NAMESPACE);
let node = Node::new_element(&doc, "test", HashMap::new(), HTML_NAMESPACE, Location::default());

let mut document = doc.get_mut();

Expand All @@ -134,8 +141,8 @@ mod tests {
fn register_node_through_document() {
let mut doc = Document::shared(None);

let parent = Node::new_element(&doc, "parent", HashMap::new(), HTML_NAMESPACE);
let child = Node::new_element(&doc, "child", HashMap::new(), HTML_NAMESPACE);
let parent = Node::new_element(&doc, "parent", HashMap::new(), HTML_NAMESPACE, Location::default());
let child = Node::new_element(&doc, "child", HashMap::new(), HTML_NAMESPACE, Location::default());

let mut document = doc.get_mut();
let parent_id = document.arena.register_node(parent);
Expand Down
Loading

0 comments on commit 7fa0c14

Please sign in to comment.