1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:47:45 +00:00

LibJS: Trim all types of whitespace characters before parsing numbers

This commit is contained in:
Idan Horowitz 2022-02-19 22:47:05 +02:00 committed by Linus Groh
parent 232e830a0a
commit 60bc5e3b5b
3 changed files with 3 additions and 2 deletions

View file

@ -452,7 +452,6 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::pad_end)
return pad_string(global_object, move(string), PadPlacement::End);
}
static constexpr Utf8View whitespace_characters = Utf8View("\x09\x0A\x0B\x0C\x0D\x20\xC2\xA0\xE1\x9A\x80\xE2\x80\x80\xE2\x80\x81\xE2\x80\x82\xE2\x80\x83\xE2\x80\x84\xE2\x80\x85\xE2\x80\x86\xE2\x80\x87\xE2\x80\x88\xE2\x80\x89\xE2\x80\x8A\xE2\x80\xAF\xE2\x81\x9F\xE3\x80\x80\xE2\x80\xA8\xE2\x80\xA9\xEF\xBB\xBF"sv);
ThrowCompletionOr<String> trim_string(GlobalObject& global_object, Value input_value, TrimMode where)
{
// 1. Let str be ? RequireObjectCoercible(string).

View file

@ -17,6 +17,7 @@ struct CodePoint {
};
CodePoint code_point_at(Utf16View const& string, size_t position);
static constexpr Utf8View whitespace_characters = Utf8View("\x09\x0A\x0B\x0C\x0D\x20\xC2\xA0\xE1\x9A\x80\xE2\x80\x80\xE2\x80\x81\xE2\x80\x82\xE2\x80\x83\xE2\x80\x84\xE2\x80\x85\xE2\x80\x86\xE2\x80\x87\xE2\x80\x88\xE2\x80\x89\xE2\x80\x8A\xE2\x80\xAF\xE2\x81\x9F\xE3\x80\x80\xE2\x80\xA8\xE2\x80\xA9\xEF\xBB\xBF"sv);
ThrowCompletionOr<String> trim_string(GlobalObject&, Value string, TrimMode where);
class StringPrototype final : public StringObject {

View file

@ -31,6 +31,7 @@
#include <LibJS/Runtime/ProxyObject.h>
#include <LibJS/Runtime/RegExpObject.h>
#include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/StringPrototype.h>
#include <LibJS/Runtime/SymbolObject.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
@ -476,7 +477,7 @@ ThrowCompletionOr<Value> Value::to_number(GlobalObject& global_object) const
case Type::Double:
return *this;
case Type::String: {
auto string = as_string().string().trim_whitespace();
String string = Utf8View(as_string().string()).trim(whitespace_characters, AK::TrimMode::Both).as_string();
if (string.is_empty())
return Value(0);
if (string == "Infinity" || string == "+Infinity")