[PATCH 3 of 7] hgweb: properly iterate over arrays and objects in mercurial.js

Anton Shestakov av6 at dwimlabs.net
Mon Nov 27 04:28:10 EST 2017


# HG changeset patch
# User Anton Shestakov <av6 at dwimlabs.net>
# Date 1511356544 -28800
#      Wed Nov 22 21:15:44 2017 +0800
# Node ID fd4a726a386dee3181007fc948513af7f7ae028c
# Parent  01aaffb4652759bb0f2eca4e18c74eedd316b806
# EXP-Topic hgweb-cleanup
hgweb: properly iterate over arrays and objects in mercurial.js

In JavaScript, using for-in loops to access every property of an object can
have unexpected results when inheritance is involved. For example, if some
piece of code adds a property (it may be a method too) to Object.prototype,
then all for-in loops that iterate over keys of any object (also anything that
inherits Object) will get that property on one of the iterations. To filter out
such unexpected properties for-in loops have to use Object.hasOwnProperty()
method. (This corresponds to "forin" option of jshint).

In the two first cases "data" and "edges" are arrays, to it's simpler to just
switch to using a regular for-with-a-counter loop.

diff --git a/mercurial/templates/static/mercurial.js b/mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js
+++ b/mercurial/templates/static/mercurial.js
@@ -95,7 +95,7 @@ function Graph() {
 		var backgrounds = '';
 		var nodedata = '';
 
-		for (var i in data) {
+		for (var i = 0; i < data.length; i++) {
 
 			var parity = i % 2;
 			this.cell[1] += this.bg_height;
@@ -107,7 +107,7 @@ function Graph() {
 			var fold = false;
 
 			var prevWidth = this.ctx.lineWidth;
-			for (var j in edges) {
+			for (var j = 0; j < edges.length; j++) {
 
 				line = edges[j];
 				start = line[0];
@@ -227,6 +227,7 @@ function process_dates(parentSelector){
 		}
 
 		for (var unit in scales){
+			if (!scales.hasOwnProperty(unit)) { continue; }
 			var s = scales[unit];
 			var n = Math.floor(delta / s);
 			if ((n >= 2) || (s === 1)){


More information about the Mercurial-devel mailing list