fix(comments): handle adjacent run boundaries

Use half-open command ranges so a zero-length attachment at the shared boundary of adjacent command runs is rendered exactly once. Add a regression spec for the boundary case.
This commit is contained in:
¨chico10117¨ 2026-08-07 16:16:41 -06:00
parent ff8aa06f84
commit 2f1fac21f6
2 changed files with 42 additions and 1 deletions

View File

@ -147,4 +147,43 @@ Spectator.describe "parse_description" do
"cd</a>"
)
end
it "assigns boundary attachments to only the following command run" do
description = JSON.parse(<<-JSON)
{
"content": "abcd",
"commandRuns": [
{
"startIndex": 0,
"length": 2,
"onTap": {"innertubeCommand": {"watchEndpoint": {"videoId": "first-video"}}}
},
{
"startIndex": 2,
"length": 2,
"onTap": {"innertubeCommand": {"watchEndpoint": {"videoId": "second-video"}}}
}
],
"attachmentRuns": [{
"startIndex": 2,
"length": 0,
"element": {
"type": {
"imageType": {
"image": {
"sources": [{"url": "https://lh3.googleusercontent.com/emoji-boundary=s16", "width": 16, "height": 16}]
}
}
}
},
"properties": {"accessibilityProperties": {"label": "emoji-boundary"}}
}]
}
JSON
expect(parse_description(description, "video-id")).to eq(
%(<a href="/watch?v=first-video">ab</a>) +
%(<a href="/watch?v=second-video"><img alt="emoji-boundary" src="/ggpht/emoji-boundary=s16" title="emoji-boundary" width="16" height="16" class="channel-emoji" />cd</a>)
)
end
end

View File

@ -99,7 +99,9 @@ private def attachment_run_inside_command?(attachment, command) : Bool
command_start = command["startIndex"].as_i
command_end = command_start + command["length"].as_i
attachment_start >= command_start && attachment_end <= command_end
attachment_start >= command_start &&
attachment_start < command_end &&
attachment_end <= command_end
end
private def parse_command_run(command, nested_attachments, iter, video_id : String) : String