mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:07:36 +00:00
LibJS: Make RegExp.prototype.source spec-compliant
Basically: - And edge case for this object being RegExp.prototype.source - Return "(?:)" for empty pattern - Escape some things properly
This commit is contained in:
parent
b6e5442d55
commit
8a9a7f1677
3 changed files with 36 additions and 5 deletions
|
@ -30,6 +30,7 @@
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/RegExpObject.h>
|
#include <LibJS/Runtime/RegExpObject.h>
|
||||||
#include <LibJS/Runtime/RegExpPrototype.h>
|
#include <LibJS/Runtime/RegExpPrototype.h>
|
||||||
|
#include <LibJS/Token.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
|
@ -84,6 +85,20 @@ static RegExpObject* regexp_object_from(VM& vm, GlobalObject& global_object)
|
||||||
return static_cast<RegExpObject*>(this_object);
|
return static_cast<RegExpObject*>(this_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String escape_regexp_pattern(const RegExpObject& regexp_object)
|
||||||
|
{
|
||||||
|
auto pattern = regexp_object.pattern();
|
||||||
|
if (pattern.is_empty())
|
||||||
|
return "(?:)";
|
||||||
|
// FIXME: Check u flag and escape accordingly
|
||||||
|
pattern.replace("\n", "\\n", true);
|
||||||
|
pattern.replace("\r", "\\r", true);
|
||||||
|
pattern.replace(LINE_SEPARATOR, "\\u2028", true);
|
||||||
|
pattern.replace(PARAGRAPH_SEPARATOR, "\\u2029", true);
|
||||||
|
pattern.replace("/", "\\/", true);
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::dot_all)
|
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::dot_all)
|
||||||
{
|
{
|
||||||
auto regexp_object = regexp_object_from(vm, global_object);
|
auto regexp_object = regexp_object_from(vm, global_object);
|
||||||
|
@ -142,11 +157,20 @@ JS_DEFINE_NATIVE_GETTER(RegExpPrototype::multiline)
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::source)
|
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::source)
|
||||||
{
|
{
|
||||||
|
auto this_object = this_object_from(vm, global_object);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// FIXME: This is obnoxious - we should have an easier way of looking up %RegExp.prototype%.
|
||||||
|
auto& regexp_prototype = global_object.get(vm.names.RegExp).as_object().get(vm.names.prototype).as_object();
|
||||||
|
if (this_object == ®exp_prototype)
|
||||||
|
return js_string(vm, "(?:)");
|
||||||
|
|
||||||
auto regexp_object = regexp_object_from(vm, global_object);
|
auto regexp_object = regexp_object_from(vm, global_object);
|
||||||
if (!regexp_object)
|
if (!regexp_object)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
return js_string(vm, regexp_object->pattern());
|
return js_string(vm, escape_regexp_pattern(*regexp_object));
|
||||||
}
|
}
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::sticky)
|
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::sticky)
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
test("basic functionality", () => {
|
test("basic functionality", () => {
|
||||||
// FIXME: update when toString is spec-compliant
|
expect(RegExp().toString()).toBe("/(?:)/");
|
||||||
expect(RegExp().toString()).toBe("//");
|
expect(RegExp(undefined).toString()).toBe("/(?:)/");
|
||||||
expect(RegExp(undefined).toString()).toBe("//");
|
|
||||||
expect(RegExp("foo").toString()).toBe("/foo/");
|
expect(RegExp("foo").toString()).toBe("/foo/");
|
||||||
expect(RegExp("foo", undefined).toString()).toBe("/foo/");
|
expect(RegExp("foo", undefined).toString()).toBe("/foo/");
|
||||||
expect(RegExp("foo", "g").toString()).toBe("/foo/g");
|
expect(RegExp("foo", "g").toString()).toBe("/foo/g");
|
||||||
expect(RegExp(undefined, "g").toString()).toBe("//g");
|
expect(RegExp(undefined, "g").toString()).toBe("/(?:)/g");
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(RegExp.prototype.source).toBe("(?:)");
|
||||||
|
expect(RegExp().source).toBe("(?:)");
|
||||||
|
expect(/test/.source).toBe("test");
|
||||||
|
expect(/\n/.source).toBe("\\n");
|
||||||
|
// FIXME: RegExp parse doesn't parse \/ :(
|
||||||
|
// expect(/foo\/bar/.source).toBe("foo\\/bar");
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue