//! The TTML reader used to sit on `roxmltree` and now sits on `craft-xml`. Its logic is the same; //! what changed is the tree it is handed. This compares the two trees on TTML documents: element //! names by namespace, attributes by namespace, and character data, in document order. use craft_captions::{Cue, Document, Format, FrameRate, WriteOptions, write}; const NAMESPACES: [(&str, &str); 11] = [ ("tt", "http://www.w3.org/ns/ttml"), ("tt10", "http://www.w3.org/2006/10/ttaf1"), ("tt04", "http://www.w3.org/2006/04/ttaf1"), ("ttm", "http://www.w3.org/ns/ttml#metadata"), ("ttm10", "http://www.w3.org/2006/10/ttaf1#metadata"), ("ttm04", "http://www.w3.org/2006/04/ttaf1#metadata"), ("tts", "http://www.w3.org/ns/ttml#styling"), ("tts10", "http://www.w3.org/2006/10/ttaf1#style"), ("tts10s", "http://www.w3.org/2006/10/ttaf1#styling"), ("tts04", "http://www.w3.org/2006/04/ttaf1#style"), ("tts04s", "http://www.w3.org/2006/04/ttaf1#styling"), ]; /// A name the way the reader's canonical names spell it. fn name(namespace: Option<&str>, local: &str) -> String { match namespace { None => local.to_string(), Some("http://www.w3.org/XML/1998/namespace") => format!("xml:{local}"), Some(uri) => format!("{}:{local}", NAMESPACES.iter().find(|(_, u)| *u == uri).map_or("?", |(p, _)| p)), } } fn dump_rox(n: roxmltree::Node, out: &mut Vec) { let mut attrs: Vec = n.attributes().map(|a| format!("@{}={}", name(a.namespace(), a.name()), a.value())).collect(); attrs.sort(); out.push(format!("<{} {}", name(n.tag_name().namespace(), n.tag_name().name()), attrs.join(" "))); let mut text = String::new(); for c in n.children() { if c.is_text() { text.push_str(c.text().unwrap_or("")); } else if c.is_element() { out.push(format!("#{}", std::mem::take(&mut text))); dump_rox(c, out); } } out.push(format!("#{text}")); out.push(">".into()); } fn dump_craft(n: &craft_xml::El, out: &mut Vec) { let mut attrs: Vec = n.attrs.iter().map(|(k, v)| format!("@{k}={v}")).collect(); attrs.sort(); out.push(format!("<{} {}", n.name, attrs.join(" "))); let mut text = String::new(); for c in &n.children { match c { craft_xml::Node::Text(t) | craft_xml::Node::CData(t) => text.push_str(t), craft_xml::Node::El(e) => { out.push(format!("#{}", std::mem::take(&mut text))); dump_craft(e, out); } _ => {} } } out.push(format!("#{text}")); out.push(">".into()); } fn documents() -> Vec { let mut docs: Vec = [ r#"

Hi

"#, // prefixed content, styling and metadata; xml:lang, xml:id, xml:space r#" Ann Lee onetwo three four "#, // the 2006 drafts r#" Bo

x

"#, r#"Bo

x

"#, // no namespace at all, an unknown namespace, and a foreign element inside r#"

x

"#, r#"

x

"#, // character data: references, CDATA, comments, processing instructions, mixed whitespace "

\n a <b> & © 😀 & ]]>tail\u{a0}more end\r\n

", // a namespace redeclared in an inner scope r#"no"#, ] .into_iter() .map(str::to_string) .collect(); // and what the writer produces, in both flavours let rate = FrameRate::FPS_25; let cue = |f: i64, text: &str, speaker: Option<&str>| Cue { start: rate.tick_of(f), end: rate.tick_of(f + 25), text: text.into(), speaker: speaker.map(str::to_string), ..Default::default() }; let doc = Document { cues: vec![cue(25, "Olá & tchau\n1 < 2", Some("Ana \"A\" ")), cue(75, "second\u{a0}line", None)], language: Some("pt".into()), ..Default::default() }; for format in [Format::Ttml, Format::Dfxp] { docs.push(String::from_utf8(write(&doc, format, WriteOptions { rate: Some(rate), ..Default::default() })).unwrap()); } docs } #[test] fn craft_xml_hands_the_ttml_reader_the_tree_roxmltree_did() { let canonical = craft_xml::Canonical::new(&NAMESPACES); for text in documents() { let rox = roxmltree::Document::parse(&text).unwrap(); let craft = craft_xml::Options::canonical(&canonical).strict().parse(text.as_bytes()).unwrap(); let (mut a, mut b) = (Vec::new(), Vec::new()); dump_rox(rox.root_element(), &mut a); dump_craft(&craft, &mut b); assert_eq!(a, b, "{text}"); } }