Add latest jsonld.js. Update idltest page.
authorDave Longley <dlongley@digitalbazaar.com>
Thu, 04 Apr 2013 13:53:12 -0400
changeset 1538 4159183e173c
parent 1537 394709b48ee2
child 1539 e4fc7f542228
Add latest jsonld.js. Update idltest page.
playground/jsonld.js
test-suite/idltest/index.html
--- a/playground/jsonld.js	Thu Apr 04 17:21:02 2013 +0200
+++ b/playground/jsonld.js	Thu Apr 04 13:53:12 2013 -0400
@@ -66,6 +66,7 @@
     callback = options;
     options = {};
   }
+  options = options || {};
 
   // nothing to compact
   if(input === null) {
@@ -211,16 +212,13 @@
  *          [loadContext(url, callback(err, url, result))] the context loader.
  * @param callback(err, expanded) called once the operation completes.
  */
-jsonld.expand = function(input) {
+jsonld.expand = function(input, options, callback) {
   // get arguments
-  var options = {};
-  var callback;
-  var callbackArg = 1;
-  if(arguments.length > 2) {
-    options = arguments[1] || {};
-    callbackArg += 1;
-  }
-  callback = arguments[callbackArg];
+  if(typeof options === 'function') {
+    callback = options;
+    options = {};
+  }
+  options = options || {};
 
   // set default options
   if(!('base' in options)) {
@@ -282,6 +280,7 @@
     callback = options;
     options = {};
   }
+  options = options || {};
 
   // set default options
   if(!('base' in options)) {
@@ -338,15 +337,13 @@
  *          [loadContext(url, callback(err, url, result))] the context loader.
  * @param callback(err, framed) called once the operation completes.
  */
-jsonld.frame = function(input, frame) {
+jsonld.frame = function(input, frame, options, callback) {
   // get arguments
-  var options = {};
-  var callbackArg = 2;
-  if(arguments.length > 3) {
-    options = arguments[2] || {};
-    callbackArg += 1;
-  }
-  var callback = arguments[callbackArg];
+  if(typeof options === 'function') {
+    callback = options;
+    options = {};
+  }
+  options = options || {};
 
   // set default options
   if(!('base' in options)) {
@@ -419,15 +416,13 @@
  *          [loadContext(url, callback(err, url, result))] the context loader.
  * @param callback(err, objectified) called once the operation completes.
  */
-jsonld.objectify = function(input, ctx) {
+jsonld.objectify = function(input, ctx, options, callback) {
   // get arguments
-  var options = {};
-  var callbackArg = 2;
-  if(arguments.length > 3) {
-    options = arguments[2] || {};
-    callbackArg += 1;
-  }
-  var callback = arguments[callbackArg];
+  if(typeof options === 'function') {
+    callback = options;
+    options = {};
+  }
+  options = options || {};
 
   // set default options
   if(!('base' in options)) {
@@ -716,6 +711,18 @@
     'jsonld.ContextUrlError'), url);
 };
 
+/* WebIDL API */
+
+function JsonLdProcessor() {};
+JsonLdProcessor.prototype.expand = jsonld.expand;
+JsonLdProcessor.prototype.compact = jsonld.compact;
+JsonLdProcessor.prototype.flatten = jsonld.flatten;
+JsonLdProcessor.prototype.frame = jsonld.frame;
+JsonLdProcessor.prototype.fromRDF = jsonld.fromRDF;
+JsonLdProcessor.prototype.toRDF = jsonld.toRDF;
+JsonLdProcessor.prototype.normalize = jsonld.normalize;
+jsonld.JsonLdProcessor = JsonLdProcessor;
+
 /* Utility API */
 
 // define nextTick
@@ -780,8 +787,7 @@
   var key2 = JSON.stringify(localCtx);
   var level1 = this.cache[key1];
   if(level1 && key2 in level1) {
-    // get shareable copy of cached active context
-    return level1[key2].share();
+    return level1[key2];
   }
   return null;
 };
@@ -989,7 +995,8 @@
 
   // retrieve URLs in localCtx
   localCtx = _clone(localCtx);
-  if(_isObject(localCtx) && !('@context' in localCtx)) {
+  if(_isString(localCtx) ||
+    (_isObject(localCtx) && !('@context' in localCtx))) {
     localCtx = {'@context': localCtx};
   }
   _retrieveContextUrls(localCtx, options, function(err, ctx) {
@@ -3263,8 +3270,15 @@
       continue;
     }
 
-    // iterate over objects (ensure property is added for empty arrays)
+    // iterate over objects
     var objects = input[property];
+
+    // if property is a bnode, assign it a new id
+    if(property.indexOf('_:') === 0) {
+      property = namer.getName(property);
+    }
+
+    // ensure property is added for empty arrays
     if(objects.length === 0) {
       jsonld.addValue(subject, property, [], {propertyIsArray: true});
       continue;
@@ -3802,9 +3816,8 @@
   }
   prefs.push('@none');
 
-  var term = null;
   var containerMap = activeCtx.inverse[iri];
-  for(var ci = 0; term === null && ci < containers.length; ++ci) {
+  for(var ci = 0; ci < containers.length; ++ci) {
     // if container not available in the map, continue
     var container = containers[ci];
     if(!(container in containerMap)) {
@@ -3812,7 +3825,7 @@
     }
 
     var typeOrLanguageValueMap = containerMap[container][typeOrLanguage];
-    for(var pi = 0; term === null && pi < prefs.length; ++pi) {
+    for(var pi = 0; pi < prefs.length; ++pi) {
       // if type/language option not available in the map, continue
       var pref = prefs[pi];
       if(!(pref in typeOrLanguageValueMap)) {
@@ -3820,11 +3833,11 @@
       }
 
       // select term
-      term = typeOrLanguageValueMap[pref];
-    }
-  }
-
-  return term;
+      return typeOrLanguageValueMap[pref];
+    }
+  }
+
+  return null;
 }
 
 /**
@@ -3835,7 +3848,6 @@
  * @param iri the IRI to compact.
  * @param value the value to check or null.
  * @param relativeTo options for how to compact IRIs:
- *          base: true to resolve against the base IRI, false not to.
  *          vocab: true to split after @vocab, false not to.
  * @param reverse true if a reverse property is being compacted, false if not.
  *
@@ -3964,7 +3976,22 @@
     }
   }
 
-  // no term match, check for possible CURIEs
+  // no term match, use @vocab if available
+  if(relativeTo.vocab) {
+    if('@vocab' in activeCtx) {
+      // determine if vocab is a prefix of the iri
+      var vocab = activeCtx['@vocab'];
+      if(iri.indexOf(vocab) === 0 && iri !== vocab) {
+        // use suffix as relative iri if it is not a term in the active context
+        var suffix = iri.substr(vocab.length);
+        if(!(suffix in activeCtx.mappings)) {
+          return suffix;
+        }
+      }
+    }
+  }
+
+  // no term or @vocab match, check for possible CURIEs
   var choice = null;
   for(var term in activeCtx.mappings) {
     // skip terms with colons, they can't be prefixes
@@ -4000,22 +4027,8 @@
     return choice;
   }
 
-  // no matching terms or curies, use @vocab if available
-  if(relativeTo.vocab) {
-    if('@vocab' in activeCtx) {
-      // determine if vocab is a prefix of the iri
-      var vocab = activeCtx['@vocab'];
-      if(iri.indexOf(vocab) === 0 && iri !== vocab) {
-        // use suffix as relative iri if it is not a term in the active context
-        var suffix = iri.substr(vocab.length);
-        if(!(suffix in activeCtx.mappings)) {
-          return suffix;
-        }
-      }
-    }
-  }
   // compact IRI relative to base
-  else {
+  if(!relativeTo.vocab) {
     return _removeBase(activeCtx['@base'], iri);
   }
 
@@ -4360,17 +4373,19 @@
 
     // do not expand blank nodes (prefix of '_') or already-absolute
     // IRIs (suffix of '//')
-    if(prefix !== '_' && suffix.indexOf('//') !== 0) {
-      // prefix dependency not defined, define it
-      if(localCtx && prefix in localCtx) {
-        _createTermDefinition(activeCtx, localCtx, prefix, defined);
-      }
-
-      // use mapping if prefix is defined
-      var mapping = activeCtx.mappings[prefix];
-      if(mapping) {
-        return mapping['@id'] + suffix;
-      }
+    if(prefix === '_' || suffix.indexOf('//') === 0) {
+      return value;
+    }
+
+    // prefix dependency not defined, define it
+    if(localCtx && prefix in localCtx) {
+      _createTermDefinition(activeCtx, localCtx, prefix, defined);
+    }
+
+    // use mapping if prefix is defined
+    var mapping = activeCtx.mappings[prefix];
+    if(mapping) {
+      return mapping['@id'] + suffix;
     }
 
     // already absolute IRI
@@ -4560,8 +4575,7 @@
     mappings: {},
     inverse: null,
     getInverse: _createInverseContext,
-    clone: _cloneActiveContext,
-    share: _shareActiveContext
+    clone: _cloneActiveContext
   };
 
   /**
@@ -4671,7 +4685,6 @@
     child['@base'] = this['@base'];
     child.mappings = _clone(this.mappings);
     child.clone = this.clone;
-    child.share = this.share;
     child.inverse = null;
     child.getInverse = this.getInverse;
     if('@language' in this) {
@@ -4682,30 +4695,6 @@
     }
     return child;
   }
-
-  /**
-   * Returns a copy of this active context that can be shared between
-   * different processing algorithms. This method only copies the parts
-   * of the active context that can't be shared.
-   *
-   * @return a shareable copy of this active context.
-   */
-  function _shareActiveContext() {
-    var rval = {};
-    rval['@base'] = this['@base'];
-    rval.mappings = this.mappings;
-    rval.clone = this.clone;
-    rval.share = this.share;
-    rval.inverse = this.inverse;
-    rval.getInverse = this.getInverse;
-    if('@language' in this) {
-      rval['@language'] = this['@language'];
-    }
-    if('@vocab' in this) {
-      rval['@vocab'] = this['@vocab'];
-    }
-    return rval;
-  }
 }
 
 /**
--- a/test-suite/idltest/index.html	Thu Apr 04 17:21:02 2013 +0200
+++ b/test-suite/idltest/index.html	Thu Apr 04 13:53:12 2013 -0400
@@ -21,17 +21,17 @@
 <h1>idlharness test</h1>
 <p>This test validates the WebIDL included in the JSON-LD 1.0 Processing Algorithms and API specification.</p>
 
-<pre id='untested_idl' style='display:none'>
+<pre id="untested_idl" style="display:none">
 interface Window {
     JsonLdProcessor jsonld;
 };
 </pre>
 
-<pre id='idl'>
+<pre id="idl">
 interface JsonLdProcessor {
-    void expand ((object or object[] or DOMString) input, JsonLdCallback callback, optional JsonLdOptions? options);
-    void compact ((object or object[] or DOMString) input, (object or DOMString)? context, JsonLdCallback callback, optional JsonLdOptions? options);
-    void flatten ((object or object[] or DOMString) input, (object or DOMString)? context, JsonLdCallback callback, optional JsonLdOptions? options);
+    void expand ((object or object[] or DOMString) input, optional JsonLdOptions? options, JsonLdCallback callback);
+    void compact ((object or object[] or DOMString) input, (object or DOMString)? context, optional JsonLdOptions? options, JsonLdCallback callback);
+    void flatten ((object or object[] or DOMString) input, (object or DOMString)? context, optional JsonLdOptions? options, JsonLdCallback callback);
 };
 
 callback JsonLdCallback = void (JsonLdError error, (object or object[]) document);
@@ -94,17 +94,15 @@
 <script>
 
 (function() {
+  window.JsonLdProcessor = window.jsonld.JsonLdProcessor;
+  window.jsonldProcessor = new JsonLdProcessor();
   var idl_array = new IdlArray();
-
   //idl_array.add_untested_idls(document.getElementById("untested_idl").textContent);
-  idl_array.add_idls(document.getElementById("idl").textContent);
-
-  idl_array.add_objects( { JsonLdProcessor: ["window.jsonld"] } );
-
+  idl_array.add_idls(document.getElementById('idl').textContent);
+  idl_array.add_objects({JsonLdProcessor: ['window.jsonldProcessor']});
   idl_array.test();
 })();
 
-
 </script>
 
 <div id="log"></div>