diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-04-03 09:31:04 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-04-05 00:14:29 +0100 |
commit | b36c3a68d8a034d15e4b9164e5d4d4cb82c0cbc7 (patch) | |
tree | abf04e99b9e736d63af318765b44815d0113bede /Userland/Utilities/js.cpp | |
parent | 9e5abec6f16ae5ded9374a4ba8de5300d6aaf7d0 (diff) | |
download | serenity-b36c3a68d8a034d15e4b9164e5d4d4cb82c0cbc7.zip |
js: Convert non-UTF-8 encoded files to UTF-8 before parsing
Diffstat (limited to 'Userland/Utilities/js.cpp')
-rw-r--r-- | Userland/Utilities/js.cpp | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 961b6e5449..56f0a306aa 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -71,6 +71,7 @@ #include <LibJS/SourceTextModule.h> #include <LibLine/Editor.h> #include <LibMain/Main.h> +#include <LibTextCodec/Decoder.h> #include <fcntl.h> #include <signal.h> #include <stdio.h> @@ -1653,7 +1654,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly)); auto file_contents = file->read_all(); auto source = StringView { file_contents }; - builder.append(source); + + if (Utf8View { file_contents }.validate()) { + builder.append(source); + } else { + auto* decoder = TextCodec::decoder_for("windows-1252"); + VERIFY(decoder); + + auto utf8_source = TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, source); + builder.append(utf8_source); + } } source_name = script_paths[0]; |