Pass more WebIDL tests; update to latest jsonld.js.
authorDave Longley <dlongley@digitalbazaar.com>
Tue, 09 Apr 2013 12:44:28 -0400
changeset 1574 d3ec89ac50c3
parent 1573 83da380c21a7
child 1575 4e87cae7111a
Pass more WebIDL tests; update to latest jsonld.js.
playground/jsonld.js
test-suite/idltest/index.html
--- a/playground/jsonld.js	Tue Apr 09 18:29:35 2013 +0200
+++ b/playground/jsonld.js	Tue Apr 09 12:44:28 2013 -0400
@@ -68,6 +68,12 @@
   }
   options = options || {};
 
+  if(ctx === null) {
+    return callback(new JsonLdError(
+      'The compaction context must not be null.',
+      'jsonld.CompactError'));
+  }
+
   // nothing to compact
   if(input === null) {
     return callback(null, null);
@@ -175,25 +181,23 @@
       ctx = ctx[0];
     }
 
-    // add context
-    if(hasContext || options.graph) {
-      if(_isArray(compacted)) {
-        // use '@graph' keyword
-        var kwgraph = _compactIri(activeCtx, '@graph');
-        var graph = compacted;
-        compacted = {};
-        if(hasContext) {
-          compacted['@context'] = ctx;
-        }
-        compacted[kwgraph] = graph;
-      }
-      else if(_isObject(compacted)) {
-        // reorder keys so @context is first
-        var graph = compacted;
-        compacted = {'@context': ctx};
-        for(var key in graph) {
-          compacted[key] = graph[key];
-        }
+    // add context and/or @graph
+    if(_isArray(compacted)) {
+      // use '@graph' keyword
+      var kwgraph = _compactIri(activeCtx, '@graph');
+      var graph = compacted;
+      compacted = {};
+      if(hasContext) {
+        compacted['@context'] = ctx;
+      }
+      compacted[kwgraph] = graph;
+    }
+    else if(_isObject(compacted) && hasContext) {
+      // reorder keys so @context is first
+      var graph = compacted;
+      compacted = {'@context': ctx};
+      for(var key in graph) {
+        compacted[key] = graph[key];
       }
     }
 
@@ -714,37 +718,16 @@
 /* WebIDL API */
 
 function JsonLdProcessor() {};
-// callback param order unconventional w/WebIDL API
-JsonLdProcessor.prototype.expand = function(input, callback) {
-  var options = {};
-  if(arguments.length > 2) {
-    options = callback;
-    callback = arguments[2];
-  }
-  jsonld.expand(input, options, callback);
-};
-// callback param order unconventional w/WebIDL API
-JsonLdProcessor.prototype.compact = function(input, ctx, callback) {
-  var options = {};
-  if(arguments.length > 3) {
-    options = callback;
-    callback = arguments[3];
-  }
-  jsonld.compact(input, ctx, options, callback);
-};
-// callback param order unconventional w/WebIDL API
-JsonLdProcessor.prototype.flatten = function(input, ctx, callback) {
-  var options = {};
-  if(arguments.length > 3) {
-    options = callback;
-    callback = arguments[3];
-  }
-  jsonld.flatten(input, ctx, options, callback);
-};
+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;
+JsonLdProcessor.prototype.toString = function() {
+  return '[object JsonLdProcessor]';
+};
 jsonld.JsonLdProcessor = JsonLdProcessor;
 
 /* Utility API */
--- a/test-suite/idltest/index.html	Tue Apr 09 18:29:35 2013 +0200
+++ b/test-suite/idltest/index.html	Tue Apr 09 12:44:28 2013 -0400
@@ -28,9 +28,12 @@
 <pre id="idl">
 [Constructor]
 interface JsonLdProcessor {
-    void expand (JsonLdInput input, JsonLdCallback callback, optional JsonLdOptions? options);
-    void compact (JsonLdInput input, JsonLdContext? context, JsonLdCallback callback, optional JsonLdOptions? options);
-    void flatten (JsonLdInput input, JsonLdContext? context, JsonLdCallback callback, optional JsonLdOptions? options);
+    void expand (JsonLdInput input, JsonLdCallback callback);
+    void expand (JsonLdInput input, JsonLdOptions? options, JsonLdCallback callback);
+    void compact (JsonLdInput input, JsonLdContext? context, JsonLdCallback callback);
+    void compact (JsonLdInput input, JsonLdContext? context, JsonLdOptions? options, JsonLdCallback callback);
+    void flatten (JsonLdInput input, JsonLdContext? context, JsonLdCallback callback);
+    void flatten (JsonLdInput input, JsonLdContext? context, JsonLdOptions? options, JsonLdCallback callback);
 };
 
 typedef (object or object[] or DOMString) JsonLdInput;
@@ -98,6 +101,31 @@
 (function() {
   window.JsonLdProcessor = window.jsonld.JsonLdProcessor;
   window.processor = new JsonLdProcessor();
+  
+  // pass writable/enumerable tests  
+  if(Object.defineProperty) {
+    Object.defineProperty(JsonLdProcessor, 'prototype', {
+      writable: false,
+      enumerable: false
+    });
+    Object.defineProperty(window, 'JsonLdProcessor', {
+      writable: true,
+      enumerable: false
+    });
+  }
+  
+  // pass stringification tests
+  var toString = Object.prototype.toString;
+  Object.prototype.toString = function() {
+    if(this === window.JsonLdProcessor.prototype) {
+      return '[object JsonLdProcessorPrototype]';
+    }
+    if(this === window.processor) {
+      return window.processor.toString();
+    }
+    return toString.apply(this, arguments);
+  };
+  
   var idl_array = new IdlArray();
 
   //idl_array.add_untested_idls(document.getElementById("untested_idl").textContent);