[webkit-dev] DOM tree surgery and DOM tree destruction

Pitaga achats at avvanta.com
Wed Jun 25 10:06:25 PDT 2008

Given that Node methods have parameters and return values that are variously specified to be raw pointers and PassRefPtr's, what's the best practice for DOM tree surgery? Also, what's the best way to destroy DOM trees and constituents of DOM trees? I've read the first draft of Darin Adler's "RefPtr and PassRefPtr Basics," and I've searched in WebKit source for further guidance, but I still need more help. 
For example, suppose that a browser based on WebKit has loaded a Web page and parsed it, producing a DOM tree T. As an exercise, I want to perform surgery on T and then destroy both T and the constituents that the surgery removed from T, without leaking. For purposes of this exercise, the browser session itself is of no interest. 

Suppose a method M takes a PassRefPtr<Node> A as a parameter, where A is the root of a subtree of T. The method will find a node X meeting certain criteria within this subtree. The method will then create a new node N and interpose it between X and its children, so that [X [Y0 ... Yn]] becomes [X [N [Y0 ... Yn]]]. N isn't to be used as either a parameter or a return value. Further, M will find a node W within T that meets certain criteria and remove it and all the nodes that it dominates from T. 

Suppose that A (in the method that calls M), X, Y0, ... ,Yn and W are initially supplied as raw pointers via calls to firstChild() and nextSibling(). N can be declared as a RefPtr, and assigned, with casting as required, to the output of createElement.  Logically, removeChild will apply to X with Y0 as a parameter, and so on for Y1, ... Yn, and to the parent of W with W as a parameter. appendChild will apply to N with Y0 as a parameter, and so on for Y1, ... Yn, and to A with N as a parameter. (Of course there are other ways to handle this simple case, but I want to do it with removeChild and appendChild for purposes of the exercise.) removeChild takes a raw pointer as a parameter. appendChild takes a PassRefPtr as a parameter. What's the best practice here with respect to raw pointers, PassRefPtr's, and RefPtr's? How do I destroy the subtree dominated by W after I remove it? How do I destroy T when I'm all finished? If any part of this question is ill-formed, I'll be grateful if the response reformulates it appropriately. 

Thanks for any help. 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.webkit.org/pipermail/webkit-dev/attachments/20080625/6735dae7/attachment.htm 

More information about the webkit-dev mailing list