Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,7 @@
}
}

var lastInlineElement;
var inlineEvent = function (event) {
var element = event.target;
while (
Expand Down Expand Up @@ -1173,6 +1174,13 @@
.trim()
.slice(0, 200)
: "";
} else {
// A label click also clicks its control, count one click per task
if (element === lastInlineElement) return;
lastInlineElement = element;
setTimeout(function () {
lastInlineElement = undefinedVar;
});
}
sendEvent(name, metadata, undefinedVar, trueVar);
};
Expand Down
21 changes: 20 additions & 1 deletion test/unit/inline-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ describe("inline events", function () {

afterEach(() => windows.splice(0).forEach((window) => window.close()));

it("tracks nested clicks and new elements with current metadata", function () {
it("tracks nested clicks and new elements with current metadata", async function () {
const dom = setup();
dom.window.document.body.innerHTML =
'<button data-simple-event="choose" data-simple-event-plan-name="pro"><span>Choose</span></button>';
const button = dom.window.document.querySelector("button");
click(dom, button.firstChild);
await new Promise((resolve) => setTimeout(resolve, 10));
button.setAttribute("data-simple-event", "upgrade");
button.setAttribute("data-simple-event-plan-name", "enterprise");
click(dom, button);
Expand All @@ -57,6 +58,24 @@ describe("inline events", function () {
]);
});

it("counts a label click once, and independent clicks separately", async function () {
const dom = setup();
dom.window.document.body.innerHTML =
'<div data-simple-event="plan"><label for="y">Yearly</label><input id="y" type="radio"><label><input type="checkbox"> <span>Monthly</span></label></div>';
const [forLabel, wrapLabel] = dom.window.document.querySelectorAll("label");
const nextTask = () => new Promise((resolve) => setTimeout(resolve, 10));
click(dom, forLabel);
await nextTask();
click(dom, wrapLabel.querySelector("span"));
await nextTask();
wrapLabel.control.click();
await nextTask();
forLabel.addEventListener("click", (event) => event.preventDefault());
click(dom, forLabel);
expect(forLabel.control.checked).to.equal(true);
expect(events(dom)).to.have.length(4);
});

it("leaves link behavior and modified clicks untouched", function () {
const dom = setup();
dom.window.document.body.innerHTML =
Expand Down
Loading