From cdd4fc68f2f75cb27127739d398a69e664fdf17f Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 4 Apr 2024 13:59:44 +0000 Subject: [PATCH] [FIX] web: fix formatting of url field href Before this commit, we didn't correctly detect when to prefix the value of an url field for the href of its link. closes odoo/odoo#160916 X-original-commit: 27458e3bdb545f550e60f1bd39addf188151c38b Signed-off-by: Samuel Degueldre (sad) Signed-off-by: Aaron Bohy (aab) --- .../web/static/src/views/fields/url/url_field.js | 14 ++++---------- .../static/tests/views/fields/url_field_tests.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/addons/web/static/src/views/fields/url/url_field.js b/addons/web/static/src/views/fields/url/url_field.js index a2408deff9d..78aadb9cbf9 100644 --- a/addons/web/static/src/views/fields/url/url_field.js +++ b/addons/web/static/src/views/fields/url/url_field.js @@ -21,16 +21,10 @@ export class UrlField extends Component { } get formattedHref() { - let value = ""; - if (typeof this.props.record.data[this.props.name] === "string") { - const shouldaddPrefix = !( - this.props.websitePath || - this.props.record.data[this.props.name].includes("://") || - /^\//.test(this.props.record.data[this.props.name]) - ); - value = shouldaddPrefix - ? `http://${this.props.record.data[this.props.name]}` - : this.props.record.data[this.props.name]; + let value = this.props.record.data[this.props.name]; + if (value && !this.props.websitePath) { + const regex = /^((ftp|http)s?:\/)?\//i; // http(s)://... ftp(s)://... /... + value = !regex.test(value) ? `http://${value}` : value; } return value; } diff --git a/addons/web/static/tests/views/fields/url_field_tests.js b/addons/web/static/tests/views/fields/url_field_tests.js index 162fa55a7dc..ab9b1892096 100644 --- a/addons/web/static/tests/views/fields/url_field_tests.js +++ b/addons/web/static/tests/views/fields/url_field_tests.js @@ -325,4 +325,20 @@ QUnit.module("Fields", (hooks) => { "Placeholder" ); }); + + QUnit.test("url field with non falsy, but non url value", async function (assert) { + serverData.models.partner.fields.foo.default = "odoo://hello"; + + await makeView({ + type: "form", + resModel: "partner", + serverData, + arch: `
`, + }); + + assert.strictEqual( + target.querySelector(".o_field_widget[name='foo'] a").getAttribute("href"), + "http://odoo://hello" + ); + }); });