Anomalous WebView editing behavior
-
In building an application using QWebView with content editable I've run some weird behavior. After some work I've pared down my code to illustrate some of this behavior. The test HTML I'm using is in a file called undotest.html and is as follows:
@
<!DOCTYPE html>
<html>
<head>
<title>undo_redo test</title>
<body>
<!-- undo_redo test -->
<p>start undo_redo test</p>
<iframe width="420" height="315" src="http://www.youtube.com/embed/fwbH8dgASRc?rel=0" frameborder="0" allowfullscreen></iframe>
<p>end undo_redo test</p>
</body>
</html>
@The test program that displays the HTML is simply a webView with three push buttons. An undo button, a redo button, and a cut button. The mainwindow.cpp where all the action takes place is:
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUrl>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl url("path to undotest.html");
ui->webView->showMaximized();
ui->webView->settings()->setAttribute(QWebSettings::PluginsEnabled,true);
ui->webView->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows,true);
ui->webView->settings()->setAttribute(QWebSettings::JavascriptCanCloseWindows,true);
ui->webView->settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard,true);
ui->webView->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,true);
ui->webView->page()->setContentEditable(true);
ui->webView->load(url);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_undoButton_clicked()
{
ui->webView->page()->action(QWebPage::Undo)->activate(QAction::Trigger);
}// end on_undoButton_clicked
void MainWindow::on_redoButton_clicked()
{
ui->webView->page()->action(QWebPage::Redo)->activate(QAction::Trigger);
}// end on_redoButton_clicked
void MainWindow::on_cutButton_clicked()
{
ui->webView->page()->action(QWebPage::Cut)->activate(QAction::Trigger);
}// end on_cutButton_clicked@As simple as this code is it produces the following anomalous results:
- If you select the iframe (i.e., the youtube video) and press cut the video is successfully cut. You can then press undo to restore the video but after that the redo button no longer works (but you can still press cut and it will work).
- If you first select some text from start “undo_redo test” and cut it then cut the video you should be able to press undo twice to restore the original content but it doesn't happen. Pressing the undo button once restores the video but pressing it again has no effect. After some further investigation I found that the first undo causes webview to emit a loadStarted() signal which appears to clear the undo stack. Note: if the order of operations is reversed everything works okay.
If I use the <object> HTML from YouTube to display the video then the QWebView code works correctly so as a temporary measure I'm using the old YouTube HTML rather than their recommended HTML but eventually one would have to be able to switch to the recommended HTML.
Does anyone have any suggestions for an actual work around for this problem?