Skip to content
Permalink
Browse files
Fix behavior of nested click event on label element with checkbox
Fix behavior of nested click event on label element with checkbox
https://bugs.webkit.org/show_bug.cgi?id=249789

Reviewed by Ryosuke Niwa.

This patch is to align WebKit with Blink / Chromium and Gecko / Firefox.

Merge - https://src.chromium.org/viewvc/blink?view=revision&revision=183243

This patch removes static variable in HTMLLabelElement::defaultEventHandler, which interferes with handling of nested event on another label element. This is to promote this variable to class variable in the header so this interference can be avoided.

* Source/WebCore/html/HTMLLabelElement.cpp:
(HTMLLabelElement::defaultEventHandler): removed static variable and rename usage with SetForScope
* Source/WebCore/html/HTMLLabelElement.h: Add bool 'm_processingClick' with false
* LayoutTests/fast/form/checkbox-nested-click-event-on-label.html: Add Test Case
* LayoutTests/fast/form/checkbox-nested-click-event-on-label-expected.txt: Add Test Case Expectation

Canonical link: https://commits.webkit.org/258287@main
  • Loading branch information
Ahmad-S792 authored and Ahmad Saleem committed Dec 23, 2022
1 parent 0894432 commit 4ee89ea3f12cb36cc05e36f80c77c73553e62d28
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
@@ -0,0 +1,11 @@
checkbox1 checkbox2
Tests nested click event on label element with checkbox

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


PASS document.getElementById('ch2').checked is true
PASS successfullyParsed is true

TEST COMPLETE

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<input type="checkbox" name="ch1" id="ch1" value="1" /><label id="for_ch1" for="ch1">checkbox1</label>
<input type="checkbox" name="ch2" id="ch2" value="2" /><label id="for_ch2" for="ch2">checkbox2</label>
<p id="description"></p>
<div id="console"></div>
<script src="../../resources/js-test.js"></script>
<script>
description('Tests nested click event on label element with checkbox');
document.getElementById('ch1').addEventListener('click', function() {
document.getElementById('for_ch2').click();
}, false);
document.getElementById('for_ch1').click();
shouldBeTrue("document.getElementById('ch2').checked");
</script>
@@ -2,8 +2,9 @@
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004-2019 Apple Inc. All rights reserved.
* Copyright (C) 2004-2022 Apple Inc. All rights reserved.
* (C) 2006 Alexey Proskuryakov (ap@nypop.com)
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -34,6 +35,7 @@
#include "HTMLNames.h"
#include "SelectionRestorationMode.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/SetForScope.h>

namespace WebCore {

@@ -130,9 +132,7 @@ bool HTMLLabelElement::isEventTargetedAtInteractiveDescendants(Event& event) con
}
void HTMLLabelElement::defaultEventHandler(Event& event)
{
static bool processingClick = false;

if (event.type() == eventNames().clickEvent && !processingClick) {
if (event.type() == eventNames().clickEvent && !m_processingClick) {
auto control = this->control();

// If we can't find a control or if the control received the click
@@ -151,16 +151,14 @@ void HTMLLabelElement::defaultEventHandler(Event& event)
return;
}

processingClick = true;
SetForScope processingClick(m_processingClick, true);

control->dispatchSimulatedClick(&event);

document().updateLayoutIgnorePendingStylesheets();
if (control->isMouseFocusable())
control->focus({ { }, { }, { }, FocusTrigger::Click, { } });

processingClick = false;

event.setDefaultHandled();
}

@@ -2,7 +2,8 @@
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2000 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004-2017 Apple Inc. All rights reserved.
* Copyright (C) 2004-2022 Apple Inc. All rights reserved.
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -57,6 +58,8 @@ class HTMLLabelElement final : public HTMLElement {
void focus(const FocusOptions&) final;

bool isInteractiveContent() const final { return true; }

bool m_processingClick { false };
};

} //namespace

0 comments on commit 4ee89ea

Please sign in to comment.