From 846d637aa7512ce28665a029126c9c68ead00bf3 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 16 May 2014 12:37:02 -0400 Subject: [PATCH 1/3] Trim tag keys, and prevent duplicate tag keys (#2043) --- js/id/ui/raw_tag_editor.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/js/id/ui/raw_tag_editor.js b/js/id/ui/raw_tag_editor.js index 92fdc45be..643fe274f 100644 --- a/js/id/ui/raw_tag_editor.js +++ b/js/id/ui/raw_tag_editor.js @@ -162,10 +162,22 @@ iD.ui.RawTagEditor = function(context) { } function keyChange(d) { - var tag = {}; - tag[d.key] = undefined; - tag[this.value] = d.value; - d.key = this.value; // Maintain DOM identity through the subsequent update. + var kOld = d.key.trim(), + kNew = this.value.trim(), + tag = {}; + + if (kNew && kNew !== kOld) { + var a = kNew.split(/_(\d+)$/), + base = a[0], + suffix = (a.length > 1) ? parseInt(a[1]): 1; + while (tags[kNew]) { // rename key if already in use + kNew = base + '_' + suffix++; + } + } + tag[kOld] = undefined; + tag[kNew] = d.value; + d.key = kNew; // Maintain DOM identity through the subsequent update. + this.value = kNew; event.change(tag); } From 22c71bffca366fb22c2ff9ae49e211797c6ebc83 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 16 May 2014 16:31:34 -0400 Subject: [PATCH 2/3] Update regexp, and remove trim from old key.. --- js/id/ui/raw_tag_editor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/id/ui/raw_tag_editor.js b/js/id/ui/raw_tag_editor.js index 643fe274f..17645406c 100644 --- a/js/id/ui/raw_tag_editor.js +++ b/js/id/ui/raw_tag_editor.js @@ -162,12 +162,12 @@ iD.ui.RawTagEditor = function(context) { } function keyChange(d) { - var kOld = d.key.trim(), + var kOld = d.key, kNew = this.value.trim(), tag = {}; if (kNew && kNew !== kOld) { - var a = kNew.split(/_(\d+)$/), + var a = _.compact(kNew.split(/^(.*)_(\d+)$/)), base = a[0], suffix = (a.length > 1) ? parseInt(a[1]): 1; while (tags[kNew]) { // rename key if already in use From f9520362c9759a3e18bd123a331d0708bb7235de Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Sun, 18 May 2014 12:35:22 -0700 Subject: [PATCH 3/3] Use match rather than split --- js/id/ui/raw_tag_editor.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/id/ui/raw_tag_editor.js b/js/id/ui/raw_tag_editor.js index 17645406c..22b795bc3 100644 --- a/js/id/ui/raw_tag_editor.js +++ b/js/id/ui/raw_tag_editor.js @@ -167,9 +167,9 @@ iD.ui.RawTagEditor = function(context) { tag = {}; if (kNew && kNew !== kOld) { - var a = _.compact(kNew.split(/^(.*)_(\d+)$/)), - base = a[0], - suffix = (a.length > 1) ? parseInt(a[1]): 1; + var match = kNew.match(/^(.*?)(?:_(\d+))?$/), + base = match[1], + suffix = +(match[2] || 1); while (tags[kNew]) { // rename key if already in use kNew = base + '_' + suffix++; }