From 7922dcbe48e8f370e4d28cf815e3fbaa6cc5fade Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Wed, 28 Feb 2018 11:00:06 +0100 Subject: [PATCH] [FIX] web: increase unit test speed The method removeSrcAttribute is critical, because each changes trigger this one. this method must be optimized to the maximum to be the fastest. Before this fix, removeSrcAttribute method search the image and iframe for all body nodes instead of search only in the inserted nodes, and the method are very slow. --- addons/web/static/tests/helpers/test_utils.js | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/addons/web/static/tests/helpers/test_utils.js b/addons/web/static/tests/helpers/test_utils.js index ee85164d9cc..c2d82775e70 100644 --- a/addons/web/static/tests/helpers/test_utils.js +++ b/addons/web/static/tests/helpers/test_utils.js @@ -314,8 +314,8 @@ function addMockEnvironment(widget, params) { }); // make sure images do not trigger a GET on the server - $('body').on('DOMNodeInserted.removeSRC', function () { - removeSrcAttribute($(this), widget); + $('body').on('DOMNodeInserted.removeSRC', function (event) { + removeSrcAttribute(event.target, widget); }); // make sure the debounce value for input fields is set to 0 @@ -641,27 +641,35 @@ function triggerKeypressEvent(char) { /** * Removes the src attribute on images and iframes to prevent not found errors, * and optionally triggers an rpc with the src url as route on a widget. + * This method is critical and must be fastest (=> no jQuery, no underscore) * - * @param {JQueryElement} $el + * @param {DOM Node} el * @param {[Widget]} widget the widget on which the rpc should be performed */ -function removeSrcAttribute($el, widget) { - $el.find('img, iframe[src]').each(function () { - var $el = $(this); - var src = $el.attr('src'); +function removeSrcAttribute(el, widget) { + var nodes; + if (el.nodeName === 'IMG' || el.nodeName === 'IFRAME') { + nodes = [el]; + } else { + nodes = Array.prototype.slice.call(el.getElementsByTagName('img')) + .concat(Array.prototype.slice.call(el.getElementsByTagName('iframe'))); + } + var node; + while (node = nodes.pop()) { + var src = node.attributes.src && node.attributes.src.value; if (src && src !== 'about:blank') { - if ($el[0].nodeName === 'IMG') { - $el.attr('data-src', src); - $el.removeAttr('src'); + var $el = $(node); + node.setAttribute('data-src', src); + if (node.nodeName === 'IMG') { + node.attributes.removeNamedItem('src'); } else { - $el.attr('data-src', src); - $el.attr('src', 'about:blank'); + node.setAttribute('src', 'about:blank'); } if (widget) { widget._rpc({route: src}); } } - }); + } } var patches = {};