Repository: SubtitleEdit/subtitleedit (2026-03-24T20:55:01Z)

Issue:
Timed Text IMSC 1.1 subs - Spaces inside italic spans
There seems to be an issue on dealing with spaces inside italic spans in subs using the format Timed Text IMSC 1.1.

I send you 2 files in attachment where the behaviour is different, one is correct and the other one is not.

In the file "italics_space_before.xml", there is a space inside the italic span before the word on lines 26 and 451 and it keeps the space correctly when converting to srt.

In the file "italics_space_after.xml", there is a space inside the italic span after the word on lines 31, 50 and 170 and it does not keep the space so the words are incorrectly joined.

[italics_space_after.xml](https://github.com/user-attachments/files/26225587/italics_space_after.xml)
[italics_space_before.xml](https://github.com/user-attachments/files/26225586/italics_space_before.xml)


Discussion:

Patch:
diff --git a/src/libse/SubtitleFormats/TimedTextImsc11.cs b/src/libse/SubtitleFormats/TimedTextImsc11.cs
index 3fde25ff766..29547afa30e 100644
--- a/src/libse/SubtitleFormats/TimedTextImsc11.cs
+++ b/src/libse/SubtitleFormats/TimedTextImsc11.cs
@@ -476,7 +476,7 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
                     assStyle = GetAssStyleFromRegion(region.InnerText, xml);
                 }
 
-                var text = assStyle + ReadParagraph(node, xml);
+                var text = assStyle + ReadParagraph(node, xml).TrimEnd();
                 var p = new Paragraph(begin, end, text);
                 subtitle.Paragraphs.Add(p);
             }
@@ -662,7 +662,7 @@ private static string ReadParagraph(XmlNode node, XmlDocument xml)
                 }
             }
 
-            return pText.ToString().TrimEnd();
+            return pText.ToString();
         }
 
         public static string RemoveTags(string text)


Test patch:
diff --git a/tests/libse/SubtitleFormats/TimedTextImsc11Test.cs b/tests/libse/SubtitleFormats/TimedTextImsc11Test.cs
new file mode 100644
index 00000000000..736756a0184
--- /dev/null
+++ b/tests/libse/SubtitleFormats/TimedTextImsc11Test.cs
@@ -0,0 +1,49 @@
+﻿using Nikse.SubtitleEdit.Core.Common;
+using Nikse.SubtitleEdit.Core.SubtitleFormats;
+
+namespace LibSETests.SubtitleFormats;
+
+public class TimedTextImsc11Test
+{
+    [Fact]
+    public void Italic_Trailing_Space_Preserved()
+    {
+        var sut = new TimedTextImsc11();
+
+        var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+                  "<tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:tts=\"http://www.w3.org/ns/ttml#styling\" xmlns:ttp=\"http://www.w3.org/ns/ttml#parameter\" ttp:profile=\"http://www.w3.org/ns/ttml/profile/imsc1/text\" ttp:timeBase=\"media\" xml:lang=\"en\">" +
+                  "<head><styling>" +
+                  "<style xml:id=\"italic\" tts:fontStyle=\"italic\"/>" +
+                  "</styling></head>" +
+                  "<body><div>" +
+                  "<p begin=\"00:00:01.000\" end=\"00:00:03.000\"><span style=\"italic\">word </span><span>next</span></p>" +
+                  "</div></body></tt>";
+
+        var subtitle = new Subtitle();
+        sut.LoadSubtitle(subtitle, xml.SplitToLines(), "test.xml");
+
+        Assert.Single(subtitle.Paragraphs);
+        Assert.Equal("<i>word </i>next", subtitle.Paragraphs[0].Text);
+    }
+
+    [Fact]
+    public void Italic_Leading_Space_Preserved()
+    {
+        var sut = new TimedTextImsc11();
+
+        var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+                  "<tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:tts=\"http://www.w3.org/ns/ttml#styling\" xmlns:ttp=\"http://www.w3.org/ns/ttml#parameter\" ttp:profile=\"http://www.w3.org/ns/ttml/profile/imsc1/text\" ttp:timeBase=\"media\" xml:lang=\"en\">" +
+                  "<head><styling>" +
+                  "<style xml:id=\"italic\" tts:fontStyle=\"italic\"/>" +
+                  "</styling></head>" +
+                  "<body><div>" +
+                  "<p begin=\"00:00:01.000\" end=\"00:00:03.000\"><span>word</span><span style=\"italic\"> next</span></p>" +
+                  "</div></body></tt>";
+
+        var subtitle = new Subtitle();
+        sut.LoadSubtitle(subtitle, xml.SplitToLines(), "test.xml");
+
+        Assert.Single(subtitle.Paragraphs);
+        Assert.Equal("word<i> next</i>", subtitle.Paragraphs[0].Text);
+    }
+}