Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web Inspector: provide a way to enable/disable event listeners
https://bugs.webkit.org/show_bug.cgi?id=177451 Reviewed by Joseph Pecoraro. Source/JavaScriptCore: * inspector/protocol/DOM.json: Add `setEventListenerDisabled` command that enables/disables a specific event listener during event dispatch. When a disabled event listener is fired, the listener's callback will not be called. Source/WebCore: Test: inspector/dom/setEventListenerDisabled.html * dom/EventTarget.cpp: (WebCore::EventTarget::fireEventListeners): Add InspectorInstrumentation call to isEventListenerDisabled. If true, the event listener's callback will not be called. * inspector/InspectorDOMAgent.h: * inspector/InspectorDOMAgent.cpp: (WebCore::InspectorDOMAgent::discardBindings): (WebCore::InspectorDOMAgent::getEventListenersForNode): (WebCore::InspectorDOMAgent::setEventListenerDisabled): (WebCore::InspectorDOMAgent::buildObjectForEventListener): (WebCore::InspectorDOMAgent::willRemoveEventListener): (WebCore::InspectorDOMAgent::isEventListenerDisabled): Introduce a mapping of `EventListener*` to `InspectorEventListener`, a struct for uniquely identifying event listeners so they can be referenced from the frontend. We only add items to this mapping when `getEventListenersForNode` is called, as that is when EventListener data is sent to the frontend. This allows us to defer creating an Inspector "mirror" object for each EventListener until it is needed. Items are removed whenever an event listener is removed or when the document changes. * inspector/InspectorInstrumentation.h: (WebCore::InspectorInstrumentation::isEventListenerDisabled): * inspector/InspectorInstrumentation.cpp: (WebCore::InspectorInstrumentation::willRemoveEventListenerImpl): (WebCore::InspectorInstrumentation::isEventListenerDisabledImpl): Pass additional parameters to InspectorDOMAgent so it can determine if the event listener actually exists. If not, don't dispatch an event to the frontend as nothing will change. Source/WebInspectorUI: * Localizations/en.lproj/localizedStrings.js: * UserInterface/Controllers/DOMTreeManager.js: (WI.DOMTreeManager.prototype.setEventListenerDisabled): * UserInterface/Views/DOMNodeDetailsSidebarPanel.js: (WI.DOMNodeDetailsSidebarPanel.prototype.attached): (WI.DOMNodeDetailsSidebarPanel.prototype.detached): (WI.DOMNodeDetailsSidebarPanel.prototype._eventListenersChanged): (WI.DOMNodeDetailsSidebarPanel.prototype.addEventListeners): Deleted. (WI.DOMNodeDetailsSidebarPanel.prototype.removeEventListeners): Deleted. Listen for `WI.DOMNode.Event.EventListenersChanged` on all instances of WI.DOMNode, since we will still want to refresh the event listeners section in the event that an event listener is removed from a parent node. * UserInterface/Views/EventListenerSectionGroup.js: (WI.EventListenerSectionGroup): (WI.EventListenerSectionGroup.prototype._eventText): (WI.EventListenerSectionGroup.prototype._nodeTextOrLink): (WI.EventListenerSectionGroup.prototype._createDisabledToggleElement): (WI.EventListenerSectionGroup.prototype._createDisabledToggleElement.updateTitle): * UserInterface/Views/EventListenerSectionGroup.css: (.event-listener-section > .content input[type="checkbox"]): * UserInterface/Views/DetailsSectionSimpleRow.js: (WI.DetailsSectionSimpleRow.prototype.get label): (WI.DetailsSectionSimpleRow.prototype.set label): LayoutTests: * inspector/dom/setEventListenerDisabled-expected.txt: Added. * inspector/dom/setEventListenerDisabled.html: Added. Canonical link: https://commits.webkit.org/194528@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@223321 268f45cc-cd09-0410-ab3c-d52691b4dbfc
- Loading branch information
Showing
18 changed files
with
442 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
LayoutTests/inspector/dom/setEventListenerDisabled-expected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Testing DOMAgent.setEventListenerDisabled. | ||
|
||
|
||
== Running test suite: DOM.setEventListenerDisabled | ||
-- Running test case: DOM.setEventListenerDisabled.DisabledClickEvent | ||
Click event listener is enabled. | ||
Disabling event listener... | ||
<body> clicked. | ||
PASS: Click event listener did not fire. | ||
Click event listener is disabled. | ||
|
||
-- Running test case: DOM.setEventListenerDisabled.ReenabledClickEvent | ||
Click event listener is disabled. | ||
Enabling event listener... | ||
<body> clicked. | ||
PASS: Click event listener fired. | ||
Click event listener is enabled. | ||
|
||
-- Running test case: DOM.setEventListenerDisabled.Invalid | ||
PASS: Should produce an error. | ||
Error: No event listener for given identifier. | ||
|
129 changes: 129 additions & 0 deletions
129
LayoutTests/inspector/dom/setEventListenerDisabled.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<script src="../../http/tests/inspector/resources/inspector-test.js"></script> | ||
<script> | ||
function clickBody() { | ||
document.body.click(); | ||
|
||
TestPage.dispatchEventToFrontend("TestPageAfterClick"); | ||
} | ||
|
||
function test() { | ||
let clickEventListener = null; | ||
|
||
let suite = InspectorTest.createAsyncSuite("DOM.setEventListenerDisabled"); | ||
|
||
function logListener() { | ||
return DOMAgent.getEventListenersForNode(clickEventListener.nodeId).then(({listeners}) => { | ||
InspectorTest.assert(listeners.length === 1, "There should only be one event listener."); | ||
InspectorTest.assert(listeners[0].type === "click", `There event listener should be for "click".`); | ||
InspectorTest.log("Click event listener is " + (listeners[0].disabled ? "disabled" : "enabled") + "."); | ||
}); | ||
} | ||
|
||
suite.addTestCase({ | ||
name: "DOM.setEventListenerDisabled.DisabledClickEvent", | ||
test(resolve, reject) { | ||
let listener = InspectorTest.singleFireEventListener("TestPageDocumentClicked", () => { | ||
reject("Click event listener should not be called"); | ||
}); | ||
|
||
InspectorTest.singleFireEventListener("TestPageAfterClick", () => { | ||
InspectorTest.pass("Click event listener did not fire."); | ||
|
||
InspectorTest.removeEventListener(listener); | ||
|
||
logListener().then(resolve, reject); | ||
}); | ||
|
||
logListener().then(() => { | ||
InspectorTest.log("Disabling event listener..."); | ||
|
||
const disabled = true; | ||
DOMAgent.setEventListenerDisabled(clickEventListener.eventListenerId, disabled, (error) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
|
||
InspectorTest.evaluateInPage(`clickBody()`, () => { | ||
InspectorTest.log("<body> clicked."); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
suite.addTestCase({ | ||
name: "DOM.setEventListenerDisabled.ReenabledClickEvent", | ||
test(resolve, reject) { | ||
InspectorTest.singleFireEventListener("TestPageDocumentClicked", () => { | ||
InspectorTest.pass("Click event listener fired."); | ||
|
||
logListener().then(resolve, reject); | ||
}); | ||
|
||
logListener().then(() => { | ||
InspectorTest.log("Enabling event listener..."); | ||
|
||
const disabled = false; | ||
DOMAgent.setEventListenerDisabled(clickEventListener.eventListenerId, disabled, (error) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
|
||
InspectorTest.evaluateInPage(`clickBody()`, () => { | ||
InspectorTest.log("<body> clicked."); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
suite.addTestCase({ | ||
name: "DOM.setEventListenerDisabled.Invalid", | ||
description: "Invalid event listener identifiers should cause an error.", | ||
test(resolve, reject) { | ||
const invalidEventListenerId = 9999999; | ||
const disabled = false; | ||
DOMAgent.setEventListenerDisabled(invalidEventListenerId, disabled, (error) => { | ||
InspectorTest.expectThat(error, "Should produce an error."); | ||
InspectorTest.log("Error: " + error); | ||
resolve(); | ||
}); | ||
} | ||
}); | ||
|
||
WI.domTreeManager.requestDocument((documentNode) => { | ||
DOMAgent.getEventListenersForNode(documentNode.id, (error, eventListeners) => { | ||
if (error) { | ||
InspectorTest.fail("Unable to retrieve event listeners."); | ||
InspectorTest.completeTest(); | ||
return; | ||
} | ||
|
||
clickEventListener = eventListeners[0]; | ||
if (!clickEventListener || clickEventListener.type !== "click") { | ||
InspectorTest.fail("Missing click event listener."); | ||
InspectorTest.completeTest(); | ||
return; | ||
} | ||
|
||
suite.runTestCasesAndFinish(); | ||
}); | ||
}); | ||
} | ||
</script> | ||
</head> | ||
<body onload="runTest()"> | ||
<p>Testing DOMAgent.setEventListenerDisabled.</p> | ||
|
||
<script> | ||
document.addEventListener("click", (event) => { | ||
TestPage.dispatchEventToFrontend("TestPageDocumentClicked"); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.