Skip to content

Commit

Permalink
jQuery.print: keep track of seen objects to prevent infinite recursio…
Browse files Browse the repository at this point in the history
…n; truncate strings inside objcts
  • Loading branch information
tmm1 committed May 11, 2008
1 parent 49a5dda commit 4e47ead
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/jquery.print.js
Expand Up @@ -3,8 +3,8 @@
// helper functions

function print_array(obj, opts){
var max = obj.length > opts.maxArray ? opts.maxArray : obj.length
opts.maxString = 25
var max = obj.length > opts.maxArray ? opts.maxArray : obj.length

var result = [];
for (var i = 0; i < max; i++) {
Expand All @@ -25,10 +25,16 @@
">"
}

function print_object(obj){
function print_object(obj, opts){
opts.maxString = 25
if (!opts.seen) opts.seen = []

var result = []
for (var k in obj) {
result.push(k + ": " + $.print(obj[k]))
if (opts.seen.indexOf(obj[k]) == -1) {
opts.seen.push(obj[k])
result.push(k + ": " + $.print(obj[k], opts))
}
}

if (result.length == 0) return "{}"
Expand Down Expand Up @@ -107,7 +113,7 @@
return print_jquery(obj)

else if (obj instanceof Object)
return print_object(obj)
return print_object(obj, opts)

else
return obj.toString().replace(/\n\s*/g, '')
Expand Down

0 comments on commit 4e47ead

Please sign in to comment.