diff options
-rw-r--r-- | src/declarative/qml/qmldom.cpp | 98 | ||||
-rw-r--r-- | src/declarative/qml/qmldom.h | 25 | ||||
-rw-r--r-- | src/declarative/qml/qmldom_p.h | 17 | ||||
-rw-r--r-- | tests/auto/declarative/qmldom/data/MyComponent.qml (renamed from tests/auto/declarative/qmldom/MyComponent.qml) | 0 | ||||
-rw-r--r-- | tests/auto/declarative/qmldom/data/importdir/Bar.qml | 0 | ||||
-rw-r--r-- | tests/auto/declarative/qmldom/data/importlib/sublib/qmldir/Foo.qml | 0 | ||||
-rw-r--r-- | tests/auto/declarative/qmldom/data/top.qml (renamed from tests/auto/declarative/qmldom/top.qml) | 0 | ||||
-rw-r--r-- | tests/auto/declarative/qmldom/tst_qmldom.cpp | 46 |
8 files changed, 178 insertions, 8 deletions
diff --git a/src/declarative/qml/qmldom.cpp b/src/declarative/qml/qmldom.cpp index 648eb36..e293a93 100644 --- a/src/declarative/qml/qmldom.cpp +++ b/src/declarative/qml/qmldom.cpp @@ -145,9 +145,9 @@ int QmlDomDocument::version() const } /*! - Return the URIs listed by "import <dir>" in the qml. + Returns all import statements in qml. */ -QList<QUrl> QmlDomDocument::imports() const +QList<QmlDomImport> QmlDomDocument::imports() const { return d->imports; } @@ -191,7 +191,13 @@ bool QmlDomDocument::load(QmlEngine *engine, const QByteArray &data, const QUrl } for (int i = 0; i < td->data.imports().size(); ++i) { - d->imports += QUrl(td->data.imports().at(i).uri); + QmlScriptParser::Import parserImport = td->data.imports().at(i); + QmlDomImport domImport; + domImport.d->type = static_cast<QmlDomImportPrivate::Type>(parserImport.type); + domImport.d->uri = parserImport.uri; + domImport.d->qualifier = parserImport.qualifier; + domImport.d->version = parserImport.version; + d->imports += domImport; } if (td->data.tree()) { @@ -207,7 +213,6 @@ bool QmlDomDocument::load(QmlEngine *engine, const QByteArray &data, const QUrl return true; } - /*! Returns the last load errors. The load errors will be reset after a successful call to load(). @@ -1767,4 +1772,89 @@ void QmlDomComponent::setComponentRoot(const QmlDomObject &root) qWarning("QmlDomComponent::setComponentRoot(const QmlDomObject &): Not implemented"); } + +QmlDomImportPrivate::QmlDomImportPrivate() +: type(File) +{ +} + +QmlDomImportPrivate::QmlDomImportPrivate(const QmlDomImportPrivate &other) +: QSharedData(other) +{ +} + +QmlDomImportPrivate::~QmlDomImportPrivate() +{ +} + +/*! + \class QmlDomImport + \internal + \brief The QmlDomImport class represents an import statement. +*/ + +/*! + Construct an empty QmlDomImport. +*/ +QmlDomImport::QmlDomImport() +: d(new QmlDomImportPrivate) +{ +} + +/*! + Create a copy of \a other QmlDomImport. +*/ +QmlDomImport::QmlDomImport(const QmlDomImport &other) +: d(other.d) +{ +} + +/*! + Destroy the QmlDomImport. +*/ +QmlDomImport::~QmlDomImport() +{ +} + +/*! + Assign \a other to this QmlDomImport. +*/ +QmlDomImport &QmlDomImport::operator=(const QmlDomImport &other) +{ + d = other.d; + return *this; +} + +/*! + Returns the type of the import. + */ +QmlDomImport::Type QmlDomImport::type() const +{ + return static_cast<QmlDomImport::Type>(d->type); +} + +/*! + Returns the URI of the import (e.g. 'subdir' or 'com.nokia.Qt') + */ +QString QmlDomImport::uri() const +{ + return d->uri; +} + +/*! + Returns the version specified by the import. An empty string if no version was specified. + */ +QString QmlDomImport::version() const +{ + return d->version; +} + +/*! + Returns the (optional) qualifier string (the token following the 'as' keyword) of the import. + */ +QString QmlDomImport::qualifier() const +{ + return d->qualifier; +} + QT_END_NAMESPACE diff --git a/src/declarative/qml/qmldom.h b/src/declarative/qml/qmldom.h index ef9fe25..456202a 100644 --- a/src/declarative/qml/qmldom.h +++ b/src/declarative/qml/qmldom.h @@ -59,9 +59,11 @@ class QmlDomList; class QmlDomValue; class QmlEngine; class QmlDomComponent; +class QmlDomImport; class QIODevice; class QmlDomDocumentPrivate; + class Q_DECLARATIVE_EXPORT QmlDomDocument { public: @@ -71,7 +73,7 @@ public: QmlDomDocument &operator=(const QmlDomDocument &); int version() const; - QList<QUrl> imports() const; + QList<QmlDomImport> imports() const; QList<QmlError> errors() const; bool load(QmlEngine *, const QByteArray &, const QUrl & = QUrl()); @@ -304,6 +306,27 @@ private: QSharedDataPointer<QmlDomValuePrivate> d; }; +class QmlDomImportPrivate; +class Q_DECLARATIVE_EXPORT QmlDomImport +{ +public: + enum Type { Library, File }; + + QmlDomImport(); + QmlDomImport(const QmlDomImport &); + ~QmlDomImport(); + QmlDomImport &operator=(const QmlDomImport &); + + Type type() const; + QString uri() const; + QString version() const; + QString qualifier() const; + +private: + friend class QmlDomDocument; + QSharedDataPointer<QmlDomImportPrivate> d; +}; + QT_END_NAMESPACE QT_END_HEADER diff --git a/src/declarative/qml/qmldom_p.h b/src/declarative/qml/qmldom_p.h index 5345c44..b15844a 100644 --- a/src/declarative/qml/qmldom_p.h +++ b/src/declarative/qml/qmldom_p.h @@ -66,7 +66,7 @@ public: ~QmlDomDocumentPrivate(); QList<QmlError> errors; - QList<QUrl> imports; + QList<QmlDomImport> imports; QmlParser::Object *root; QList<int> automaticSemicolonOffsets; }; @@ -129,6 +129,21 @@ public: QmlParser::Value *value; }; +class QmlDomImportPrivate : public QSharedData +{ +public: + QmlDomImportPrivate(); + QmlDomImportPrivate(const QmlDomImportPrivate &); + ~QmlDomImportPrivate(); + + enum Type { Library, File }; + + Type type; + QString uri; + QString version; + QString qualifier; +}; + QT_END_NAMESPACE #endif // QMLDOM_P_H diff --git a/tests/auto/declarative/qmldom/MyComponent.qml b/tests/auto/declarative/qmldom/data/MyComponent.qml index 0940753..0940753 100644 --- a/tests/auto/declarative/qmldom/MyComponent.qml +++ b/tests/auto/declarative/qmldom/data/MyComponent.qml diff --git a/tests/auto/declarative/qmldom/data/importdir/Bar.qml b/tests/auto/declarative/qmldom/data/importdir/Bar.qml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/auto/declarative/qmldom/data/importdir/Bar.qml diff --git a/tests/auto/declarative/qmldom/data/importlib/sublib/qmldir/Foo.qml b/tests/auto/declarative/qmldom/data/importlib/sublib/qmldir/Foo.qml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/auto/declarative/qmldom/data/importlib/sublib/qmldir/Foo.qml diff --git a/tests/auto/declarative/qmldom/top.qml b/tests/auto/declarative/qmldom/data/top.qml index 142d4c8..142d4c8 100644 --- a/tests/auto/declarative/qmldom/top.qml +++ b/tests/auto/declarative/qmldom/data/top.qml diff --git a/tests/auto/declarative/qmldom/tst_qmldom.cpp b/tests/auto/declarative/qmldom/tst_qmldom.cpp index ca8929d..20419fb 100644 --- a/tests/auto/declarative/qmldom/tst_qmldom.cpp +++ b/tests/auto/declarative/qmldom/tst_qmldom.cpp @@ -17,6 +17,7 @@ private slots: void loadProperties(); void loadChildObject(); void loadComposite(); + void loadImports(); void testValueSource(); @@ -52,7 +53,7 @@ void tst_qmldom::loadProperties() QmlDomObject rootObject = document.rootObject(); QVERIFY(rootObject.isValid()); QVERIFY(rootObject.objectId() == "item"); - QVERIFY(rootObject.properties().size() == 2); + QCOMPARE(rootObject.properties().size(), 3); QmlDomProperty xProperty = rootObject.property("x"); QVERIFY(xProperty.propertyName() == "x"); @@ -91,7 +92,7 @@ void tst_qmldom::loadChildObject() void tst_qmldom::loadComposite() { - QFile file(SRCDIR "/top.qml"); + QFile file(SRCDIR "/data/top.qml"); QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); QmlDomDocument document; @@ -141,6 +142,47 @@ void tst_qmldom::testValueSource() QVERIFY(sourceValue.toBinding().binding() == "Math.min(Math.max(-130, value*2.2 - 130), 133)"); } +void tst_qmldom::loadImports() +{ + QByteArray qml = "import importlib.sublib 4.7\n" + "import importlib.sublib 4.6 as NewFoo\n" + "import 'import'\n" + "import 'import' as X\n" + "Item {}"; + + QmlEngine engine; + engine.addImportPath(SRCDIR "/data"); + QmlDomDocument document; + QVERIFY(document.load(&engine, qml)); + + QCOMPARE(document.imports().size(), 4); + + QmlDomImport import1 = document.imports().at(0); + QCOMPARE(import1.type(), QmlDomImport::Library); + QCOMPARE(import1.uri(), QLatin1String("importlib.sublib")); + QCOMPARE(import1.qualifier(), QString()); + QCOMPARE(import1.version(), QLatin1String("4.7")); + + QmlDomImport import2 = document.imports().at(1); + QCOMPARE(import2.type(), QmlDomImport::Library); + QCOMPARE(import2.uri(), QLatin1String("importlib.sublib")); + QCOMPARE(import2.qualifier(), QLatin1String("NewFoo")); + QCOMPARE(import2.version(), QLatin1String("4.6")); + + QmlDomImport import3 = document.imports().at(2); + QCOMPARE(import3.type(), QmlDomImport::File); + QCOMPARE(import3.uri(), QLatin1String("import")); + QCOMPARE(import3.qualifier(), QLatin1String("")); + QCOMPARE(import3.version(), QLatin1String("")); + + QmlDomImport import4 = document.imports().at(3); + QCOMPARE(import4.type(), QmlDomImport::File); + QCOMPARE(import4.uri(), QLatin1String("import")); + QCOMPARE(import4.qualifier(), QLatin1String("X")); + QCOMPARE(import4.version(), QLatin1String("")); +} + + QTEST_MAIN(tst_qmldom) #include "tst_qmldom.moc" |