annotate remedial.js @ 10:c0856c392096 default tip

Fixed link color on MSIE.
author Atul Varma <varmaa@toolness.com>
date Sun, 13 Jul 2008 14:12:39 +0000
parents 30b149f2cdf1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
1 // Taken from "Remedial Javascript" by Douglas Crockford:
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
2 // http://javascript.crockford.com/remedial.html
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
3
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
4 function typeOf(value) {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
5 var s = typeof value;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
6 if (s === 'object') {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
7 if (value) {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
8 if (typeof value.length === 'number' &&
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
9 !(value.propertyIsEnumerable('length')) &&
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
10 typeof value.splice === 'function') {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
11 s = 'array';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
12 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
13 } else {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
14 s = 'null';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
15 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
16 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
17 return s;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
18 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
19
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
20
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
21 function isEmpty(o) {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
22 var i, v;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
23 if (typeOf(o) === 'object') {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
24 for (i in o) {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
25 v = o[i];
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
26 if (v !== undefined && typeOf(v) !== 'function') {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
27 return false;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
28 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
29 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
30 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
31 return true;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
32 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
33
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
34 String.prototype.entityify = function () {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
35 return this.replace(/&/g, "&amp;").replace(/</g,
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
36 "&lt;").replace(/>/g, "&gt;");
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
37 };
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
38
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
39 String.prototype.quote = function () {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
40 var c, i, l = this.length, o = '"';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
41 for (i = 0; i < l; i += 1) {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
42 c = this.charAt(i);
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
43 if (c >= ' ') {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
44 if (c === '\\' || c === '"') {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
45 o += '\\';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
46 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
47 o += c;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
48 } else {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
49 switch (c) {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
50 case '\b':
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
51 o += '\\b';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
52 break;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
53 case '\f':
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
54 o += '\\f';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
55 break;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
56 case '\n':
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
57 o += '\\n';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
58 break;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
59 case '\r':
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
60 o += '\\r';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
61 break;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
62 case '\t':
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
63 o += '\\t';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
64 break;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
65 default:
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
66 c = c.charCodeAt();
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
67 o += '\\u00' + Math.floor(c / 16).toString(16) +
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
68 (c % 16).toString(16);
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
69 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
70 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
71 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
72 return o + '"';
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
73 };
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
74
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
75 String.prototype.supplant = function (o) {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
76 return this.replace(/{([^{}]*)}/g,
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
77 function (a, b) {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
78 var r = o[b];
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
79 return typeof r === 'string' || typeof r === 'number' ? r : a;
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
80 }
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
81 );
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
82 };
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
83
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
84 String.prototype.trim = function () {
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
85 return this.replace(/^\s+|\s+$/g, "");
30b149f2cdf1 Origination, copied over from http://code.google.com/p/parchment/ revision 32.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
86 };