From 95128e6472fc23997bc47fbefd7730e4c43a7431 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 21 Apr 2021 06:22:48 +0000 Subject: [PATCH] [FIX] web: test broken in Firefox This is arguably a 20 years old Firefox bug (at the very least an under-specified area of the spec where Firefox's behaviour is technically allowed under spec but not super useful or convenient): `window.getSelection()` simply doesn't work when invoked on a form field: https://bugzilla.mozilla.org/show_bug.cgi?id=85686. Getting the selection data more explicitly by looking up the focused form element, then checking *its* selection, seems to work fine and be cross-browser. Also focus() the input while at it: according to MDN > Calling element.select() will not necessarily focus the input, so it > is often used with HTMLOrForeignElement.focus. And jQuery doesn't really document whether `select()` will implicitly `focus()`, so better safe than sorry. closes odoo/odoo#69579 Signed-off-by: Xavier Morel (xmo) --- addons/web/static/src/js/fields/basic_fields.js | 1 + addons/web/static/tests/fields/basic_fields_tests.js | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index de48b193490..d5fcec21399 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -881,6 +881,7 @@ var FieldDate = InputField.extend({ */ activate: function () { if (this.isFocusable() && this.datewidget) { + this.datewidget.$input.focus(); this.datewidget.$input.select(); return true; } diff --git a/addons/web/static/tests/fields/basic_fields_tests.js b/addons/web/static/tests/fields/basic_fields_tests.js index ba86b2896f4..125ab7f4d9c 100644 --- a/addons/web/static/tests/fields/basic_fields_tests.js +++ b/addons/web/static/tests/fields/basic_fields_tests.js @@ -4314,7 +4314,7 @@ QUnit.module('basic_fields', { }); QUnit.test('field date should select its content onclick when there is one', async function (assert) { - assert.expect(2); + assert.expect(3); var done = assert.async(); var form = await createView({ @@ -4332,7 +4332,10 @@ QUnit.module('basic_fields', { 'show.datetimepicker': function () { assert.ok($('.bootstrap-datetimepicker-widget').is(':visible'), 'bootstrap-datetimepicker is visible'); - assert.strictEqual(window.getSelection().toString(), "02/03/2017", + const active = document.activeElement; + assert.equal(active.tagName, 'INPUT', "The datepicker input should be focused"); + const sel = active.value.slice(active.selectionStart, active.selectionEnd); + assert.strictEqual(sel, "02/03/2017", 'The whole input of the date field should have been selected'); done(); }