Skip to content

Commit

Permalink
jQuery.print: add support for RegExp (typeof /abc/ == "function"); pr…
Browse files Browse the repository at this point in the history
…event truncation of strings in error messages; print out interesting attributes on common DOM nodes
  • Loading branch information
tmm1 committed May 12, 2008
1 parent fc0967f commit e8345cb
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/jquery.print.js
Expand Up @@ -19,15 +19,28 @@
}

function print_element(obj){
if (obj.nodeType == 1)
return "<" + obj.tagName.toLowerCase() +
(obj.className != "" ? " class='" + obj.className + "'" : "") +
(obj.id != "" ? " id='" + obj.id + "'" : "") +
">"
if (obj.nodeType == 1) {
var result = []
var properties = [ 'className', 'id' ]

var extra = {
'input': ['type', 'name', 'value'],
'a': ['href', 'target'],
'form': ['method', 'action']
}

$.each(properties.concat(extra[obj.tagName.toLowerCase()] || []), function(){
if (obj[this])
result.push(' ' + this.replace('className', 'class') + "=" + $.print(obj[this]))
})

return "<" + obj.tagName.toLowerCase()
+ result.join('') + ">"
}
}

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

var result = []
Expand Down Expand Up @@ -105,6 +118,9 @@
else if (typeof obj == 'string')
return print_string(obj, opts)

else if (obj instanceof RegExp)
return obj.toString()

else if (typeof obj == 'function' || obj instanceof Function)
return (m = obj.toString().match(/^([^\{]*?)\s*{/)) ? m[1].replace(' (','(') : 'function()'

Expand All @@ -117,6 +133,9 @@
else if (obj instanceof jQuery)
return print_jquery(obj)

else if (obj instanceof Error)
return print_object(obj, { maxString: 200 })

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

Expand Down

0 comments on commit e8345cb

Please sign in to comment.