From 792566207967436501bcce1ec2cac6307d6f761f Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Fri, 8 Mar 2013 14:07:02 -0500 Subject: [PATCH] Avoid writing undefined properties in JSON localStorage --- js/id/core/graph.js | 3 +-- js/id/core/history.js | 15 ++++++++------- js/id/presets/preset.js | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/js/id/core/graph.js b/js/id/core/graph.js index 793027b7c..8fb54e914 100644 --- a/js/id/core/graph.js +++ b/js/id/core/graph.js @@ -9,7 +9,7 @@ iD.Graph = function(other, mutable) { this.inherited = true; } else { - if (_.isArray(other)) { + if (Array.isArray(other)) { var entities = {}; for (var i = 0; i < other.length; i++) { entities[other[i].id] = other[i]; @@ -273,5 +273,4 @@ iD.Graph.prototype = { } return this; } - }; diff --git a/js/id/core/history.js b/js/id/core/history.js index cce3f3330..b95334eca 100644 --- a/js/id/core/history.js +++ b/js/id/core/history.js @@ -31,6 +31,7 @@ iD.History = function(context) { return difference; } + // iD uses namespaced keys so multiple installations do not conflict function getKey(n) { return 'iD_' + window.location.origin + '_' + n; } @@ -42,7 +43,6 @@ iD.History = function(context) { merge: function(entities) { - var base = stack[0].graph.base(), newentities = Object.keys(entities).filter(function(i) { return !base.entities[i]; @@ -192,11 +192,10 @@ iD.History = function(context) { if (stack.length <= 1) return; var json = JSON.stringify(stack.map(function(i) { - return { - annotation: i.annotation, - imagery_used: i.imagery_used, - entities: i.graph.entities - }; + var x = { entities: i.graph.entities }; + if (i.imagery_used) x.imagery_used = i.imagery_used; + if (i.annotation) x.annotation = i.annotation; + return x; }), function includeUndefined(key, value) { if (typeof value === 'undefined') return 'undefined'; return value; @@ -221,10 +220,13 @@ iD.History = function(context) { return lock; }, + // is iD not open in another window and it detects that + // there's a history stored in localStorage that's recoverable? restorableChanges: function() { return lock && !!context.storage(getKey('history')); }, + // load history from a version stored in localStorage load: function() { if (!lock) return; @@ -246,7 +248,6 @@ iD.History = function(context) { }); stack[0].graph.inherited = false; dispatch.change(); - }, _getKey: getKey diff --git a/js/id/presets/preset.js b/js/id/presets/preset.js index 64f61ab78..36efa9a89 100644 --- a/js/id/presets/preset.js +++ b/js/id/presets/preset.js @@ -17,7 +17,7 @@ iD.presets.Preset = function(preset, forms) { score = 0; for (var t in tags) { if (entity.tags[t] === tags[t]) { - score ++; + score++; } else if (tags[t] === '*' && t in entity.tags) { score += 0.5; } else {