[webkit-dev] Identifying a div element

Darin Adler darin at apple.com
Tue Oct 21 10:43:32 PDT 2008


On Oct 21, 2008, at 3:11 AM, Julien Chaffraix wrote:

> Node* curNode = ... ;
>
> if (curNode->isElement() &&
> static_cast<Element*>(curNode)->hasTagName(HTMLNames::divTag)) {
>    // This is an html <div> element.
> }

There's no need to check isElement() here or cast to Element*. The  
hasTagName function is defined in the Node class, and always returns  
false for nodes that are not elements. So the best style code is  
something like this:

     if (curNode->hasTagName(HTMLNames::divTag)) {
         HTLMLDivElement* curDivElement =  
static_cast<HTMLDivElement*>(curNode);
         // do things with the element
     }

It's important that once you determine something is a specific element  
that you use the more specific pointer type, because you'll get more  
efficient algorithms for many functions if you use a more specific  
type, eliminating the need for the code to keep checking the type over  
and over again. So inside this if statement you would want to use  
curDivElement, not curNode.

     -- Darin



More information about the webkit-dev mailing list