/* see.js version 0.2
see
===
see.js: See and debug local variables.
The see.js debugger can be used to inspect and change local variable
state when you are not stopped at a breakpoint. This is a good solution
for debugging code wrapped in a top-level anonymous closure or for
debugging more complicated uses of closures. The see.js debugger
also provides simple in-page logging with tree view inspection of
objects; and it provides support for debugging using CoffeeScript.
Overview
--------
Any local scope can be debugged by calling eval(see.here)
within the scope of interest. For example, the following nicely
encapsulated code would normally be painful to debug without the
added see line:
(function() {
var private_var = 0;
function myclosuremaker() {
eval(see.here); // Debug variables visible in this scope.
var counter = 0;
return function() { ++counter; }
}
var inc = myclosuremaker();
inc();
})();
When eval(see.here) is called, the see debugger is shown at the
bottom of the page, an en eval hook is set up within the current scope.
The debugging panel works like the Firebug or Chrome debugger
console, except that it uses the eval hook to give visibility into
local variable scope. In this example, "counter" and "private_var"
and "inc" and "myclosuremaker" will all be visible symbols that
can be used and manipulated in the debugger.
Debugging multiple scopes
-------------------------
It is possible to attach to multiple scopes with a single program
by adding the following at the scope of interest:
eval(see.scope('scopename'));
To switch scopes within the interactive panel, just enter ":" followed
by the scope name, for example, ":scopename". ":top" goes to global
scope, and ":" goes back to the default scope defined at init.
![Screenshot of see panel](see-usage.png?raw=true)
Bookmarklet
-----------
This URL can be used as a bookmarklet that loads see.js on any page.
When you are using the bookmarklet, eval(see.here) calls
may not be present in the code, but it is possible to insert the
see eval loop by evaluating using your regular (Chrome or Firebug)
debugger to run eval(see.here) when at a breakpoint in the scope
of interest.
CoffeeScript
------------
The see.js script originally started as a teaching tool in a
CoffeeScript environment, so it also supports use of CoffeeScript
as the console language. Here it how to initialize see.js to
interpret code entered in the panel as CoffeeScript instead of
Javascript:
see.init(eval(see.cs))
Logging
-------
The top-level see function logs output to the see panel. Logged
objects are shown in a tree view of the object state at the
moment when the object is logged.
see(a, b, c);
The panel can be cleared with see.clear().
Using the regular debugger
--------------------------
To inspect an object visible to see in the regular debugger, just
use see.eval('mylocal'), which evaluates the expression in the scope
of interest and returns the value. To focus on a different named
scope, use the two-argument form, see.eval('scopename', 'myexpression').
More examples of usage
----------------------
see.init(); // Creates the interactive panel with global scope.
see.init({height: 30, title: 'test panel'}); // Sets some options.
eval(see.init()); // Does the same thing as eval(see.here).
eval(see.scope('name')); // Type ":name" in the panel to use this scope.
see(a, b, c); // Logs values into the panel.
see.loghtml('<b>ok</b>'); // Logs HTML without escaping.
r = see.repr(a, 3); // Builds a tree representation of a to depth 3.
x = see.noconflict(); // Relinguishes use of the 'see' name; use 'x'.
Options to pass to init
-----------------------
eval
The default function (or closure) to use to evaluate expressions.
this
The object to use as "this" within the evaluation.
depth
The depth to which to traverse logged and evaluated objects.
height
The pixel height of the interactive panel.
title
A title shown at the top of the panel.
panel
false if no interactive panel is desired.
console
Set to window.console to echo logging to the console also.
history
Set to false to disable localStorage use for interactive history.
linestyle
CSS style for a single log line.
element
(if panel is false) - The element into which to logging is done.
autoscroll
(if panel is false) - The element to autoscroll to bottom.
jQuery
A local copy of jQuery to reuse instead of loading one.
coffee
The CoffeeScript compiler object, for coffeescript support.
abbreviate
Array of return values (e.g. undefined) to silence in interaction.
noconflict
Name to use instead of "see".
Implementation notes
--------------------
If eval(see.init()) or eval(see.scope('name')) is called multiple
times for the same name, the scope is reset to the last scope set.
If multiple closures are created at the same line of code, that means that
you will only see the last one. You can generate a name using
eval(see.scope('name' + index)) if you want to preserve visibility
into many scopes.
When see.init() is called, a private (noconflict) copy of jQuery is
loaded if window.jQuery is not already present on the page (unless
the 'panel' option is false, or the 'jQuery' option is explicity
supplied to init).
Every expression entered in the panel is stored to '_loghistory' in
localStorage unless the 'history' option set to a different key name, or
set to false to disable history persistence.
*/
(function() {
var $ = window.jQuery;
var pulljQueryVersion = "1.9.1";
var seepkg = 'see'; // Defines the global package name used.
var version = '0.2';
var oldvalue = noteoldvalue(seepkg);
// Option defaults
var linestyle = 'position:relative;display:block;font-family:monospace;' +
'word-break:break-all;margin-bottom:3px;padding-left:1em;';
var logdepth = 5;
var autoscroll = false;
var logelement = 'body';
var panel = true;
var see; // defined below.
var paneltitle = '';
var logconsole = null;
var uselocalstorage = '_loghistory';
var panelheight = 100;
var currentscope = '';
var scopes = {
'': { e: window.eval, t: window },
top: { e: window.eval, t: window }
};
var coffeescript = window.CoffeeScript;
var seejs = '(function(){return eval(arguments[0]);})';
// If see has already been loaded, then return without doing anything.
if (window.see && window.see.js && window.see.js == seejs &&
window.see.version == version) {
return;
}
function init(options) {
if (arguments.length === 0) {
options = {};
} else if (arguments.length == 2) {
var newopt = {};
newopt[arguments[0]] = arguments[1];
options = newopt;
} else if (arguments.length == 1 && typeof arguments[0] == 'function') {
options = {'eval': arguments[0]};
}
if (options.hasOwnProperty('jQuery')) { $ = options.jQuery; }
if (options.hasOwnProperty('eval')) { scopes[''].e = options['eval']; }
if (options.hasOwnProperty('this')) { scopes[''].t = options['this']; }
if (options.hasOwnProperty('element')) { logelement = options.element; }
if (options.hasOwnProperty('autoscroll')) { autoscroll = options.autoscroll; }
if (options.hasOwnProperty('linestyle')) { linestyle = options.linestyle; }
if (options.hasOwnProperty('depth')) { logdepth = options.depth; }
if (options.hasOwnProperty('panel')) { panel = options.panel; }
if (options.hasOwnProperty('height')) { panelheight = options.height; }
if (options.hasOwnProperty('title')) { paneltitle = options.title; }
if (options.hasOwnProperty('console')) { logconsole = options.console; }
if (options.hasOwnProperty('history')) { uselocalstorage = options.history; }
if (options.hasOwnProperty('coffee')) { coffeescript = options.coffee; }
if (options.hasOwnProperty('abbreviate')) { abbreviate = options.abbreviate; }
if (options.hasOwnProperty('noconflict')) { noconflict(options.noconflict); }
if (panel) {
// panel overrides element and autoscroll.
logelement = '#_testlog';
autoscroll = '#_testscroll';
pulljQuery(tryinitpanel);
}
return scope();
}
function scope(name, evalfuncarg, evalthisarg) {
if (arguments.length <= 1) {
if (!arguments.length) {
name = '';
}
return seepkg + '.scope(' + cstring(name) + ',' + seejs + ',this)';
}
scopes[name] = { e: evalfuncarg, t: evalthisarg };
}
function seeeval(scope, code) {
if (arguments.length == 1) {
code = scope;
scope = '';
}
var ef = scopes[''].e, et = scopes[''].t;
if (scopes.hasOwnProperty(scope)) {
if (scopes[scope].e) { ef = scopes[scope].e; }
if (scopes[scope].t) { et = scopes[scope].t; }
}
return ef.call(et, code);
}
var varpat = '[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*';
var initialvardecl = new RegExp(
'^\\s*var\\s+(?:' + varpat + '\\s*,)*' + varpat + '\\s*;\\s*');
function barecs(s) {
// Compile coffeescript in bare mode.
var compiler = coffeescript || window.CoffeeScript;
var compiled = compiler.compile(s, {bare:1});
if (compiled) {
// Further strip top-level var decls out of the coffeescript so
// that assignments can leak out into the enclosing scope.
compiled = compiled.replace(initialvardecl, '');
}
return compiled;
}
function exportsee() {
see.repr = repr;
see.html = loghtml;
see.noconflict = noconflict;
see.init = init;
see.scope = scope;
see.eval = seeeval;
see.barecs = barecs;
see.here = 'eval(' + seepkg + '.init())';
see.clear = clear;
see.js = seejs;
see.cs = '(function(){return eval(' + seepkg + '.barecs(arguments[0]));})';
see.version = version;
window[seepkg] = see;
}
function noteoldvalue(name) {
return {
name: name,
has: window.hasOwnProperty(name),
value: window[name],
};
}
function restoreoldvalue(old) {
if (!old.has) {
delete window[old.name];
} else {
window[old.name] = old.value;
}
}
function noconflict(newname) {
if (!newname || typeof(newname) != 'string') {
newname = 'see' + (1 + Math.random() + '').substr(2);
}
if (oldvalue) {
restoreoldvalue(oldvalue);
}
seepkg = newname;
oldvalue = noteoldvalue(newname);
exportsee();
return see;
}
function pulljQuery(callback) {
if (!pulljQueryVersion || ($ && $.fn && $.fn.jquery)) {
callback();
return;
}
function loadscript(src, callback) {
function setonload(script, fn) {
script.onload = script.onreadystatechange = fn;
}
var script = document.createElement("script"),
head = document.getElementsByTagName("head")[0],
pending = 1;
setonload(script, function() {
if (pending && (!script.readyState ||
{loaded:1,complete:1}[script.readyState])) {
pending = 0;
callback();
setonload(script, null);
head.removeChild(script);
}
});
script.src = src;
head.appendChild(script);
}
loadscript(
'//ajax.googleapis.com/ajax/libs/jquery/' +
pulljQueryVersion + '/jquery.min.js',
function() {
$ = jQuery.noConflict(true);
callback();
});
}
// ---------------------------------------------------------------------
// LOG FUNCTION SUPPORT
// ---------------------------------------------------------------------
var logcss = "input._log:focus{outline:none;}label._log > span:first-of-type:hover{text-decoration:underline;}samp._log > label._log,samp_.log > span > label._log{display:inline-block;vertical-align:top;}label._log > span:first-of-type{margin-left:2em;text-indent:-1em;}label._log > ul{display:none;padding-left:14px;margin:0;}label._log > span:before{content:'';font-size:70%;font-style:normal;display:inline-block;width:0;text-align:center;}label._log > span:first-of-type:before{content:'\\0025B6';}label._log > ul > li{display:block;white-space:pre-line;margin-left:2em;text-indent:-1em}label._log > ul > li > samp{margin-left:-1em;text-indent:0;white-space:pre;}label._log > input[type=checkbox]:checked ~ span{margin-left:2em;text-indent:-1em;}label._log > input[type=checkbox]:checked ~ span:first-of-type:before{content:'\\0025BC';}label._log > input[type=checkbox]:checked ~ span:before{content:'';}label._log,label._log > input[type=checkbox]:checked ~ ul{display:block;}label._log > span:first-of-type,label._log > input[type=checkbox]:checked ~ span{display:inline-block;}label._log > input[type=checkbox],label._log > input[type=checkbox]:checked ~ span > span{display:none;}";
var addedcss = false;
var cescapes = {
'\0': '\\0', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r',
'\t': '\\t', '\v': '\\v', "'": "\\'", '"': '\\"', '\\': '\\\\'
};
var retrying = null;
var queue = [];
see = function see() {
if (logconsole && typeof(logconsole.log) == 'function') {
logconsole.log.apply(window.console, arguments);
}
var args = Array.prototype.slice.call(arguments);
queue.push('');
while (args.length) {
var obj = args.shift();
if (vtype(obj) == 'String') {
// Logging a string just outputs the string without quotes.
queue.push(htmlescape(obj));
} else {
queue.push(repr(obj, logdepth, queue));
}
if (args.length) { queue.push(' '); }
}
queue.push('');
flushqueue();
};
function loghtml(html) {
queue.push('');
queue.push(html);
queue.push('');
flushqueue();
}
function vtype(obj) {
var bracketed = Object.prototype.toString.call(obj);
var vt = bracketed.substring(8, bracketed.length - 1);
if (vt == 'Object') {
if ('length' in obj && 'slice' in obj && 'number' == typeof obj.length) {
return 'Array';
}
if ('originalEvent' in obj && 'target' in obj && 'type' in obj) {
return vtype(obj.originalEvent);
}
}
return vt;
}
function isprimitive(vt) {
switch (vt) {
case 'String':
case 'Number':
case 'Boolean':
case 'Undefined':
case 'Date':
case 'RegExp':
case 'Null':
return true;
}
return false;
}
function isdom(obj) {
return (obj.nodeType && obj.nodeName && typeof(obj.cloneNode) == 'function');
}
function midtruncate(s, maxlen) {
if (maxlen && maxlen > 3 && s.length > maxlen) {
return s.substring(0, Math.floor(maxlen / 2) - 1) + '...' +
s.substring(s.length - (Math.ceil(maxlen / 2) - 2));
}
return s;
}
function cstring(s, maxlen) {
function cescape(c) {
if (cescapes.hasOwnProperty(c)) {
return cescapes[c];
}
var temp = '0' + c.charCodeAt(0).toString(16);
return '\\x' + temp.substring(temp.length - 2);
}
if (s.indexOf('"') == -1 || s.indexOf('\'') != -1) {
return midtruncate('"' +
htmlescape(s.replace(/[\0-\x1f\x7f-\x9f"\\]/g, cescape)) + '"', maxlen);
} else {
return midtruncate("'" +
htmlescape(s.replace(/[\0-\x1f\x7f-\x9f'\\]/g, cescape)) + "'", maxlen);
}
}
function tiny(obj, maxlen) {
var vt = vtype(obj);
if (vt == 'String') { return cstring(obj, maxlen); }
if (vt == 'Undefined' || vt == 'Null') { return vt.toLowerCase(); }
if (isprimitive(vt)) { return '' + obj; }
if (vt == 'Array' && obj.length === 0) { return '[]'; }
if (vt == 'Object' && isshort(obj)) { return '{}'; }
if (isdom(obj) && obj.nodeType == 1) {
if (obj.hasAttribute('id')) {
return obj.tagName.toLowerCase() +
'#' + htmlescape(obj.getAttribute('id'));
} else {
if (obj.hasAttribute('class')) {
var classname = obj.getAttribute('class').split(' ')[0];
if (classname) {
return obj.tagName.toLowerCase() + '.' + htmlescape(classname);
}
}
return obj.tagName.toLowerCase();
}
}
return vt;
}
function isnonspace(dom) {
return (dom.nodeType != 3 || /[^\s]/.exec(dom.textContent));
}
function trimemptystartline(s) {
return s.replace(/^\s*\n/, '');
}
function isshort(obj, shallow, maxlen) {
var vt = vtype(obj);
if (isprimitive(vt)) { return true; }
if (!shallow && vt == 'Array') { return !maxlen || obj.length <= maxlen; }
if (isdom(obj)) {
if (obj.nodeType == 9 || obj.nodeType == 11) return false;
if (obj.nodeType == 1) {
return (obj.firstChild === null ||
obj.firstChild.nextSibling === null &&
obj.firstChild.nodeType == 3 &&
obj.firstChild.textContent.length <= maxlen);
}
return true;
}
if (vt == 'Function') {
var sc = obj.toString();
return (sc.length - sc.indexOf('{') <= maxlen);
}
if (vt == 'Error') {
return !!obj.stack;
}
var count = 0;
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
count += 1;
if (shallow && !isprimitive(vtype(obj[prop]))) { return false; }
if (maxlen && count > maxlen) { return false; }
}
}
return true;
}
function domsummary(dom, maxlen) {
var short;
if ('outerHTML' in dom) {
short = isshort(dom, true, maxlen);
var html = dom.cloneNode(short).outerHTML;
var tail = null;
if (!short) {
var m = /^(.*)(<\/[^\s]*>$)/.exec(html);
if (m) {
tail = m[2];
html = m[1];
}
}
return [htmlescape(html), tail && htmlescape(tail)];
}
if (dom.nodeType == 1) {
var parts = ['<' + dom.tagName];
for (var j = 0; j < dom.attributes.length; ++j) {
parts.push(domsummary(dom.attributes[j], maxlen)[0]);
}
short = isshort(dom, true, maxlen);
if (short && dom.firstChild) {
return [htmlescape(parts.join(' ') + '>' +
dom.firstChild.textContent + '' + dom.tagName + '>'), null];
}
return [htmlescape(parts.join(' ') + (dom.firstChild? '>' : '/>')),
!dom.firstChild ? null : htmlescape('' + dom.tagName + '>')];
}
if (dom.nodeType == 2) {
return [htmlescape(dom.name + '="' +
htmlescape(midtruncate(dom.value, maxlen), '"') + '"'), null];
}
if (dom.nodeType == 3) {
return [htmlescape(trimemptystartline(dom.textContent)), null];
}
if (dom.nodeType == 4) {
return ['', null];
}
if (dom.nodeType == 8) {
return ['', null];
}
if (dom.nodeType == 10) {
return ['', null];
}
return [dom.nodeName, null];
}
function summary(obj, maxlen) {
var vt = vtype(obj);
if (isprimitive(vt)) {
return tiny(obj, maxlen);
}
if (isdom(obj)) {
var ds = domsummary(obj, maxlen);
return ds[0] + (ds[1] ? '...' + ds[1] : '');
}
if (vt == 'Function') {
var ft = obj.toString();
if (ft.length - ft.indexOf('{') > maxlen) {
ft = ft.replace(/\{(?:.|\n)*$/, '').trim();
}
return ft;
}
if ((vt == 'Error' || vt == 'ErrorEvent') && 'message' in obj) {
return obj.message;
}
var pieces = [];
if (vt == 'Array' && obj.length < maxlen) {
var identical = (obj.length > 1);
var firstobj = identical && obj[0];
for (var j = 0; j < obj.length; ++j) {
if (identical && obj[j] !== firstobj) { identical = false; }
pieces.push(tiny(obj[j], maxlen));
}
if (identical) {
return '[' + tiny(firstobj, maxlen) + '] \xd7 ' + obj.length;
}
return '[' + pieces.join(', ') + ']';
} else if (isshort(obj, false, maxlen)) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
pieces.push(quotekey(key) + ': ' + tiny(obj[key], maxlen));
}
}
return (vt == 'Object' ? '{' : vt + '{') + pieces.join(', ') + '}';
}
if (vt == 'Array') { return 'Array(' + obj.length + ')'; }
return vt;
}
function quotekey(k) {
if (/^\w+$/.exec(k)) { return k; }
return cstring(k);
}
function htmlescape(s, q) {
var pat = /[<>&]/g;
if (q) { pat = new RegExp('[<>&' + q + ']', 'g'); }
return s.replace(pat, function(c) {
return c == '<' ? '<' : c == '>' ? '>' : c == '&' ? '&' :
c == '"' ? '"' : '' + c.charCodeAt(0) + ';';
});
}
function unindented(s) {
s = s.replace(/^\s*\n/, '');
var leading = s.match(/^\s*\S/mg);
var spaces = leading.length && leading[0].length - 1;
var j = 1;
// If the block begins with a {, ignore those spaces.
if (leading.length > 1 && leading[0].trim() == '{') {
spaces = leading[1].length - 1;
j = 2;
}
for (; j < leading.length; ++j) {
spaces = Math.min(leading[j].length - 1, spaces);
if (spaces <= 0) { return s; }
}
var removal = new RegExp('^\\s{' + spaces + '}', 'mg');
return s.replace(removal, '');
}
function expand(prefix, obj, depth, output) {
output.push('');
} else {
output.push(summary(obj, 10));
output.push('
');
var vt = vtype(obj);
if (vt == 'Function') {
var ft = obj.toString();
var m = /\{(?:.|\n)*$/.exec(ft);
if (m) { ft = m[0]; }
output.push('