Tweaks EscapeStore to restore based on index not metadata.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Thu, 16 Oct 2014 18:26:43 +0000 (19:26 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 7 Dec 2014 11:55:17 +0000 (11:55 +0000)
lib/text/escape-store.js
test/text/escape-store-test.js

index 32a158d..4da28d0 100644 (file)
@@ -5,10 +5,14 @@ var EscapeStore = function EscapeStore(placeholderRoot) {
   this.placeholderToData = {};
   this.dataToPlaceholder = {};
   this.count = 0;
+  this.restoreMatcher = new RegExp(this.placeholderRoot + '(\\d+)');
 };
 
 EscapeStore.prototype._nextPlaceholder = function (metadata) {
-  return placeholderBrace + this.placeholderRoot + (this.count++) + metadata + placeholderBrace;
+  return {
+    index: this.count,
+    value: placeholderBrace + this.placeholderRoot + this.count++ + metadata + placeholderBrace
+  };
 };
 
 EscapeStore.prototype.store = function (data, metadata) {
@@ -18,9 +22,10 @@ EscapeStore.prototype.store = function (data, metadata) {
   var placeholder = this.dataToPlaceholder[data];
 
   if (!placeholder) {
-    placeholder = this._nextPlaceholder(encodedMetadata);
-    this.placeholderToData[placeholder] = data;
-    this.dataToPlaceholder[data] = placeholder;
+    var nextPlaceholder = this._nextPlaceholder(encodedMetadata);
+    placeholder = nextPlaceholder.value;
+    this.placeholderToData[nextPlaceholder.index] = data;
+    this.dataToPlaceholder[data] = nextPlaceholder.value;
   }
 
   if (metadata)
@@ -41,7 +46,8 @@ EscapeStore.prototype.nextMatch = function (data, cursor) {
 };
 
 EscapeStore.prototype.restore = function (placeholder) {
-  return this.placeholderToData[placeholder];
+  var index = this.restoreMatcher.exec(placeholder)[1];
+  return this.placeholderToData[index];
 };
 
 module.exports = EscapeStore;
index d0ae788..bcdf592 100644 (file)
@@ -46,6 +46,18 @@ vows.describe(EscapeStore)
 
         assert.equal(placeholder, '__ESCAPED_TEST_CLEAN_CSS0(1,2,3)__');
       }
+    },
+    'with different metadata but same index': {
+      topic: function () {
+        var escapeStore = new EscapeStore('TEST');
+        escapeStore.store('data', ['brown', 'fox']);
+        escapeStore.store('data', ['jumped', 'over']);
+        return escapeStore;
+      },
+      restore: function (escapeStore) {
+        var data = escapeStore.restore('__ESCAPED_TEST_CLEAN_CSS0(a,lazy,dog)__');
+        assert.equal(data, 'data');
+      }
     }
   })
   .export(module);