forked from nathansobo/screw-unit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.print.js
126 lines (97 loc) · 3.05 KB
/
jquery.print.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
(function($) {
// helper functions
function print_array(obj, opts){
opts.maxString = 25
var max = obj.length > opts.maxArray ? opts.maxArray : obj.length
var result = [];
for (var i = 0; i < max; i++) {
result.push($.print(obj[i], opts));
}
if (obj.length > max)
result.push('... ' + (obj.length - max) + ' more')
if (result.length == 0) return "[]"
return "[ " + result.join(", ") + " ]"
}
function print_element(obj){
return "<" + obj.tagName.toLowerCase() +
(obj.className != "" ? " class='" + obj.className + "'" : "") +
(obj.id != "" ? " id='" + obj.id + "'" : "") +
">"
}
function print_object(obj, opts){
opts.maxString = 25
if (!opts.seen) opts.seen = []
var result = []
for (var k in obj) {
if (opts.seen.indexOf(obj[k]) == -1) {
opts.seen.push(obj[k])
result.push(k + ": " + $.print(obj[k], opts))
}
}
if (result.length == 0) return "{}"
return "{ " + result.join(", ") + " }"
}
function print_jquery(obj){
var result = []
obj.each(function(){ result.push(this) })
return "jQuery(length="+obj.length+")" + $.print(result)
}
function print_string(value, opts){
// adapted from json2.js
var m = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
}
var r = /["\\\x00-\x1f\x7f-\x9f]/g
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe sequences.
var str = r.test(value) ?
'"' + value.replace(r, function (a) {
var c = m[a];
if (c) {
return c;
}
c = a.charCodeAt();
return '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"' :
'"' + value + '"';
if (opts.maxString && str.length > opts.maxString)
return str.slice(0, opts.maxString) + '..."'
else
return str
}
$.print = function(obj, options) {
var opts = $.extend({}, $.print.options, options || {})
if (typeof obj == 'undefined')
return "undefined"
else if (typeof obj == 'boolean')
return obj.toString()
else if (!obj)
return "null"
else if (typeof obj == 'string')
return print_string(obj, opts)
else if ($.isFunction(obj))
return obj.toString().match(/^([^\{]*)\s{/)[1]
else if (obj instanceof Array)
return print_array(obj, opts)
else if (obj instanceof HTMLElement)
return print_element(obj)
else if (obj instanceof jQuery)
return print_jquery(obj)
else if (obj instanceof Object)
return print_object(obj, opts)
else
return obj.toString().replace(/\n\s*/g, '')
}
$.print.options = {
maxArray: 6,
maxString: 0
}
})(jQuery);