Title: [173907] trunk/Source/WebCore
Revision
173907
Author
cdu...@apple.com
Date
2014-09-23 20:31:50 -0700 (Tue, 23 Sep 2014)

Log Message

Add support for is<HTML*Element>() for type checking
https://bugs.webkit.org/show_bug.cgi?id=137015

Reviewed by Benjamin Poulain.

Add support for is<HTML*Element>() for type checking while keeping
support for the legacy toHTML*Element() form until the code base is
ported.

toHTML*Element() helpers are now macros to the new is<HTML*Element>()
type checking helpers.

No new tests, no behavior change.

* dom/Document.cpp:
(WebCore::Document::setTitle):
* dom/Element.cpp:
(WebCore::Element::childShouldCreateRenderer):
* dom/Element.h:
(WebCore::is):
(WebCore::downcast):
(WebCore::isElementOfType): Deleted.
* dom/ElementAncestorIterator.h:
(WebCore::lineageOfType):
* dom/ElementIterator.h:
(WebCore::findElementAncestorOfType):
* dom/ElementTraversal.h:
(WebCore::Traversal<ElementType>::firstChildTemplate):
(WebCore::Traversal<ElementType>::lastChildTemplate):
(WebCore::Traversal<ElementType>::firstWithinTemplate):
(WebCore::Traversal<ElementType>::lastWithinTemplate):
(WebCore::Traversal<ElementType>::nextTemplate):
(WebCore::Traversal<ElementType>::previous):
(WebCore::Traversal<ElementType>::nextSibling):
(WebCore::Traversal<ElementType>::previousSibling):
(WebCore::Traversal<ElementType>::nextSkippingChildren):
* dom/TypedElementDescendantIterator.h:
(WebCore::TypedElementDescendantIteratorAdapter<ElementType>::from):
(WebCore::TypedElementDescendantConstIteratorAdapter<ElementType>::from):
* dom/make_names.pl:
(printTypeHelpers):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (173906 => 173907)


--- trunk/Source/WebCore/ChangeLog	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/ChangeLog	2014-09-24 03:31:50 UTC (rev 173907)
@@ -1,3 +1,47 @@
+2014-09-23  Chris Dumez  <cdu...@apple.com>
+
+        Add support for is<HTML*Element>() for type checking
+        https://bugs.webkit.org/show_bug.cgi?id=137015
+
+        Reviewed by Benjamin Poulain.
+
+        Add support for is<HTML*Element>() for type checking while keeping
+        support for the legacy toHTML*Element() form until the code base is
+        ported.
+
+        toHTML*Element() helpers are now macros to the new is<HTML*Element>()
+        type checking helpers.
+
+        No new tests, no behavior change.
+
+        * dom/Document.cpp:
+        (WebCore::Document::setTitle):
+        * dom/Element.cpp:
+        (WebCore::Element::childShouldCreateRenderer):
+        * dom/Element.h:
+        (WebCore::is):
+        (WebCore::downcast):
+        (WebCore::isElementOfType): Deleted.
+        * dom/ElementAncestorIterator.h:
+        (WebCore::lineageOfType):
+        * dom/ElementIterator.h:
+        (WebCore::findElementAncestorOfType):
+        * dom/ElementTraversal.h:
+        (WebCore::Traversal<ElementType>::firstChildTemplate):
+        (WebCore::Traversal<ElementType>::lastChildTemplate):
+        (WebCore::Traversal<ElementType>::firstWithinTemplate):
+        (WebCore::Traversal<ElementType>::lastWithinTemplate):
+        (WebCore::Traversal<ElementType>::nextTemplate):
+        (WebCore::Traversal<ElementType>::previous):
+        (WebCore::Traversal<ElementType>::nextSibling):
+        (WebCore::Traversal<ElementType>::previousSibling):
+        (WebCore::Traversal<ElementType>::nextSkippingChildren):
+        * dom/TypedElementDescendantIterator.h:
+        (WebCore::TypedElementDescendantIteratorAdapter<ElementType>::from):
+        (WebCore::TypedElementDescendantConstIteratorAdapter<ElementType>::from):
+        * dom/make_names.pl:
+        (printTypeHelpers):
+
 2014-09-23  Benjamin Poulain  <bpoul...@apple.com>
 
         The style resolution cache applies properties incorrectly whenever direction != ltr

Modified: trunk/Source/WebCore/dom/Document.cpp (173906 => 173907)


--- trunk/Source/WebCore/dom/Document.cpp	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/dom/Document.cpp	2014-09-24 03:31:50 UTC (rev 173907)
@@ -1551,7 +1551,7 @@
     // The DOM API has no method of specifying direction, so assume LTR.
     updateTitle(StringWithDirection(title, LTR));
 
