//! Styling identity must not depend on XML attribute order or foreign extensions. use craft_captions::{Format, parse}; use craft_captions::{TICKS_PER_SECOND, Tick}; #[test] fn recognized_styling_namespaces_have_fixed_priority() { let namespaces = [ "http://www.w3.org/ns/ttml#styling", "http://www.w3.org/2006/10/ttaf1#style", "http://www.w3.org/2006/10/ttaf1#styling", "http://www.w3.org/2006/04/ttaf1#style", "http://www.w3.org/2006/04/ttaf1#styling", ]; for (property, positive, reset, tag) in [("fontStyle", "italic", "normal", "i"), ("fontWeight", "bold", "normal", "b"), ("textDecoration", "underline", "noUnderline", "u")] { for (i, high) in namespaces.iter().enumerate() { for low in namespaces.iter().skip(i + 1) { for attrs in [ format!(r#"lo:{property}="{reset}" hi:{property}="{positive}""#), format!(r#"hi:{property}="{positive}" lo:{property}="{reset}""#), ] { check( "", &format!(r#"

Hello

"#), &format!("<{tag}>Hello"), ); } } } } } fn check(head: &str, body: &str, expected: &str) { let xml = format!( r#"{head}{body}"# ); for format in [Format::Ttml, Format::Dfxp] { let doc = parse(xml.as_bytes(), format).unwrap(); assert!(doc.warnings.is_empty(), "{xml}"); assert_eq!(doc.cues.len(), 1, "{xml}"); assert_eq!((doc.cues[0].start, doc.cues[0].end), (Tick(TICKS_PER_SECOND), Tick(2 * TICKS_PER_SECOND))); assert_eq!(doc.cues[0].text, expected, "{format:?}, {xml}"); } } #[test] fn genuine_styling_wins_over_foreign_and_unqualified_properties() { for (property, positive, reset, tag) in [("fontStyle", "italic", "normal", "i"), ("fontWeight", "bold", "normal", "b"), ("textDecoration", "underline", "noUnderline", "u")] { for other in ["app:", ""] { for reversed in [false, true] { let actual = format!(r#"tts:{property}="{positive}""#); let collision = format!(r#"{other}{property}="{reset}""#); let attrs = if reversed { format!("{actual} {collision}") } else { format!("{collision} {actual}") }; for body in [ format!(r#"

Hello

"#), format!(r#"

Hello

"#), format!(r#"

Hello

"#), ] { check("", &body, &format!("<{tag}>Hello")); } let head = format!(r#"