//! 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#"
"#),
&format!("<{tag}>Hello{tag}>"),
);
}
}
}
}
}
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#""#),
format!(r#""#),
format!(r#""#),
] {
check("", &body, &format!("<{tag}>Hello{tag}>"));
}
let head = format!(r#""#);
check(&head, r#""#, &format!("<{tag}>Hello{tag}>"));
let actual_reset = format!(r#"tts:{property}="{reset}""#);
let foreign_positive = format!(r#"app:{property}="{positive}""#);
let reset_attrs = if reversed { format!("{actual_reset} {foreign_positive}") } else { format!("{foreign_positive} {actual_reset}") };
check("", &format!(r#""#), "Hello");
}
}
check("", &format!(r#""#), "Hello");
check(
"",
&format!(r#""#),
&format!("<{tag}>Hello{tag}>"),
);
// Body styling resolves before the cue walk, so namespace priority must hold on that path too.
let xml = format!(
r#""#
);
for format in [Format::Ttml, Format::Dfxp] {
let doc = parse(xml.as_bytes(), format).unwrap();
assert_eq!(doc.cues.len(), 1);
assert_eq!(doc.cues[0].text, format!("<{tag}>Hello{tag}>"));
}
}
}
#[test]
fn foreign_style_references_and_definitions_are_ignored() {
let real = r#""#;
let foreign = r#""#;
for definitions in [format!("{real}{foreign}"), format!("{foreign}{real}")] {
let head = format!("{definitions}");
for attrs in [r#"app:style="missing" style="emphasis""#, r#"style="emphasis" app:style="missing""#] {
check(&head, &format!(r#""#), "Hello");
}
check(&head, r#""#, "Hello");
}
}
#[test]
fn genuine_style_ids_win_over_foreign_and_legacy_ids() {
for attrs in [
r#"app:id="wrong" xml:id="emphasis""#,
r#"xml:id="emphasis" app:id="wrong""#,
r#"id="wrong" xml:id="emphasis""#,
r#"xml:id="emphasis" id="wrong""#,
r#"id="emphasis""#,
] {
check(
&format!(r#""#),
r#""#,
"Hello",
);
}
check(
r#""#,
r#""#,
"Hello",
);
}
#[test]
fn modern_draft_and_unqualified_styling_remain_supported() {
for (tt, tts, id) in [
("http://www.w3.org/ns/ttml", "http://www.w3.org/ns/ttml#styling", "xml:id"),
("http://www.w3.org/2006/04/ttaf1", "http://www.w3.org/2006/04/ttaf1#style", "id"),
("http://www.w3.org/2006/10/ttaf1", "http://www.w3.org/2006/10/ttaf1#style", "xml:id"),
("http://www.w3.org/2006/04/ttaf1", "http://www.w3.org/2006/04/ttaf1#styling", "id"),
("http://www.w3.org/2006/10/ttaf1", "http://www.w3.org/2006/10/ttaf1#styling", "xml:id"),
("", "", "id"),
] {
let ns = if tt.is_empty() { String::new() } else { format!(r#"xmlns="{tt}" xmlns:tts="{tts}""#) };
let prefix = if tts.is_empty() { "" } else { "tts:" };
let xml = format!(
r#""#
);
for format in [Format::Ttml, Format::Dfxp] {
let doc = parse(xml.as_bytes(), format).unwrap();
assert_eq!(doc.cues.len(), 1);
assert!(doc.warnings.is_empty());
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, "Hello", "{format:?}, {xml}");
}
}
}