From 45d652d30b26c4ea9f06031da295ae2b902e36fe Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Sat, 4 May 2013 22:09:55 +0200 Subject: [PATCH] Smaller fixes to RegExp 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 --- src/qml/qml/v4/qv4regexpobject.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/qml/qml/v4/qv4regexpobject.cpp b/src/qml/qml/v4/qv4regexpobject.cpp index a5723cf..bf488ff 100644 --- a/src/qml/qml/v4/qv4regexpobject.cpp +++ b/src/qml/qml/v4/qv4regexpobject.cpp @@ -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'); -- 2.7.4