From cf04d5ee18344d45da538810654690bd3936f46a Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 22 Jan 2019 13:38:44 +0100 Subject: Bring behavior of String.replace() in line with other engines "x".replace("x", "$1") gives "$1" in both JSC and V8, as there are no captures that could be used as a replacement for $1. Implement the same behavior as it's the most logical thing to do (even though it's undefined according to the spec). Two digit captures ($nm) work in a way that they get applied if $nm captures exist. If there are less than nm but more than n captures available $n is replaced by the n'th capture and m is copied over verbatim. Change-Id: I8b5f576f2c42c8334859ab7854dcdf07104dd35b Fixes: QTBUG-73152 Reviewed-by: Simon Hausmann --- tests/auto/qml/qjsengine/tst_qjsengine.cpp | 59 ++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'tests/auto/qml/qjsengine/tst_qjsengine.cpp') diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index cf7b9c7224..42d8f37ca8 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -220,6 +220,8 @@ private slots: void functionToString_data(); void functionToString(); + void stringReplace(); + void protoChanges_QTBUG68369(); void multilineStrings(); @@ -4367,6 +4369,63 @@ void tst_QJSEngine::functionToString() QCOMPARE(evaluationResult.toString(), expectedString); } +void tst_QJSEngine::stringReplace() +{ + QJSEngine engine; + + QJSValue val = engine.evaluate("'x'.replace('x', '$1')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$1")); + + val = engine.evaluate("'x'.replace('x', '$10')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$10")); + + val = engine.evaluate("'x'.replace('x', '$01')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$01")); + + val = engine.evaluate("'x'.replace('x', '$0')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$0")); + + val = engine.evaluate("'x'.replace('x', '$00')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$00")); + + val = engine.evaluate("'x'.replace(/(x)/, '$1')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("x")); + + val = engine.evaluate("'x'.replace(/(x)/, '$01')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("x")); + + val = engine.evaluate("'x'.replace(/(x)/, '$2')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$2")); + + val = engine.evaluate("'x'.replace(/(x)/, '$02')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$02")); + + val = engine.evaluate("'x'.replace(/(x)/, '$0')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$0")); + + val = engine.evaluate("'x'.replace(/(x)/, '$00')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("$00")); + + val = engine.evaluate("'x'.replace(/()()()()()()()()()(x)/, '$11')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("1")); + + val = engine.evaluate("'x'.replace(/()()()()()()()()()(x)/, '$10')"); + QVERIFY(val.isString()); + QCOMPARE(val.toString(), QString("x")); +} + void tst_QJSEngine::protoChanges_QTBUG68369() { QJSEngine engine; -- cgit v1.2.3