summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/RegExpPrototype.cpp26
-rw-r--r--Libraries/LibJS/Tests/builtins/RegExp/RegExp.js7
-rw-r--r--Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.source.js8
3 files changed, 36 insertions, 5 deletions
diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Libraries/LibJS/Runtime/RegExpPrototype.cpp
index c35b8f987f..12327df652 100644
--- a/Libraries/LibJS/Runtime/RegExpPrototype.cpp
+++ b/Libraries/LibJS/Runtime/RegExpPrototype.cpp
@@ -30,6 +30,7 @@
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/RegExpObject.h>
#include <LibJS/Runtime/RegExpPrototype.h>
+#include <LibJS/Token.h>
namespace JS {
@@ -84,6 +85,20 @@ static RegExpObject* regexp_object_from(VM& vm, GlobalObject& global_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)
{
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)
{
+ 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 == &regexp_prototype)
+ return js_string(vm, "(?:)");
+
auto regexp_object = regexp_object_from(vm, global_object);
if (!regexp_object)
return {};
- return js_string(vm, regexp_object->pattern());
+ return js_string(vm, escape_regexp_pattern(*regexp_object));
}
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::sticky)
diff --git a/Libraries/LibJS/Tests/builtins/RegExp/RegExp.js b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.js
index aa18a0b907..9ae8ab97c0 100644
--- a/Libraries/LibJS/Tests/builtins/RegExp/RegExp.js
+++ b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.js
@@ -1,9 +1,8 @@
test("basic functionality", () => {
- // FIXME: update when toString is spec-compliant
- expect(RegExp().toString()).toBe("//");
- expect(RegExp(undefined).toString()).toBe("//");
+ expect(RegExp().toString()).toBe("/(?:)/");
+ expect(RegExp(undefined).toString()).toBe("/(?:)/");
expect(RegExp("foo").toString()).toBe("/foo/");
expect(RegExp("foo", undefined).toString()).toBe("/foo/");
expect(RegExp("foo", "g").toString()).toBe("/foo/g");
- expect(RegExp(undefined, "g").toString()).toBe("//g");
+ expect(RegExp(undefined, "g").toString()).toBe("/(?:)/g");
});
diff --git a/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.source.js b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.source.js
new file mode 100644
index 0000000000..4f0e14297f
--- /dev/null
+++ b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.source.js
@@ -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");
+});