Trolltech Home | Qt4-preview-feedback Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt4-preview-feedback Archive, February 2008
QtWebKit fails to load external <script> references at times


Message 1 in thread

Hi,
I just tried to get QtWebKit to display some (on-the-fly generated) 
HTML+JavaScript code that loads an external JavaScript file as a library.
The resulting html is in a QString.

With

webview->setHtml(html);

the library calls fail, apparently the external JavaScript file is never 
loaded.

This works, though:

QFile f("test.html");
f.open(QFile::ReadWrite);
QTextStream ts(&f);
ts << html;
f.close();
webview->load(QUrl("test.html"));


While trying to come up with a small, self-contained test case, I found 
another similar problem:

This works:

QString html="<html><head></head><body><script 
type=\"text/javascript\">function junk() { document.write(\"test\"); } 
junk();</script></body></html>";
webview->setHtml(html)

But this doesn't:

QFile f("/tmp/test.js");
f.open(QFile::ReadWrite);
QTextStream ts(&f);
ts << "function junk() { document.write(\"test\"); }";
f.close();
QString html="<html><head></head><body><script type=\"text/javascript\" 
src=\"/tmp/test.js\" /><script 
type=\"text/javascript\">junk();</script></body></html>";
webview->setHtml(html);

I couldn't get this variant to work even by storing the html in a temp file 
and opening that. (The same HTML/JS code works fine in Konqueror).

To unsubscribe - send "unsubscribe" in the subject to qt4-preview-feedback-request@xxxxxxxxxxxxx


Message 2 in thread

On Tuesday 12 February 2008 21:07:41 Bernhard Rosenkränzer wrote:
> Hi,
> I just tried to get QtWebKit to display some (on-the-fly generated)
> HTML+JavaScript code that loads an external JavaScript file as a library.
> The resulting html is in a QString.
>
> With
>
> webview->setHtml(html);
>
> the library calls fail, apparently the external JavaScript file is never
> loaded.
>
> This works, though:
>
> QFile f("test.html");
> f.open(QFile::ReadWrite);
> QTextStream ts(&f);
> ts << html;
> f.close();
> webview->load(QUrl("test.html"));
>
>
> While trying to come up with a small, self-contained test case, I found
> another similar problem:
>
> This works:
>
> QString html="<html><head></head><body><script
> type=\"text/javascript\">function junk() { document.write(\"test\"); }
> junk();</script></body></html>";
> webview->setHtml(html)
>
> But this doesn't:
>
> QFile f("/tmp/test.js");
> f.open(QFile::ReadWrite);
> QTextStream ts(&f);
> ts << "function junk() { document.write(\"test\"); }";
> f.close();
> QString html="<html><head></head><body><script type=\"text/javascript\"
> src=\"/tmp/test.js\" /><script
> type=\"text/javascript\">junk();</script></body></html>";
> webview->setHtml(html);
>
> I couldn't get this variant to work even by storing the html in a temp file
> and opening that. (The same HTML/JS code works fine in Konqueror).

Hi Bero,

this is a guess (with some confidence). Your last example will work when you 
add file:// before the local file path. But all issues relate to URI schemes 
and how to resolve the path then.


kind regards
	holger

To unsubscribe - send "unsubscribe" in the subject to qt4-preview-feedback-request@xxxxxxxxxxxxx


Message 3 in thread

Hi,

On Tuesday 12 February 2008 20:04:59 Holger Freyther wrote:
> this is a guess (with some confidence). Your last example will work when
> you add file:// before the local file path. But all issues relate to URI
> schemes and how to resolve the path then.

Thanks - unfortunately it doesn't work with file:/ or file:// either. I've 
attached a small testcase that shows the problem (it just brings up an empty 
window).

An interesting bit: The sample browser included in upstream WebKit shows the 
test.html file generated with -DUSE_TEMPFILE correctly.

Best regards,
bero
#include <QApplication>
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QWebView>
#include <QString>

int main(int argc, char **argv)
{
	QApplication app(argc, argv);

	QFile f("/tmp/test.js");
	f.open(QFile::ReadWrite|QFile::Truncate);
	QTextStream ts(&f);
	ts << "function junk() {\n"
	      "	document.write(\"GTK sucks\");\n"
	      "}\n";
	f.close();

	QWebView *wv=new QWebView(0);
	QString html="<html><head></head><body><script type=\"text/javascript\" src=\"file:///tmp/test.js\" /><script type=\"text/javascript\">junk();</script></body></html>";
#ifdef USE_TEMPFILE
	QFile f2("test.html");
	f2.open(QFile::ReadWrite|QFile::Truncate);
	QTextStream ts2(&f2);
	ts2 << html;
	f2.close();
	wv->load(QUrl("test.html"));
#else
	wv->setHtml(html);
#endif
	wv->show();
	app.exec();
}

Message 4 in thread

On Wednesday 13 February 2008 11:18:33 Bernhard Rosenkränzer wrote:
> Hi,
>
> On Tuesday 12 February 2008 20:04:59 Holger Freyther wrote:
> > this is a guess (with some confidence). Your last example will work when
> > you add file:// before the local file path. But all issues relate to URI
> > schemes and how to resolve the path then.
>
> Thanks - unfortunately it doesn't work with file:/ or file:// either. I've
> attached a small testcase that shows the problem (it just brings up an
> empty window).

Hi Bero,

interesting. I have used the QWebSettings to enable the Web Inspector. With 
right clicking in the empty window you can bring it up. It is showing that 
the external javascript file gets loaded but it is claiming a syntax error.

I will investigate further.



To unsubscribe - send "unsubscribe" in the subject to qt4-preview-feedback-request@xxxxxxxxxxxxx


Message 5 in thread

On Wednesday 13 February 2008 11:18:33 Bernhard Rosenkränzer wrote:
> Hi,
>
> On Tuesday 12 February 2008 20:04:59 Holger Freyther wrote:
> > this is a guess (with some confidence). Your last example will work when
> > you add file:// before the local file path. But all issues relate to URI
> > schemes and how to resolve the path then.
>
> Thanks - unfortunately it doesn't work with file:/ or file:// either. I've
> attached a small testcase that shows the problem (it just brings up an
> empty window).
>
> An interesting bit: The sample browser included in upstream WebKit shows
> the test.html file generated with -DUSE_TEMPFILE correctly.

I just had a look at this and I can confirm that it's a QtWebKit bug. The 
problem lies in the way WebKit decodes the external javascript. It assumes 
that it uses the same encoding as the input, which in the case of

	setHtml(const QString &html)

is UTF-16. So if in theory you encoded your script in utf-16 it would 
work ;-).

I'll see about changing this to assume utf-8 as default encoding for external 
resources when using setHtml() with a QString, but in the meantime using 
setContent() may work as workaround until we fix this (will definitely fix in 
4.4.0 final :)

Thanks for the feedback, btw!

Simon

Attachment:

Attachment: signature.asc
Description: This is a digitally signed message part.