[webkit-dev] DOM tree surgery and DOM tree destruction
achats at avvanta.com
Wed Jun 25 12:16:19 PDT 2008
Thanks very much for this response.
We (my co-workers and I) want to use WebKit modules selectively, without
running anything like full browser sessions. Over time, we'll do this as
cleanly as we can, taking full advantage of smart pointers. For now, we're
focused on implementing our own (non-trivial) algorithms. We're breaking
into browser sessions, running our algorithms on DOM trees, and worrying as
little as possible about API issues. We're more than willing to write code
to destroy objects. We're coping with smart pointers, rather than taking
advantage of them. If this seems like the wrong attitude, please excuse us
on the grounds that it's appropriate for us to focus first on algorithm
Given that we're interfering with the mechanisms for automatic destruction,
and need to write code to destroy trees, how do we do this?
Given that we're interfering with mechanisms for automatic destruction, my
question on best practice for using raw pointers, RefPtr's, and PassRefPtr's
in the surgery example may be ill-conceived. But when we're ready to use
smart pointers intelligently, we'd like our algorithm methods to need as
little recoding as possible. So let me ask some more specific questions
A is a node pointer parameter of method M. A corresponds to a subtree of T
(where T is a DOM tree corresponding to a parsed Web page). Inside M, N is a
node pointer to which the output of createElement (suitably cast) will be
assigned. X, Y, and W are nodes underneath A (at whatever level of descent).
I'm going to find X, Y, and W by traversing the subtree dominated by A with
firstChild and nextSibling calls, assigning to a local node pointer variable
L and applying tests as I go. (By assumption, there's guaranteed to be one
each of X, Y, and W, and Y will be the only child of X). I'm going to
interpose N between X and Y, as the child of X and the parent of Y, using
removeChild and appendChild. I'm going to remove W and all its descendants
from T using removeChild, and destroy them. After I call M, I'm going to
manually destroy T.
How should A be declared in the method that calls M?
How should A be supplied to M?
How should N be declared inside M?
How should L be declared inside M?
When I find X, it will be assigned to L. How do I call removeChild on L with
a parameter corresponding to firstChild of L? How do I call appendChild on L
with a parameter corresponding to N? How do I call appendChild on N with a
parameter corresponding to firstChild of L?
When I find W, its parent will be assigned to L. How do I call removeChild
on L with a parameter corresponding to the appropriate child of L? How do I
make sure that the removed child and its descendants are destroyed?
How do I destroy T?
These are questions about smart pointers, and about destruction, not about
tree traversal and tree surgery. I'll be grateful for any further help.
----- Original Message -----
From: "Darin Adler" <darin at apple.com>
To: "Pitaga" <achats at avvanta.com>
Cc: <webkit-dev at lists.webkit.org>
Sent: Wednesday, June 25, 2008 10:15 AM
Subject: Re: [webkit-dev] DOM tree surgery and DOM tree destruction
> On Jun 25, 2008, at 10:06 AM, Pitaga wrote:
>> 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.
> You don't explicitly destroy anything. DOM objects will be destroyed when
> the last owner goes away; that's what reference counting is used for.
> So "How do I destroy the tree?" is the wrong question. If there's a test
> case where something's not getting destroyed, you could ask "Why isn't it
> being destroyed?" but there's no need to write code to explicitly destroy
>> What's the best practice here with respect to raw pointers,
>> PassRefPtr's, and RefPtr's?
> That question is too vague and broad for me to answer. Maybe you could
> ask a more specific question?
>> How do I destroy the subtree dominated by W after I remove it?
> I had a lot of trouble following the letters in your example, so I'm not
> sure about "W" or even what "dominated" means.
> Generally if you remove a child with removeChild, then that child and all
> its descendants will be destroyed when the last reference to it goes
> away. Typically you are holding a RefPtr to the child you are removing;
> in that case the most likely time it will be destroyed is when that
> RefPtr goes out of scope. Or you might only have a raw pointer. In that
> case, it's likely the child will destroyed within the removeChild
>> How do I destroy T when I'm all finished?
> You say that "WebKit has loaded a web page and parsed it [to produce T]".
> Given that, T is a document and it's owned by the Frame that loaded it.
> As long as its the current document in that Frame it will be kept alive.
> When the Frame either goes away or loads a new document, then it will be
> destroyed, unless someone else is holding a reference to it. When the
> last reference to it goes away, it will be destroyed.
> -- Darin
More information about the webkit-dev