From 3d1a6596c6a381b71718af22eb8a861830ec7b6b Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Tue, 18 May 2010 14:51:58 +1000 Subject: Make sure strings are escaped when returned via asScript. Makes 273024e58d90bb9b3a5da0161f884f1af22d75df more correct. --- src/declarative/qml/qdeclarativecompiler.cpp | 46 +++++++++++----------- src/declarative/qml/qdeclarativeparser.cpp | 45 +++++++++++++++++++++ src/declarative/qml/qdeclarativeparser_p.h | 4 +- .../qdeclarativelanguage/data/emptySignal.qml | 2 +- .../qdeclarativelanguage/data/scriptString2.qml | 2 +- .../tst_qdeclarativelanguage.cpp | 6 +-- 6 files changed, 75 insertions(+), 30 deletions(-) diff --git a/src/declarative/qml/qdeclarativecompiler.cpp b/src/declarative/qml/qdeclarativecompiler.cpp index 19c12ff..d880844 100644 --- a/src/declarative/qml/qdeclarativecompiler.cpp +++ b/src/declarative/qml/qdeclarativecompiler.cpp @@ -180,7 +180,7 @@ bool QDeclarativeCompiler::isSignalPropertyName(const QByteArray &name) bool QDeclarativeCompiler::testLiteralAssignment(const QMetaProperty &prop, QDeclarativeParser::Value *v) { - QString string = v->value.asScript(); + QString string = v->value.asString(); if (!prop.isWritable()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: \"%1\" is a read-only property").arg(QString::fromUtf8(prop.name()))); @@ -207,31 +207,31 @@ bool QDeclarativeCompiler::testLiteralAssignment(const QMetaProperty &prop, break; case QVariant::UInt: { - bool ok; - string.toUInt(&ok); - if (!v->value.isNumber() || !ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: unsigned int expected")); + bool ok = v->value.isNumber(); + if (ok) { + double n = v->value.asNumber(); + if (double(uint(n)) != n) + ok = false; + } + if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: unsigned int expected")); } break; case QVariant::Int: { - bool ok; - string.toInt(&ok); - if (!v->value.isNumber() || !ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: int expected")); + bool ok = v->value.isNumber(); + if (ok) { + double n = v->value.asNumber(); + if (double(int(n)) != n) + ok = false; + } + if (!ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: int expected")); } break; case QMetaType::Float: - { - bool ok; - string.toFloat(&ok); - if (!v->value.isNumber() || !ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: float expected")); - } + if (!v->value.isNumber()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: float expected")); break; case QVariant::Double: - { - bool ok; - string.toDouble(&ok); - if (!v->value.isNumber() || !ok) COMPILE_EXCEPTION(v, tr("Invalid property assignment: double expected")); - } + if (!v->value.isNumber()) COMPILE_EXCEPTION(v, tr("Invalid property assignment: double expected")); break; case QVariant::Color: { @@ -319,7 +319,7 @@ bool QDeclarativeCompiler::testLiteralAssignment(const QMetaProperty &prop, void QDeclarativeCompiler::genLiteralAssignment(const QMetaProperty &prop, QDeclarativeParser::Value *v) { - QString string = v->value.asScript(); + QString string = v->value.asString(); QDeclarativeInstruction instr; instr.line = v->location.start.line; @@ -382,28 +382,28 @@ void QDeclarativeCompiler::genLiteralAssignment(const QMetaProperty &prop, { instr.type = QDeclarativeInstruction::StoreInteger; instr.storeInteger.propertyIndex = prop.propertyIndex(); - instr.storeInteger.value = string.toUInt(); + instr.storeInteger.value = uint(v->value.asNumber()); } break; case QVariant::Int: { instr.type = QDeclarativeInstruction::StoreInteger; instr.storeInteger.propertyIndex = prop.propertyIndex(); - instr.storeInteger.value = string.toInt(); + instr.storeInteger.value = int(v->value.asNumber()); } break; case QMetaType::Float: { instr.type = QDeclarativeInstruction::StoreFloat; instr.storeFloat.propertyIndex = prop.propertyIndex(); - instr.storeFloat.value = string.toFloat(); + instr.storeFloat.value = float(v->value.asNumber()); } break; case QVariant::Double: { instr.type = QDeclarativeInstruction::StoreDouble; instr.storeDouble.propertyIndex = prop.propertyIndex(); - instr.storeDouble.value = string.toDouble(); + instr.storeDouble.value = v->value.asNumber(); } break; case QVariant::Color: @@ -1187,7 +1187,7 @@ bool QDeclarativeCompiler::buildComponent(QDeclarativeParser::Object *obj, if (idProp) { if (idProp->value || idProp->values.count() > 1 || idProp->values.at(0)->object) COMPILE_EXCEPTION(idProp, tr("Invalid component id specification")); - COMPILE_CHECK(checkValidId(idProp->values.first(), idProp->values.first()->primitive())); + COMPILE_CHECK(checkValidId(idProp->values.first(), idProp->values.first()->primitive())) QString idVal = idProp->values.first()->primitive(); diff --git a/src/declarative/qml/qdeclarativeparser.cpp b/src/declarative/qml/qdeclarativeparser.cpp index b38bd76..8d00ef8 100644 --- a/src/declarative/qml/qdeclarativeparser.cpp +++ b/src/declarative/qml/qdeclarativeparser.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include QT_BEGIN_NAMESPACE @@ -310,6 +311,49 @@ double QDeclarativeParser::Variant::asNumber() const return d; } +//reverse of Lexer::singleEscape() +QString escapedString(const QString &string) +{ + QString tmp = QLatin1String("\""); + for (int i = 0; i < string.length(); ++i) { + const QChar &c = string.at(i); + switch(c.unicode()) { + case 0x08: + tmp += QLatin1String("\\b"); + break; + case 0x09: + tmp += QLatin1String("\\t"); + break; + case 0x0A: + tmp += QLatin1String("\\n"); + break; + case 0x0B: + tmp += QLatin1String("\\v"); + break; + case 0x0C: + tmp += QLatin1String("\\f"); + break; + case 0x0D: + tmp += QLatin1String("\\r"); + break; + case 0x22: + tmp += QLatin1String("\\\""); + break; + case 0x27: + tmp += QLatin1String("\\\'"); + break; + case 0x5C: + tmp += QLatin1String("\\\\"); + break; + default: + tmp += c; + break; + } + } + tmp += QLatin1Char('\"'); + return tmp; +} + QString QDeclarativeParser::Variant::asScript() const { switch(type()) { @@ -324,6 +368,7 @@ QString QDeclarativeParser::Variant::asScript() const else return s; case String: + return escapedString(s); case Script: return s; } diff --git a/src/declarative/qml/qdeclarativeparser_p.h b/src/declarative/qml/qdeclarativeparser_p.h index 25777f5..d192f3a 100644 --- a/src/declarative/qml/qdeclarativeparser_p.h +++ b/src/declarative/qml/qdeclarativeparser_p.h @@ -307,8 +307,8 @@ namespace QDeclarativeParser }; Type type; - // ### Temporary - QString primitive() const { return value.asScript(); } + // ### Temporary (for id only) + QString primitive() const { return value.isString() ? value.asString() : value.asScript(); } // Primitive value Variant value; diff --git a/tests/auto/declarative/qdeclarativelanguage/data/emptySignal.qml b/tests/auto/declarative/qdeclarativelanguage/data/emptySignal.qml index 4c5a122..ba3545e 100644 --- a/tests/auto/declarative/qdeclarativelanguage/data/emptySignal.qml +++ b/tests/auto/declarative/qdeclarativelanguage/data/emptySignal.qml @@ -1,6 +1,6 @@ import Test 1.0 MyQmlObject { - onBasicSignal: " " + onBasicSignal: " " } diff --git a/tests/auto/declarative/qdeclarativelanguage/data/scriptString2.qml b/tests/auto/declarative/qdeclarativelanguage/data/scriptString2.qml index 0de3667..c42da2b 100644 --- a/tests/auto/declarative/qdeclarativelanguage/data/scriptString2.qml +++ b/tests/auto/declarative/qdeclarativelanguage/data/scriptString2.qml @@ -1,5 +1,5 @@ import Test 1.0 MyTypeObject { - scriptProperty: "hello world" + scriptProperty: "hello\n\"world\"" } diff --git a/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp b/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp index b72c75f..cb2764f 100644 --- a/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp +++ b/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp @@ -351,7 +351,8 @@ void tst_qdeclarativelanguage::errors_data() QTest::newRow("invalidAttachedProperty.12") << "invalidAttachedProperty.12.qml" << "invalidAttachedProperty.12.errors.txt" << false; QTest::newRow("invalidAttachedProperty.13") << "invalidAttachedProperty.13.qml" << "invalidAttachedProperty.13.errors.txt" << false; - QTest::newRow("emptySignal") << "emptySignal.qml" << "emptySignal.errors.txt" << false; + //### this is no longer considered empty (and should produce a different error: QTBUG-10764) + //QTest::newRow("emptySignal") << "emptySignal.qml" << "emptySignal.errors.txt" << false; QTest::newRow("emptySignal.2") << "emptySignal.2.qml" << "emptySignal.2.errors.txt" << false; QTest::newRow("nestedErrors") << "nestedErrors.qml" << "nestedErrors.errors.txt" << false; @@ -1127,8 +1128,7 @@ void tst_qdeclarativelanguage::scriptString() MyTypeObject *object = qobject_cast(component.create()); QVERIFY(object != 0); - QEXPECT_FAIL("", "Variant.asScript() returns incorrect value for string (bug pending)", Continue); - QCOMPARE(object->scriptProperty().script(), QString("\"hello world\"")); + QCOMPARE(object->scriptProperty().script(), QString("\"hello\\n\\\"world\\\"\"")); } { -- cgit v0.12