Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Unified Diff: Source/core/editing/FrameSelection.cpp

Issue 201293002: Add Traversal<*Element>::firstAncestor() API (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix editing failures Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/editing/FrameSelection.cpp
diff --git a/Source/core/editing/FrameSelection.cpp b/Source/core/editing/FrameSelection.cpp
index d505320bc89b81913907799205e2ccb8db257927..cbb2823692f3b836843f1e8043188f6e5a5706dc 100644
--- a/Source/core/editing/FrameSelection.cpp
+++ b/Source/core/editing/FrameSelection.cpp
@@ -1720,18 +1720,23 @@ FloatRect FrameSelection::bounds(bool clipToVisibleContent) const
return clipToVisibleContent ? intersection(selectionRect, view->visibleContentRect()) : selectionRect;
}
+static inline HTMLFormElement* associatedFormElement(HTMLElement& element)
+{
+ if (isHTMLFormElement(element))
+ return &toHTMLFormElement(element);
+ return element.formOwner();
+}
+
// Scans logically forward from "start", including any child frames.
static HTMLFormElement* scanForForm(Node* start)
{
if (!start)
return 0;
+
HTMLElement* element = start->isHTMLElement() ? toHTMLElement(start) : Traversal<HTMLElement>::next(*start);
for (; element; element = Traversal<HTMLElement>::next(*element)) {
- if (isHTMLFormElement(*element))
- return toHTMLFormElement(element);
-
- if (HTMLFormElement* owner = element->formOwner())
- return owner;
+ if (HTMLFormElement* form = associatedFormElement(*element))
+ return form;
if (isHTMLFrameElementBase(*element)) {
Node* childDocument = toHTMLFrameElementBase(*element).contentDocument();
@@ -1749,17 +1754,19 @@ HTMLFormElement* FrameSelection::currentForm() const
Node* start = m_frame->document()->focusedElement();
if (!start)
start = this->start().deprecatedNode();
+ if (!start)
+ return 0;
+
+ // Check current Node to find a form element.
+ if (start->isHTMLElement()) {
+ if (HTMLFormElement* form = associatedFormElement(toHTMLElement(*start)))
+ return form;
+ }
// Try walking up the node tree to find a form element.
- Node* node;
- for (node = start; node; node = node->parentNode()) {
- if (isHTMLFormElement(*node))
- return toHTMLFormElement(node);
- if (node->isHTMLElement()) {
- HTMLFormElement* owner = toHTMLElement(node)->formOwner();
- if (owner)
- return owner;
- }
+ for (HTMLElement* element = Traversal<HTMLElement>::firstAncestor(*start); element; element = Traversal<HTMLElement>::firstAncestor(*element)) {
esprehn 2014/03/27 13:28:16 Couldn't we just add ::firstAncestorOrSelf(Node*)
Inactive 2014/03/27 13:32:05 Excellent idea. I thought about it but couldn't co
+ if (HTMLFormElement* form = associatedFormElement(*element))
+ return form;
}
// Try walking forward in the node tree to find a form element.

Powered by Google App Engine
This is Rietveld 408576698