summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/helpers/test_utils.js
diff options
context:
space:
mode:
authorChristophe Matthieu <chm@odoo.com>2018-02-28 11:00:06 +0100
committerChristophe Matthieu <chm@odoo.com>2018-02-28 11:55:20 +0100
commit7922dcbe48e8f370e4d28cf815e3fbaa6cc5fade (patch)
treeee8809aff7b49a2138a3b02a183deb11c68985bd /addons/web/static/tests/helpers/test_utils.js
parentb9a763ba3cf310f394b7c80b5f56c114e9242ebb (diff)
[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.
Diffstat (limited to 'addons/web/static/tests/helpers/test_utils.js')
-rw-r--r--addons/web/static/tests/helpers/test_utils.js34
1 files 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 = {};