QmlDesigner: Fix some escape characters

Task-number: QDS-9415
Change-Id: I6459451d0f7699c727b2e0baad726f5c6af2ca74
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Aleksei German
2023-05-11 12:00:19 +02:00
parent d9cc2dc8c5
commit 3c36e77858
6 changed files with 60 additions and 36 deletions

View File

@@ -40,7 +40,7 @@ StudioControls.TextField {
function escapeString(string) { function escapeString(string) {
var str = string var str = string
str = str.replace(/\\/g, "\\\\") str = str.replace(/\\/g, "\\\\")
str.replace(/\"/g, "\\\"") str = str.replace(/\"/g, "\\\"")
str = str.replace(/\t/g, "\\t") str = str.replace(/\t/g, "\\t")
str = str.replace(/\r/g, "\\r") str = str.replace(/\r/g, "\\r")
str = str.replace(/\n/g, '\\n') str = str.replace(/\n/g, '\\n')

View File

@@ -252,6 +252,7 @@ extend_qtc_library(QmlDesignerCore
rewriterview.h rewriterview.h
rewritingexception.h rewritingexception.h
signalhandlerproperty.h signalhandlerproperty.h
stringutils.h
stylesheetmerger.h stylesheetmerger.h
subcomponentmanager.h subcomponentmanager.h
synchronousimagecache.h synchronousimagecache.h

View File

@@ -0,0 +1,42 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <QString>
namespace QmlDesigner {
inline QString escape(const QString &value)
{
QString result = value;
if (value.length() == 6 && value.startsWith("\\u")) //Do not double escape unicode chars
return value;
result.replace(QStringLiteral("\\"), QStringLiteral("\\\\"));
result.replace(QStringLiteral("\""), QStringLiteral("\\\""));
result.replace(QStringLiteral("\t"), QStringLiteral("\\t"));
result.replace(QStringLiteral("\r"), QStringLiteral("\\r"));
result.replace(QStringLiteral("\n"), QStringLiteral("\\n"));
return result;
}
inline QString deescape(const QString &value)
{
QString result = value;
if (value.length() == 6 && value.startsWith("\\u")) //Ignore unicode chars
return value;
result.replace(QStringLiteral("\\\\"), QStringLiteral("\\"));
result.replace(QStringLiteral("\\\""), QStringLiteral("\""));
result.replace(QStringLiteral("\\t"), QStringLiteral("\t"));
result.replace(QStringLiteral("\\r"), QStringLiteral("\r"));
result.replace(QStringLiteral("\\n"), QStringLiteral("\n"));
return result;
}
} // namespace QmlDesigner

View File

@@ -14,6 +14,7 @@
#include "qmlstate.h" #include "qmlstate.h"
#include "qmltimelinekeyframegroup.h" #include "qmltimelinekeyframegroup.h"
#include "qmlvisualnode.h" #include "qmlvisualnode.h"
#include "stringutils.h"
#include "variantproperty.h" #include "variantproperty.h"
#include <auxiliarydataproperties.h> #include <auxiliarydataproperties.h>
@@ -256,7 +257,7 @@ QString QmlObjectNode::stripedTranslatableText(const PropertyName &name) const
const QRegularExpressionMatch match = regularExpressionPattern.match( const QRegularExpressionMatch match = regularExpressionPattern.match(
modelNode().bindingProperty(name).expression()); modelNode().bindingProperty(name).expression());
if (match.hasMatch()) if (match.hasMatch())
return match.captured(2); return deescape(match.captured(2));
return instanceValue(name).toString(); return instanceValue(name).toString();
} }
return instanceValue(name).toString(); return instanceValue(name).toString();
@@ -536,15 +537,17 @@ QVariant QmlObjectNode::instanceValue(const ModelNode &modelNode, const Property
QString QmlObjectNode::generateTranslatableText([[maybe_unused]] const QString &text, QString QmlObjectNode::generateTranslatableText([[maybe_unused]] const QString &text,
const DesignerSettings &settings) const DesignerSettings &settings)
{ {
const QString escapedText = escape(text);
if (settings.value(DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt()) if (settings.value(DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt())
switch (settings.value(DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt()) { switch (settings.value(DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt()) {
case 0: return QString(QStringLiteral("qsTr(\"%1\")")).arg(text); case 0: return QString(QStringLiteral("qsTr(\"%1\")")).arg(escapedText);
case 1: return QString(QStringLiteral("qsTrId(\"%1\")")).arg(text); case 1: return QString(QStringLiteral("qsTrId(\"%1\")")).arg(escapedText);
case 2: return QString(QStringLiteral("qsTranslate(\"%1\", \"context\")")).arg(text); case 2: return QString(QStringLiteral("qsTranslate(\"%1\", \"context\")")).arg(escapedText);
default: default:
break; break;
} }
return QString(QStringLiteral("qsTr(\"%1\")")).arg(text); return QString(QStringLiteral("qsTr(\"%1\")")).arg(escapedText);
} }
QString QmlObjectNode::stripedTranslatableTextFunction(const QString &text) QString QmlObjectNode::stripedTranslatableTextFunction(const QString &text)
@@ -553,7 +556,7 @@ QString QmlObjectNode::stripedTranslatableTextFunction(const QString &text)
QLatin1String("^qsTr(|Id|anslate)\\(\"(.*)\"\\)$")); QLatin1String("^qsTr(|Id|anslate)\\(\"(.*)\"\\)$"));
const QRegularExpressionMatch match = regularExpressionPattern.match(text); const QRegularExpressionMatch match = regularExpressionPattern.match(text);
if (match.hasMatch()) if (match.hasMatch())
return match.captured(2); return deescape(match.captured(2));
return text; return text;
} }

View File

@@ -3,19 +3,20 @@
#include "qmltextgenerator.h" #include "qmltextgenerator.h"
#include <QVariant>
#include <QColor> #include <QColor>
#include <QVector4D> #include <QVariant>
#include <QVector3D>
#include <QVector2D> #include <QVector2D>
#include <QVector3D>
#include <QVector4D>
#include "bindingproperty.h" #include "bindingproperty.h"
#include "signalhandlerproperty.h" #include "model.h"
#include "nodeproperty.h"
#include "nodelistproperty.h" #include "nodelistproperty.h"
#include "nodeproperty.h"
#include "signalhandlerproperty.h"
#include "stringutils.h"
#include "variantproperty.h" #include "variantproperty.h"
#include <nodemetainfo.h> #include <nodemetainfo.h>
#include "model.h"
using namespace QmlDesigner; using namespace QmlDesigner;
using namespace QmlDesigner::Internal; using namespace QmlDesigner::Internal;
@@ -114,13 +115,9 @@ QString QmlTextGenerator::toQml(const AbstractProperty &property, int indentDept
if (property.name() == "id") if (property.name() == "id")
return stringValue; return stringValue;
if (false) {
}
if (variantProperty.holdsEnumeration()) { if (variantProperty.holdsEnumeration()) {
return variantProperty.enumeration().toString(); return variantProperty.enumeration().toString();
} else { } else {
switch (value.userType()) { switch (value.userType()) {
case QMetaType::Bool: case QMetaType::Bool:
if (value.toBool()) if (value.toBool())
@@ -284,20 +281,3 @@ QString QmlTextGenerator::propertyToQml(const AbstractProperty &property, int in
return result; return result;
} }
QString QmlTextGenerator::escape(const QString &value)
{
QString result = value;
if (value.count() == 6 && value.startsWith("\\u")) //Do not dobule escape unicode chars
return result;
result.replace(QStringLiteral("\\"), QStringLiteral("\\\\"));
result.replace(QStringLiteral("\""), QStringLiteral("\\\""));
result.replace(QStringLiteral("\t"), QStringLiteral("\\t"));
result.replace(QStringLiteral("\r"), QStringLiteral("\\r"));
result.replace(QStringLiteral("\n"), QStringLiteral("\\n"));
return result;
}

View File

@@ -32,8 +32,6 @@ private:
QString propertiesToQml(const ModelNode &node, int indentDepth) const; QString propertiesToQml(const ModelNode &node, int indentDepth) const;
QString propertyToQml(const AbstractProperty &property, int indentDepth) const; QString propertyToQml(const AbstractProperty &property, int indentDepth) const;
static QString escape(const QString &value);
private: private:
PropertyNameList m_propertyOrder; PropertyNameList m_propertyOrder;
TextEditor::TabSettings m_tabSettings; TextEditor::TabSettings m_tabSettings;