-    if (m_titleElement && isHTMLTitleElement(*m_titleElement))
+    if (m_titleElement && is<HTMLTitleElement>(*m_titleElement))
         downcast<HTMLTitleElement>(*m_titleElement).setText(title);
 }
 

Modified: trunk/Source/WebCore/dom/Element.cpp (173906 => 173907)


--- trunk/Source/WebCore/dom/Element.cpp	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/dom/Element.cpp	2014-09-24 03:31:50 UTC (rev 173907)
@@ -2405,7 +2405,7 @@
     if (child.isSVGElement()) {
         ASSERT(!isSVGElement());
         const SVGElement& childElement = downcast<SVGElement>(child);
-        return isSVGSVGElement(childElement) && childElement.isValid();
+        return is<SVGSVGElement>(childElement) && childElement.isValid();
     }
     return true;
 }

Modified: trunk/Source/WebCore/dom/Element.h (173906 => 173907)


--- trunk/Source/WebCore/dom/Element.h	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/dom/Element.h	2014-09-24 03:31:50 UTC (rev 173907)
@@ -675,10 +675,22 @@
     static bool is(ArgType&);
 };
 
-// This is needed so that the compiler can deduce the second template parameter (ArgType).
+// Type checking function for Elements, to use before casting with downcast<>().
 template <typename ExpectedType, typename ArgType>
-inline bool isElementOfType(const ArgType& node) { return ElementTypeCastTraits<ExpectedType, const ArgType>::is(node); }
+inline bool is(ArgType& node)
+{
+    static_assert(!std::is_base_of<ExpectedType, ArgType>::value, "Unnecessary type check");
+    return ElementTypeCastTraits<const ExpectedType, const ArgType>::is(node);
+}
 
