strings.js 745 B

12345678910111213141516171819202122232425262728
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2019 The noVNC Authors
  4. * Licensed under MPL 2.0 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. // Decode from UTF-8
  9. export function decodeUTF8(utf8string, allowLatin1=false) {
  10. try {
  11. return decodeURIComponent(escape(utf8string));
  12. } catch (e) {
  13. if (e instanceof URIError) {
  14. if (allowLatin1) {
  15. // If we allow Latin1 we can ignore any decoding fails
  16. // and in these cases return the original string
  17. return utf8string;
  18. }
  19. }
  20. throw e;
  21. }
  22. }
  23. // Encode to UTF-8
  24. export function encodeUTF8(DOMString) {
  25. return unescape(encodeURIComponent(DOMString));
  26. }