Switch createContextualFragment to element iterator
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 3 Nov 2013 20:19:52 +0000 (20:19 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 3 Nov 2013 20:19:52 +0000 (20:19 +0000)
https://bugs.webkit.org/show_bug.cgi?id=123704

Reviewed by Andreas Kling.

* editing/FrameSelection.cpp:
(WebCore::scanForForm):

    Use type helpers instead of hasTagName

* editing/markup.cpp:
(WebCore::collectElementsToRemoveFromFragment):
(WebCore::removeElementFromFragmentPreservingChildren):
(WebCore::createContextualFragment):
* html/HTMLFormElement.cpp:
(WebCore::HTMLFormElement::formElementIndex):

    Use type helpers instead of hasTagName

* html/HTMLTagNames.in:

    Generate type helpers for <html>.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@158537 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/editing/FrameSelection.cpp
Source/WebCore/editing/markup.cpp
Source/WebCore/html/HTMLFormElement.cpp
Source/WebCore/html/HTMLTagNames.in

index a518a66..4d5a462 100644 (file)
@@ -1,5 +1,30 @@
 2013-11-03  Antti Koivisto  <antti@apple.com>
 
+        Switch createContextualFragment to element iterator
+        https://bugs.webkit.org/show_bug.cgi?id=123704
+
+        Reviewed by Andreas Kling.
+
+        * editing/FrameSelection.cpp:
+        (WebCore::scanForForm):
+        
+            Use type helpers instead of hasTagName
+
+        * editing/markup.cpp:
+        (WebCore::collectElementsToRemoveFromFragment):
+        (WebCore::removeElementFromFragmentPreservingChildren):
+        (WebCore::createContextualFragment):
+        * html/HTMLFormElement.cpp:
+        (WebCore::HTMLFormElement::formElementIndex):
+        
+            Use type helpers instead of hasTagName
+
+        * html/HTMLTagNames.in:
+        
+            Generate type helpers for <html>.
+
+2013-11-03  Antti Koivisto  <antti@apple.com>
+
         ChildNodeList should not be LiveNodeList
         https://bugs.webkit.org/show_bug.cgi?id=123708
 
index c0d75f1..efab6c0 100644 (file)
@@ -1959,10 +1959,13 @@ static HTMLFormElement* scanForForm(Element* start)
         HTMLElement& element = *it;
         if (isHTMLFormElement(&element))
             return toHTMLFormElement(&element);
-        if (element.isFormControlElement())
-            return static_cast<HTMLFormControlElement&>(element).form();
-        if (element.hasTagName(frameTag) || element.hasTagName(iframeTag)) {
-            if (HTMLFormElement* frameResult = scanForForm(toHTMLFrameElementBase(element).contentDocument()->documentElement()))
+        if (isHTMLFormControlElement(element))
+            return toHTMLFormControlElement(element).form();
+        if (isHTMLFrameElementBase(element)) {
+            Document* contentDocument = toHTMLFrameElementBase(element).contentDocument();
+            if (!contentDocument)
+                continue;
+            if (HTMLFormElement* frameResult = scanForForm(contentDocument->documentElement()))
                 return frameResult;
         }
     }
index 5e62009..d2e75c3 100644 (file)
@@ -902,15 +902,32 @@ PassRefPtr<DocumentFragment> createFragmentForTransformToFragment(const String&
     return fragment.release();
 }
 
-static inline void removeElementPreservingChildren(PassRefPtr<DocumentFragment> fragment, HTMLElement* element)
+static Vector<Ref<HTMLElement>> collectElementsToRemoveFromFragment(ContainerNode& container)
+{
+    Vector<Ref<HTMLElement>> toRemove;
+    auto children = childrenOfType<HTMLElement>(container);
+    for (auto it = children.begin(), end = children.end(); it != end; ++it) {
+        HTMLElement& element = *it;
+        if (isHTMLHtmlElement(element)) {
+            toRemove.append(element);
+            collectElementsToRemoveFromFragment(element);
+            continue;
+        }
+        if (isHTMLHeadElement(element) || isHTMLBodyElement(element))
+            toRemove.append(element);
+    }
+    return toRemove;
+}
+
+static void removeElementFromFragmentPreservingChildren(DocumentFragment& fragment, HTMLElement& element)
 {
     RefPtr<Node> nextChild;
-    for (RefPtr<Node> child = element->firstChild(); child; child = nextChild) {
+    for (RefPtr<Node> child = element.firstChild(); child; child = nextChild) {
         nextChild = child->nextSibling();
-        element->removeChild(child.get(), ASSERT_NO_EXCEPTION);
-        fragment->insertBefore(child, element, ASSERT_NO_EXCEPTION);
+        element.removeChild(child.get(), ASSERT_NO_EXCEPTION);
+        fragment.insertBefore(child, &element, ASSERT_NO_EXCEPTION);
     }
-    fragment->removeChild(element, ASSERT_NO_EXCEPTION);
+    fragment.removeChild(&element, ASSERT_NO_EXCEPTION);
 }
 
 PassRefPtr<DocumentFragment> createContextualFragment(const String& markup, HTMLElement* element, ParserContentPolicy parserContentPolicy, ExceptionCode& ec)
@@ -934,16 +951,10 @@ PassRefPtr<DocumentFragment> createContextualFragment(const String& markup, HTML
     // We need to pop <html> and <body> elements and remove <head> to
     // accommodate folks passing complete HTML documents to make the
     // child of an element.
+    auto toRemove = collectElementsToRemoveFromFragment(*fragment);
+    for (unsigned i = 0; i < toRemove.size(); ++i)
+        removeElementFromFragmentPreservingChildren(*fragment, toRemove[i].get());
 
-    RefPtr<HTMLElement> nextElement;
-    for (RefPtr<HTMLElement> element = Traversal<HTMLElement>::firstWithin(fragment.get()); element; element = nextElement) {
-        nextElement = Traversal<HTMLElement>::nextSibling(element.get());
-        if (element->hasTagName(htmlTag) || element->hasTagName(headTag) || element->hasTagName(bodyTag)) {
-            if (HTMLElement* firstChild = Traversal<HTMLElement>::firstChild(element.get()))
-                nextElement = firstChild;
-            removeElementPreservingChildren(fragment, element.get());
-        }
-    }
     return fragment.release();
 }
 
index 8b4307e..6c7dde5 100644 (file)
@@ -480,7 +480,7 @@ unsigned HTMLFormElement::formElementIndex(FormAssociatedElement* associatedElem
         HTMLElement& element = *it;
         if (&element == &associatedHTMLElement)
             return i;
-        if (!element.isFormControlElement() && !element.hasTagName(objectTag))
+        if (!isHTMLFormControlElement(element) && !isHTMLObjectElement(element))
             continue;
         if (element.form() != this)
             continue;
index 11f5ccd..8ca38b7 100644 (file)
@@ -61,7 +61,7 @@ head generateTypeHelpers
 header interfaceName=HTMLElement
 hgroup interfaceName=HTMLElement
 hr interfaceName=HTMLHRElement
-html
+html generateTypeHelpers
 i interfaceName=HTMLElement
 iframe interfaceName=HTMLIFrameElement, generateTypeHelpers
 image interfaceName=HTMLElement