From e8345cb671477a32da671065d85c9798ca3de906 Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Mon, 12 May 2008 01:05:50 -0700 Subject: [PATCH] jQuery.print: add support for RegExp (typeof /abc/ == "function"); prevent truncation of strings in error messages; print out interesting attributes on common DOM nodes --- lib/jquery.print.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/jquery.print.js b/lib/jquery.print.js index f76e791..07b3604 100644 --- a/lib/jquery.print.js +++ b/lib/jquery.print.js @@ -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 = [] @@ -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()' @@ -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)