aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/qml/jsruntime/qv4stringobject.cpp56
-rw-r--r--src/qml/qml/qqmlvaluetypewrapper_p.h4
2 files changed, 56 insertions, 4 deletions
diff --git a/src/qml/jsruntime/qv4stringobject.cpp b/src/qml/jsruntime/qv4stringobject.cpp
index 31219228f0..f7c7eae199 100644
--- a/src/qml/jsruntime/qv4stringobject.cpp
+++ b/src/qml/jsruntime/qv4stringobject.cpp
@@ -1101,9 +1101,55 @@ ReturnedValue StringPrototype::method_toLowerCase(const FunctionObject *b, const
return Encode(v4->newString(value.toLower()));
}
+static const QLocale *getLocaleDataResource(const QV4::Value &val)
+{
+ if (const QV4::QQmlValueTypeWrapper *wrapper = val.as<QQmlValueTypeWrapper>())
+ return wrapper->cast<const QLocale>();
+
+ return nullptr;
+}
+
+static QLocale getLocaleFromArgs(const FunctionObject *b, const QV4::Value *argv, int argc)
+{
+ if (argc == 0)
+ return QLocale();
+
+ ExecutionEngine *v4 = b->engine();
+ Scope scope(b);
+ QString stringifiedLocale;
+ // First argument is a string, we check if it is a valid locale
+ if (const QV4::String *that = argv[0].as<QV4::String>()) {
+ stringifiedLocale = that->toQString();
+ } else if (const QLocale *locale = getLocaleDataResource(argv[0])) {
+ return *locale;
+ } else if (argv[0].isObject()) {
+ // First argument is an array, we check if the first element is
+ // a string and a valid locale.
+ ScopedObject arrayLike(scope, argv[0].toObject(scope.engine));
+ ScopedValue kValue(scope);
+ if (arrayLike->getLength() > 0) {
+ kValue = arrayLike->get(uint(0));
+ if (kValue->isString())
+ stringifiedLocale = kValue->toQString();
+ else if (const QLocale *locale = getLocaleDataResource(kValue))
+ return *locale;
+ }
+ } else {
+ v4->throwTypeError();
+ }
+
+ return QLocale(stringifiedLocale);
+}
+
ReturnedValue StringPrototype::method_toLocaleLowerCase(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
{
- return method_toLowerCase(b, thisObject, argv, argc);
+ ExecutionEngine *v4 = b->engine();
+ const QString value = getThisString(v4, thisObject);
+ if (v4->hasException)
+ return QV4::Encode::undefined();
+
+ QLocale locale = getLocaleFromArgs(b, argv, argc);
+ return Encode(v4->newString(locale.toLower(value)));
}
ReturnedValue StringPrototype::method_toUpperCase(const FunctionObject *b, const Value *thisObject, const Value *, int)
@@ -1118,7 +1164,13 @@ ReturnedValue StringPrototype::method_toUpperCase(const FunctionObject *b, const
ReturnedValue StringPrototype::method_toLocaleUpperCase(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
{
- return method_toUpperCase(b, thisObject, argv, argc);
+ ExecutionEngine *v4 = b->engine();
+ const QString value = getThisString(v4, thisObject);
+ if (v4->hasException)
+ return QV4::Encode::undefined();
+
+ QLocale locale = getLocaleFromArgs(b, argv, argc);
+ return Encode(v4->newString(locale.toUpper(value)));
}
ReturnedValue StringPrototype::method_trim(const FunctionObject *b, const Value *thisObject, const Value *, int)
diff --git a/src/qml/qml/qqmlvaluetypewrapper_p.h b/src/qml/qml/qqmlvaluetypewrapper_p.h
index 7c086d8378..703c63bdab 100644
--- a/src/qml/qml/qqmlvaluetypewrapper_p.h
+++ b/src/qml/qml/qqmlvaluetypewrapper_p.h
@@ -113,8 +113,8 @@ public:
QVariant toVariant() const;
- template<typename ValueType>
- ValueType *cast()
+ template <typename ValueType>
+ ValueType *cast() const
{
if (QMetaType::fromType<ValueType>() != d()->metaType())
return nullptr;