From adbc7e878fe68f5e1d05745ecb6a40ce7a4b31df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= Date: Tue, 20 Jan 2015 16:40:56 +0100 Subject: Add loadHtml() function on Android and iOS. This adds a loadHtml() function to the WebView, making it possible for the user to set the HTML content of the WebView. [ChangeLog][WebView] Added loadHtml() function. Task-number: QTBUG-42335 Change-Id: I8a044e4fb2c0fc4bacff049f02a8cb525d593e15 Reviewed-by: Friedemann Kleint Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/imports/plugins.qmltypes | 9 +++++++++ src/webview/qquickwebview.cpp | 21 +++++++++++++++++++++ src/webview/qquickwebview.h | 1 + src/webview/qwebview.cpp | 6 ++++++ src/webview/qwebview_android.cpp | 20 ++++++++++++++++++++ src/webview/qwebview_android_p.h | 1 + src/webview/qwebview_ios.mm | 5 +++++ src/webview/qwebview_ios_p.h | 1 + src/webview/qwebview_p.h | 1 + src/webview/qwebviewinterface_p.h | 1 + tests/auto/webview/qwebview/tst_qwebview.cpp | 11 +++++++++++ 11 files changed, 77 insertions(+) diff --git a/src/imports/plugins.qmltypes b/src/imports/plugins.qmltypes index a0a57bf..3532199 100644 --- a/src/imports/plugins.qmltypes +++ b/src/imports/plugins.qmltypes @@ -20,6 +20,15 @@ Module { Method { name: "goForward" } Method { name: "reload" } Method { name: "stop" } + Method { + name: "loadHtml" + Parameter { name: "html"; type: "string" } + Parameter { name: "baseUrl"; type: "QUrl" } + } + Method { + name: "loadHtml" + Parameter { name: "html"; type: "string" } + } Method { name: "runJavaScript" Parameter { name: "script"; type: "string" } diff --git a/src/webview/qquickwebview.cpp b/src/webview/qquickwebview.cpp index 1803994..d825d2f 100644 --- a/src/webview/qquickwebview.cpp +++ b/src/webview/qquickwebview.cpp @@ -218,6 +218,27 @@ void QQuickWebView::stop() m_webView->stop(); } +/*! + \qmlmethod void QtWebView::WebView::loadHtml(string html, url baseUrl) + + Loads the specified \a html content to the web view. + + This method offers a lower-level alternative to the \c{url} property, + which references HTML pages via URL. + + External objects such as stylesheets or images referenced in the HTML + document should be located relative to \a baseUrl. For example, if \a html + is retrieved from \c http://www.example.com/documents/overview.html, which + is the base url, then an image referenced with the relative url, \c diagram.png, + should be at \c{http://www.example.com/documents/diagram.png}. + + \sa QtWebView::WebView::url +*/ +void QQuickWebView::loadHtml(const QString &html, const QUrl &baseUrl) +{ + m_webView->loadHtml(html, baseUrl); +} + /*! \qmlmethod void QtWebView::WebView::runJavaScript(string script, variant callback) diff --git a/src/webview/qquickwebview.h b/src/webview/qquickwebview.h index c36cc9d..8b2207a 100644 --- a/src/webview/qquickwebview.h +++ b/src/webview/qquickwebview.h @@ -84,6 +84,7 @@ public Q_SLOTS: void goForward() Q_DECL_OVERRIDE; void reload() Q_DECL_OVERRIDE; void stop() Q_DECL_OVERRIDE; + void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) Q_DECL_OVERRIDE; void runJavaScript(const QString& script, const QJSValue &callback = QJSValue()); diff --git a/src/webview/qwebview.cpp b/src/webview/qwebview.cpp index 1865c7e..8c1b804 100644 --- a/src/webview/qwebview.cpp +++ b/src/webview/qwebview.cpp @@ -152,6 +152,12 @@ void QWebView::setFocus(bool focus) d->setFocus(focus); } +void QWebView::loadHtml(const QString &html, const QUrl &baseUrl) +{ + Q_D(QWebView); + d->loadHtml(html, baseUrl); +} + void QWebView::runJavaScriptPrivate(const QString &script, int callbackId) { diff --git a/src/webview/qwebview_android.cpp b/src/webview/qwebview_android.cpp index 1ff4271..b7e19b6 100644 --- a/src/webview/qwebview_android.cpp +++ b/src/webview/qwebview_android.cpp @@ -113,6 +113,26 @@ void QAndroidWebViewPrivate::setUrl(const QUrl &url) QJNIObjectPrivate::fromString(url.toString()).object()); } +void QAndroidWebViewPrivate::loadHtml(const QString &html, const QUrl &baseUrl) +{ + const QJNIObjectPrivate &htmlString = QJNIObjectPrivate::fromString(html); + const QJNIObjectPrivate &mimeTypeString = QJNIObjectPrivate::fromString(QLatin1String("text/html;charset=UTF-8")); + + baseUrl.isEmpty() ? m_viewController.callMethod("loadData", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", + htmlString.object(), + mimeTypeString.object(), + 0) + + : m_viewController.callMethod("loadDataWithBaseURL", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", + QJNIObjectPrivate::fromString(baseUrl.toString()).object(), + htmlString.object(), + mimeTypeString.object(), + 0, + 0); +} + bool QAndroidWebViewPrivate::canGoBack() const { return m_viewController.callMethod("canGoBack"); diff --git a/src/webview/qwebview_android_p.h b/src/webview/qwebview_android_p.h index 45a0213..fdf9ac5 100644 --- a/src/webview/qwebview_android_p.h +++ b/src/webview/qwebview_android_p.h @@ -82,6 +82,7 @@ public Q_SLOTS: void goForward() Q_DECL_OVERRIDE; void reload() Q_DECL_OVERRIDE; void stop() Q_DECL_OVERRIDE; + void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) Q_DECL_OVERRIDE; protected: void runJavaScriptPrivate(const QString& script, diff --git a/src/webview/qwebview_ios.mm b/src/webview/qwebview_ios.mm index b9fdff9..c2c09c9 100644 --- a/src/webview/qwebview_ios.mm +++ b/src/webview/qwebview_ios.mm @@ -202,6 +202,11 @@ void QIosWebViewPrivate::setUrl(const QUrl &url) [uiWebView loadRequest:[NSURLRequest requestWithURL:url.toNSURL()]]; } +void QIosWebViewPrivate::loadHtml(const QString &html, const QUrl &baseUrl) +{ + [uiWebView loadHTMLString:html.toNSString() baseURL:baseUrl.toNSURL()]; +} + bool QIosWebViewPrivate::canGoBack() const { return uiWebView.canGoBack; diff --git a/src/webview/qwebview_ios_p.h b/src/webview/qwebview_ios_p.h index 8302a68..864d5f1 100644 --- a/src/webview/qwebview_ios_p.h +++ b/src/webview/qwebview_ios_p.h @@ -85,6 +85,7 @@ public Q_SLOTS: void goForward() Q_DECL_OVERRIDE; void reload() Q_DECL_OVERRIDE; void stop() Q_DECL_OVERRIDE; + void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) Q_DECL_OVERRIDE; protected: void runJavaScriptPrivate(const QString& script, diff --git a/src/webview/qwebview_p.h b/src/webview/qwebview_p.h index 578403a..7ecacd8 100644 --- a/src/webview/qwebview_p.h +++ b/src/webview/qwebview_p.h @@ -87,6 +87,7 @@ public Q_SLOTS: void goForward() Q_DECL_OVERRIDE; void reload() Q_DECL_OVERRIDE; void stop() Q_DECL_OVERRIDE; + void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) Q_DECL_OVERRIDE; Q_SIGNALS: void titleChanged(); diff --git a/src/webview/qwebviewinterface_p.h b/src/webview/qwebviewinterface_p.h index ef02042..7cc9dc4 100644 --- a/src/webview/qwebviewinterface_p.h +++ b/src/webview/qwebviewinterface_p.h @@ -76,6 +76,7 @@ public: virtual void goForward() = 0; virtual void stop() = 0; virtual void reload() = 0; + virtual void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) = 0; virtual void runJavaScriptPrivate(const QString &script, int callbackId) = 0; diff --git a/tests/auto/webview/qwebview/tst_qwebview.cpp b/tests/auto/webview/qwebview/tst_qwebview.cpp index 9702324..bde8545 100644 --- a/tests/auto/webview/qwebview/tst_qwebview.cpp +++ b/tests/auto/webview/qwebview/tst_qwebview.cpp @@ -60,6 +60,7 @@ private slots: void initTestCase(); void load(); void runJavaScript(); + void loadHtml(); private: const QString m_cacheLocation; @@ -123,6 +124,16 @@ void tst_QWebView::runJavaScript() #endif // QT_NO_QQUICKWEBVIEW_TESTS } +void tst_QWebView::loadHtml() +{ + QWebView view; + QCOMPARE(view.loadProgress(), 0); + view.loadHtml(QString("WebViewTitle")); + QTRY_COMPARE(view.loadProgress(), 100); + QTRY_VERIFY(!view.isLoading()); + QCOMPARE(view.title(), QStringLiteral("WebViewTitle")); +} + QTEST_MAIN(tst_QWebView) #include "tst_qwebview.moc" -- cgit v1.2.3