blitz_dom_api/
character_data.rs1use blitz_dom::node::NodeData;
8use blitz_dom::{BaseDocument, NodeId};
9
10use crate::Result;
11
12pub fn data(doc: &BaseDocument, node: NodeId) -> Result<Option<String>> {
25 Ok(match doc.get_node(node).map(|node| &node.data) {
26 Some(NodeData::Text(data)) => Some(data.content.clone()),
27 Some(NodeData::Comment { contents }) => Some(contents.clone()),
28 _ => None,
29 })
30}
31
32pub fn set_data(doc: &mut BaseDocument, node: NodeId, text: &str) -> Result<()> {
42 doc.mutate().set_node_text(node, text);
43 Ok(())
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49 use crate::document;
50 use crate::test_support::skeleton;
51
52 #[test]
53 fn data_reads_a_text_node() {
54 let (mut doc, _html, _head, _body) = skeleton();
55 let text = document::create_text_node(&mut doc, "hello").unwrap();
56 assert_eq!(data(&doc, text).unwrap(), Some("hello".to_string()));
57 }
58
59 #[test]
60 fn data_is_null_for_an_element() {
61 let (doc, _html, _head, body) = skeleton();
62 assert_eq!(data(&doc, body).unwrap(), None);
63 }
64
65 #[test]
66 fn data_reads_a_comments_contents() {
67 let (mut doc, _html, _head, _body) = skeleton();
68 let comment = document::create_comment(&mut doc, "note").unwrap();
69 assert_eq!(data(&doc, comment).unwrap(), Some("note".to_string()));
70 }
71
72 #[test]
73 fn set_data_rewrites_the_text_in_place() {
74 let (mut doc, _html, _head, _body) = skeleton();
75 let text = document::create_text_node(&mut doc, "before").unwrap();
76 set_data(&mut doc, text, "after").unwrap();
77 assert_eq!(data(&doc, text).unwrap(), Some("after".to_string()));
78 }
79
80 #[test]
85 fn a_comments_data_round_trips_through_a_write_and_a_clone() {
86 let (mut doc, _html, _head, _body) = skeleton();
87 let comment = document::create_comment(&mut doc, "first").unwrap();
88 assert_eq!(data(&doc, comment).unwrap(), Some("first".to_string()));
89
90 set_data(&mut doc, comment, "second").unwrap();
91 assert_eq!(data(&doc, comment).unwrap(), Some("second".to_string()));
92
93 let copy = crate::node::clone_node(&mut doc, comment, false).unwrap();
94 assert_ne!(copy, comment);
95 assert_eq!(
96 data(&doc, copy).unwrap(),
97 data(&doc, comment).unwrap(),
98 "a comment and its clone must report the same data"
99 );
100 assert_eq!(data(&doc, copy).unwrap(), Some("second".to_string()));
101 }
102}