sindresorhus / Pasteboard-Viewer

📋 Inspect the system pasteboards on macOS
https://sindresorhus.com/pasteboard-viewer
MIT License
708 stars 35 forks source link

If strings are encoded as UTF-16, they are interpreted, whereas they’re not if encoded as UTF-8 #29

Closed avidrissman closed 10 months ago

avidrissman commented 10 months ago

Pasteboard Viewer is a useful tool to show exactly what’s on the pasteboard, so it should show it in the lowest-level format. However, when strings are encoded as UTF-16 they are interpreted.

For example, compile and run:

// save as pasteboard8.m, compile with
// cc pasteboard8.m -o pasteboard8 -framework AppKit

#import <AppKit/AppKit.h>

int main() {
  NSPasteboard* pb = NSPasteboard.generalPasteboard;
  [pb clearContents];

  [pb setData:[@"<html><p>hi</html>" dataUsingEncoding:NSUTF8StringEncoding] forType:NSPasteboardTypeHTML];
}

If you run that, and then use Pasteboard Viewer, you get:

image

Perfect. Now compile and run:

// save as pasteboard16.m, compile with
// cc pasteboard16.m -o pasteboard16 -framework AppKit

#import <AppKit/AppKit.h>

int main() {
  NSPasteboard* pb = NSPasteboard.generalPasteboard;
  [pb clearContents];

  // Loaded and displayed as the actual HTML contents:
  [pb setData:[@"<html><p>hi</html>" dataUsingEncoding:NSUTF16StringEncoding] forType:NSPasteboardTypeHTML];
}

If you run that, and then use Pasteboard Viewer, you get:

image

which interprets the data as HTML and loads the styled string.

It would be best if the raw string were shown, even if it is encoded other than UTF-8.

sindresorhus commented 10 months ago

Fixed in the latest update.