Added a sandbox area to the JSON-LD website.
authorManu Sporny <msporny@digitalbazaar.com>
Tue, 05 Jul 2011 22:24:05 -0400
changeset 48 982ac1af096c
parent 47 84bb477f5c6d
child 50 900839f91c79
Added a sandbox area to the JSON-LD website.
sandbox/bg_button.gif
sandbox/bg_button_span.gif
sandbox/index.html
sandbox/js_beautify.js
sandbox/jsonld.js
sandbox/sandbox.css
sandbox/sandbox.js
Binary file sandbox/bg_button.gif has changed
Binary file sandbox/bg_button_span.gif has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sandbox/index.html	Tue Jul 05 22:24:05 2011 -0400
@@ -0,0 +1,96 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
+ "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"> 
+<html version="XHTML+RDFa 1.0" xmlns="http://www.w3.org/1999/xhtml"
+   xmlns:xhv="http://www.w3.org/1999/xhtml/vocab#"
+   xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
+   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
+   xmlns:dcterms="http://purl.org/dc/terms/"
+   xmlns:vcard="http://www.w3.org/2006/vcard/ns#"
+   xmlns:v="http://rdf.data-vocabulary.org/#"
+   > 
+   <head> 
+      <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
+      <title>The JSON-LD Sandbox</title> 
+      <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" type="text/css"/>
+      <link rel="stylesheet" href="../site.css" type="text/css" /> 
+      <link rel="stylesheet" href="sandbox.css" type="text/css" /> 
+      <link rel="shortcut icon" href="favicon.ico" /> 
+      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
+      <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
+      <script type="text/javascript" src="js_beautify.js"></script>
+      <script type="text/javascript" src="jsonld.js"></script>
+      <script type="text/javascript" src="sandbox.js"></script>
+   </head> 
+ 
+   <body onload="sandbox.init();"> 
+      <div id="container"> 
+         <div id="header"> 
+            <div class="col"> 
+               <h1>The JSON-LD Sandbox</h1>
+            </div> 
+         </div> 
+ 
+         <div id="content"> 
+            <div id="info"> 
+               <p>Play around with JSON-LD markup by typing out some JSON below
+               and seeing what gets generated from it at the bottom of the page. 
+               Pick any of the examples below to get started.
+            </div>
+
+            <ul id="examples">
+              <li class="button-list">Examples:</li>
+              <li class="button">
+                 <span onclick="sandbox.populate('Person')">Person</span>
+              </li>
+              <li class="button">
+                 <span onclick="sandbox.populate('Place')">Place</span>
+              </li>
+              <li class="button">
+                 <span onclick="sandbox.populate('Event')">Event</span>
+              </li>
+              <li class="button">
+                 <span onclick="sandbox.populate('Product')">Product</span>
+              </li>
+              <li class="button">
+                 <span onclick="sandbox.populate('Recipe')">Recipe</span>
+              </li>
+            </ul>
+
+            <textarea id="markup"
+               placeholder="Enter your JSON-LD markup here..."
+               onkeyup="sandbox.process()"></textarea>
+
+            <div id="tabs">
+                <ul>
+                    <li><a href="#turtle"><span>TURTLE</span></a></li>
+                    <li><a href="#compacted"><span>Compacted</span></a></li>
+                    <li><a href="#expanded"><span>Expanded</span></a></li>
+                    <li><a href="#normalized"><span>Normalized</span></a></li>
+                    <li><a href="#framed"><span>Framed</span></a></li>
+                </ul>
+                <div id="turtle">NOT IMPLEMENTED
+                </div>
+                <pre id="compacted">
+                </pre>
+                <pre id="expanded">
+                </pre>
+                <pre id="normalized">
+                </pre>
+                <div id="framed">NOT IMPLEMENTED
+                </div>
+            </div>
+
+         </div>
+ 
+         <div id="footer"> 
+            <p id="copyright"> 
+               Website content released under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution Share-Alike license</a> except where an alternate is specified.
+            </p> 
+            <p id="legal"> 
+               Part of the <a href="http://payswarm.com/">payswarm.com</a> initiative.
+            </p>
+         </div> 
+      </div> 
+   </body> 
+</html>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sandbox/js_beautify.js	Tue Jul 05 22:24:05 2011 -0400
@@ -0,0 +1,1170 @@
+/*jslint onevar: false, plusplus: false */
+/*
+
+ JS Beautifier
+---------------
+
+
+  Written by Einar Lielmanis, <einar@jsbeautifier.org>
+      http://jsbeautifier.org/
+
+  Originally converted to javascript by Vital, <vital76@gmail.com>
+  "End braces on own line" added by Chris J. Shull, <chrisjshull@gmail.com>
+
+  You are free to use this in any way you want, in case you find this useful or working for you.
+
+  Usage:
+    js_beautify(js_source_text);
+    js_beautify(js_source_text, options);
+
+  The options are:
+    indent_size (default 4)          — indentation size,
+    indent_char (default space)      — character to indent with,
+    preserve_newlines (default true) — whether existing line breaks should be preserved,
+    preserve_max_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk,
+    indent_level (default 0)         — initial indentation level, you probably won't need this ever,
+
+    jslint_happy (default false) — if true, then jslint-stricter mode is enforced.
+
+            jslint_happy   !jslint_happy
+            ---------------------------------
+             function ()      function()
+
+    brace_style (default "collapse") - "collapse" | "expand" | "end-expand"
+            put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line.
+
+    e.g
+
+    js_beautify(js_source_text, {
+      'indent_size': 1,
+      'indent_char': '\t'
+    });
+
+
+*/
+
+function js_beautify(js_source_text, options) {
+
+    var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string;
+    var whitespace, wordchar, punct, parser_pos, line_starters, digits;
+    var prefix, token_type, do_block_just_closed;
+    var wanted_newline, just_added_newline, n_newlines;
+
+
+    // Some interpreters have unexpected results with foo = baz || bar;
+    options = options ? options : {};
+
+    var opt_brace_style;
+
+    // compatibility
+    if (options.space_after_anon_function !== undefined && options.jslint_happy === undefined) {
+        options.jslint_happy = options.space_after_anon_function;
+    }
+    if (options.braces_on_own_line !== undefined) { //graceful handling of depricated option
+        opt_brace_style = options.braces_on_own_line ? "expand" : "collapse";
+    }
+    opt_brace_style = options.brace_style ? options.brace_style : (opt_brace_style ? opt_brace_style : "collapse");
+
+
+    var opt_indent_size = options.indent_size ? options.indent_size : 4;
+    var opt_indent_char = options.indent_char ? options.indent_char : ' ';
+    var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
+    var opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines;
+    var opt_indent_level = options.indent_level ? options.indent_level : 0; // starting indentation
+    var opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy;
+    var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation;
+
+    just_added_newline = false;
+
+    // cache the source's length.
+    var input_length = js_source_text.length;
+
+    function trim_output(eat_newlines) {
+        eat_newlines = typeof eat_newlines === 'undefined' ? false : eat_newlines;
+        while (output.length && (output[output.length - 1] === ' '
+            || output[output.length - 1] === indent_string
+            || (eat_newlines && (output[output.length - 1] === '\n' || output[output.length - 1] === '\r')))) {
+            output.pop();
+        }
+    }
+
+    function trim(s) {
+        return s.replace(/^\s\s*|\s\s*$/, '');
+    }
+
+    function force_newline()
+    {
+        var old_keep_array_indentation = opt_keep_array_indentation;
+        opt_keep_array_indentation = false;
+        print_newline()
+        opt_keep_array_indentation = old_keep_array_indentation;
+    }
+
+    function print_newline(ignore_repeated) {
+
+        flags.eat_next_space = false;
+        if (opt_keep_array_indentation && is_array(flags.mode)) {
+            return;
+        }
+
+        ignore_repeated = typeof ignore_repeated === 'undefined' ? true : ignore_repeated;
+
+        flags.if_line = false;
+        trim_output();
+
+        if (!output.length) {
+            return; // no newline on start of file
+        }
+
+        if (output[output.length - 1] !== "\n" || !ignore_repeated) {
+            just_added_newline = true;
+            output.push("\n");
+        }
+        for (var i = 0; i < flags.indentation_level + opt_indent_level; i += 1) {
+            output.push(indent_string);
+        }
+        if (flags.var_line && flags.var_line_reindented) {
+            if (opt_indent_char === ' ') {
+                output.push('    '); // var_line always pushes 4 spaces, so that the variables would be one under another
+            } else {
+                output.push(indent_string); // skip space-stuffing, if indenting with a tab
+            }
+        }
+    }
+
+
+
+    function print_single_space() {
+        if (flags.eat_next_space) {
+            flags.eat_next_space = false;
+            return;
+        }
+        var last_output = ' ';
+        if (output.length) {
+            last_output = output[output.length - 1];
+        }
+        if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
+            output.push(' ');
+        }
+    }
+
+
+    function print_token() {
+        just_added_newline = false;
+        flags.eat_next_space = false;
+        output.push(token_text);
+    }
+
+    function indent() {
+        flags.indentation_level += 1;
+    }
+
+
+    function remove_indent() {
+        if (output.length && output[output.length - 1] === indent_string) {
+            output.pop();
+        }
+    }
+
+    function set_mode(mode) {
+        if (flags) {
+            flag_store.push(flags);
+        }
+        flags = {
+            previous_mode: flags ? flags.mode : 'BLOCK',
+            mode: mode,
+            var_line: false,
+            var_line_tainted: false,
+            var_line_reindented: false,
+            in_html_comment: false,
+            if_line: false,
+            in_case: false,
+            eat_next_space: false,
+            indentation_baseline: -1,
+            indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0),
+            ternary_depth: 0
+        };
+    }
+
+    function is_array(mode) {
+        return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]';
+    }
+
+    function is_expression(mode) {
+        return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]' || mode === '(EXPRESSION)';
+    }
+
+    function restore_mode() {
+        do_block_just_closed = flags.mode === 'DO_BLOCK';
+        if (flag_store.length > 0) {
+            flags = flag_store.pop();
+        }
+    }
+
+    function all_lines_start_with(lines, c) {
+        for (var i = 0; i < lines.length; i++) {
+            if (trim(lines[i])[0] != c) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    function in_array(what, arr) {
+        for (var i = 0; i < arr.length; i += 1) {
+            if (arr[i] === what) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    function get_next_token() {
+        n_newlines = 0;
+
+        if (parser_pos >= input_length) {
+            return ['', 'TK_EOF'];
+        }
+
+        wanted_newline = false;
+
+        var c = input.charAt(parser_pos);
+        parser_pos += 1;
+
+
+        var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode);
+
+        if (keep_whitespace) {
+
+            //
+            // slight mess to allow nice preservation of array indentation and reindent that correctly
+            // first time when we get to the arrays:
+            // var a = [
+            // ....'something'
+            // we make note of whitespace_count = 4 into flags.indentation_baseline
+            // so we know that 4 whitespaces in original source match indent_level of reindented source
+            //
+            // and afterwards, when we get to
+            //    'something,
+            // .......'something else'
+            // we know that this should be indented to indent_level + (7 - indentation_baseline) spaces
+            //
+            var whitespace_count = 0;
+
+            while (in_array(c, whitespace)) {
+
+                if (c === "\n") {
+                    trim_output();
+                    output.push("\n");
+                    just_added_newline = true;
+                    whitespace_count = 0;
+                } else {
+                    if (c === '\t') {
+                        whitespace_count += 4;
+                    } else if (c === '\r') {
+                        // nothing
+                    } else {
+                        whitespace_count += 1;
+                    }
+                }
+
+                if (parser_pos >= input_length) {
+                    return ['', 'TK_EOF'];
+                }
+
+                c = input.charAt(parser_pos);
+                parser_pos += 1;
+
+            }
+            if (flags.indentation_baseline === -1) {
+                flags.indentation_baseline = whitespace_count;
+            }
+
+            if (just_added_newline) {
+                var i;
+                for (i = 0; i < flags.indentation_level + 1; i += 1) {
+                    output.push(indent_string);
+                }
+                if (flags.indentation_baseline !== -1) {
+                    for (i = 0; i < whitespace_count - flags.indentation_baseline; i++) {
+                        output.push(' ');
+                    }
+                }
+            }
+
+        } else {
+            while (in_array(c, whitespace)) {
+
+                if (c === "\n") {
+                    n_newlines += ( (opt_max_preserve_newlines) ? (n_newlines <= opt_max_preserve_newlines) ? 1: 0: 1 );
+                }
+
+
+                if (parser_pos >= input_length) {
+                    return ['', 'TK_EOF'];
+                }
+
+                c = input.charAt(parser_pos);
+                parser_pos += 1;
+
+            }
+
+            if (opt_preserve_newlines) {
+                if (n_newlines > 1) {
+                    for (i = 0; i < n_newlines; i += 1) {
+                        print_newline(i === 0);
+                        just_added_newline = true;
+                    }
+                }
+            }
+            wanted_newline = n_newlines > 0;
+        }
+
+
+        if (in_array(c, wordchar)) {
+            if (parser_pos < input_length) {
+                while (in_array(input.charAt(parser_pos), wordchar)) {
+                    c += input.charAt(parser_pos);
+                    parser_pos += 1;
+                    if (parser_pos === input_length) {
+                        break;
+                    }
+                }
+            }
+
+            // small and surprisingly unugly hack for 1E-10 representation
+            if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) {
+
+                var sign = input.charAt(parser_pos);
+                parser_pos += 1;
+
+                var t = get_next_token(parser_pos);
+                c += sign + t[0];
+                return [c, 'TK_WORD'];
+            }
+
+            if (c === 'in') { // hack for 'in' operator
+                return [c, 'TK_OPERATOR'];
+            }
+            if (wanted_newline && last_type !== 'TK_OPERATOR'
+                && last_type !== 'TK_EQUALS'
+                && !flags.if_line && (opt_preserve_newlines || last_text !== 'var')) {
+                print_newline();
+            }
+            return [c, 'TK_WORD'];
+        }
+
+        if (c === '(' || c === '[') {
+            return [c, 'TK_START_EXPR'];
+        }
+
+        if (c === ')' || c === ']') {
+            return [c, 'TK_END_EXPR'];
+        }
+
+        if (c === '{') {
+            return [c, 'TK_START_BLOCK'];
+        }
+
+        if (c === '}') {
+            return [c, 'TK_END_BLOCK'];
+        }
+
+        if (c === ';') {
+            return [c, 'TK_SEMICOLON'];
+        }
+
+        if (c === '/') {
+            var comment = '';
+            // peek for comment /* ... */
+            var inline_comment = true;
+            if (input.charAt(parser_pos) === '*') {
+                parser_pos += 1;
+                if (parser_pos < input_length) {
+                    while (! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/') && parser_pos < input_length) {
+                        c = input.charAt(parser_pos);
+                        comment += c;
+                        if (c === '\x0d' || c === '\x0a') {
+                            inline_comment = false;
+                        }
+                        parser_pos += 1;
+                        if (parser_pos >= input_length) {
+                            break;
+                        }
+                    }
+                }
+                parser_pos += 2;
+                if (inline_comment) {
+                    return ['/*' + comment + '*/', 'TK_INLINE_COMMENT'];
+                } else {
+                    return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
+                }
+            }
+            // peek for comment // ...
+            if (input.charAt(parser_pos) === '/') {
+                comment = c;
+                while (input.charAt(parser_pos) !== '\r' && input.charAt(parser_pos) !== '\n') {
+                    comment += input.charAt(parser_pos);
+                    parser_pos += 1;
+                    if (parser_pos >= input_length) {
+                        break;
+                    }
+                }
+                parser_pos += 1;
+                if (wanted_newline) {
+                    print_newline();
+                }
+                return [comment, 'TK_COMMENT'];
+            }
+
+        }
+
+        if (c === "'" || // string
+        c === '"' || // string
+        (c === '/' &&
+            ((last_type === 'TK_WORD' && in_array(last_text, ['return', 'do'])) ||
+                (last_type === 'TK_COMMENT' || last_type === 'TK_START_EXPR' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EQUALS' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
+            var sep = c;
+            var esc = false;
+            var resulting_string = c;
+
+            if (parser_pos < input_length) {
+                if (sep === '/') {
+                    //
+                    // handle regexp separately...
+                    //
+                    var in_char_class = false;
+                    while (esc || in_char_class || input.charAt(parser_pos) !== sep) {
+                        resulting_string += input.charAt(parser_pos);
+                        if (!esc) {
+                            esc = input.charAt(parser_pos) === '\\';
+                            if (input.charAt(parser_pos) === '[') {
+                                in_char_class = true;
+                            } else if (input.charAt(parser_pos) === ']') {
+                                in_char_class = false;
+                            }
+                        } else {
+                            esc = false;
+                        }
+                        parser_pos += 1;
+                        if (parser_pos >= input_length) {
+                            // incomplete string/rexp when end-of-file reached.
+                            // bail out with what had been received so far.
+                            return [resulting_string, 'TK_STRING'];
+                        }
+                    }
+
+                } else {
+                    //
+                    // and handle string also separately
+                    //
+                    while (esc || input.charAt(parser_pos) !== sep) {
+                        resulting_string += input.charAt(parser_pos);
+                        if (!esc) {
+                            esc = input.charAt(parser_pos) === '\\';
+                        } else {
+                            esc = false;
+                        }
+                        parser_pos += 1;
+                        if (parser_pos >= input_length) {
+                            // incomplete string/rexp when end-of-file reached.
+                            // bail out with what had been received so far.
+                            return [resulting_string, 'TK_STRING'];
+                        }
+                    }
+                }
+
+
+
+            }
+
+            parser_pos += 1;
+
+            resulting_string += sep;
+
+            if (sep === '/') {
+                // regexps may have modifiers /regexp/MOD , so fetch those, too
+                while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) {
+                    resulting_string += input.charAt(parser_pos);
+                    parser_pos += 1;
+                }
+            }
+            return [resulting_string, 'TK_STRING'];
+        }
+
+        if (c === '#') {
+
+
+            if (output.length === 0 && input.charAt(parser_pos) === '!') {
+                // shebang
+                resulting_string = c;
+                while (parser_pos < input_length && c != '\n') {
+                    c = input.charAt(parser_pos);
+                    resulting_string += c;
+                    parser_pos += 1;
+                }
+                output.push(trim(resulting_string) + '\n');
+                print_newline();
+                return get_next_token();
+            }
+
+
+
+            // Spidermonkey-specific sharp variables for circular references
+            // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
+            // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
+            var sharp = '#';
+            if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) {
+                do {
+                    c = input.charAt(parser_pos);
+                    sharp += c;
+                    parser_pos += 1;
+                } while (parser_pos < input_length && c !== '#' && c !== '=');
+                if (c === '#') {
+                    //
+                } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') {
+                    sharp += '[]';
+                    parser_pos += 2;
+                } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') {
+                    sharp += '{}';
+                    parser_pos += 2;
+                }
+                return [sharp, 'TK_WORD'];
+            }
+        }
+
+        if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') {
+            parser_pos += 3;
+            flags.in_html_comment = true;
+            return ['<!--', 'TK_COMMENT'];
+        }
+
+        if (c === '-' && flags.in_html_comment && input.substring(parser_pos - 1, parser_pos + 2) === '-->') {
+            flags.in_html_comment = false;
+            parser_pos += 2;
+            if (wanted_newline) {
+                print_newline();
+            }
+            return ['-->', 'TK_COMMENT'];
+        }
+
+        if (in_array(c, punct)) {
+            while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) {
+                c += input.charAt(parser_pos);
+                parser_pos += 1;
+                if (parser_pos >= input_length) {
+                    break;
+                }
+            }
+
+            if (c === '=') {
+                return [c, 'TK_EQUALS'];
+            } else {
+                return [c, 'TK_OPERATOR'];
+            }
+        }
+
+        return [c, 'TK_UNKNOWN'];
+    }
+
+    //----------------------------------
+    indent_string = '';
+    while (opt_indent_size > 0) {
+        indent_string += opt_indent_char;
+        opt_indent_size -= 1;
+    }
+
+    input = js_source_text;
+
+    last_word = ''; // last 'TK_WORD' passed
+    last_type = 'TK_START_EXPR'; // last token type
+    last_text = ''; // last token text
+    last_last_text = ''; // pre-last token text
+    output = [];
+
+    do_block_just_closed = false;
+
+    whitespace = "\n\r\t ".split('');
+    wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
+    digits = '0123456789'.split('');
+
+    punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' ');
+
+    // words which should always start on new line.
+    line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(',');
+
+    // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
+    // some formatting depends on that.
+    flag_store = [];
+    set_mode('BLOCK');
+
+    parser_pos = 0;
+    while (true) {
+        var t = get_next_token(parser_pos);
+        token_text = t[0];
+        token_type = t[1];
+        if (token_type === 'TK_EOF') {
+            break;
+        }
+
+        switch (token_type) {
+
+        case 'TK_START_EXPR':
+
+            if (token_text === '[') {
+
+                if (last_type === 'TK_WORD' || last_text === ')') {
+                    // this is array index specifier, break immediately
+                    // a[x], fn()[x]
+                    if (in_array(last_text, line_starters)) {
+                        print_single_space();
+                    }
+                    set_mode('(EXPRESSION)');
+                    print_token();
+                    break;
+                }
+
+                if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') {
+                    if (last_last_text === ']' && last_text === ',') {
+                        // ], [ goes to new line
+                        if (flags.mode === '[EXPRESSION]') {
+                            flags.mode = '[INDENTED-EXPRESSION]';
+                            if (!opt_keep_array_indentation) {
+                                indent();
+                            }
+                        }
+                        set_mode('[EXPRESSION]');
+                        if (!opt_keep_array_indentation) {
+                            print_newline();
+                        }
+                    } else if (last_text === '[') {
+                        if (flags.mode === '[EXPRESSION]') {
+                            flags.mode = '[INDENTED-EXPRESSION]';
+                            if (!opt_keep_array_indentation) {
+                                indent();
+                            }
+                        }
+                        set_mode('[EXPRESSION]');
+
+                        if (!opt_keep_array_indentation) {
+                            print_newline();
+                        }
+                    } else {
+                        set_mode('[EXPRESSION]');
+                    }
+                } else {
+                    set_mode('[EXPRESSION]');
+                }
+
+
+
+            } else {
+                set_mode('(EXPRESSION)');
+            }
+
+            if (last_text === ';' || last_type === 'TK_START_BLOCK') {
+                print_newline();
+            } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') {
+                // do nothing on (( and )( and ][ and ]( and .(
+            } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
+                print_single_space();
+            } else if (last_word === 'function' || last_word === 'typeof') {
+                // function() vs function ()
+                if (opt_jslint_happy) {
+                    print_single_space();
+                }
+            } else if (in_array(last_text, line_starters) || last_text === 'catch') {
+                print_single_space();
+            }
+            print_token();
+
+            break;
+
+        case 'TK_END_EXPR':
+            if (token_text === ']') {
+                if (opt_keep_array_indentation) {
+                    if (last_text === '}') {
+                        // trim_output();
+                        // print_newline(true);
+                        remove_indent();
+                        print_token();
+                        restore_mode();
+                        break;
+                    }
+                } else {
+                    if (flags.mode === '[INDENTED-EXPRESSION]') {
+                        if (last_text === ']') {
+                            restore_mode();
+                            print_newline();
+                            print_token();
+                            break;
+                        }
+                    }
+                }
+            }
+            restore_mode();
+            print_token();
+            break;
+
+        case 'TK_START_BLOCK':
+
+            if (last_word === 'do') {
+                set_mode('DO_BLOCK');
+            } else {
+                set_mode('BLOCK');
+            }
+            if (opt_brace_style=="expand") {
+                if (last_type !== 'TK_OPERATOR') {
+                    if (last_text === 'return' || last_text === '=') {
+                        print_single_space();
+                    } else {
+                        print_newline(true);
+                    }
+                }
+                print_token();
+                indent();
+            } else {
+                if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
+                    if (last_type === 'TK_START_BLOCK') {
+                        print_newline();
+                    } else {
+                        print_single_space();
+                    }
+                } else {
+                    // if TK_OPERATOR or TK_START_EXPR
+                    if (is_array(flags.previous_mode) && last_text === ',') {
+                        if (last_last_text === '}') {
+                            // }, { in array context
+                            print_single_space();
+                        } else {
+                            print_newline(); // [a, b, c, {
+                        }
+                    }
+                }
+                indent();
+                print_token();
+            }
+
+            break;
+
+        case 'TK_END_BLOCK':
+            restore_mode();
+            if (opt_brace_style=="expand") {
+                if (last_text !== '{') {
+                    print_newline();
+                }
+                print_token();
+            } else {
+                if (last_type === 'TK_START_BLOCK') {
+                    // nothing
+                    if (just_added_newline) {
+                        remove_indent();
+                    } else {
+                        // {}
+                        trim_output();
+                    }
+                } else {
+                    if (is_array(flags.mode) && opt_keep_array_indentation) {
+                        // we REALLY need a newline here, but newliner would skip that
+                        opt_keep_array_indentation = false;
+                        print_newline();
+                        opt_keep_array_indentation = true;
+
+                    } else {
+                        print_newline();
+                    }
+                }
+                print_token();
+            }
+            break;
+
+        case 'TK_WORD':
+
+            // no, it's not you. even I have problems understanding how this works
+            // and what does what.
+            if (do_block_just_closed) {
+                // do {} ## while ()
+                print_single_space();
+                print_token();
+                print_single_space();
+                do_block_just_closed = false;
+                break;
+            }
+
+            if (token_text === 'function') {
+                if (flags.var_line) {
+                    flags.var_line_reindented = true;
+                }
+                if ((just_added_newline || last_text === ';') && last_text !== '{') {
+                    // make sure there is a nice clean space of at least one blank line
+                    // before a new function definition
+                    n_newlines = just_added_newline ? n_newlines : 0;
+                    if ( ! opt_preserve_newlines) {
+                        n_newlines = 1;
+                    }
+
+                    for (var i = 0; i < 2 - n_newlines; i++) {
+                        print_newline(false);
+                    }
+                }
+            }
+
+            if (token_text === 'case' || token_text === 'default') {
+                if (last_text === ':') {
+                    // switch cases following one another
+                    remove_indent();
+                } else {
+                    // case statement starts in the same line where switch
+                    flags.indentation_level--;
+                    print_newline();
+                    flags.indentation_level++;
+                }
+                print_token();
+                flags.in_case = true;
+                break;
+            }
+
+            prefix = 'NONE';
+
+            if (last_type === 'TK_END_BLOCK') {
+
+                if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
+                    prefix = 'NEWLINE';
+                } else {
+                    if (opt_brace_style=="expand" || opt_brace_style=="end-expand") {
+                        prefix = 'NEWLINE';
+                    } else {
+                        prefix = 'SPACE';
+                        print_single_space();
+                    }
+                }
+            } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) {
+                prefix = 'NEWLINE';
+            } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) {
+                prefix = 'SPACE';
+            } else if (last_type === 'TK_STRING') {
+                prefix = 'NEWLINE';
+            } else if (last_type === 'TK_WORD') {
+                if (last_text === 'else') {
+                    // eat newlines between ...else *** some_op...
+                    // won't preserve extra newlines in this place (if any), but don't care that much
+                    trim_output(true);
+                }
+                prefix = 'SPACE';
+            } else if (last_type === 'TK_START_BLOCK') {
+                prefix = 'NEWLINE';
+            } else if (last_type === 'TK_END_EXPR') {
+                print_single_space();
+                prefix = 'NEWLINE';
+            }
+
+            if (in_array(token_text, line_starters) && last_text !== ')') {
+                if (last_text == 'else') {
+                    prefix = 'SPACE';
+                } else {
+                    prefix = 'NEWLINE';
+                }
+            }
+
+            if (flags.if_line && last_type === 'TK_END_EXPR') {
+                flags.if_line = false;
+            }
+            if (in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
+                if (last_type !== 'TK_END_BLOCK' || opt_brace_style=="expand" || opt_brace_style=="end-expand") {
+                    print_newline();
+                } else {
+                    trim_output(true);
+                    print_single_space();
+                }
+            } else if (prefix === 'NEWLINE') {
+                if ((last_type === 'TK_START_EXPR' || last_text === '=' || last_text === ',') && token_text === 'function') {
+                    // no need to force newline on 'function': (function
+                    // DONOTHING
+                } else if (token_text === 'function' && last_text == 'new') {
+                    print_single_space();
+                } else if (last_text === 'return' || last_text === 'throw') {
+                    // no newline between 'return nnn'
+                    print_single_space();
+                } else if (last_type !== 'TK_END_EXPR') {
+                    if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') {
+                        // no need to force newline on 'var': for (var x = 0...)
+                        if (token_text === 'if' && last_word === 'else' && last_text !== '{') {
+                            // no newline for } else if {
+                            print_single_space();
+                        } else {
+                            flags.var_line = false;
+                            flags.var_line_reindented = false;
+                            print_newline();
+                        }
+                    }
+                } else if (in_array(token_text, line_starters) && last_text != ')') {
+                    flags.var_line = false;
+                    flags.var_line_reindented = false;
+                    print_newline();
+                }
+            } else if (is_array(flags.mode) && last_text === ',' && last_last_text === '}') {
+                print_newline(); // }, in lists get a newline treatment
+            } else if (prefix === 'SPACE') {
+                print_single_space();
+            }
+            print_token();
+            last_word = token_text;
+
+            if (token_text === 'var') {
+                flags.var_line = true;
+                flags.var_line_reindented = false;
+                flags.var_line_tainted = false;
+            }
+
+            if (token_text === 'if') {
+                flags.if_line = true;
+            }
+            if (token_text === 'else') {
+                flags.if_line = false;
+            }
+
+            break;
+
+        case 'TK_SEMICOLON':
+
+            print_token();
+            flags.var_line = false;
+            flags.var_line_reindented = false;
+            if (flags.mode == 'OBJECT') {
+                // OBJECT mode is weird and doesn't get reset too well.
+                flags.mode = 'BLOCK';
+            }
+            break;
+
+        case 'TK_STRING':
+
+            if (last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') {
+                print_newline();
+            } else if (last_type === 'TK_WORD') {
+                print_single_space();
+            }
+            print_token();
+            break;
+
+        case 'TK_EQUALS':
+            if (flags.var_line) {
+                // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
+                flags.var_line_tainted = true;
+            }
+            print_single_space();
+            print_token();
+            print_single_space();
+            break;
+
+        case 'TK_OPERATOR':
+
+            var space_before = true;
+            var space_after = true;
+
+            if (flags.var_line && token_text === ',' && (is_expression(flags.mode))) {
+                // do not break on comma, for(var a = 1, b = 2)
+                flags.var_line_tainted = false;
+            }
+
+            if (flags.var_line) {
+                if (token_text === ',') {
+                    if (flags.var_line_tainted) {
+                        print_token();
+                        flags.var_line_reindented = true;
+                        flags.var_line_tainted = false;
+                        print_newline();
+                        break;
+                    } else {
+                        flags.var_line_tainted = false;
+                    }
+                // } else if (token_text === ':') {
+                    // hmm, when does this happen? tests don't catch this
+                    // flags.var_line = false;
+                }
+            }
+
+            if (last_text === 'return' || last_text === 'throw') {
+                // "return" had a special handling in TK_WORD. Now we need to return the favor
+                print_single_space();
+                print_token();
+                break;
+            }
+
+            if (token_text === ':' && flags.in_case) {
+                print_token(); // colon really asks for separate treatment
+                print_newline();
+                flags.in_case = false;
+                break;
+            }
+
+            if (token_text === '::') {
+                // no spaces around exotic namespacing syntax operator
+                print_token();
+                break;
+            }
+
+            if (token_text === ',') {
+                if (flags.var_line) {
+                    if (flags.var_line_tainted) {
+                        print_token();
+                        print_newline();
+                        flags.var_line_tainted = false;
+                    } else {
+                        print_token();
+                        print_single_space();
+                    }
+                } else if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") {
+                    print_token();
+                    if (flags.mode === 'OBJECT' && last_text === '}') {
+                        print_newline();
+                    } else {
+                        print_single_space();
+                    }
+                } else {
+                    if (flags.mode === 'OBJECT') {
+                        print_token();
+                        print_newline();
+                    } else {
+                        // EXPR or DO_BLOCK
+                        print_token();
+                        print_single_space();
+                    }
+                }
+                break;
+            // } else if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS']) || in_array(last_text, line_starters) || in_array(last_text, ['==', '!=', '+=', '-=', '*=', '/=', '+', '-'])))) {
+            } else if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) || in_array(last_text, line_starters)))) {
+                // unary operators (and binary +/- pretending to be unary) special cases
+
+                space_before = false;
+                space_after = false;
+
+                if (last_text === ';' && is_expression(flags.mode)) {
+                    // for (;; ++i)
+                    //        ^^^
+                    space_before = true;
+                }
+                if (last_type === 'TK_WORD' && in_array(last_text, line_starters)) {
+                    space_before = true;
+                }
+
+                if (flags.mode === 'BLOCK' && (last_text === '{' || last_text === ';')) {
+                    // { foo; --i }
+                    // foo(); --bar;
+                    print_newline();
+                }
+            } else if (token_text === '.') {
+                // decimal digits or object.property
+                space_before = false;
+
+            } else if (token_text === ':') {
+                if (flags.ternary_depth == 0) {
+                    flags.mode = 'OBJECT';
+                    space_before = false;
+                } else {
+                    flags.ternary_depth -= 1;
+                }
+            } else if (token_text === '?') {
+                flags.ternary_depth += 1;
+            }
+            if (space_before) {
+                print_single_space();
+            }
+
+            print_token();
+
+            if (space_after) {
+                print_single_space();
+            }
+
+            if (token_text === '!') {
+                // flags.eat_next_space = true;
+            }
+
+            break;
+
+        case 'TK_BLOCK_COMMENT':
+
+            var lines = token_text.split(/\x0a|\x0d\x0a/);
+
+            if (all_lines_start_with(lines.slice(1), '*')) {
+                // javadoc: reformat and reindent
+                print_newline();
+                output.push(lines[0]);
+                for (i = 1; i < lines.length; i++) {
+                    print_newline();
+                    output.push(' ');
+                    output.push(trim(lines[i]));
+                }
+
+            } else {
+
+                // simple block comment: leave intact
+                if (lines.length > 1) {
+                    // multiline comment block starts with a new line
+                    print_newline();
+                    trim_output();
+                } else {
+                    // single-line /* comment */ stays where it is
+                    print_single_space();
+
+                }
+
+                for (i = 0; i < lines.length; i++) {
+                    output.push(lines[i]);
+                    output.push('\n');
+                }
+
+            }
+            print_newline();
+            break;
+
+        case 'TK_INLINE_COMMENT':
+
+            print_single_space();
+            print_token();
+            if (is_expression(flags.mode)) {
+                print_single_space();
+            } else {
+                force_newline();
+            }
+            break;
+
+        case 'TK_COMMENT':
+
+            // print_newline();
+            if (wanted_newline) {
+                print_newline();
+            } else {
+                print_single_space();
+            }
+            print_token();
+            force_newline();
+            break;
+
+        case 'TK_UNKNOWN':
+            if (last_text === 'return' || last_text === 'throw') {
+                print_single_space();
+            }
+            print_token();
+            break;
+        }
+
+        last_last_text = last_text;
+        last_type = token_type;
+        last_text = token_text;
+    }
+
+    var sweet_code = output.join('').replace(/[\n ]+$/, '');
+    if (opt_indent_level) {
+        for (i = 0 ; i < opt_indent_level; i++) {
+            sweet_code = indent_string + sweet_code;
+        }
+    }
+    return sweet_code;
+
+}
+
+// Add support for CommonJS. Just put this file somewhere on your require.paths
+// and you will be able to `var js_beautify = require("beautify").js_beautify`.
+if (typeof exports !== "undefined")
+    exports.js_beautify = js_beautify;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sandbox/jsonld.js	Tue Jul 05 22:24:05 2011 -0400
@@ -0,0 +1,2368 @@
+/**
+ * Javascript implementation of JSON-LD.
+ *
+ * @author Dave Longley
+ *
+ * Copyright (c) 2011 Digital Bazaar, Inc. All rights reserved.
+ */
+(function()
+{
+
+// define forge
+if(typeof(window) !== 'undefined')
+{
+   var forge = window.forge = window.forge || {};
+   forge.jsonld = {};
+}
+// define node.js module
+else if(typeof(module) !== 'undefined' && module.exports)
+{
+   var forge = {};
+   module.exports = forge.jsonld = {};
+}
+
+/*
+ * JSON-LD API.
+ */ 
+var jsonld = forge.jsonld;
+jsonld.ns =
+{
+   rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
+   xsd: 'http://www.w3.org/2001/XMLSchema#'
+};
+
+var xsd =
+{
+   anyType: jsonld.ns.xsd + 'anyType',
+   boolean: jsonld.ns.xsd + 'boolean',
+   double: jsonld.ns.xsd + 'double',
+   integer: jsonld.ns.xsd + 'integer',
+   anyURI: jsonld.ns.xsd + 'anyURI'
+};
+
+/**
+ * Creates the JSON-LD default context.
+ *
+ * @return the JSON-LD default context.
+ */
+var _createDefaultContext = function()
+{
+   var ctx =
+   {
+      a: jsonld.ns.rdf + 'type',
+      rdf: jsonld.ns.rdf,
+      rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
+      owl: 'http://www.w3.org/2002/07/owl#',
+      xsd: 'http://www.w3.org/2001/XMLSchema#',
+      dcterms: 'http://purl.org/dc/terms/',
+      foaf: 'http://xmlns.com/foaf/0.1/',
+      cal: 'http://www.w3.org/2002/12/cal/ical#',
+      vcard: 'http://www.w3.org/2006/vcard/ns#',
+      geo: 'http://www.w3.org/2003/01/geo/wgs84_pos#',
+      cc: 'http://creativecommons.org/ns#',
+      sioc: 'http://rdfs.org/sioc/ns#',
+      doap: 'http://usefulinc.com/ns/doap#',
+      com: 'http://purl.org/commerce#',
+      ps: 'http://purl.org/payswarm#',
+      gr: 'http://purl.org/goodrelations/v1#',
+      sig: 'http://purl.org/signature#',
+      ccard: 'http://purl.org/commerce/creditcard#',
+      '@coerce':
+      {
+         'xsd:anyURI': ['foaf:homepage', 'foaf:member'],
+         'xsd:integer': 'foaf:age'
+      },
+      '@vocab': ''
+   };
+   return ctx;
+};
+
+/**
+ * Compacts an IRI into a term or CURIE if it can be. IRIs will not be
+ * compacted to relative IRIs if they match the given context's default
+ * vocabulary.
+ *
+ * @param ctx the context to use.
+ * @param iri the IRI to compact.
+ * @param usedCtx a context to update if a value was used from "ctx".
+ *
+ * @return the compacted IRI as a term or CURIE or the original IRI.
+ */
+var _compactIri = function(ctx, iri, usedCtx)
+{
+   var rval = null;
+   
+   // check the context for a term that could shorten the IRI
+   // (give preference to terms over CURIEs)
+   for(var key in ctx)
+   {
+      // skip special context keys (start with '@')
+      if(key.length > 0 && key.indexOf('@') !== 0)
+      {
+         // compact to a term
+         if(iri === ctx[key])
+         {
+            rval = key;
+            if(usedCtx !== null)
+            {
+               usedCtx[key] = ctx[key];
+            }
+            break;
+         }
+      }
+   }
+   
+   // term not found, check the context for a CURIE prefix
+   if(rval === null)
+   {
+      for(var key in ctx)
+      {
+         // skip special context keys (start with '@')
+         if(key.length > 0 && key.indexOf('@') !== 0)
+         {
+            // see if IRI begins with the next IRI from the context
+            var ctxIri = ctx[key];
+            var idx = iri.indexOf(ctxIri);
+            
+            // compact to a CURIE
+            if(idx === 0 && iri.length > ctxIri.length)
+            {
+               rval = key + ':' + iri.substr(idx + ctxIri.length);
+               if(usedCtx !== null)
+               {
+                  usedCtx[key] = ctxIri;
+               }
+               break;
+            }
+         }
+      }
+   }
+
+   // could not compact IRI
+   if(rval === null)
+   {
+      rval = iri;
+   }
+
+   return rval;
+};
+
+/**
+ * Expands a term into an absolute IRI. The term may be a regular term, a
+ * CURIE, a relative IRI, or an absolute IRI. In any case, the associated
+ * absolute IRI will be returned.
+ *
+ * @param ctx the context to use.
+ * @param term the term to expand.
+ * @param usedCtx a context to update if a value was used from "ctx".
+ *
+ * @return the expanded term as an absolute IRI.
+ */
+var _expandTerm = function(ctx, term, usedCtx)
+{
+   var rval;
+   
+   // 1. If the property has a colon, then it is a CURIE or an absolute IRI:
+   var idx = term.indexOf(':');
+   if(idx != -1)
+   {
+      // get the potential CURIE prefix
+      var prefix = term.substr(0, idx);
+
+      // 1.1. See if the prefix is in the context:
+      if(prefix in ctx)
+      {
+         // prefix found, expand property to absolute IRI
+         rval = ctx[prefix] + term.substr(idx + 1);
+         if(usedCtx !== null)
+         {
+            usedCtx[prefix] = ctx[prefix];
+         }
+      }
+      // 1.2. Prefix is not in context, property is already an absolute IRI:
+      else
+      {
+         rval = term;
+      }
+   }
+   // 2. If the property is in the context, then it's a term.
+   else if(term in ctx)
+   {
+      rval = ctx[term];
+      if(usedCtx !== null)
+      {
+         usedCtx[term] = rval;
+      }
+   }
+   // 3. The property is the special-case '@'.
+   else if(term === "@")
+   {
+      rval = "@";
+   }
+   // 4. The property is a relative IRI, prepend the default vocab.
+   else
+   {
+      rval = ctx['@vocab'] + term;
+      if(usedCtx !== null)
+      {
+         usedCtx['@vocab'] = ctx['@vocab'];
+      }
+   }
+
+   return rval;
+};
+
+/**
+ * Sets a subject's property to the given object value. If a value already
+ * exists, it will be appended to an array.
+ *
+ * @param s the subject.
+ * @param p the property.
+ * @param o the object.
+ */
+var _setProperty = function(s, p, o)
+{
+   if(p in s)
+   {
+      if(s[p].constructor === Array)
+      {
+         s[p].push(o);
+      }
+      else
+      {
+         s[p] = [s[p], o];
+      }
+   }
+   else
+   {
+      s[p] = o;
+   }
+};
+
+/**
+ * Clones a string/number or an object and sorts the keys. Deep clone
+ * is not performed. This function will not shallow or deep copy arrays, but
+ * that feature isn't needed in this implementation at present. If it is
+ * needed in the future, it will have to be implemented here.
+ * 
+ * @param value the value to clone.
+ * 
+ * @return the cloned value.
+ */
+var _clone = function(value)
+{
+   var rval;
+   
+   if(value.constructor === Object)
+   {
+      rval = {};
+      var keys = Object.keys(value).sort();
+      for(var i in keys)
+      {
+         var key = keys[i];
+         rval[key] = value[key];
+      }
+   }
+   else
+   {
+      rval = value;
+   }
+   
+   return rval;
+};
+
+/**
+ * Clones a context.
+ * 
+ * @param ctx the context to clone.
+ * 
+ * @return the clone of the context.
+ */
+var _cloneContext = function(ctx)
+{
+   var rval = {};
+   for(var key in ctx)
+   {
+      // deep-copy @coerce
+      if(key === '@coerce')
+      {
+         rval['@coerce'] = {};
+         for(var type in ctx['@coerce'])
+         {
+            var p = ctx['@coerce'][type];
+            rval['@coerce'][type] = (p.constructor === Array) ? p.slice() : p;
+         }
+      }
+      else
+      {
+         rval[key] = ctx[key];
+      }
+   }
+   return rval;
+};
+
+/**
+ * Gets the coerce type for the given property.
+ *
+ * @param ctx the context to use.
+ * @param property the property to get the coerced type for.
+ * @param usedCtx a context to update if a value was used from "ctx".
+ *
+ * @return the coerce type, null for none.
+ */
+var _getCoerceType = function(ctx, property, usedCtx)
+{
+   var rval = null;
+
+   // get expanded property
+   var p = _expandTerm(ctx, property, null);
+
+   // built-in type coercion JSON-LD-isms
+   if(p === '@' || p === jsonld.ns.rdf + 'type')
+   {
+      rval = xsd.anyURI;
+   }
+   // check type coercion for property
+   else
+   {
+      // force compacted property
+      p = _compactIri(ctx, p, null);
+      
+      for(var type in ctx['@coerce'])
+      {
+         // get coerced properties (normalize to an array)
+         var props = ctx['@coerce'][type];
+         if(props.constructor !== Array)
+         {
+            props = [props];
+         }
+         
+         // look for the property in the array
+         for(var i in props)
+         {
+            // property found
+            if(props[i] === p)
+            {
+               rval = _expandTerm(ctx, type, usedCtx);
+               if(usedCtx !== null)
+               {
+                  if(!('@coerce' in usedCtx))
+                  {
+                     usedCtx['@coerce'] = {};
+                  }
+                  
+                  if(!(type in usedCtx['@coerce']))
+                  {
+                     usedCtx['@coerce'][type] = p;
+                  }
+                  else
+                  {
+                     var c = usedCtx['@coerce'][type];
+                     if((c.constructor === Array && c.indexOf(p) == -1) ||
+                        (c.constructor === String && c !== p))
+                     {
+                        _setProperty(usedCtx['@coerce'], type, p);
+                     }
+                  }
+               }
+               break;
+            }
+         }
+      }
+   }
+
+   return rval;
+};
+
+/**
+ * Recursively compacts a value. This method will compact IRIs to CURIEs or
+ * terms and do reverse type coercion to compact a value.
+ *
+ * @param ctx the context to use.
+ * @param property the property that points to the value, NULL for none.
+ * @param value the value to compact.
+ * @param usedCtx a context to update if a value was used from "ctx".
+ *
+ * @return the compacted value.
+ */
+var _compact = function(ctx, property, value, usedCtx)
+{
+   var rval;
+   
+   if(value === null)
+   {
+      rval = null;
+   }
+   else if(value.constructor === Array)
+   {
+      // recursively add compacted values to array
+      rval = [];
+      for(var i in value)
+      {
+         rval.push(_compact(ctx, property, value[i], usedCtx));
+      }
+   }
+   // graph literal/disjoint graph
+   else if(
+      value.constructor === Object &&
+      '@' in value && value['@'].constructor === Array)
+   {
+      rval = {};
+      rval['@'] = _compact(ctx, property, value['@'], usedCtx);
+   }
+   // value has sub-properties if it doesn't define a literal or IRI value
+   else if(
+      value.constructor === Object &&
+      !('@literal' in value) && !('@iri' in value))
+   {
+      // recursively handle sub-properties that aren't a sub-context
+      rval = {};
+      for(var key in value)
+      {
+         if(value[key] !== '@context')
+         {
+            // set object to compacted property
+            _setProperty(
+               rval, _compactIri(ctx, key, usedCtx),
+               _compact(ctx, key, value[key], usedCtx));
+         }
+      }
+   }
+   else
+   {
+      // get coerce type
+      var coerce = _getCoerceType(ctx, property, usedCtx);
+
+      // get type from value, to ensure coercion is valid
+      var type = null;
+      if(value.constructor === Object)
+      {
+         // type coercion can only occur if language is not specified
+         if(!('@language' in value))
+         {
+            // datatype must match coerce type if specified
+            if('@datatype' in value)
+            {
+               type = value['@datatype'];
+            }
+            // datatype is IRI
+            else if('@iri' in value)
+            {
+               type = xsd.anyURI;
+            }
+            // can be coerced to any type
+            else
+            {
+               type = coerce;
+            }
+         }
+      }
+      // type can be coerced to anything
+      else if(value.constructor === String)
+      {
+         type = coerce;
+      }
+
+      // types that can be auto-coerced from a JSON-builtin
+      if(coerce === null &&
+         (type === xsd.boolean || type === xsd.integer || type === xsd.double))
+      {
+         coerce = type;
+      }
+
+      // do reverse type-coercion
+      if(coerce !== null)
+      {
+         // type is only null if a language was specified, which is an error
+         // if type coercion is specified
+         if(type === null)
+         {
+            throw {
+               message: 'Cannot coerce type when a language is specified. ' +
+                  'The language information would be lost.'
+            };
+         }
+         // if the value type does not match the coerce type, it is an error
+         else if(type !== coerce)
+         {
+            throw {
+               message: 'Cannot coerce type because the datatype does ' +
+                  'not match.'
+            };
+         }
+         // do reverse type-coercion
+         else
+         {
+            if(value.constructor === Object)
+            {
+               if('@iri' in value)
+               {
+                  rval = value['@iri'];
+               }
+               else if('@literal' in value)
+               {
+                  rval = value['@literal'];
+               }
+            }
+            else
+            {
+               rval = value;
+            }
+
+            // do basic JSON types conversion
+            if(coerce === xsd.boolean)
+            {
+               rval = (rval === 'true' || rval != 0);
+            }
+            else if(coerce === xsd.double)
+            {
+               rval = parseFloat(rval);
+            }
+            else if(coerce === xsd.integer)
+            {
+               rval = parseInt(rval);
+            }
+         }
+      }
+      // no type-coercion, just copy value
+      else
+      {
+         rval = _clone(value);
+      }
+
+      // compact IRI
+      if(type === xsd.anyURI)
+      {
+         if(rval.constructor === Object)
+         {
+            rval['@iri'] = _compactIri(ctx, rval['@iri'], usedCtx);
+         }
+         else
+         {
+            rval = _compactIri(ctx, rval, usedCtx);
+         }
+      }
+   }
+
+   return rval;
+};
+
+/**
+ * Recursively expands a value using the given context. Any context in
+ * the value will be removed.
+ *
+ * @param ctx the context.
+ * @param property the property that points to the value, NULL for none.
+ * @param value the value to expand.
+ * @param expandSubjects true to expand subjects (normalize), false not to.
+ *
+ * @return the expanded value.
+ */
+var _expand = function(ctx, property, value, expandSubjects)
+{
+   var rval;
+   
+   // TODO: add data format error detection?
+
+   // if no property is specified and the value is a string (this means the
+   // value is a property itself), expand to an IRI
+   if(property === null && value.constructor === String)
+   {
+      rval = _expandTerm(ctx, value, null);
+   }
+   else if(value.constructor === Array)
+   {
+      // recursively add expanded values to array
+      rval = [];
+      for(var i in value)
+      {
+         rval.push(_expand(ctx, property, value[i], expandSubjects));
+      }
+   }
+   else if(value.constructor === Object)
+   {
+      // value has sub-properties if it doesn't define a literal or IRI value
+      if(!('@literal' in value || '@iri' in value))
+      {
+         // if value has a context, use it
+         if('@context' in value)
+         {
+            ctx = jsonld.mergeContexts(ctx, value['@context']);
+         }
+
+         // recursively handle sub-properties that aren't a sub-context
+         rval = {};
+         for(var key in value)
+         {
+            if(key.length === 1 || key.indexOf('@') !== 0)
+            {
+               // set object to expanded property
+               _setProperty(
+                  rval, _expandTerm(ctx, key, null),
+                  _expand(ctx, key, value[key], expandSubjects));
+            }
+            else if(key !== '@context')
+            {
+               // preserve non-context json-ld keywords
+               _setProperty(rval, key, _clone(value[key]));
+            }
+         }
+      }
+      // value is already expanded
+      else
+      {
+         rval = _clone(value);
+      }
+   }
+   else
+   {
+      // do type coercion
+      var coerce = _getCoerceType(ctx, property, null);
+
+      // automatic coercion for basic JSON types
+      if(coerce === null &&
+         (value.constructor === Number || value.constructor === Boolean))
+      {
+         if(value.constructor === Boolean)
+         {
+            coerce = xsd.boolean;
+         }
+         else if(('' + value).indexOf('.') == -1)
+         {
+            coerce = xsd.integer;
+         }
+         else
+         {
+            coerce = xsd.double;
+         }
+      }
+
+      // coerce to appropriate datatype, only expand subjects if requested
+      if(coerce !== null && (property !== '@' || expandSubjects))
+      {
+         rval = {};
+         
+         // expand IRI
+         if(coerce === xsd.anyURI)
+         {
+            rval['@iri'] = _expandTerm(ctx, value, null);
+         }
+         // other datatype
+         else
+         {
+            rval['@datatype'] = coerce;
+            if(coerce === xsd.double)
+            {
+               // do special JSON-LD double format
+               value = value.toExponential(6).replace(
+                  /(e(?:\+|-))([0-9])$/, '$10$2');
+            }
+            rval['@literal'] = '' + value;
+         }
+      }
+      // nothing to coerce
+      else
+      {
+         rval = '' + value;
+      }
+   }
+   
+   return rval;
+};
+
+var _isBlankNodeIri = function(v)
+{
+   return v.indexOf('_:') === 0;
+};
+
+var _isNamedBlankNode = function(v)
+{
+   // look for "_:" at the beginning of the subject
+   return (
+      v.constructor === Object && '@' in v &&
+      '@iri' in v['@'] && _isBlankNodeIri(v['@']['@iri']));
+};
+
+var _isBlankNode = function(v)
+{
+   // look for no subject or named blank node
+   return (
+      v.constructor === Object &&
+      !('@iri' in v || '@literal' in v) &&
+      (!('@' in v) || _isNamedBlankNode(v)));
+};
+
+/**
+ * Compares two values.
+ * 
+ * @param v1 the first value.
+ * @param v2 the second value.
+ * 
+ * @return -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2.
+ */
+var _compare = function(v1, v2)
+{
+   var rval = 0;
+   
+   if(v1.constructor === Array && v2.constructor === Array)
+   {
+      for(var i = 0; i < v1.length && rval === 0; ++i)
+      {
+         rval = _compare(v1[i], v2[i]);
+      }
+   }
+   else
+   {
+      rval = (v1 < v2 ? -1 : (v1 > v2 ? 1 : 0));
+   }
+   
+   return rval;
+};
+
+/**
+ * Compares two keys in an object. If the key exists in one object
+ * and not the other, that object is less. If the key exists in both objects,
+ * then the one with the lesser value is less.
+ * 
+ * @param o1 the first object.
+ * @param o2 the second object.
+ * @param key the key.
+ * 
+ * @return -1 if o1 < o2, 0 if o1 == o2, 1 if o1 > o2.
+ */
+var _compareObjectKeys = function(o1, o2, key)
+{
+   var rval = 0;
+   if(key in o1)
+   {
+      if(key in o2)
+      {
+         rval = _compare(o1[key], o2[key]);
+      }
+      else
+      {
+         rval = -1;
+      }
+   }
+   else if(key in o2)
+   {
+      rval = 1;
+   }
+   return rval;
+};
+
+/**
+ * Compares two object values.
+ * 
+ * @param o1 the first object.
+ * @param o2 the second object.
+ * 
+ * @return -1 if o1 < o2, 0 if o1 == o2, 1 if o1 > o2.
+ */
+var _compareObjects = function(o1, o2)
+{
+   var rval = 0;
+   
+   if(o1.constructor === String)
+   {
+      if(o2.constructor !== String)
+      {
+         rval = -1;
+      }
+      else
+      {
+         rval = _compare(o1, o2);
+      }
+   }
+   else if(o2.constructor === String)
+   {
+      rval = 1;
+   }
+   else
+   {
+      rval = _compareObjectKeys(o1, o2, '@literal');
+      if(rval === 0)
+      {
+         if('@literal' in o1)
+         {
+            rval = _compareObjectKeys(o1, o2, '@datatype');
+            if(rval === 0)
+            {
+               rval = _compareObjectKeys(o1, o2, '@language');
+            }
+         }
+         // both are '@iri' objects
+         else
+         {
+            rval = _compare(o1['@iri'], o2['@iri']);
+         }
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Compares the object values between two bnodes.
+ * 
+ * @param a the first bnode.
+ * @param b the second bnode.
+ * 
+ * @return -1 if a < b, 0 if a == b, 1 if a > b.
+ */
+var _compareBlankNodeObjects = function(a, b)
+{
+   var rval = 0;
+   
+   /*
+   3. For each property, compare sorted object values.
+   3.1. The bnode with fewer objects is first.
+   3.2. For each object value, compare only literals and non-bnodes.
+   3.2.1.  The bnode with fewer non-bnodes is first.
+   3.2.2. The bnode with a string object is first.
+   3.2.3. The bnode with the alphabetically-first string is first.
+   3.2.4. The bnode with a @literal is first.
+   3.2.5. The bnode with the alphabetically-first @literal is first.
+   3.2.6. The bnode with the alphabetically-first @datatype is first.
+   3.2.7. The bnode with a @language is first.
+   3.2.8. The bnode with the alphabetically-first @language is first.
+   3.2.9. The bnode with the alphabetically-first @iri is first.
+   */
+   
+   for(var p in a)
+   {
+      // step #3.1
+      var lenA = (a[p].constructor === Array) ? a[p].length : 1;
+      var lenB = (b[p].constructor === Array) ? b[p].length : 1;
+      rval = _compare(lenA, lenB);
+      
+      // step #3.2.1
+      if(rval === 0)
+      {
+         // normalize objects to an array
+         var objsA = a[p];
+         var objsB = b[p];
+         if(objsA.constructor !== Array)
+         {
+            objsA = [objsA];
+            objsB = [objsB];
+         }
+         
+         // filter non-bnodes (remove bnodes from comparison)
+         objsA = objsA.filter(function(e) {
+            return (e.constructor === String ||
+               !('@iri' in e && _isBlankNodeIri(e['@iri'])));
+         });
+         objsB = objsB.filter(function(e) {
+            return (e.constructor === String ||
+               !('@iri' in e && _isBlankNodeIri(e['@iri'])));
+         });
+         
+         rval = _compare(objsA.length, objsB.length);
+      }
+      
+      // steps #3.2.2-3.2.9
+      if(rval === 0)
+      {
+         for(var i = 0; i < objsA.length && rval === 0; ++i)
+         {
+            rval = _compareObjects(objsA[i], objsB[i]);
+         }
+      }
+      
+      if(rval !== 0)
+      {
+         break;
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Creates a blank node name generator using the given prefix for the
+ * blank nodes. 
+ * 
+ * @param prefix the prefix to use.
+ * 
+ * @return the blank node name generator.
+ */
+var _createNameGenerator = function(prefix)
+{
+   var count = -1;
+   var ng = {
+      next: function()
+      {
+         ++count;
+         return ng.current();
+      },
+      current: function()
+      {
+         return '_:' + prefix + count;
+      },
+      inNamespace: function(iri)
+      {
+         return iri.indexOf('_:' + prefix) === 0;
+      }
+   };
+   return ng;
+};
+
+/**
+ * Populates a map of all named subjects from the given input and an array
+ * of all unnamed bnodes (includes embedded ones).
+ * 
+ * @param input the input (must be expanded, no context).
+ * @param subjects the subjects map to populate.
+ * @param bnodes the bnodes array to populate.
+ */
+var _collectSubjects = function(input, subjects, bnodes)
+{
+   if(input.constructor === Array)
+   {
+      for(var i in input)
+      {
+         _collectSubjects(input[i], subjects, bnodes);
+      }
+   }
+   else if(input.constructor === Object)
+   {
+      if('@' in input)
+      {
+         // graph literal
+         if(input['@'].constructor == Array)
+         {
+            _collectSubjects(input['@'], subjects, bnodes);
+         }
+         // named subject
+         else
+         {
+            subjects[input['@']['@iri']] = input;
+         }
+      }
+      // unnamed blank node
+      else if(_isBlankNode(input))
+      {
+         bnodes.push(input);
+      }
+      
+      // recurse through subject properties
+      for(var key in input)
+      {
+         _collectSubjects(input[key], subjects, bnodes);
+      }
+   }
+};
+
+/**
+ * Flattens the given value into a map of unique subjects. It is assumed that
+ * all blank nodes have been uniquely named before this call. Array values for
+ * properties will be sorted.
+ *
+ * @param parent the value's parent, NULL for none.
+ * @param parentProperty the property relating the value to the parent.
+ * @param value the value to flatten.
+ * @param subjects the map of subjects to write to.
+ */
+var _flatten = function(parent, parentProperty, value, subjects)
+{
+   var flattened = null;
+   
+   if(value.constructor === Array)
+   {
+      // list of objects or a disjoint graph
+      for(var i in value)
+      {
+         _flatten(parent, parentProperty, value[i], subjects);
+      }
+      
+      // if value is a list of objects, sort them
+      if(value.length > 0 &&
+         (value[0].constructor === String ||
+         (value[0].constructor === Object &&
+         ('@literal' in value[0] || '@iri' in value[0]))))
+      {
+         // sort values
+         value.sort(_compareObjects);
+      }
+   }
+   else if(value.constructor === Object)
+   {
+      // graph literal/disjoint graph
+      if('@' in value && value['@'].constructor === Array)
+      {
+         // cannot flatten embedded graph literals
+         if(parent !== null)
+         {
+            throw {
+               message: 'Embedded graph literals cannot be flattened.'
+            };
+         }
+         
+         // top-level graph literal
+         for(var key in value['@'])
+         {
+            _flatten(parent, parentProperty, value['@'][key], subjects);
+         }
+      }
+      // already-expanded value
+      else if('@literal' in value || '@iri' in value)
+      {
+         flattened = _clone(value);
+      }
+      // subject
+      else
+      {
+         // create or fetch existing subject
+         var subject;
+         if(value['@']['@iri'] in subjects)
+         {
+            // FIXME: '@' might be a graph literal (as {})
+            subject = subjects[value['@']['@iri']];
+         }
+         else
+         {
+            subject = {};
+            if('@' in value)
+            {
+               // FIXME: '@' might be a graph literal (as {})
+               subjects[value['@']['@iri']] = subject;
+            }
+         }
+         flattened = subject;
+
+         // flatten embeds
+         for(var key in value)
+         {
+            if(value[key].constructor === Array)
+            {
+               subject[key] = [];
+               _flatten(subject[key], null, value[key], subjects);
+               if(subject[key].length === 1)
+               {
+                  // convert subject[key] to object if only 1 value was added
+                  subject[key] = subject[key][0];
+               }
+            }
+            else
+            {
+               _flatten(subject, key, value[key], subjects);
+            }
+         }
+      }
+   }
+   // string value
+   else
+   {
+      flattened = value;
+   }
+
+   // add flattened value to parent
+   if(flattened !== null && parent !== null)
+   {
+      // remove top-level '@' for subjects
+      // 'http://mypredicate': {'@': {'@iri': 'http://mysubject'}} becomes
+      // 'http://mypredicate': {'@iri': 'http://mysubject'}
+      if(flattened.constructor === Object && '@' in flattened)
+      {
+         flattened = flattened['@'];
+      }
+
+      if(parent.constructor === Array)
+      {
+         // do not add duplicate IRIs for the same property
+         var duplicate = false;
+         if(flattened.constructor === Object && '@iri' in flattened)
+         {
+            duplicate = (parent.filter(function(e)
+            {
+               return (e.constructor === Object && '@iri' in e &&
+                  e['@iri'] === flattened['@iri']);
+            }).length > 0);
+         }
+         if(!duplicate)
+         {
+            parent.push(flattened);
+         }
+      }
+      else
+      {
+         parent[parentProperty] = flattened;
+      }
+   }
+};
+
+/**
+ * Constructs a new JSON-LD processor.
+ */
+jsonld.Processor = function()
+{
+   this.memo = {};
+};
+
+/**
+ * Normalizes a JSON-LD object.
+ *
+ * @param input the JSON-LD object to normalize.
+ * 
+ * @return the normalized JSON-LD object.
+ */
+jsonld.Processor.prototype.normalize = function(input)
+{
+   var rval = [];
+
+   // TODO: validate context
+
+   if(input !== null)
+   {
+      // get default context
+      var ctx = _createDefaultContext();
+
+      // expand input
+      var expanded = _expand(ctx, null, input, true);
+      
+      // assign names to unnamed bnodes
+      this.nameBlankNodes(expanded);
+
+      // flatten
+      var subjects = {};
+      _flatten(null, null, expanded, subjects);
+
+      // append subjects to array
+      for(var key in subjects)
+      {
+         rval.push(subjects[key]);
+      }
+
+      // canonicalize blank nodes
+      this.canonicalizeBlankNodes(rval);
+
+      // sort output
+      rval.sort(function(a, b)
+      {
+         return _compare(a['@']['@iri'], b['@']['@iri']);
+      });
+   }
+
+   return rval;
+};
+
+/**
+ * Assigns unique names to blank nodes that are unnamed in the given input.
+ * 
+ * @param input the input to assign names to.
+ */
+jsonld.Processor.prototype.nameBlankNodes = function(input)
+{
+   // create temporary blank node name generator
+   var ng = this.ng = _createNameGenerator('tmp');
+   
+   // collect subjects and unnamed bnodes
+   var subjects = {};
+   var bnodes = [];
+   _collectSubjects(input, subjects, bnodes);
+   
+   // uniquely name all unnamed bnodes
+   for(var i in bnodes)
+   {
+      var bnode = bnodes[i];
+      if(!('@' in bnode))
+      {
+         // generate names until one is unique
+         while(ng.next() in subjects);
+         bnode['@'] =
+         {
+            '@iri': ng.current()
+         };
+         subjects[ng.current()] = bnode;
+      }
+   }
+};
+
+/**
+ * Renames a blank node, changing its references, etc. The method assumes
+ * that the given name is unique.
+ * 
+ * @param b the blank node to rename.
+ * @param id the new name to use.
+ */
+jsonld.Processor.prototype.renameBlankNode = function(b, id)
+{
+   var old = b['@']['@iri'];
+   
+   // update bnode IRI
+   b['@']['@iri'] = id;
+   
+   // update subjects map
+   var subjects = this.subjects;
+   subjects[id] = subjects[old];
+   delete subjects[old];
+   
+   // update reference and property lists
+   this.edges.refs[id] = this.edges.refs[old];
+   this.edges.props[id] = this.edges.props[old];
+   delete this.edges.refs[old];
+   delete this.edges.props[old];
+   
+   // update references to this bnode
+   var refs = this.edges.refs[id].all;
+   for(var i in refs)
+   {
+      var iri = refs[i].s;
+      if(iri === old)
+      {
+         iri = id;
+      }
+      var ref = subjects[iri];
+      var props = this.edges.props[iri].all;
+      for(var i2 in props)
+      {
+         if(props[i2].s === old)
+         {
+            props[i2].s = id;
+            
+            // normalize property to array for single code-path
+            var p = props[i2].p;
+            var tmp = (ref[p].constructor === Object) ? [ref[p]] :
+               (ref[p].constructor === Array) ? ref[p] : [];
+            for(var n in tmp)
+            {
+               if(tmp[n].constructor === Object &&
+                  '@iri' in tmp[n] && tmp[n]['@iri'] === old)
+               {
+                  tmp[n]['@iri'] = id;
+               }
+            }
+         }
+      }
+   }
+   
+   // update references from this bnode 
+   var props = this.edges.props[id].all;
+   for(var i in props)
+   {
+      var iri = props[i].s;
+      refs = this.edges.refs[iri].all;
+      for(var r in refs)
+      {
+         if(refs[r].s === old)
+         {
+            refs[r].s = id;
+         }
+      }
+   }
+};
+
+/**
+ * Deeply names the given blank node by first naming it if it doesn't already
+ * have an appropriate prefix, and then by naming its properties and then
+ * references.
+ * 
+ * @param b the bnode to name.
+ */
+jsonld.Processor.prototype.deepNameBlankNode = function(b)
+{
+   // rename bnode (if not already renamed)
+   var iri = b['@']['@iri'];
+   var ng = this.ng;
+   if(!ng.inNamespace(iri))
+   {
+      this.renameBlankNode(b, ng.next());
+      iri = ng.current();
+      
+      var self = this;
+      var subjects = this.subjects;
+      
+      // FIXME: can bnode edge sorting be optimized out due to sorting them
+      // when they are unequal in other parts of this algorithm?
+      
+      // rename bnode properties
+      var props = this.edges.props[iri].bnodes.sort(
+         function(a, b) { return self.compareEdges(a, b); });
+      for(var i in props)
+      {
+         if(props[i].s in subjects)
+         {
+            this.deepNameBlankNode(subjects[props[i].s]);
+         }
+      }
+      
+      // rename bnode references
+      var refs = this.edges.refs[iri].bnodes.sort(
+         function(a, b) { return self.compareEdges(a, b); });
+      for(var i in refs)
+      {
+         if(refs[i].s in subjects)
+         {
+            this.deepNameBlankNode(subjects[refs[i].s]);
+         }
+      }
+   }
+};
+
+/**
+ * Canonically names blank nodes in the given input.
+ * 
+ * @param input the flat input graph to assign names to.
+ */
+jsonld.Processor.prototype.canonicalizeBlankNodes = function(input)
+{
+   // collect subjects and bnodes from flat input graph
+   var memo = this.memo = {};
+   var edges = this.edges =
+   {
+      refs: {},
+      props: {}
+   };
+   var subjects = this.subjects = {};
+   var bnodes = [];
+   for(var i in input)
+   {
+      var iri = input[i]['@']['@iri'];
+      subjects[iri] = input[i];
+      edges.refs[iri] =
+      {
+         all: [],
+         bnodes: []
+      };
+      edges.props[iri] =
+      {
+         all: [],
+         bnodes: []
+      };
+      if(_isBlankNodeIri(iri))
+      {
+         bnodes.push(input[i]);
+      }
+   }
+   
+   // build map of memoized bnode comparisons
+   for(var i1 in bnodes)
+   {
+      var iri1 = bnodes[i1]['@']['@iri'];
+      memo[iri1] = {};
+   }
+   
+   // collect edges in the graph
+   this.collectEdges();
+   
+   // sort blank nodes
+   var self = this;
+   bnodes.sort(function(a, b)
+   {
+      return self.deepCompareBlankNodes(a, b, {});
+   });
+   
+   // create canonical blank node name generator
+   var c14n = _createNameGenerator('c14n');
+   
+   // rename all bnodes that have canonical names to temporary names
+   var tmp = this.ng;
+   for(var i in bnodes)
+   {
+      var bnode = bnodes[i];
+      if(c14n.inNamespace(bnode['@']['@iri']))
+      {
+         // generate names until one is unique
+         while(tmp.next() in subjects);
+         this.renameBlankNode(bnode, tmp.current());
+      }
+   }
+   
+   // change internal name generator from tmp one to canonical one
+   this.ng = c14n;
+   
+   // deeply-iterate over bnodes canonically-naming them
+   for(var i in bnodes)
+   {
+      this.deepNameBlankNode(bnodes[i]);
+   }
+   
+   // sort property lists that now have canonically-named bnodes
+   for(var key in edges.props)
+   {
+      if(edges.props[key].bnodes.length > 0)
+      {
+         var bnode = subjects[key];
+         for(var p in bnode)
+         {
+            if(p.indexOf('@') !== 0 && bnode[p].constructor === Array)
+            {
+               bnode[p].sort(_compareObjects);
+            }
+         }
+      }
+   }
+};
+
+/**
+ * Checks to see if the given bnode IRIs are equivalent in the given
+ * isomorphism.
+ * 
+ * @param iso the isomorphism to check.
+ * @param iriA the first bnode IRI.
+ * @param iriB the second bnode IRI.
+ * @param cycle a map to prevent cycles when checking. 
+ * 
+ * @return true if iriA and iriB are for equivalent bnodes per the isomorphism.
+ */
+var _isIsoMatch = function(iso, iriA, iriB, cycle)
+{
+   var rval = false;
+   
+   if(iriA === iriB)
+   {
+      rval = true;
+   }
+   else if(iriA in iso)
+   {
+      if(iso[iriA] === iriB)
+      {
+         rval = true;
+      }
+      else if(!(iriA in cycle))
+      {
+         cycle[iriA] = true;
+         rval = _isIsoMatch(iso, iso[iriA], iriB, cycle);
+      }
+   }
+   else if(iriB in iso)
+   {
+      rval = _isIsoMatch(iso, iriB, iriA, cycle);
+   }
+   
+   return rval;
+};
+
+/**
+ * Compares the edges between two nodes for equivalence.
+ * 
+ * @param a the first bnode.
+ * @param b the second bnode.
+ * @param dir the edge direction ('props' or 'refs').
+ * @param iso the current subgraph isomorphism for connected bnodes.
+ * 
+ * @return -1 if a < b, 0 if a == b, 1 if a > b. 
+ */
+jsonld.Processor.prototype.deepCompareEdges = function(a, b, dir, iso)
+{
+   var rval = 0;
+   
+   /* Edge comparison algorithm:
+      1. Compare adjacent bnode lists for matches.
+      1.1. If a bnode IRI is in the potential isomorphism, then the other bnode
+         under the same edge must be equivalent in that isomorphism.
+      1.2. If a bnode IRI is not in the potential isomorphism yet, then the
+         associated bnode *must* have a bnode with the same edge that isn't
+         in the isomorphism yet to match up. Iterate over each bnode until an
+         equivalent one is found.
+      1.3. Recurse to compare the chosen bnodes.
+      1.4. The least bnode is the one with the least bnode for the edge.
+    */
+   
+   // for every bnode edge in A, make sure there's a match in B
+   var iriA = a['@']['@iri'];
+   var iriB = b['@']['@iri'];
+   var edgesA = this.edges[dir][iriA].bnodes;
+   var edgesB = this.edges[dir][iriB].bnodes;
+   for(var i1 = 0; i1 < edgesA.length && rval === 0; ++i1)
+   {
+      var found = false;
+      var edgeA = edgesA[i1];
+      
+      // step #1.1
+      if(edgeA.s in iso)
+      {
+         for(var i2 = 0;
+            !found && i2 < edgesB.length && edgesB[i2].p <= edgeA.p; ++i2)
+         {
+            var edgeB = edgesB[i2];
+            if(edgeB.p === edgeA.p && _isIsoMatch(iso, edgeA.s, edgeB.s, {}))
+            {
+               found = true;
+            }
+         }
+      }
+      // step #1.2
+      else
+      {
+         for(var i2 = 0; i2 < edgesB.length && edgesB[i2].p <= edgeA.p; ++i2)
+         {
+            var edgeB = edgesB[i2];
+            if(edgeB.p === edgeA.p)
+            {
+               // identical edge case
+               if(edgeA.s === edgeB.s)
+               {
+                  found = true;
+                  break;
+               }
+               else if(!(edgeB.s in iso))
+               {
+                  // add bnode pair temporarily to iso
+                  iso[edgeB.s] = edgeA.s;
+                  
+                  // step #1.3
+                  var sA = this.subjects[edgeA.s];
+                  var sB = this.subjects[edgeB.s];
+                  if(this.deepCompareBlankNodes(sA, sB, iso) === 0)
+                  {
+                     found = true;
+                     break;
+                  }
+                  
+                  // remove non-matching bnode pair from iso
+                  delete iso[edgeB.s];
+               }
+            }
+         }
+      }
+      
+      // step #1.4
+      if(!found)
+      {
+         // no matching bnode pair found, sort order is the bnode with the
+         // least bnode for edgeA's property
+         rval = this.compareEdgeType(a, b, edgeA.p, dir, iso);
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Compares bnodes along the same edge type to determine which is less.
+ * 
+ * @param a the first bnode.
+ * @param b the second bnode.
+ * @param p the property.
+ * @param dir the direction of the edge ('props' or 'refs').
+ * @param iso the current subgraph isomorphism for connected bnodes.
+ * 
+ * @return -1 if a < b, 0 if a == b, 1 if a > b.
+ */
+jsonld.Processor.prototype.compareEdgeType = function(a, b, p, dir, iso)
+{
+   var rval = 0;
+   
+   // compare adjacent bnodes for smallest
+   var adjA = this.getSortedAdjacents(a, p, dir, iso);
+   var adjB = this.getSortedAdjacents(a, p, dir, iso);
+   for(var i = 0; i < adjA.length && rval === 0; ++i)
+   {
+      rval = this.deepCompareBlankNodes(adjA[i], adjB[i], iso);
+   }
+   
+   return rval;
+};
+
+/**
+ * Returns the bnode properties for a particular bnode in sorted order.
+ * 
+ * @param b the bnode.
+ * @param p the property (edge type).
+ * @param direction the direction of the edge ('props' or 'refs').
+ * @param iso the current subgraph isomorphism for connected bnodes.
+ * 
+ * @return the sorted bnodes for the property.
+ */
+jsonld.Processor.prototype.getSortedAdjacents = function(b, p, dir, iso)
+{
+   var rval = [];
+   
+   // add all bnodes for the given property
+   var iri = b['@']['@iri'];
+   var edges = this.edges[dir][iri].bnodes;
+   for(var i = 0; i < edges.length && edges[i].p <= p; ++i)
+   {
+      if(edges[i].p === p)
+      {
+         rval.push(this.subjects[edges[i].s]);
+      }
+   }
+   
+   // sort bnodes
+   var self = this;
+   return rval.sort(function(a, b)
+   {
+      return self.deepCompareBlankNodes(a, b, iso);
+   });
+};
+
+/**
+ * Compares two blank nodes for equivalence.
+ * 
+ * @param a the first blank node.
+ * @param b the second blank node.
+ * @param iso the current subgraph isomorphism for connected bnodes.
+ * 
+ * @return -1 if a < b, 0 if a == b, 1 if a > b.
+ */
+jsonld.Processor.prototype.deepCompareBlankNodes = function(a, b, iso)
+{
+   var rval = 0;
+   
+   // compare IRIs
+   var iriA = a['@']['@iri'];
+   var iriB = b['@']['@iri'];
+   if(iriA === iriB)
+   {
+      rval = 0;
+   }
+   // use memoized comparison if available
+   else if(iriB in this.memo[iriA])
+   {
+      rval = this.memo[iriA][iriB];
+   }
+   else
+   {
+      // do shallow compare first
+      rval = this.shallowCompareBlankNodes(a, b);
+      if(rval !== 0)
+      {
+         // compare done
+         this.memo[iriA][iriB] = rval;
+         this.memo[iriB][iriA] = -rval;
+      }
+      // deep comparison is necessary
+      else
+      {
+         // compare properties
+         rval = this.deepCompareEdges(a, b, 'props', iso);
+         
+         // compare references
+         if(rval === 0)
+         {
+            rval = this.deepCompareEdges(a, b, 'refs', iso);
+         }
+         
+         // update memo
+         if(!(iriB in this.memo[iriA]))
+         {
+            this.memo[iriA][iriB] = rval;
+            this.memo[iriB][iriA] = -rval;
+         }
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Performs a shallow sort comparison on the given bnodes.
+ * 
+ * @param a the first bnode.
+ * @param b the second bnode.
+ * 
+ * @return -1 if a < b, 0 if a == b, 1 if a > b.
+ */
+jsonld.Processor.prototype.shallowCompareBlankNodes = function(a, b)
+{
+   var rval = 0;
+   
+   /* ShallowSort Algorithm (when comparing two bnodes):
+      1. Compare the number of properties.
+      1.1. The bnode with fewer properties is first.
+      2. Compare alphabetically sorted-properties.
+      2.1. The bnode with the alphabetically-first property is first.
+      3. For each property, compare object values.
+      4. Compare the number of references.
+      4.1. The bnode with fewer references is first.
+      5. Compare sorted references.
+      5.1. The bnode with the reference iri (vs. bnode) is first.
+      5.2. The bnode with the alphabetically-first reference iri is first.
+      5.3. The bnode with the alphabetically-first reference property is first.
+    */
+   var pA = Object.keys(a);
+   var pB = Object.keys(b);
+   
+   // step #1
+   rval = _compare(pA.length, pB.length);
+   
+   // step #2
+   if(rval === 0)
+   {
+      rval = _compare(pA.sort(), pB.sort());
+   }
+   
+   // step #3
+   if(rval === 0)
+   {
+      rval = _compareBlankNodeObjects(a, b);
+   }
+   
+   // step #4
+   if(rval === 0)
+   {
+      var edgesA = this.edges.refs[a['@']['@iri']].all;
+      var edgesB = this.edges.refs[b['@']['@iri']].all;
+      rval = _compare(edgesA.length, edgesB.length);
+   }
+   
+   // step #5
+   if(rval === 0)
+   {
+      for(var i = 0; i < edgesA.length && rval === 0; ++i)
+      {
+         rval = this.compareEdges(edgesA[i], edgesB[i]);
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Compares two edges. Edges with an IRI (vs. a bnode ID) come first, then
+ * alphabetically-first IRIs, then alphabetically-first properties. If a blank
+ * node appears in the blank node equality memo then they will be compared
+ * after properties, otherwise they won't be.
+ * 
+ * @param a the first edge.
+ * @param b the second edge.
+ * 
+ * @return -1 if a < b, 0 if a == b, 1 if a > b.
+ */
+jsonld.Processor.prototype.compareEdges = function(a, b)
+{
+   var rval = 0;
+   
+   var bnodeA = _isBlankNodeIri(a.s);
+   var bnodeB = _isBlankNodeIri(b.s);
+   var memo = this.memo;
+   
+   // if not both bnodes, one that is a bnode is greater
+   if(bnodeA != bnodeB)
+   {
+      rval = bnodeA ? 1 : -1;
+   }
+   else
+   {
+      if(!bnodeA)
+      {
+         rval = _compare(a.s, b.s);
+      }
+      if(rval === 0)
+      {
+         rval = _compare(a.p, b.p);
+      }
+      if(rval === 0 && bnodeA && a.s in memo && b.s in memo[a.s])
+      {
+         rval = memo[a.s][b.s];
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Populates the given reference map with all of the subject edges in the
+ * graph. The references will be categorized by the direction of the edges,
+ * where 'props' is for properties and 'refs' is for references to a subject as
+ * an object. The edge direction categories for each IRI will be sorted into
+ * groups 'all' and 'bnodes'.
+ */
+jsonld.Processor.prototype.collectEdges = function()
+{
+   var refs = this.edges.refs;
+   var props = this.edges.props;
+   
+   // collect all references and properties
+   for(var iri in this.subjects)
+   {
+      var subject = this.subjects[iri];
+      for(var key in subject)
+      {
+         if(key !== '@')
+         {
+            // normalize to array for single codepath
+            var object = subject[key];
+            var tmp = (object.constructor !== Array) ? [object] : object;
+            for(var i in tmp)
+            {
+               var o = tmp[i];
+               if(o.constructor === Object && '@iri' in o &&
+                  o['@iri'] in this.subjects)
+               {
+                  var objIri = o['@iri'];
+                  
+                  // map object to this subject
+                  refs[objIri].all.push({ s: iri, p: key });
+                  
+                  // map this subject to object
+                  props[iri].all.push({ s: objIri, p: key });
+               }
+            }
+         }
+      }
+   }
+   
+   // create sorted categories
+   var self = this;
+   for(var iri in refs)
+   {
+      refs[iri].all.sort(function(a, b) { return self.compareEdges(a, b); });
+      refs[iri].bnodes = refs[iri].all.filter(function(edge) {
+         return _isBlankNodeIri(edge.s)
+      });
+   }
+   for(var iri in props)
+   {
+      props[iri].all.sort(function(a, b) { return self.compareEdges(a, b); });
+      props[iri].bnodes = props[iri].all.filter(function(edge) {
+         return _isBlankNodeIri(edge.s);
+      });
+   }
+};
+
+/**
+ * Returns true if the given input is a subject and has one of the given types
+ * in the given frame.
+ * 
+ * @param input the input.
+ * @param frame the frame with types to look for.
+ * 
+ * @return true if the input has one of the given types.
+ */
+var _isType = function(input, frame)
+{
+   var rval = false;
+   
+   // check if type(s) are specified in frame and input
+   var type = jsonld.ns.rdf + 'type';
+   if(type in frame &&
+      input.constructor === Object && '@' in input && type in input)
+   {
+      var tmp = (input[type].constructor === Array) ?
+         input[type] : [input[type]];
+      var types = (frame[type].constructor === Array) ?
+         frame[type] : [frame[type]];
+      for(var t = 0; t < types.length && !rval; ++t)
+      {
+         type = types[t]['@iri'];
+         for(var i in tmp)
+         {
+            if(tmp[i]['@iri'] === type)
+            {
+               rval = true;
+               break;
+            }
+         }
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Returns true if the given input matches the given frame via duck-typing.
+ * 
+ * @param input the input.
+ * @param frame the frame to check against.
+ * 
+ * @return true if the input matches the frame.
+ */
+var _isDuckType = function(input, frame)
+{
+   var rval = false;
+   
+   // frame must not have a specific type
+   var type = jsonld.ns.rdf + 'type';
+   if(!(type in frame))
+   {
+      // get frame properties that must exist on input
+      var props = Object.keys(frame);
+      if(props.length === 0)
+      {
+         // input always matches if there are no properties
+         rval = true;
+      }
+      // input must be a subject with all the given properties
+      else if(input.constructor === Object && '@' in input)
+      {
+         rval = true;
+         for(i in props)
+         {
+            if(!(props[i] in input))
+            {
+               rval = false;
+               break;
+            }
+         }
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Recursively frames the given input according to the given frame.
+ * 
+ * @param subjects a map of subjects in the graph.
+ * @param input the input to frame.
+ * @param frame the frame to use.
+ * @param embeds a map of previously embedded subjects, used to prevent cycles.
+ * @param options the framing options.
+ * 
+ * @return the framed input.
+ */
+var _frame = function(subjects, input, frame, embeds, options)
+{
+   var rval = null;
+   
+   // prepare output, set limit, get array of frames
+   var limit = -1;
+   var frames;
+   if(frame.constructor === Array)
+   {
+      rval = [];
+      frames = frame;
+   }
+   else
+   {
+      frames = [frame];
+      limit = 1;
+   }
+   
+   // iterate over frames adding input matches to list
+   var values = [];
+   for(var i = 0; i < frames.length && limit !== 0; ++i)
+   {
+      // get next frame
+      frame = frames[i];
+      if(frame.constructor !== Object)
+      {
+         throw {
+            message: 'Invalid JSON-LD frame. Frame type is not a map or array.'
+         };
+      }
+      
+      // create array of values for each frame
+      values[i] = [];
+      for(var n = 0; n < input.length && limit !== 0; ++n)
+      {
+         // add input to list if it matches frame specific type or duck-type
+         if(_isType(input[n], frame) || _isDuckType(input[n], frame))
+         {
+            values[i].push(input[n]);
+            --limit;
+         }
+      }
+   }
+   
+   // for each matching value, add it to the output
+   for(var i1 in values)
+   {
+      for(var i2 in values[i1])
+      {
+         frame = frames[i1];
+         var value = values[i1][i2];
+         
+         // determine if value should be embedded or referenced
+         var embedOn = ('@embed' in frame) ?
+            frame['@embed'] : options.defaults.embedOn;
+         if(!embedOn)
+         {
+            // if value is a subject, only use subject IRI as reference 
+            if(value.constructor === Object && '@' in value)
+            {
+               value = value['@'];
+            }
+         }
+         else if(
+            value.constructor === Object &&
+            '@' in value && value['@']['@iri'] in embeds)
+         {
+            // TODO: possibly support multiple embeds in the future ... and
+            // instead only prevent cycles?
+            throw {
+               message: 'Multiple embeds of the same subject is not supported.',
+               subject: value['@']['@iri']
+            };
+         }
+         // if value is a subject, do embedding and subframing
+         else if(value.constructor === Object && '@' in value)
+         {
+            embeds[value['@']['@iri']] = true;
+            
+            // if explicit is on, remove keys from value that aren't in frame
+            var explicitOn = ('@explicit' in frame) ?
+               frame['@explicit'] : options.defaults.explicitOn;
+            if(explicitOn)
+            {
+               for(key in value)
+               {
+                  // always include subject
+                  if(key !== '@' && !(key in frame))
+                  {
+                     delete value[key];
+                  }
+               }
+            }
+            
+            // iterate over frame keys to do subframing
+            for(key in frame)
+            {
+               // skip keywords and type query
+               if(key.indexOf('@') !== 0 && key !== jsonld.ns.rdf + 'type')
+               {
+                  if(key in value)
+                  {
+                     // build input and do recursion
+                     input = (value[key].constructor === Array) ?
+                        value[key] : [value[key]];
+                     for(var n in input)
+                     {
+                        // replace reference to subject w/subject
+                        if(input[n].constructor === Object &&
+                           '@iri' in input[n] && input[n]['@iri'] in subjects)
+                        {
+                           input[n] = subjects[input[n]['@iri']];
+                        }
+                     }
+                     value[key] = _frame(
+                        subjects, input, frame[key], embeds, options);
+                  }
+                  else
+                  {
+                     // add null property to value
+                     value[key] = null;
+                  }
+               }
+            }
+         }
+         
+         // add value to output
+         if(rval === null)
+         {
+            rval = value;
+         }
+         else
+         {
+            rval.push(value);
+         }
+      }
+   }
+   
+   return rval;
+};
+
+/**
+ * Frames JSON-LD input.
+ * 
+ * @param input the JSON-LD input.
+ * @param frame the frame to use.
+ * @param options framing options to use.
+ * 
+ * @return the framed output.
+ */
+jsonld.Processor.prototype.frame = function(input, frame, options)
+{
+   var rval;
+   
+   // normalize input
+   input = jsonld.normalize(input);
+   
+   // save frame context
+   var ctx = null;
+   if('@context' in frame)
+   {
+      ctx = jsonld.mergeContexts(_createDefaultContext(), frame['@context']);
+   }
+   
+   // remove context from frame
+   frame = jsonld.removeContext(frame);
+   
+   // create framing options
+   // TODO: merge in options from function parameter
+   options =
+   {
+      defaults:
+      {
+         embedOn: true,
+         explicitOn: false
+      }
+   };
+   
+   // build map of all subjects
+   var subjects = {};
+   for(var i in input)
+   {
+      subjects[input[i]['@']['@iri']] = input[i];
+   }
+   
+   // frame input
+   rval = _frame(subjects, input, frame, {}, options);
+   
+   // apply context
+   if(ctx !== null && rval !== null)
+   {
+      rval = jsonld.addContext(ctx, rval);
+   }
+   
+   return rval;
+};
+
+/**
+ * Normalizes a JSON-LD object.
+ *
+ * @param input the JSON-LD object to normalize.
+ * 
+ * @return the normalized JSON-LD object.
+ */
+jsonld.normalize = function(input)
+{
+   return new jsonld.Processor().normalize(input);
+};
+
+/**
+ * Removes the context from a JSON-LD object.
+ *
+ * @param input the JSON-LD object to remove the context from.
+ * 
+ * @return the context-neutral JSON-LD object.
+ */
+jsonld.removeContext = function(input)
+{
+   var rval = null;
+   
+   if(input !== null)
+   {
+      var ctx = _createDefaultContext();
+      rval = _expand(ctx, null, input, false);
+   }
+
+   return rval;
+};
+
+/**
+ * Adds the given context to the given context-neutral JSON-LD object.
+ *
+ * @param ctx the new context to use.
+ * @param input the context-neutral JSON-LD object to add the context to.
+ * 
+ * @return the JSON-LD object with the new context.
+ */
+jsonld.addContext = function(ctx, input)
+{
+   var rval;
+
+   // TODO: should context simplification be optional? (ie: remove context
+   // entries that are not used in the output)
+   
+   ctx = jsonld.mergeContexts(_createDefaultContext(), ctx);
+   
+   // setup output context
+   var ctxOut = {};
+   
+   // compact
+   rval = _compact(ctx, null, input, ctxOut);
+   
+   // add context if used
+   if(Object.keys(ctxOut).length > 0)
+   {
+      // add copy of context to every entry in output array
+      if(rval.constructor === Array)
+      {
+         for(var i in rval)
+         {
+            rval[i]['@context'] = _cloneContext(ctxOut);
+         }
+      }
+      else
+      {
+         rval['@context'] = ctxOut;
+      }
+   }
+
+   return rval;
+};
+
+/**
+ * Changes the context of JSON-LD object "input" to "context", returning the
+ * output.
+ *
+ * @param ctx the new context to use.
+ * @param input the input JSON-LD object.
+ * 
+ * @return the output JSON-LD object.
+ */
+jsonld.changeContext = function(ctx, input)
+{
+   // remove context and then add new one
+   return jsonld.addContext(ctx, jsonld.removeContext(input));
+};
+
+/**
+ * Merges one context with another.
+ *
+ * @param ctx1 the context to overwrite/append to.
+ * @param ctx2 the new context to merge onto ctx1.
+ *
+ * @return the merged context.
+ */
+jsonld.mergeContexts = function(ctx1, ctx2)
+{
+   // copy contexts
+   var merged = _cloneContext(ctx1);
+   var copy = _cloneContext(ctx2);
+
+   // if the new context contains any IRIs that are in the merged context,
+   // remove them from the merged context, they will be overwritten
+   for(var key in copy)
+   {
+      // ignore special keys starting with '@'
+      if(key.indexOf('@') !== 0)
+      {
+         for(var mkey in merged)
+         {
+            if(merged[mkey] === copy[key])
+            {
+               delete merged[mkey];
+               break;
+            }
+         }
+      }
+   }
+
+   // @coerce must be specially-merged, remove from contexts
+   var coerceExists = ('@coerce' in merged) || ('@coerce' in copy);
+   if(coerceExists)
+   {
+      var c1 = ('@coerce' in merged) ? merged['@coerce'] : {};
+      var c2 = ('@coerce' in copy) ? copy['@coerce'] : {};
+      delete merged['@coerce'];
+      delete copy['@coerce'];
+   }
+
+   // merge contexts
+   for(var key in copy)
+   {
+      merged[key] = copy[key];
+   }
+   
+   // special-merge @coerce
+   if(coerceExists)
+   {
+      for(var type in c1)
+      {
+         // append existing-type properties that don't already exist
+         if(type in c2)
+         {
+            var p1 = c1[type];
+            var p2 = c2[type];
+            
+            // normalize props in c2 to array for single-code-path iterating
+            if(p2.constructor !== Array)
+            {
+               p2 = [p2];
+            }
+            
+            // add unique properties from p2 to p1
+            for(var i in p2)
+            {
+               var p = p2[i];
+               if((p1.constructor !== Array && p1 !== p) ||
+                  (p1.constructor === Array && p1.indexOf(p) == -1))
+               {
+                  if(p1.constructor === Array)
+                  {
+                     p1.push(p);
+                  }
+                  else
+                  {
+                     p1 = c1[type] = [p1, p];
+                  }
+               }
+            }
+         }
+      }
+      
+      // add new types from new @coerce
+      for(var type in c2)
+      {
+         if(!(type in c1))
+         {
+            c1[type] = c2[type]; 
+         }
+      }
+      
+      // ensure there are no property duplicates in @coerce
+      var unique = {};
+      var dups = [];
+      for(var type in c1)
+      {
+         var p = c1[type];
+         if(p.constructor === String)
+         {
+            p = [p];
+         }
+         for(var i in p)
+         {
+            if(!(p[i] in unique))
+            {
+               unique[p[i]] = true;
+            }
+            else if(dups.indexOf(p[i]) == -1)
+            {
+               dups.push(p[i]);
+            }
+         }
+      }
+
+      if(dups.length > 0)
+      {
+         throw {
+            message: 'Invalid type coercion specification. More than one ' +
+               'type specified for at least one property.',
+            duplicates: dups
+         };
+      }
+      
+      merged['@coerce'] = c1;
+   }
+
+   return merged;
+};
+
+/**
+ * Expands a term into an absolute IRI. The term may be a regular term, a
+ * CURIE, a relative IRI, or an absolute IRI. In any case, the associated
+ * absolute IRI will be returned.
+ *
+ * @param ctx the context to use.
+ * @param term the term to expand.
+ *
+ * @return the expanded term as an absolute IRI.
+ */
+jsonld.expandTerm = _expandTerm;
+
+/**
+ * Compacts an IRI into a term or CURIE if it can be. IRIs will not be
+ * compacted to relative IRIs if they match the given context's default
+ * vocabulary.
+ *
+ * @param ctx the context to use.
+ * @param iri the IRI to compact.
+ *
+ * @return the compacted IRI as a term or CURIE or the original IRI.
+ */
+jsonld.compactIri = function(ctx, iri)
+{
+   return _compactIri(ctx, iri, null);
+};
+
+/**
+ * Frames JSON-LD input.
+ * 
+ * @param input the JSON-LD input.
+ * @param frame the frame to use.
+ * @param options framing options to use.
+ * 
+ * @return the framed output.
+ */
+jsonld.frame = function(input, frame, options)
+{
+   return new jsonld.Processor().frame(input, frame, options);
+};
+
+})();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sandbox/sandbox.css	Tue Jul 05 22:24:05 2011 -0400
@@ -0,0 +1,54 @@
+textarea:focus, input:focus {
+   border: 2px solid #900;
+}
+
+textarea {
+   margin-top: 10px;
+   margin-bottom: 10px;
+   width:100% !important;
+   min-height:250px !important;
+}
+
+.clear { /* generic container (i.e. div) for floating buttons */
+    overflow: hidden;
+    width: 100%;
+}
+
+li.button-list
+{
+    display: block;
+    float: left;
+    margin-right: 6px;
+}
+
+li.button {
+    background: transparent url('bg_button.gif') no-repeat scroll top right;
+    color: #444;
+    display: block;
+    float: left;
+    font: normal 12px arial, sans-serif;
+    height: 24px;
+    margin-right: 6px;
+    padding-right: 18px; /* sliding doors padding */
+    text-decoration: none;
+    cursor: pointer;
+}
+
+li.button span {
+    background: transparent url('bg_button_span.gif') no-repeat;
+    display: block;
+    line-height: 14px;
+    padding: 5px 0 5px 18px;
+}
+
+li.button:active {
+    background-position: bottom right;
+    color: #000;
+    outline: none; /* hide dotted outline in Firefox */
+}
+
+li.button:active span {
+    background-position: bottom left;
+    padding: 6px 0 4px 18px; /* push text down 1px */
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sandbox/sandbox.js	Tue Jul 05 22:24:05 2011 -0400
@@ -0,0 +1,60 @@
+/**
+ * The JSON-LD Sandbox is used to test out JavaScript 
+ */
+(function($)
+{
+   window.sandbox = window.sandbox || {};
+   var sandbox = window.sandbox;
+
+   var examples = {};
+
+   sandbox.init = function()
+   {
+      $('#tabs').tabs();
+      
+      // Add the example of a Person
+      examples["Person"] =
+      {
+         "@context":
+         {
+            "name": "http://xmlns.com/0.1/foaf/name",
+            "homepage": "http://xmlns.com/0.1/foaf/homepage",
+            "xsd": "http://www.w3.org/2001/XMLSchema#",
+            "@coerce":
+            {
+               "xsd:anyURI": ["homepage"]
+            }
+         },
+         "name": "Manu Sporny",
+         "homepage": "http://manu.sporny.org/"
+      };
+   };
+   
+   sandbox.process = function()
+   {
+      var input = JSON.parse($("#markup").val());
+      var normalized = forge.jsonld.normalize(input);
+      var expanded = forge.jsonld.removeContext(input);
+      var compacted = forge.jsonld.changeContext(input["@context"] || {}, input);
+     
+      $("#normalized").html(js_beautify(JSON.stringify(normalized)),
+         { "indent_size": 3, "brace_style": "expand" });
+      $("#compacted").html(js_beautify(JSON.stringify(compacted)),
+         { "indent_size": 3, "brace_style": "expand" });
+      $("#expanded").html(js_beautify(JSON.stringify(expanded)),
+         { "indent_size": 3, "brace_style": "expand" });
+   }
+   
+   sandbox.populate = function(type)
+   {
+      if(type in examples)
+      {
+         $("#markup").val(js_beautify(JSON.stringify(examples[type]),
+            { "indent_size": 3, "brace_style": "expand" }));
+      }
+
+      sandbox.process();
+   };
+
+})(jQuery);
+