Bug 13843

Summary: WebKitQt doesn't provide any function which open html code from a String
Product: WebKit Reporter: sebastien le faou <plicplicplic>
Component: WebKit QtAssignee: George Staikos <staikos>
Severity: Normal CC: hausmann
Priority: P2    
Version: 523.x (Safari 3)   
Hardware: Other   
OS: Linux   
Description Flags
a solution to this bug
mjs: review-
new revision of the path sam: review-

Description sebastien le faou 2007-05-23 08:30:56 PDT
If there is an application which wants to use WebKitQt to render html code, there is no way to load the html code from memory.
Indeed, there is only one way to load html lines, it is the QWebPage::open(QUrl) function which can read only a file.
So, it might be interesting to have a function which allow html rendering from a string,and because we are using Qt, from a QString.
Comment 1 sebastien le faou 2007-05-23 08:53:12 PDT
Created attachment 14687 [details]
a solution to this bug

Here is my solution for this bug.
I created a function which allow to load html in a Qstring.
Comment 2 Simon Hausmann 2007-05-29 00:24:18 PDT
IMHO the function should just be called setHtml.
Comment 3 Simon Hausmann 2007-05-29 00:29:11 PDT
Oh, I forgot: I think the function should take a QByteArray as argument. Unfortunately not every piece of HTML is utf-8 encoded these days.
Comment 4 Maciej Stachowiak 2007-05-29 00:52:22 PDT
setHTML might be misleaqding, since the load may still not be completely synchronous (at least if it works like data loads in other browsers). Also, I believe this particular approach to implementing the method is wrong. What I suggest instead is to make the FrameLoader do a data load, as that

You also need a way to set the base URL.

I suggest looking at the following methods in WebFrame in the Mac/ObjC WebKit API:

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)URL
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)URL

The former is a convenience for a string but still lets you set a base URL for subresources, the later would be the equivalent of the QByteArray suggestion, it also lets you define the encoding and MIME type.

In Qt terms, you could have:

void loadHTMLString(const QString& string, const QUrl& baseURL);
void loadData(const QByteArray& data, const QString& MIMEType, const QString& textEncodingName, const QUrl& baseURL);

You should also look at the Mac implementation of these methods, they ultimately call down to FrameLoader::load(const ResourceRequest&, const SubstituteData&). This isx ultimately the right way to do it, just using begin/write/end can skip many necessary steps.

r- to consider my and Simon's comments.

Comment 5 sebastien le faou 2007-05-29 13:22:52 PDT
Thank you guys for the advice,
but if I want to add this functions in FrameLoader, I can't use QString parameter in loadHTMLString(const QString& string, const QUrl& baseURL) function, because FrameLoader is not a specific class for QT. So, what is the good type to use???

Also, in order to create my openString(const QString &htmlString) function, I actualy looked at the loadHTMLString(...) function.
I didn't put the URL parameter, but I did the same steps as in this function, I think.
So I don't understand, what are the steps I missed.
Comment 6 sebastien le faou 2007-05-30 07:47:33 PDT
Also I don't see that the Mac loadHTMLString function call the FrameLoader::load function.
So, I don't know how to change my patch with adding the load function tacitly or not.
Comment 7 sebastien le faou 2007-05-30 08:32:53 PDT
actually I was looking in the wrong files to look for MAC loadHTMLString
I was looking into a WebFrame.cpp file instead of a WebFrame.mm
So now I know exactly where I have to look. :D
Comment 8 sebastien le faou 2007-05-31 13:32:50 PDT
Created attachment 14809 [details]
new revision of the path

I think this patch should be good, I follow all the steps in the MAC function.
Nevertheless, there is on thing I didn't use :

// hack because Mail checks for this property to detect data / archive loads
[NSURLProtocol setProperty:@"" forKey:@"WebDataRequest"
inRequest:(NSMutableURLRequest *)request.nsURLRequest()];

I think it's a MAC hack, and think it's not useful, but I'm not sure.
Comment 9 Maciej Stachowiak 2007-05-31 14:20:14 PDT
Yeah, that step is mac-specific and should not be needed. I would suggest doing the SubstituteData thing on one line. I think one of the WebKitQt developers should review the API.
Comment 10 George Staikos 2007-06-13 10:40:18 PDT
(In reply to comment #9)
I don't really like a few things about the API:

- URL vs Url, MIME vs mime, HTML vs Html naming
- we may want to do this from Frame too, so maybe we're in the wrong place.
- it seems really special cased.  You either have to use utf-8 text/html, or you have to fill in all parameters manually, and there is no default at all for baseUrl.
Comment 11 Sam Weinig 2007-08-19 18:00:03 PDT
Comment on attachment 14809 [details]
new revision of the path

r- based on George's comments above.
Comment 12 Simon Hausmann 2008-01-24 01:24:36 PST
setHtml has been added in revision 29123 :)