DOM tree traversing from C++ // QueryInterface returns NULL - Mozilla

This is a discussion on DOM tree traversing from C++ // QueryInterface returns NULL - Mozilla ; Hello to everyone! I'm trying to walk through DOM tree of HTML page node by node using C+ + component (linked against Gecko 1.9.2, MS Windows). I'd like to use nsIDOMNode interface and it's child interfaces like nsIDOMHTML*Element ( , ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: DOM tree traversing from C++ // QueryInterface returns NULL

  1. DOM tree traversing from C++ // QueryInterface returns NULL

    Hello to everyone!

    I'm trying to walk through DOM tree of HTML page node by node using C+
    + component (linked against Gecko 1.9.2, MS Windows). I'd like to use
    nsIDOMNode interface and it's child interfaces like nsIDOMHTML*Element
    (,
    and other elements).

    I think that tree traversal should be easily done using nsIDOMNode's
    methods like GetFirstChild, GetNextSibling and others.

    My problem is that I can't correctly cast from parent interface, let's
    say that nsIDOMNode to interface that belongs to actual node, e.g.
    nsIDOMHTMLElement for tag.

    It is possible to cast natively, like:
    nsIDOMHTMLElement * ptrNsIDOMHTMLElement = (nsIDOMHTMLElement *)
    ptrNsIDOMNode;
    however documentation reads that this shouldn't be never used.

    Correct cast between interfaces should be done via QueryInterface, am
    I right? Something like this:

    nsIDOMHTMLDocument *htmlDocument = ...; // assume that it's
    initialized OK
    nsCOMPtr ptrNode;
    htmlDocument->GetFirstChild(getter_AddRefs(ptrNode));
    // if it is element, let's cast it
    nsCOMPtr htmlElement = do_QueryInterface(ptrNode);

    Unfortunately, such a code gives me NULL pointer. Why is it so?
    I can provide more code that I use for testing - it's an extension
    that analyzes pages after loading.
    JavaScript overlay: http://pastebin.mozilla.org/691084
    C++ method called from overlay: http://pastebin.mozilla.org/691085

  2. Re: DOM tree traversing from C++ // QueryInterface returns NULL

    On 16 pro, 21:42, Eric Jung wrote:
    > I suspect you don't even need to do a QI because nsIDOMNode is a superclass
    > of nsIDOMHTMLElement. But I could be wrong.
    >
    > Eric
    >
    > On Wed, Dec 16, 2009 at 5:22 AM, Gijs Kruitbosch
    > wrote:
    >


    Thank you for your answer.

    Of course, nsIDOMNode is superclass of nsIDOMHTMLElement (no problem
    to QI from subclass to superclass).

    My problem is following: I want to access nsIDOMHTMLElement from
    nsIDOMNode (only when I *know* that nsIDOMNode is actually
    nsIDOMHTMLElement). In other words, I want to access subclass (from
    superclass) -- is it possible with QueryInterface?

+ Reply to Thread