Update to latest jsonld.js.
authorDave Longley <dlongley@digitalbazaar.com>
Mon, 11 Feb 2013 16:54:07 -0500
changeset 1224 fe73f1d5f48d
parent 1223 3c6b1e40d44c
child 1225 93e8f09bbb3e
Update to latest jsonld.js.
playground/jsonld.js
--- a/playground/jsonld.js	Mon Feb 11 16:53:59 2013 -0500
+++ b/playground/jsonld.js	Mon Feb 11 16:54:07 2013 -0500
@@ -497,7 +497,7 @@
           recurse.visited[subject['@id']] = true;
         }
 
-        // each array elementt *or* object key
+        // each array element *or* object key
         for(var k in subject) {
           var obj = subject[k];
           var isid = (jsonld.getContextValue(ctx, k, '@type') === '@id');
@@ -1448,14 +1448,14 @@
         if(_isString(expandedValue)) {
           compactedValue = _compactIri(
             activeCtx, expandedValue, null,
-            {base: true, vocab: (expandedProperty === '@type')});
+            {vocab: (expandedProperty === '@type')});
         }
         // expanded value must be a @type array
         else {
           compactedValue = [];
           for(var vi = 0; vi < expandedValue.length; ++vi) {
             compactedValue.push(_compactIri(
-              activeCtx, expandedValue[vi], null, {base: true, vocab: true}));
+              activeCtx, expandedValue[vi], null, {vocab: true}));
           }
         }
 
@@ -1486,9 +1486,10 @@
 
       // preserve empty arrays
       if(expandedValue.length === 0) {
-        var activeProperty = _compactIri(
-          activeCtx, expandedProperty, [], {vocab: true}, element);
-        jsonld.addValue(rval, activeProperty, [], {propertyIsArray: true});
+        var itemActiveProperty = _compactIri(
+          activeCtx, expandedProperty, expandedValue, {vocab: true}, element);
+        jsonld.addValue(
+          rval, itemActiveProperty, expandedValue, {propertyIsArray: true});
       }
 
       // recusively process array values
@@ -1496,18 +1497,18 @@
         var expandedItem = expandedValue[vi];
 
         // compact property and get container type
-        var activeProperty = _compactIri(
+        var itemActiveProperty = _compactIri(
           activeCtx, expandedProperty, expandedItem, {vocab: true}, element);
         var container = jsonld.getContextValue(
-          activeCtx, activeProperty, '@container');
+          activeCtx, itemActiveProperty, '@container');
 
         // remove any duplicates that were (presumably) generated by a
         // property generator
-        var mapping = activeCtx.mappings[activeProperty];
+        var mapping = activeCtx.mappings[itemActiveProperty];
         if(mapping && mapping.propertyGenerator) {
           _findPropertyGeneratorDuplicates(
-            activeCtx, element, expandedProperty, expandedItem, activeProperty,
-            true);
+            activeCtx, element, expandedProperty, expandedItem,
+            itemActiveProperty, true);
         }
 
         // get @list value if appropriate
@@ -1519,7 +1520,7 @@
 
         // recursively compact expanded item
         var compactedItem = this.compact(
-          activeCtx, activeProperty, isList ? list : expandedItem, options);
+          activeCtx, itemActiveProperty, isList ? list : expandedItem, options);
 
         // handle @list
         if(isList) {
@@ -1541,7 +1542,7 @@
             }
           }
           // can't use @list container for more than 1 list
-          else if(activeProperty in rval) {
+          else if(itemActiveProperty in rval) {
             throw new JsonLdError(
               'JSON-LD compact error; property has a "@list" @container ' +
               'rule but there is more than a single @list that matches ' +
@@ -1555,11 +1556,11 @@
         if(container === '@language' || container === '@index') {
           // get or create the map object
           var mapObject;
-          if(activeProperty in rval) {
-            mapObject = rval[activeProperty];
+          if(itemActiveProperty in rval) {
+            mapObject = rval[itemActiveProperty];
           }
           else {
-            rval[activeProperty] = mapObject = {};
+            rval[itemActiveProperty] = mapObject = {};
           }
 
           // if container is a language map, simplify compacted value to
@@ -1581,7 +1582,8 @@
 
           // add compact value
           jsonld.addValue(
-            rval, activeProperty, compactedItem, {propertyIsArray: isArray});
+            rval, itemActiveProperty, compactedItem,
+            {propertyIsArray: isArray});
         }
       }
     }
@@ -2519,18 +2521,17 @@
  *
  * @param namer the UniqueNamer to use.
  * @param element the element with blank nodes to rename.
- * @param isId true if the given element is an @id (or @type).
  *
  * @return the element.
  */
-function _labelBlankNodes(namer, element, isId) {
+function _labelBlankNodes(namer, element) {
   if(_isArray(element)) {
     for(var i = 0; i < element.length; ++i) {
-      element[i] = _labelBlankNodes(namer, element[i], isId);
+      element[i] = _labelBlankNodes(namer, element[i]);
     }
   }
   else if(_isList(element)) {
-    element['@list'] = _labelBlankNodes(namer, element['@list'], isId);
+    element['@list'] = _labelBlankNodes(namer, element['@list']);
   }
   else if(_isObject(element)) {
     // rename blank node
@@ -2543,14 +2544,10 @@
     for(var ki = 0; ki < keys.length; ++ki) {
       var key = keys[ki];
       if(key !== '@id') {
-        element[key] = _labelBlankNodes(namer, element[key], key === '@type');
+        element[key] = _labelBlankNodes(namer, element[key]);
       }
     }
   }
-  // rename blank node identifier
-  else if(_isString(element) && isId && element.indexOf('_:') === 0) {
-    element = namer.getName(element);
-  }
 
   return element;
 }
@@ -4203,17 +4200,15 @@
       var ids = id;
       for(var i = 0; i < ids.length; ++i) {
         id = ids[i];
-        if(!_isString(id)) {
+        if(!_isString(id) || id === '@type') {
           throw new JsonLdError(
             'Invalid JSON-LD syntax; property generators must consist of an ' +
-            '@id array containing only strings.',
+            '@id array containing only strings and no string can be "@type".',
             'jsonld.SyntaxError', {context: localCtx});
         }
-        // expand @id if it is not @type
-        if(id !== '@type') {
-          id = _expandIri(activeCtx, id, {base: true}, localCtx, defined);
-        }
-        propertyGenerator.push(id);
+        // expand @id
+        propertyGenerator.push(_expandIri(
+          activeCtx, id, {base: true}, localCtx, defined));
       }
       // add sorted property generator as @id in mapping
       mapping['@id'] = propertyGenerator.sort();
@@ -4226,11 +4221,9 @@
         'jsonld.SyntaxError', {context: localCtx});
     }
     else {
-      // add @id to mapping, expanding it if it is not @type
-      if(id !== '@type') {
-        id = _expandIri(activeCtx, id, {base: true}, localCtx, defined);
-      }
-      mapping['@id'] = id;
+      // add @id to mapping
+      mapping['@id'] = _expandIri(
+        activeCtx, id, {base: true}, localCtx, defined);
     }
   }
   else {
@@ -4580,6 +4573,8 @@
         continue;
       }
 
+      var container = mapping['@container'] || '@none';
+
       // iterate over every IRI in the mapping
       var ids = mapping['@id'];
       if(!_isArray(ids)) {
@@ -4594,9 +4589,6 @@
           inverse[iri] = entry = {};
         }
 
-        // add term selection where it applies
-        var container = mapping['@container'] || '@none';
-
         // add new entry
         if(!entry[container]) {
           entry[container] = {