Files
craft-libs/crates/captions/tests/it/ttml_literal_angle.rs
86e59ebc08 craft-captions: ten caption fixes ported from open FilmCraft pull requests (#932)
* craft-captions: TTML cue text collapses only XML whitespace and keeps edge line breaks

Ported from storytold/filmcraft#3145. char::is_whitespace also matched U+00A0,
U+2009 and U+3000, so no-break spaces became ordinary spaces on import, and a
leading or trailing <br/> was trimmed away.

* craft-captions: TTML/DFXP export drops characters XML 1.0 forbids

Ported from storytold/filmcraft#4467. A control character in cue text made the
exported file unreadable. The filter also covers U+FFFE and U+FFFF, which the
review of that pull request asked for.

* craft-captions: WebVTT cue-text arrow escape is reversible

Ported from storytold/filmcraft#4437. A literal --&gt; in cue text came back as
--> after a write and parse; the writer now adds one amp; level and the reader
removes exactly one.

* craft-captions: a WebVTT speaker name with > no longer spills into the cue text

Ported from storytold/filmcraft#4470. The writer puts the lookalike U+203A in
place of > inside the voice tag. Also from that pull request's review: a line
break in a name is written as a space, and a name ending in - is kept apart
from the tag's closing >.

* craft-captions: an SRT text line that looks like a timing line survives a round trip

Ported from storytold/filmcraft#4448. The writer marks such a line with a
leading zero-width space and the reader removes it, so the cue is not split.

* craft-captions: back-to-back SCC captions with the same text stay separate cues

Ported from storytold/filmcraft#3808. Every End of Caption now closes the open
cue. MCC's CEA-608 path uses the same decoder.

* craft-captions: an SCC pop-on load may run across the previous caption's erase

Ported from storytold/filmcraft#1837 (filmcraft#1791). A caption that closely
follows another popped on about a second late, because its load had to be one
unbroken run of free frames. Doubled codes stay adjacent; pauses are allowed
between them.

* craft-captions: CEA-708 export starts each caption at its window's top left

Ported from storytold/filmcraft#4043 (filmcraft#4042). Each window load now
sends DeleteWindows before DefineWindow in place of the ClearWindows after it:
a window made anew is empty with the pen at its top left in any decoder, for
the same number of bytes, so cue timing is unchanged.

* craft-captions: TTML/DFXP export keeps literal < and > text

Ported from storytold/filmcraft#2830. The writer treated every < with a later >
as a tag and wrote nothing for unknown names, so "1 < 2 and 3 > 2" lost its
middle. Only a well-formed tag is markup now; a > inside a quoted attribute
value does not end the tag.

* craft-captions: TTML/DFXP export keeps cue IDs unique and valid Unicode IDs as written

Ported from the TTML commit of storytold/filmcraft#1839. A cue named s1 could
collide with the style ID, and a generated c1 could take the ID of a later cue.
Valid explicit IDs are reserved first; missing, invalid or repeated ones get a
replacement that is in no one's way. IDs are checked as XML NCNames.

* craft-captions: compare the craft-xml and roxmltree views of TTML documents

The TTML reader moved from roxmltree to craft-xml. This dev-only test checks
that both hand it the same elements, attributes and text, by namespace, on
TTML documents and on the writer's output. roxmltree becomes a dev-dependency,
which the cue ID tests use too.

---------

Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com>
Co-authored-by: SeeRay11 <searay112023@gmail.com>
Co-authored-by: jnohclee-rgb <336857373+jnohclee-rgb@users.noreply.github.com>
Co-authored-by: JonRC <jonarc06@gmail.com>
Co-authored-by: sanyamsingh001 <314051937+sanyamsingh001@users.noreply.github.com>
2026-11-11 18:44:12 -05:00

77 lines
2.7 KiB
Rust

//! TTML / DFXP export keeps literal `<` and `>` text that is not markup.
use craft_captions::FrameRate;
use craft_captions::{Cue, Document, Format, WriteOptions, write};
fn export(text: &str, format: Format) -> String {
let rate = FrameRate::FPS_25;
let doc = Document {
cues: vec![Cue { start: rate.tick_of(25), end: rate.tick_of(75), text: text.into(), ..Default::default() }],
..Default::default()
};
String::from_utf8(write(&doc, format, WriteOptions::default())).unwrap()
}
#[test]
fn literal_angle_brackets_keep_middle_text() {
for format in [Format::Ttml, Format::Dfxp] {
for (input, expected) in [("1 < 2 and 3 > 2", "1 &lt; 2 and 3 &gt; 2"), ("a<b and c>d", "a&lt;b and c&gt;d")] {
let text = export(input, format);
assert!(text.contains(expected), "{format:?}: {text}");
}
}
}
#[test]
fn real_tags_still_produce_markup() {
for format in [Format::Ttml, Format::Dfxp] {
let text = export("<b>text</b> x <y> z <i>it</i>", format);
assert!(text.contains("<span tts:fontWeight=\"bold\">text</span>"), "{format:?}: {text}");
assert!(text.contains("<span tts:fontStyle=\"italic\">it</span>"), "{format:?}: {text}");
assert!(text.contains(" x z "), "{format:?}: {text}");
}
}
#[test]
fn self_closing_tags_are_still_stripped() {
for format in [Format::Ttml, Format::Dfxp] {
let text = export("a<br/>b <b/>c", format);
assert!(text.contains(">ab c</p>"), "{format:?}: {text}");
}
}
#[test]
fn quoted_gt_in_attribute_does_not_end_tag() {
for format in [Format::Ttml, Format::Dfxp] {
let text = export("<b title=\">\">text</b>", format);
assert!(text.contains("<span tts:fontWeight=\"bold\">text</span>"), "{format:?}: {text}");
}
}
#[test]
fn single_quoted_attributes_are_stripped() {
for format in [Format::Ttml, Format::Dfxp] {
let text = export("<b title='caption'>word</b>", format);
assert!(text.contains("<span tts:fontWeight=\"bold\">word</span>"), "{format:?}: {text}");
assert!(!text.contains("caption"), "{format:?}: {text}");
}
}
#[test]
fn whitespace_after_lt_is_literal_text() {
for format in [Format::Ttml, Format::Dfxp] {
let text = export("1 < b > 2", format);
assert!(text.contains("1 &lt; b &gt; 2"), "{format:?}: {text}");
assert!(!text.contains("fontWeight"), "{format:?}: {text}");
}
}
#[test]
fn many_unmatched_lt_is_fast() {
let input = format!("{}>", "<".repeat(256_000));
let start = std::time::Instant::now();
let text = export(&input, Format::Ttml);
assert!(start.elapsed() < std::time::Duration::from_secs(2), "took {:?}", start.elapsed());
assert!(text.contains(&format!("{}&gt;", "&lt;".repeat(256_000))));
}