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

Side by Side Diff: Source/core/html/HTMLLabelElement.cpp

Issue 556813002: Fix behavior of label associated with control element (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Addressing comments Created 6 years, 3 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved. 5 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com) 6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 14 matching lines...) Expand all
25 #include "config.h" 25 #include "config.h"
26 #include "core/html/HTMLLabelElement.h" 26 #include "core/html/HTMLLabelElement.h"
27 27
28 #include "core/HTMLNames.h" 28 #include "core/HTMLNames.h"
29 #include "core/dom/Document.h" 29 #include "core/dom/Document.h"
30 #include "core/dom/ElementTraversal.h" 30 #include "core/dom/ElementTraversal.h"
31 #include "core/editing/FrameSelection.h" 31 #include "core/editing/FrameSelection.h"
32 #include "core/events/MouseEvent.h" 32 #include "core/events/MouseEvent.h"
33 #include "core/frame/LocalFrame.h" 33 #include "core/frame/LocalFrame.h"
34 #include "core/html/FormAssociatedElement.h" 34 #include "core/html/FormAssociatedElement.h"
35 #include "core/page/EventHandler.h"
35 36
36 namespace blink { 37 namespace blink {
37 38
38 using namespace HTMLNames; 39 using namespace HTMLNames;
39 40
40 inline HTMLLabelElement::HTMLLabelElement(Document& document) 41 inline HTMLLabelElement::HTMLLabelElement(Document& document)
41 : HTMLElement(labelTag, document) 42 : HTMLElement(labelTag, document)
42 { 43 {
43 } 44 }
44 45
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 node = node->parentOrShadowHostNode(); 119 node = node->parentOrShadowHostNode();
119 } 120 }
120 return false; 121 return false;
121 } 122 }
122 123
123 void HTMLLabelElement::defaultEventHandler(Event* evt) 124 void HTMLLabelElement::defaultEventHandler(Event* evt)
124 { 125 {
125 static bool processingClick = false; 126 static bool processingClick = false;
126 127
127 if (evt->type() == EventTypeNames::click && !processingClick) { 128 if (evt->type() == EventTypeNames::click && !processingClick) {
128 // If the click is not simulated and the text of label element is
129 // selected, do not pass the event to control element.
130 // Note: a click event may be not a mouse event if created by
131 // document.createEvent().
132 if (evt->isMouseEvent() && !toMouseEvent(evt)->isSimulated()) {
133 if (LocalFrame* frame = document().frame()) {
134 if (frame->selection().selection().isRange())
135 return;
136 }
137 }
138
139
140 RefPtrWillBeRawPtr<HTMLElement> element = control(); 129 RefPtrWillBeRawPtr<HTMLElement> element = control();
141 130
142 // If we can't find a control or if the control received the click 131 // If we can't find a control or if the control received the click
143 // event, then there's no need for us to do anything. 132 // event, then there's no need for us to do anything.
144 if (!element || (evt->target() && element->containsIncludingShadowDOM(ev t->target()->toNode()))) 133 if (!element || (evt->target() && element->containsIncludingShadowDOM(ev t->target()->toNode())))
145 return; 134 return;
146 135
147 if (evt->target() && isInInteractiveContent(evt->target()->toNode())) 136 if (evt->target() && isInInteractiveContent(evt->target()->toNode()))
148 return; 137 return;
149 138
139 // Behaviour of label element is as follows:
tkent 2014/09/17 00:19:14 Very good comment. It is helpful to understand th
140 // - If there is double click, two clicks will be passed to control
141 // element. Control element will *not* be focused.
142 // - If there is selection of label element by dragging, no click
143 // event is passed. Also, no focus on control element.
144 // - If there is already a selection on label element and then label
145 // is clicked, then click event is passed to control element and
146 // control element is focused.
147
148 bool isLabelTextSelected = false;
149
150 // If the click is not simulated and the text of the label element
151 // is selected by dragging over it, then return without passing the
152 // click event to control element.
153 // Note: a click event may be not a mouse event if created by
154 // document.createEvent().
155 if (evt->isMouseEvent() && !toMouseEvent(evt)->isSimulated()) {
156 if (LocalFrame* frame = document().frame()) {
157 // Check if there is a selection and click is not on the
158 // selection.
159 if (frame->selection().isRange() && !frame->eventHandler().mouse DownWasSingleClickInSelection())
160 isLabelTextSelected = true;
161 // If selection is there and is single click i.e. text is
162 // selected by dragging over label text, then return.
163 // Click count >=2, meaning double click or triple click,
164 // should pass click event to control element.
165 // Only in case of drag, *neither* we pass the click event,
166 // *nor* we focus the control element.
167 if (isLabelTextSelected && frame->eventHandler().clickCount() == 1)
168 return;
169 }
170 }
171
150 processingClick = true; 172 processingClick = true;
151 173
152 document().updateLayoutIgnorePendingStylesheets(); 174 document().updateLayoutIgnorePendingStylesheets();
153 if (element->isMouseFocusable()) 175 if (element->isMouseFocusable()) {
154 element->focus(true, FocusTypeMouse); 176 // If the label is *not* selected, or if the click happened on
177 // selection of label, only then focus the control element.
178 // In case of double click or triple click, selection will be there,
179 // so do not focus the control element.
180 if (!isLabelTextSelected)
181 element->focus(true, FocusTypeMouse);
182 }
155 183
156 // Click the corresponding control. 184 // Click the corresponding control.
157 element->dispatchSimulatedClick(evt); 185 element->dispatchSimulatedClick(evt);
158 186
159 processingClick = false; 187 processingClick = false;
160 188
161 evt->setDefaultHandled(); 189 evt->setDefaultHandled();
162 } 190 }
163 191
164 HTMLElement::defaultEventHandler(evt); 192 HTMLElement::defaultEventHandler(evt);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 { 256 {
229 if (insertionPoint->isInTreeScope() && treeScope() == document()) { 257 if (insertionPoint->isInTreeScope() && treeScope() == document()) {
230 TreeScope& treeScope = insertionPoint->treeScope(); 258 TreeScope& treeScope = insertionPoint->treeScope();
231 if (treeScope.shouldCacheLabelsByForAttribute()) 259 if (treeScope.shouldCacheLabelsByForAttribute())
232 updateLabel(treeScope, fastGetAttribute(forAttr), nullAtom); 260 updateLabel(treeScope, fastGetAttribute(forAttr), nullAtom);
233 } 261 }
234 HTMLElement::removedFrom(insertionPoint); 262 HTMLElement::removedFrom(insertionPoint);
235 } 263 }
236 264
237 } // namespace 265 } // namespace
OLDNEW
« no previous file with comments | « LayoutTests/fast/forms/label/label-selection-by-dragging-expected.txt ('k') | Source/core/page/EventHandler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698