From 18ad07c001ee5fd5fea95cf88d415fbd32657cc4 Mon Sep 17 00:00:00 2001 From: dht-odoo Date: Thu, 27 May 2021 03:24:08 +0000 Subject: [PATCH] [IMP] web: add isHtmlEmpty method in kanban view In this commit we have added new method name isHtmlEmpty in kanban view. As we are convertting text field to html field, we will need to check html field in if conditions. As Html field will contain tags we cannot check it every time perfectly. So, we need this method to make sure html field is empty or not. Task id: 2499504 X-original-commit: 2968dcea7fb58795eeb34949f4bbfc1029938cd1 --- .../src/js/views/kanban/kanban_record.js | 16 ++++++++ addons/web/static/tests/views/kanban_tests.js | 37 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/addons/web/static/src/js/views/kanban/kanban_record.js b/addons/web/static/src/js/views/kanban/kanban_record.js index 6929ca6f2a2..a217fc51b30 100644 --- a/addons/web/static/src/js/views/kanban/kanban_record.js +++ b/addons/web/static/src/js/views/kanban/kanban_record.js @@ -87,6 +87,22 @@ var KanbanRecord = Widget.extend(WidgetAdapterMixin, { // Public //-------------------------------------------------------------------------- + /** + * Check if a html content is empty. If there are only formatting tags with + * style attributes or a void content. Famous use case is + * '


' added by some web editor(s). + * Note that because the use of this method is limited, we ignore the cases + * like there's one tag in the content. In such case, even if it's the + * actual content, we consider it empty. + * + * @param {string} htmlContent + * @returns {boolean} true if no content found or if containing only formatting tags + */ + isHtmlEmpty: function (htmlContent) { + let div = document.createElement('div'); + div.innerHTML = htmlContent || ""; + return div.innerText.trim() === ""; + }, /** * Re-renders the record with a new state * diff --git a/addons/web/static/tests/views/kanban_tests.js b/addons/web/static/tests/views/kanban_tests.js index b548b5838e3..6385a430f16 100644 --- a/addons/web/static/tests/views/kanban_tests.js +++ b/addons/web/static/tests/views/kanban_tests.js @@ -7528,6 +7528,43 @@ QUnit.module('Views', { kanban.destroy(); }); + + QUnit.test('kanban with isHtmlEmpty method', async function (assert) { + assert.expect(3); + + this.data.product.fields.description = {string: 'Description', type: 'html'}; + this.data.product.records.push( + {id: 11, display_name: "product 11", description: "hello"}, + {id: 12, display_name: "product 12", description: "


"}, + ); + + var kanban = await createView({ + View: KanbanView, + model: 'product', + data: this.data, + arch: ` + + +
+ +
+ +
+
+
+
`, + domain: [['id', 'in', [11, 12]]], + + }); + assert.containsOnce(kanban, '.o_kanban_record:first div.test', + "the container is displayed if description have actual content"); + assert.strictEqual(kanban.$('.o_kanban_record:first div.test span.text-info').html().trim(), 'hello', + "the inner html content is rendered properly"); + assert.containsNone(kanban, '.o_kanban_record:last div.test', + "the container is not displayed if description just have formatting tags and no actual content"); + + kanban.destroy(); + }); }); });