[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) <sad@odoo.com>
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
Aaron Bohy
2024-04-08 14:37:54 +00:00
parent b49d5b7454
commit cdd4fc68f2
2 changed files with 20 additions and 10 deletions
@@ -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;
}
@@ -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: `<form><field name="foo" widget="url"/></form>`,
});
assert.strictEqual(
target.querySelector(".o_field_widget[name='foo'] a").getAttribute("href"),
"http://odoo://hello"
);
});
});