From 8a89676f2c82351921561fa40e795d3f120be8dd Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Fri, 4 Aug 2023 07:32:53 +0000 Subject: [PATCH] [FIX] web: renderToElement should always returns an element Before this commit, when the first element of a template is a non fulfilled condition : '' The function renderToElement returns an empty text. This occurs because it returns the first child, which in this case is an empty text. Now, the function returns the first element. closes odoo/odoo#130858 X-original-commit: 8dabc4e1d5ebd94f6a315ce56b9bf60240dd7fdd Signed-off-by: Aaron Bohy (aab) Signed-off-by: Jorge Pinna Puissant (jpp) --- addons/web/static/src/core/utils/render.js | 2 +- .../static/tests/core/utils/render_tests.js | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 addons/web/static/tests/core/utils/render_tests.js diff --git a/addons/web/static/src/core/utils/render.js b/addons/web/static/src/core/utils/render.js index e15b50643c2..ef50087d4cf 100644 --- a/addons/web/static/src/core/utils/render.js +++ b/addons/web/static/src/core/utils/render.js @@ -3,7 +3,7 @@ import { blockDom, markup } from "@odoo/owl"; export function renderToElement(template, context = {}) { - return render(template, context).firstChild; + return render(template, context).firstElementChild; } /** diff --git a/addons/web/static/tests/core/utils/render_tests.js b/addons/web/static/tests/core/utils/render_tests.js new file mode 100644 index 00000000000..a8d745ff557 --- /dev/null +++ b/addons/web/static/tests/core/utils/render_tests.js @@ -0,0 +1,26 @@ +/** @odoo-module **/ + +import { renderToElement, renderToString } from "@web/core/utils/render"; + +QUnit.module("utils", () => { + QUnit.module("render"); + + QUnit.test("renderToElement always returns an element", (assert) => { + renderToString.app.addTemplate( + "test.render.template.1", + ` +
NotOk
+
+ +
Ok
+
` + ); + const compiledTemplate = renderToElement("test.render.template.1"); + assert.strictEqual( + compiledTemplate.nodeType, + Node.ELEMENT_NODE, + "compiledTemplate must be an element" + ); + assert.strictEqual(compiledTemplate.outerHTML, "
Ok
"); + }); +});