Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljsvaluetypefromstringcheck.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6#include <private/qqmlstringconverters_p.h>
7
9
10using namespace Qt::StringLiterals;
11
12/*!
13\internal
14\class ValueTypeFromStringError
15
16Helps to differentiate between three states:
17\list
18\li a value type was constructed by a string in a deprecated way, e.g. "50x70" for a QSizeF
19\li a value type was constructed by an invalid string in a deprecated way, e.g. "50,70" for a QSizeF
20\li a value type was constructed by a string in a non-deprecated way.
21\endlist
22*/
23
24/*!
25\internal
26Checks if known value types are being constructed in a deprecated way, and constructs the code for
27the fix-it.
28*/
29QQmlJSStructuredTypeError QQmlJSValueTypeFromStringCheck::hasError(const QString &typeName,
30 const QString &value)
31{
32 if (typeName == u"QPointF" || typeName == u"QPoint") {
33 std::array<double, 2> numbers;
34 if (!QQmlStringConverters::isValidNumberString<2, u','>(value, &numbers))
35 return QQmlJSStructuredTypeError::withInvalidString();
36
37 const auto result = QQmlJSStructuredTypeError::fromSuggestedString(
38 u"({ x: %1, y: %2 })"_s.arg(numbers[0]).arg(numbers[1]));
39 return result;
40 } else if (typeName == u"QSizeF" || typeName == u"QSize") {
41 std::array<double, 2> numbers;
42 if (!QQmlStringConverters::isValidNumberString<2, u'x'>(value, &numbers))
43 return QQmlJSStructuredTypeError::withInvalidString();
44
45 const auto result = QQmlJSStructuredTypeError::fromSuggestedString(
46 u"({ width: %1, height: %2 })"_s.arg(numbers[0]).arg(numbers[1]));
47 return result;
48 } else if (typeName == u"QRectF" || typeName == u"QRect") {
49 std::array<double, 4> numbers;
50 if (!QQmlStringConverters::isValidNumberString<4, u',', u',', u'x'>(value, &numbers))
51 return QQmlJSStructuredTypeError::withInvalidString();
52
53 const auto result = QQmlJSStructuredTypeError::fromSuggestedString(
54 u"({ x: %1, y: %2, width: %3, height: %4 })"_s.arg(numbers[0])
55 .arg(numbers[1])
56 .arg(numbers[2])
57 .arg(numbers[3]));
58 return result;
59 }
60
61 return QQmlJSStructuredTypeError::withValidString();
62}
63
64QT_END_NAMESPACE