From 69e34dd2b2e78fe8ad4cf7a38181d0e77ff8c595 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Thu, 15 Oct 2026 10:16:37 +0300 Subject: [PATCH] fix(node): fire input event when clearing a text field --- CHANGELOG.md | 1 + lib/capybara/cuprite/javascripts/index.js | 8 ++++++-- spec/features/driver_spec.rb | 13 +++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9b226f..71b8257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - `Node#select_option` and `Node#unselect_option` didn't fire the `input` event on the select [#223] - `Node#trigger` silently dispatched nothing for events other than mouse, focus and `submit`, such as `input`, `change` or custom events [#197] +- `Node#set` with an empty string cleared a text field without firing the `input` event [#164] ### Removed diff --git a/lib/capybara/cuprite/javascripts/index.js b/lib/capybara/cuprite/javascripts/index.js index dd0636c..1aef8e6 100644 --- a/lib/capybara/cuprite/javascripts/index.js +++ b/lib/capybara/cuprite/javascripts/index.js @@ -220,6 +220,10 @@ class Cuprite { this.keyupdowned(node, "keyup", char, keyCode); } + + if (valueBefore !== "" && node.value === "") { + this.input(node, "deleteContentBackward"); + } } if (valueBefore !== node.value) { @@ -238,8 +242,8 @@ class Cuprite { return nativeTextareaValueSetter.call(node, value); } - input(node) { - let event = new InputEvent("input", { inputType: "insertText", bubbles: true, cancelable: false }); + input(node, inputType = "insertText") { + let event = new InputEvent("input", { inputType: inputType, bubbles: true, cancelable: false }); node.dispatchEvent(event); } diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index 8666ccf..12965cf 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -1575,6 +1575,19 @@ def create_screenshot(file, *args) input.set("a") expect(output.text).to eq("keydown keypress input keyup change keydown keypress input keyup") end + + it "calls the input event when clearing the value" do + input.set("a") + input.set("") + expect(output.text).to eq("keydown keypress input keyup change input change") + expect(output["data-input-type"]).to eq("deleteContentBackward") + expect(input.value).to be_empty + end + + it "doesn't call the input event when clearing an empty value" do + input.set("") + expect(output.text).to be_empty + end end end