Smaller fixes to RegExp
authorLars Knoll <lars.knoll@digia.com>
Sat, 4 May 2013 20:09:55 +0000 (22:09 +0200)
committerSimon Hausmann <simon.hausmann@digia.com>
Sun, 5 May 2013 12:12:03 +0000 (14:12 +0200)
The source property is supposed to contain an escaped version
of the literal, so that /source/ gives a valid regexp literal
again. toString() is supposed to use that same source property.

Change-Id: I2522ab76746002f6437839adacda7d453f8baa45
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
src/qml/qml/v4/qv4regexpobject.cpp

index a5723cf..bf488ff 100644 (file)
@@ -81,7 +81,16 @@ RegExpObject::RegExpObject(ExecutionEngine *engine, RegExp* value, bool global)
     lastIndexProperty->value = Value::fromInt32(0);
     if (!this->value)
         return;
-    defineReadonlyProperty(engine->newIdentifier(QStringLiteral("source")), Value::fromString(engine->newString(this->value->pattern())));
+
+    QString p = this->value->pattern();
+    if (p.isEmpty()) {
+        p = QStringLiteral("(?:)");
+    } else {
+        // escape certain parts, see ch. 15.10.4
+        p.replace('/', QLatin1String("\\/"));
+    }
+
+    defineReadonlyProperty(engine->newIdentifier(QStringLiteral("source")), Value::fromString(engine->newString(p)));
     defineReadonlyProperty(engine->newIdentifier(QStringLiteral("global")), Value::fromBoolean(global));
     defineReadonlyProperty(engine->newIdentifier(QStringLiteral("ignoreCase")), Value::fromBoolean(this->value->ignoreCase()));
     defineReadonlyProperty(engine->newIdentifier(QStringLiteral("multiline")), Value::fromBoolean(this->value->multiLine()));
@@ -190,9 +199,6 @@ Value RegExpCtor::construct(Managed *, ExecutionContext *ctx, Value *argv, int a
     QString pattern;
     if (!r.isUndefined())
         pattern = r.toString(ctx)->toQString();
-    // See sec 15.10.4.1
-    if (pattern.isEmpty())
-        pattern = QStringLiteral("(?:)");
 
     bool global = false;
     bool ignoreCase = false;
@@ -297,7 +303,7 @@ Value RegExpPrototype::method_toString(SimpleCallContext *ctx)
     if (!r)
         ctx->throwTypeError();
 
-    QString result = QChar('/') + r->value->pattern();
+    QString result = QChar('/') + r->get(ctx, ctx->engine->newIdentifier(QStringLiteral("source"))).stringValue()->toQString();
     result += QChar('/');
     if (r->global)
         result += QChar('g');