+template <typename ExpectedType, typename ArgType>
+inline bool is(ArgType* node)
+{
+    ASSERT(node);
+    static_assert(!std::is_base_of<ExpectedType, ArgType>::value, "Unnecessary type check");
+    return ElementTypeCastTraits<const ExpectedType, const ArgType>::is(*node);
+}
+
 template <>
 struct ElementTypeCastTraits<const Element, const Node> {
     static bool is(const Node& node) { return node.isElementNode(); }
@@ -694,13 +706,13 @@
 inline typename std::conditional<std::is_const<Source>::value, const Target&, Target&>::type downcast(Source& source)
 {
     static_assert(!std::is_base_of<Target, Source>::value, "Unnecessary cast");
-    ASSERT_WITH_SECURITY_IMPLICATION(isElementOfType<const Target>(source));
+    ASSERT_WITH_SECURITY_IMPLICATION(is<Target>(source));
     return static_cast<typename std::conditional<std::is_const<Source>::value, const Target&, Target&>::type>(source);
 }
 template<typename Target, typename Source> inline typename std::conditional<std::is_const<Source>::value, const Target*, Target*>::type downcast(Source* source)
 {
     static_assert(!std::is_base_of<Target, Source>::value, "Unnecessary cast");
-    ASSERT_WITH_SECURITY_IMPLICATION(!source || isElementOfType<const Target>(*source));
+    ASSERT_WITH_SECURITY_IMPLICATION(!source || is<Target>(*source));
     return static_cast<typename std::conditional<std::is_const<Source>::value, const Target*, Target*>::type>(source);
 }
 

Modified: trunk/Source/WebCore/dom/ElementAncestorIterator.h (173906 => 173907)


--- trunk/Source/WebCore/dom/ElementAncestorIterator.h	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/dom/ElementAncestorIterator.h	2014-09-24 03:31:50 UTC (rev 173907)
@@ -184,7 +184,7 @@
 template <typename ElementType>
 inline ElementAncestorIteratorAdapter<ElementType> lineageOfType(Element& first)
 {
-    if (isElementOfType<const ElementType>(first))
+    if (is<ElementType>(first))
         return ElementAncestorIteratorAdapter<ElementType>(static_cast<ElementType*>(&first));
     return ancestorsOfType<ElementType>(first);
 }
@@ -192,7 +192,7 @@
 template <typename ElementType>
 inline ElementAncestorConstIteratorAdapter<ElementType> lineageOfType(const Element& first)
 {
-    if (isElementOfType<const ElementType>(first))
+    if (is<ElementType>(first))
         return ElementAncestorConstIteratorAdapter<ElementType>(static_cast<const ElementType*>(&first));
     return ancestorsOfType<ElementType>(first);
 }

Modified: trunk/Source/WebCore/dom/ElementIterator.h (173906 => 173907)


--- trunk/Source/WebCore/dom/ElementIterator.h	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/dom/ElementIterator.h	2014-09-24 03:31:50 UTC (rev 173907)
@@ -183,12 +183,18 @@
 inline ElementType* findElementAncestorOfType(const Element& current)
 {
     for (Element* ancestor = current.parentElement(); ancestor; ancestor = ancestor->parentElement()) {
-        if (isElementOfType<const ElementType>(*ancestor))
-            return static_cast<ElementType*>(ancestor);
+        if (is<ElementType>(*ancestor))
+            return downcast<ElementType>(ancestor);
     }
     return nullptr;
 }
 
+template <>
+inline Element* findElementAncestorOfType<Element>(const Element& current)
+{
+    return current.parentElement();
+}
+
 template <typename ElementType>
 inline ElementIterator<ElementType>& ElementIterator<ElementType>::traverseAncestor()
 {

Modified: trunk/Source/WebCore/dom/ElementTraversal.h (173906 => 173907)


--- trunk/Source/WebCore/dom/ElementTraversal.h	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/dom/ElementTraversal.h	2014-09-24 03:31:50 UTC (rev 173907)
@@ -116,9 +116,9 @@
 inline ElementType* Traversal<ElementType>::firstChildTemplate(CurrentType* current)
 {
     Node* node = current->firstChild();
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = node->nextSibling();
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
@@ -126,9 +126,9 @@
 inline ElementType* Traversal<ElementType>::lastChildTemplate(CurrentType* current)
 {
     Node* node = current->lastChild();
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = node->previousSibling();
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
@@ -136,9 +136,9 @@
 inline ElementType* Traversal<ElementType>::firstWithinTemplate(CurrentType* current)
 {
     Node* node = current->firstChild();
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = NodeTraversal::next(node, current);
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
@@ -146,9 +146,9 @@
 inline ElementType* Traversal<ElementType>::lastWithinTemplate(CurrentType* current)
 {
     Node* node = NodeTraversal::last(current);
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = NodeTraversal::previous(node, current);
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
@@ -156,9 +156,9 @@
 inline ElementType* Traversal<ElementType>::nextTemplate(CurrentType* current)
 {
     Node* node = NodeTraversal::next(current);
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = NodeTraversal::next(node);
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
@@ -166,63 +166,63 @@
 inline ElementType* Traversal<ElementType>::nextTemplate(CurrentType* current, const Node* stayWithin)
 {
     Node* node = NodeTraversal::next(current, stayWithin);
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = NodeTraversal::next(node, stayWithin);
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
 inline ElementType* Traversal<ElementType>::previous(const Node* current)
 {
     Node* node = NodeTraversal::previous(current);
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = NodeTraversal::previous(node);
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
 inline ElementType* Traversal<ElementType>::previous(const Node* current, const Node* stayWithin)
 {
     Node* node = NodeTraversal::previous(current, stayWithin);
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = NodeTraversal::previous(node, stayWithin);
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
 inline ElementType* Traversal<ElementType>::nextSibling(const Node* current)
 {
     Node* node = current->nextSibling();
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = node->nextSibling();
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
 inline ElementType* Traversal<ElementType>::previousSibling(const Node* current)
 {
     Node* node = current->previousSibling();
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = node->previousSibling();
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
 inline ElementType* Traversal<ElementType>::nextSkippingChildren(const Node* current)
 {
     Node* node = NodeTraversal::nextSkippingChildren(current);
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = NodeTraversal::nextSkippingChildren(node);
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>
 inline ElementType* Traversal<ElementType>::nextSkippingChildren(const Node* current, const Node* stayWithin)
 {
     Node* node = NodeTraversal::nextSkippingChildren(current, stayWithin);
-    while (node && !isElementOfType<const ElementType>(*node))
+    while (node && !is<ElementType>(*node))
         node = NodeTraversal::nextSkippingChildren(node, stayWithin);
-    return static_cast<ElementType*>(node);
+    return downcast<ElementType>(node);
 }
 
 template <typename ElementType>

Modified: trunk/Source/WebCore/dom/TypedElementDescendantIterator.h (173906 => 173907)


--- trunk/Source/WebCore/dom/TypedElementDescendantIterator.h	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/dom/TypedElementDescendantIterator.h	2014-09-24 03:31:50 UTC (rev 173907)
@@ -153,8 +153,8 @@
 inline TypedElementDescendantIterator<ElementType> TypedElementDescendantIteratorAdapter<ElementType>::from(Element& descendant)
 {
     ASSERT(descendant.isDescendantOf(&m_root));
-    if (isElementOfType<const ElementType>(descendant))
-        return TypedElementDescendantIterator<ElementType>(m_root, static_cast<ElementType*>(&descendant));
+    if (is<ElementType>(descendant))
+        return TypedElementDescendantIterator<ElementType>(m_root, downcast<ElementType>(&descendant));
     ElementType* next = Traversal<ElementType>::next(&m_root, &descendant);
     return TypedElementDescendantIterator<ElementType>(m_root, next);
 }
@@ -202,8 +202,8 @@
 inline TypedElementDescendantConstIterator<ElementType> TypedElementDescendantConstIteratorAdapter<ElementType>::from(const Element& descendant) const
 {
     ASSERT(descendant.isDescendantOf(&m_root));
-    if (isElementOfType<const ElementType>(descendant))
-        return TypedElementDescendantConstIterator<ElementType>(m_root, static_cast<const ElementType*>(&descendant));
+    if (is<ElementType>(descendant))
+        return TypedElementDescendantConstIterator<ElementType>(m_root, downcast<ElementType>(&descendant));
     const ElementType* next = Traversal<ElementType>::next(&m_root, &descendant);
     return TypedElementDescendantConstIterator<ElementType>(m_root, next);
 }

Modified: trunk/Source/WebCore/dom/make_names.pl (173906 => 173907)


--- trunk/Source/WebCore/dom/make_names.pl	2014-09-24 03:17:44 UTC (rev 173906)
+++ trunk/Source/WebCore/dom/make_names.pl	2014-09-24 03:31:50 UTC (rev 173907)
@@ -633,37 +633,34 @@
         }
 
         my $class = $parsedTags{$name}{interfaceName};
-        # FIXME: Rename these helpers to is<*Element>().
-        my $checkHelper = "is$class";
-
         print F <<END
 class $class;
-void $checkHelper(const $class&); // Catch unnecessary runtime check of type known at compile time.
-void $checkHelper(const $class*); // Catch unnecessary runtime check of type known at compile time.
+template <typename ArgType>
+class ElementTypeCastTraits<const $class, ArgType> {
+public:
+    static bool is(ArgType& node) { return checkTagName(node); }
+private:
 END
-        ;
-
-        if ($parameters{namespace} eq "HTML" && $parsedTags{$name}{wrapperOnlyIfMediaIsAvailable}) {
-            # We need to check for HTMLUnknownElement if it might have been created by the factory.
-            print F <<END
-inline bool $checkHelper(const HTMLElement& element) { return !element.isHTMLUnknownElement() && element.hasTagName($parameters{namespace}Names::${name}Tag); }
-inline bool $checkHelper(const Node& node) { return node.isHTMLElement() && $checkHelper(toHTMLElement(node)); }
+       ;
+       if ($parameters{namespace} eq "HTML" && $parsedTags{$name}{wrapperOnlyIfMediaIsAvailable}) {
+           print F <<END
+    static bool checkTagName(const HTMLElement& element) { return !element.isHTMLUnknownElement() && element.hasTagName($parameters{namespace}Names::${name}Tag); }
+    static bool checkTagName(const Node& node) { return node.isHTMLElement() && checkTagName(toHTMLElement(node)); }
 END
-            ;
-        } else {
-            print F <<END
-inline bool $checkHelper(const $parameters{namespace}Element& element) { return element.hasTagName($parameters{namespace}Names::${name}Tag); }
-inline bool $checkHelper(const Node& node) { return node.hasTagName($parameters{namespace}Names::${name}Tag); }
+           ;
+       } else {
+           print F <<END
+    static bool checkTagName(const $parameters{namespace}Element& element) { return element.hasTagName($parameters{namespace}Names::${name}Tag); }
+    static bool checkTagName(const Node& node) { return node.hasTagName($parameters{namespace}Names::${name}Tag); }
 END
-            ;
-        }
-        print F <<END
-inline bool $checkHelper(const $parameters{namespace}Element* element) { ASSERT(element); return $checkHelper(*element); }
-inline bool $checkHelper(const Node* node) { ASSERT(node); return $checkHelper(*node); }
-template <typename ArgType>
-struct ElementTypeCastTraits<const $class, ArgType> {
-    static bool is(ArgType& node) { return $checkHelper(node); }
+           ;
+       }
+       print F <<END
 };
+
+// FIXME: Remove these macros once the code has been ported to using
+// is<*Element>().
+#define is$class(x) WebCore::is<WebCore::$class>(x)
 END
         ;
         print F "\n";
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to