Bug 23708

Summary: Would like to have a callback when an HTML element is first added to the DOM
Product: WebKit Reporter: Aaron Boodman <aa>
Component: WebKit APIAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: ap, hyatt, oliver
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Mac   
OS: OS X 10.5   
Attachments:
Description Flags
Adds htmlElementAvailable() callback to FrameLoaderClient
hyatt: review-
Fixes per review feedback
none
Fix whitespace issues in previous patch
hyatt: review-
More fixes
none
Add description and link to bug to ChangeLog files hyatt: review+

Description Aaron Boodman 2009-02-03 11:34:20 PST
It would be useful to have a callback (perhaps on FrameLoaderClient) for when the HTML element is first added to the DOM. This would b fired around the same time as the existing windowObjectCleared() callback, but the difference would be that clients would be guaranteed there is a root HTML element available that they can start using.

The specific use case I have in mind in user scripts in Chromium. In Firefox, developers frequently asked for a way to run their user scripts before any HTML or script had been run on the page. A typical reason would be so that they could add CSS to the page before rendering started, or to setup event handlers.

In order to add CSS, we need a callback after there is an HTML element to append new style elements to, and windowObjectCleared() happens before this point currently.
Comment 1 Aaron Boodman 2009-02-03 11:37:47 PST
Created attachment 27282 [details]
Adds htmlElementAvailable() callback to FrameLoaderClient

This is a first cut of what I'm talking about. It works for HTML and XHTML documents, but not SVG. SVG is not critical for my use case, and it looks like parsing works much differently in SVG.

Also, please let me know what the right way is to add testing for something like this.
Comment 2 Dave Hyatt 2009-02-03 11:52:19 PST
Comment on attachment 27282 [details]
Adds htmlElementAvailable() callback to FrameLoaderClient

I think this callback should just be documentElementAvailable rather than being specific to <html>.

You did not patch XHTML, but only patched the HTML parser.
Comment 3 Mark Rowe (bdash) 2009-02-03 15:34:06 PST
You'd also need to implement the new method for all platforms rather than just Mac, otherwise the build will break.
Comment 4 Aaron Boodman 2009-02-03 17:03:45 PST
Created attachment 27300 [details]
Fixes per review feedback

Changed callback name to documentElementAvailable(), implemented for XML documents (which should cover SVG, XHTML, and WML I believe), and added empty implementations of callback for other platforms.
Comment 5 Aaron Boodman 2009-02-03 17:10:06 PST
Created attachment 27301 [details]
Fix whitespace issues in previous patch
Comment 6 Dave Hyatt 2009-02-04 17:51:02 PST
Comment on attachment 27301 [details]
Fix whitespace issues in previous patch

Nearly there...

Looks like your Changelog needs to be regenerated?

The change to HTMLParser.cpp could just be a single if statement.

if (localName == htmlTag && document->frame())

I don't know if Qt wants this or not but for completeness you would need to patch their XMLTokenizer as well.
Comment 7 Aaron Boodman 2009-02-04 23:56:04 PST
Created attachment 27343 [details]
More fixes

Updated ChangeLog, made changes as suggested in HTMLParser, and added an implementation to XMLTokenizerQt.cpp.
Comment 8 Darin Fisher (:fishd, Google) 2009-02-05 00:04:11 PST
Comment on attachment 27343 [details]
More fixes

>Index: WebCore/ChangeLog
...
>+2009-02-04  Aaron Boodman  <set EMAIL_ADDRESS environment variable>
>+
>+        Reviewed by NOBODY (OOPS!).
>+
>+        WARNING: NO TEST CASES ADDED OR CHANGED
>+

Usually, this is where people add a link to the bug and a description of the change.
Comment 9 Aaron Boodman 2009-02-05 00:10:06 PST
Created attachment 27344 [details]
Add description and link to bug to ChangeLog files

Whoops, you are right. Fixed now.
Comment 10 Dave Hyatt 2009-02-05 14:52:54 PST
Comment on attachment 27344 [details]
Add description and link to bug to ChangeLog files

r=me
Comment 11 Dimitri Glazkov (Google) 2009-02-05 16:01:08 PST
Landed as http://trac.webkit.org/changeset/40694.
Comment 12 Simon Fraser (smfr) 2009-02-05 23:41:47 PST
This broke the gtk build:
http://build.webkit.org/builders/trunk-qt-linux-release/builds/13432/steps/compile-release_2/logs/stdio

/home/zrusin/WebKit-BuildSlave/trunk-qt-linux-release/build/WebKitBuild/Release/lib/libQtWebKit.so: undefined reference to `non-virtual thunk to WebCore::FrameLoaderClientQt::documentElementAvailable()'
/home/zrusin/WebKit-BuildSlave/trunk-qt-linux-release/build/WebKitBuild/Release/lib/libQtWebKit.so: undefined reference to `WebCore::FrameLoaderClientQt::documentElementAvailable()'