From fbf4c4add2079f4d9b75ebd1ecb87051cd5fcef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 24 May 2023 15:55:43 +0200 Subject: [PATCH] [FIX] parser: make t-on stricter Before this commit, the `t-on` directive assumed that the next character would be a -, and ignored it, so if someone would write `t-onclick` by mistake, Owl would then add an event handler on the `lick` event, instead of `click`. With this commit, we improve the parser to make it stricter and fail if there is no dash. closes #1441 --- src/compiler/parser.ts | 8 ++++---- tests/compiler/parser.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 42a89f13..ad902615 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -336,10 +336,10 @@ function parseDOMNode(node: Element, ctx: ParsingContext): AST | null { for (let attr of nodeAttrsNames) { const value = node.getAttribute(attr)!; - if (attr.startsWith("t-on")) { - if (attr === "t-on") { - throw new OwlError("Missing event name with t-on directive"); - } + if (attr === "t-on" || attr === "t-on-") { + throw new OwlError("Missing event name with t-on directive"); + } + if (attr.startsWith("t-on-")) { on = on || {}; on[attr.slice(5)] = value; } else if (attr.startsWith("t-model")) { diff --git a/tests/compiler/parser.test.ts b/tests/compiler/parser.test.ts index cdbbcace..d261f020 100644 --- a/tests/compiler/parser.test.ts +++ b/tests/compiler/parser.test.ts @@ -1147,6 +1147,24 @@ describe("qweb parser", () => { }); }); + test("t-onclick without dash", async () => { + expect(() => parse(``)).toThrowError( + "Unknown QWeb directive: 't-onclick'" + ); + }); + + test("t-on without event", async () => { + expect(() => parse(``)).toThrowError( + "Missing event name with t-on directive" + ); + }); + + test("t-on- without event", async () => { + expect(() => parse(``)).toThrowError( + "Missing event name with t-on directive" + ); + }); + // --------------------------------------------------------------------------- // t-model // --------------------------------------------------------------------------- @@ -1275,6 +1293,12 @@ describe("qweb parser", () => { }); }); + test("component with event handler", async () => { + expect(() => parse(``)).toThrowError( + "unsupported directive on Component: t-onclick" + ); + }); + test("component with t-ref", async () => { expect(() => parse(``)).toThrow( "t-ref is no longer supported on components. Consider exposing only the public part of the component's API through a callback prop."