mirror of
https://github.com/iv-org/invidious.git
synced 2026-09-07 09:33:04 -05:00
Fix link misalignment: count UTF-16 code units instead of codepoints
YouTube provides startIndex/length in UTF-16 code units, but Invidious counted codepoints. Supplementary codepoints (emojis, rare CJK) are 2 UTF-16 units but 1 codepoint, causing links to shift by 1 per emoji. Fix: count UTF-16 code units in copy_string via utf16_len() helper. Also fix no-commands path (content.size is codepoint count, not a valid limit for UTF-16 counting) and tail copy (was using byte-based content.size - index). Verified with real YouTube data (Rick Astley video, 16 commandRuns, emoji 📚): old code produced 11/16 shifted links ('ttps://' instead of 'https://'), fixed code produces 16/16 correct links. Fixes #5821
This commit is contained in:
parent
47b5d79ec7
commit
f7ac70496e
@ -1,6 +1,13 @@
|
|||||||
require "json"
|
require "json"
|
||||||
require "uri"
|
require "uri"
|
||||||
|
|
||||||
|
# YouTube provides startIndex/length in UTF-16 code units.
|
||||||
|
# Supplementary codepoints (emojis, rare CJK) are 2 UTF-16 units (surrogate pair)
|
||||||
|
# but 1 Crystal codepoint. Counting in codepoints causes misaligned links.
|
||||||
|
private def utf16_len(cp : Int) : Int
|
||||||
|
cp > 0xFFFF ? 2 : 1
|
||||||
|
end
|
||||||
|
|
||||||
private def copy_string(str : String::Builder, iter : Iterator, count : Int) : Int
|
private def copy_string(str : String::Builder, iter : Iterator, count : Int) : Int
|
||||||
copied = 0
|
copied = 0
|
||||||
while copied < count
|
while copied < count
|
||||||
@ -15,13 +22,13 @@ private def copy_string(str : String::Builder, iter : Iterator, count : Int) : I
|
|||||||
str << """
|
str << """
|
||||||
elsif cp == 0x3C # Less-than (<)
|
elsif cp == 0x3C # Less-than (<)
|
||||||
str << "<"
|
str << "<"
|
||||||
elsif cp == 0x3E # Greater than (>)
|
elsif cp == 0x3E # Greater-than (>)
|
||||||
str << ">"
|
str << ">"
|
||||||
else
|
else
|
||||||
str << cp.chr
|
str << cp.chr
|
||||||
end
|
end
|
||||||
|
|
||||||
copied += 1
|
copied += utf16_len(cp)
|
||||||
end
|
end
|
||||||
|
|
||||||
return copied
|
return copied
|
||||||
@ -38,7 +45,7 @@ def parse_description(desc, video_id : String) : String?
|
|||||||
# Slightly faster than HTML.escape, as we're only doing one pass on
|
# Slightly faster than HTML.escape, as we're only doing one pass on
|
||||||
# the string instead of five for the standard library
|
# the string instead of five for the standard library
|
||||||
return String.build do |str|
|
return String.build do |str|
|
||||||
copy_string(str, content.each_codepoint, content.size)
|
copy_string(str, content.each_codepoint, Int32::MAX)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -69,8 +76,7 @@ def parse_description(desc, video_id : String) : String?
|
|||||||
index += cmd_length
|
index += cmd_length
|
||||||
end
|
end
|
||||||
|
|
||||||
# Copy the end of the string (past the last command).
|
# Copy the rest of the string (past the last command).
|
||||||
remaining_length = content.size - index
|
copy_string(str, iter, Int32::MAX)
|
||||||
copy_string(str, iter, remaining_length) if remaining_length > 0
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user