In createNameArray, check for a compomenet with all periods.
Added to_uri.
diff --git a/js/Name.js b/js/Name.js
index fa28fed..9a3cfbb 100644
--- a/js/Name.js
+++ b/js/Name.js
@@ -1,7 +1,7 @@
/*
* @author: ucla-cs
* See COPYING for copyright and distribution information.
- * This class represents a Name
+ * This class represents a Name as an array of components where each is a byte array.
*/
@@ -58,6 +58,11 @@
Name.createNameArray=function(name) {
var array = name.split('/');
+ var colonIndex = array[0].indexOf(':');
+ if (colonIndex >= 0) {
+ name = name.substr(colonIndex + 1, name.length - colonIndex - 1);
+ array = name.split('/');
+ }
if(name[0]=="/")
array=array.slice(1,array.length);
@@ -65,8 +70,19 @@
array=array.slice(0,array.length-1);
// Unescape the components.
- for (var i = 0; i < array.length; ++i)
- array[i] = unescape(array[i]);
+ for (var i = 0; i < array.length; ++i) {
+ var component = unescape(array[i]);
+
+ if (component.match(/[^.]/) == null) {
+ // Special case for component of only periods. Remove 3 periods.
+ if (component.length <= 3)
+ array[i] = "";
+ else
+ array[i] = component.substr(3, component.length - 3);
+ }
+ else
+ array[i] = component;
+ }
return array;
}
@@ -106,3 +122,46 @@
return this.components.push(param);
};
+// Return the escaped name string according to "CCNx URI Scheme". Does not include "ccnx:".
+Name.prototype.to_uri = function() {
+ var result = "";
+
+ for(var i = 0; i < this.components.length; ++i)
+ result += "/"+ Name.toEscapedString(this.components[i]);
+
+ return result;
+};
+
+/**
+ * Return component as an escaped string according to "CCNx URI Scheme".
+ * We can't use encodeURIComponent because that doesn't encode all the characters we want to.
+ */
+Name.toEscapedString = function(component) {
+ var result = "";
+ var gotNonDot = false;
+ for (var i = 0; i < component.length; ++i) {
+ if (component[i] != 0x2e) {
+ gotNonDot = true;
+ break;
+ }
+ }
+ if (!gotNonDot) {
+ // Special case for component of zero or more periods. Add 3 periods.
+ result = "...";
+ for (var i = 0; i < component.length; ++i)
+ result += ".";
+ }
+ else {
+ for (var i = 0; i < component.length; ++i) {
+ var value = component[i];
+ // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
+ if (value >= 0x30 && value <= 0x39 || value >= 0x41 && value <= 0x5a ||
+ value >= 0x61 && value <= 0x7a || value == 0x2b || value == 0x2d ||
+ value == 0x2e || value == 0x5f)
+ result += String.fromCharCode(value);
+ else
+ result += "%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase();
+ }
+ }
+ return result;
+};