mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:57:45 +00:00
LibJS: Trim all types of whitespace characters before parsing numbers
This commit is contained in:
parent
232e830a0a
commit
60bc5e3b5b
3 changed files with 3 additions and 2 deletions
|
@ -452,7 +452,6 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::pad_end)
|
||||||
return pad_string(global_object, move(string), PadPlacement::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)
|
ThrowCompletionOr<String> trim_string(GlobalObject& global_object, Value input_value, TrimMode where)
|
||||||
{
|
{
|
||||||
// 1. Let str be ? RequireObjectCoercible(string).
|
// 1. Let str be ? RequireObjectCoercible(string).
|
||||||
|
|
|
@ -17,6 +17,7 @@ struct CodePoint {
|
||||||
};
|
};
|
||||||
|
|
||||||
CodePoint code_point_at(Utf16View const& string, size_t position);
|
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);
|
ThrowCompletionOr<String> trim_string(GlobalObject&, Value string, TrimMode where);
|
||||||
|
|
||||||
class StringPrototype final : public StringObject {
|
class StringPrototype final : public StringObject {
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#include <LibJS/Runtime/ProxyObject.h>
|
#include <LibJS/Runtime/ProxyObject.h>
|
||||||
#include <LibJS/Runtime/RegExpObject.h>
|
#include <LibJS/Runtime/RegExpObject.h>
|
||||||
#include <LibJS/Runtime/StringObject.h>
|
#include <LibJS/Runtime/StringObject.h>
|
||||||
|
#include <LibJS/Runtime/StringPrototype.h>
|
||||||
#include <LibJS/Runtime/SymbolObject.h>
|
#include <LibJS/Runtime/SymbolObject.h>
|
||||||
#include <LibJS/Runtime/VM.h>
|
#include <LibJS/Runtime/VM.h>
|
||||||
#include <LibJS/Runtime/Value.h>
|
#include <LibJS/Runtime/Value.h>
|
||||||
|
@ -476,7 +477,7 @@ ThrowCompletionOr<Value> Value::to_number(GlobalObject& global_object) const
|
||||||
case Type::Double:
|
case Type::Double:
|
||||||
return *this;
|
return *this;
|
||||||
case Type::String: {
|
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())
|
if (string.is_empty())
|
||||||
return Value(0);
|
return Value(0);
|
||||||
if (string == "Infinity" || string == "+Infinity")
|
if (string == "Infinity" || string == "+Infinity")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue