According to what I can derive from another answer on this site, there is no synchronous method such as my requested DOMDocumentFromHTML:
available in WebKit.
So far, the best I've been able to do is the following asynchronous combination of giveDOMDocumentFromHTML:usingBaseURL:
and takeDOMDocument:
.
- (void) giveDOMDocumentFromHTML: (NSString *) htmlString
usingBaseURL: (NSURL *) baseURL
{
WebView * webView = [[WebView alloc] init];
[webView setFrameLoadDelegate: self];
[[webView mainFrame] loadHTMLString: htmlString
baseURL: baseURL];
}
- (void) takeDOMDocument: (DOMDocument *) document
{
DOMHTMLElement * bodyNode =
(DOMHTMLElement *) [[document getElementsByTagName: @"body"] item: 0];
NSLog(@"Body is: %@", [bodyNode innerHTML]);
}
They are hooked together through the following delegate method:
- (void) webView: (WebView *) webView
didFinishLoadForFrame: (WebFrame *) frame
{
if (frame == [webView mainFrame]) {
[self takeDOMDocument: [frame DOMDocument]];
}
}
The above works, but has at least the following remaining issues:
- I'm not sure where the allocated WebView should be sent a
release
or autorelease
message.
- I would prefer/need the application to remain blocked until the HTML page has been processed. In the above scheme the application will be processing any user input while the WebView is loading/parsing the HTML. (Note that the WebView will never be shown on screen.)
So this is still very much up for improvement. Anyone who can provide a synchronous implementation for DOMDocumentFromHTML:
as outlined in the original question?