Decode UTF-16 clipboard text (#7249)

This commit is contained in:
David Heinemeier Hansson
2026-08-17 06:09:51 -04:00
committed by GitHub
parent 262d6f0681
commit 006460ad57
2 changed files with 60 additions and 1 deletions
+19 -1
View File
@@ -43,7 +43,25 @@ emit_image() {
}
emit_text() {
jq -cRs 'select(length > 0) | {type:"text", text:.}'
perl -MEncode=decode,FB_CROAK -MJSON::PP=encode_json -0777 -e '
my $raw = <STDIN>;
exit unless length $raw;
my $encoding;
if ($raw =~ /^(?:\xFF\xFE|\xFE\xFF)/) {
$encoding = "UTF-16";
} elsif ($raw =~ /^(?:[^\0]\0)+\z/s) {
# BOM-less UTF-16 is indistinguishable from NUL-separated bytes, so only
# decode the consistent whole-payload pattern seen from affected apps.
$encoding = "UTF-16LE";
} elsif ($raw =~ /^(?:\0[^\0])+\z/s) {
$encoding = "UTF-16BE";
}
my $text = $encoding ? eval { decode($encoding, $raw, FB_CROAK) } : undef;
$text = decode("UTF-8", $raw) unless defined $text;
print "{\"type\":\"text\",\"text\":", encode_json($text), "}\n";
'
}
case "${1:-}" in