forked from qt-creator/qt-creator
QmlDesigner: Fix crash when typing keywords as Ids
We don't check that an ID entered in the Navigator or Property Editor is not a JavaScript keyword. This patch revamps the RewritingException that happens in this case in a InvalidIdException. Task-number: QTCREATORBUG-1540
This commit is contained in:
@@ -36,8 +36,25 @@ InvalidIdException::InvalidIdException(int line,
|
||||
const QString &function,
|
||||
const QString &file,
|
||||
const QString &id,
|
||||
bool duplicate) :
|
||||
InvalidArgumentException(line, function, file, "id"), m_id(id), m_duplicate(duplicate)
|
||||
Reason reason) :
|
||||
InvalidArgumentException(line, function, file, "id"),
|
||||
m_id(id)
|
||||
{
|
||||
if (reason == InvalidCharacters) {
|
||||
m_description = QCoreApplication::translate("InvalidIdException", "Only alphanumeric characters and underscore allowed.\nIds must begin with a lowercase letter.");
|
||||
} else {
|
||||
m_description = QCoreApplication::translate("InvalidIdException", "Ids have to be unique.");
|
||||
}
|
||||
}
|
||||
|
||||
InvalidIdException::InvalidIdException(int line,
|
||||
const QString &function,
|
||||
const QString &file,
|
||||
const QString &id,
|
||||
const QString &description) :
|
||||
InvalidArgumentException(line, function, file, "id"),
|
||||
m_id(id),
|
||||
m_description(description)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -48,22 +65,7 @@ QString InvalidIdException::type() const
|
||||
|
||||
QString InvalidIdException::description() const
|
||||
{
|
||||
if (m_duplicate)
|
||||
return duplicateErrorMessage(m_id);
|
||||
|
||||
return invalidErrorMessage(m_id);
|
||||
}
|
||||
|
||||
QString InvalidIdException::duplicateErrorMessage(const QString &id)
|
||||
{
|
||||
return QCoreApplication::translate("InvalidIdException", "Ids have to be unique: ") + id;
|
||||
}
|
||||
|
||||
QString InvalidIdException::invalidErrorMessage(const QString &id)
|
||||
{
|
||||
return QCoreApplication::translate("InvalidIdException", "Invalid Id: ") +
|
||||
id + QCoreApplication::translate("InvalidIdException",
|
||||
"\nOnly alphanumeric characters and underscore allowed.\nIds must begin with a lowercase letter.");
|
||||
return QCoreApplication::translate("InvalidIdException", "Invalid Id: %1\n%2").arg(m_id, m_description);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -37,22 +37,26 @@ namespace QmlDesigner {
|
||||
class CORESHARED_EXPORT InvalidIdException : public InvalidArgumentException
|
||||
{
|
||||
public:
|
||||
enum Reason { InvalidCharacters, DuplicateId };
|
||||
|
||||
InvalidIdException(int line,
|
||||
const QString &function,
|
||||
const QString &file,
|
||||
const QString &id,
|
||||
bool duplicate = false);
|
||||
Reason reason);
|
||||
|
||||
InvalidIdException(int line,
|
||||
const QString &function,
|
||||
const QString &file,
|
||||
const QString &id,
|
||||
const QString &description);
|
||||
|
||||
QString type() const;
|
||||
QString description() const;
|
||||
|
||||
static QString duplicateErrorMessage(const QString &id);
|
||||
static QString invalidErrorMessage(const QString &id);
|
||||
|
||||
private:
|
||||
QString m_id;
|
||||
bool m_duplicate;
|
||||
|
||||
QString m_description;
|
||||
};
|
||||
|
||||
}
|
||||
|
@@ -65,6 +65,7 @@
|
||||
#include "rewritertransaction.h"
|
||||
#include "rewriterview.h"
|
||||
#include "rewritingexception.h"
|
||||
#include "invalididexception.h"
|
||||
|
||||
/*!
|
||||
\defgroup CoreModel
|
||||
@@ -259,7 +260,12 @@ void ModelPrivate::changeNodeId(const InternalNode::Pointer& internalNodePointer
|
||||
m_idNodeHash.remove(oldId);
|
||||
if (!id.isEmpty())
|
||||
m_idNodeHash.insert(id, internalNodePointer);
|
||||
|
||||
try {
|
||||
notifyNodeIdChanged(internalNodePointer, id, oldId);
|
||||
} catch (RewritingException &e) {
|
||||
throw InvalidIdException(__LINE__, __FUNCTION__, __FILE__, id, e.description());
|
||||
}
|
||||
}
|
||||
|
||||
void ModelPrivate::checkPropertyName(const QString &propertyName)
|
||||
|
@@ -180,13 +180,13 @@ void ModelNode::setId(const QString& id)
|
||||
}
|
||||
|
||||
if (!isValidId(id))
|
||||
throw InvalidIdException(__LINE__, __FUNCTION__, __FILE__, id);
|
||||
throw InvalidIdException(__LINE__, __FUNCTION__, __FILE__, id, InvalidIdException::InvalidCharacters);
|
||||
|
||||
if (id == ModelNode::id())
|
||||
return;
|
||||
|
||||
if (view()->hasId(id))
|
||||
throw InvalidIdException(__LINE__, __FUNCTION__, __FILE__, id, true);
|
||||
throw InvalidIdException(__LINE__, __FUNCTION__, __FILE__, id, InvalidIdException::DuplicateId);
|
||||
|
||||
m_model.data()->m_d->changeNodeId(internalNode(), id);
|
||||
}
|
||||
|
Reference in New Issue
Block a user