From 23c808598c77f1b793854424d881a89f2fe3f305 Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Sat, 24 May 2025 16:10:51 -0500 Subject: [PATCH] Functional change-only version of eatSpacesOrTabs PR Version of @jeanem's PR with built and non-functional changes stripped out. --- scripts/webvtt.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/scripts/webvtt.js b/scripts/webvtt.js index b6954407..4483361c 100644 --- a/scripts/webvtt.js +++ b/scripts/webvtt.js @@ -669,6 +669,24 @@ while (state.text[0] === '\t' || state.text[0] === ' ') { cut(state, 1); } + // Remove trailing spaces or tabs, but don't remove newlines + // Split the text by newline characters, including sequences of newlines, to process each segment + var segments = state.text.split(/(\n+)/); + // Process each segment to remove trailing spaces or tabs before newline sequences + segments = segments.map(function (segment) { + // Only process segments that are not just newline sequences + if (!segment.match(/^\n+$/)) { + var endIndex = segment.length - 1; + while ( endIndex >= 0 && ( segment[endIndex] === " " || segment[endIndex] === "\t" ) ) { + endIndex--; + } + return segment.substring(0, endIndex + 1); + } + return segment; + }); + + // Rejoin the segments, preserving newline sequences + state.text = segments.join(""); } function eatAtLeast1SpacesOrTabs(state) {