Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

jQuery.print: add support for RegExp (typeof /abc/ == "function"); pr…

…event truncation of strings in error messages; print out interesting attributes on common DOM nodes
  • Loading branch information...
commit e8345cb671477a32da671065d85c9798ca3de906 1 parent fc0967f
@tmm1 tmm1 authored
Showing with 25 additions and 6 deletions.
  1. +25 −6 lib/jquery.print.js
View
31 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)
Please sign in to comment.
Something went wrong with that request. Please try again.