]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/editor.dev.js
Wordpress 3.1
[autoinstalls/wordpress.git] / wp-admin / js / editor.dev.js
1
2 jQuery(document).ready(function($){
3         var h = wpCookies.getHash('TinyMCE_content_size');
4
5         if ( getUserSetting( 'editor' ) == 'html' ) {
6                 if ( h )
7                         $('#content').css('height', h.ch - 15 + 'px');
8         } else {
9                 if ( typeof tinyMCE != 'object' ) {
10                         $('#content').css('color', '#000');
11                 } else {
12                         $('#quicktags').hide();
13                 }
14         }
15 });
16
17 var switchEditors = {
18
19         mode : '',
20
21         I : function(e) {
22                 return document.getElementById(e);
23         },
24
25         _wp_Nop : function(content) {
26                 var blocklist1, blocklist2;
27
28                 // Protect pre|script tags
29                 if ( content.indexOf('<pre') != -1 || content.indexOf('<script') != -1 ) {
30                         content = content.replace(/<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function(a) {
31                                 a = a.replace(/<br ?\/?>(\r\n|\n)?/g, '<wp_temp>');
32                                 return a.replace(/<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp_temp>');
33                         });
34                 }
35
36                 // Pretty it up for the source editor
37                 blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset';
38                 content = content.replace(new RegExp('\\s*</('+blocklist1+')>\\s*', 'g'), '</$1>\n');
39                 content = content.replace(new RegExp('\\s*<((?:'+blocklist1+')(?: [^>]*)?)>', 'g'), '\n<$1>');
40
41                 // Mark </p> if it has any attributes.
42                 content = content.replace(/(<p [^>]+>.*?)<\/p>/g, '$1</p#>');
43
44                 // Sepatate <div> containing <p>
45                 content = content.replace(/<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n');
46
47                 // Remove <p> and <br />
48                 content = content.replace(/\s*<p>/gi, '');
49                 content = content.replace(/\s*<\/p>\s*/gi, '\n\n');
50                 content = content.replace(/\n[\s\u00a0]+\n/g, '\n\n');
51                 content = content.replace(/\s*<br ?\/?>\s*/gi, '\n');
52
53                 // Fix some block element newline issues
54                 content = content.replace(/\s*<div/g, '\n<div');
55                 content = content.replace(/<\/div>\s*/g, '</div>\n');
56                 content = content.replace(/\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n');
57                 content = content.replace(/caption\]\n\n+\[caption/g, 'caption]\n\n[caption');
58
59                 blocklist2 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|pre|fieldset';
60                 content = content.replace(new RegExp('\\s*<((?:'+blocklist2+')(?: [^>]*)?)\\s*>', 'g'), '\n<$1>');
61                 content = content.replace(new RegExp('\\s*</('+blocklist2+')>\\s*', 'g'), '</$1>\n');
62                 content = content.replace(/<li([^>]*)>/g, '\t<li$1>');
63
64                 if ( content.indexOf('<hr') != -1 ) {
65                         content = content.replace(/\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n');
66                 }
67
68                 if ( content.indexOf('<object') != -1 ) {
69                         content = content.replace(/<object[\s\S]+?<\/object>/g, function(a){
70                                 return a.replace(/[\r\n]+/g, '');
71                         });
72                 }
73
74                 // Unmark special paragraph closing tags
75                 content = content.replace(/<\/p#>/g, '</p>\n');
76                 content = content.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1');
77
78                 // Trim whitespace
79                 content = content.replace(/^\s+/, '');
80                 content = content.replace(/[\s\u00a0]+$/, '');
81
82                 // put back the line breaks in pre|script
83                 content = content.replace(/<wp_temp>/g, '\n');
84
85                 return content;
86         },
87
88         go : function(id, mode) {
89                 id = id || 'content';
90                 mode = mode || this.mode || '';
91
92                 var ed, qt = this.I('quicktags'), H = this.I('edButtonHTML'), P = this.I('edButtonPreview'), ta = this.I(id);
93
94                 try { ed = tinyMCE.get(id); }
95                 catch(e) { ed = false; }
96
97                 if ( 'tinymce' == mode ) {
98                         if ( ed && ! ed.isHidden() )
99                                 return false;
100
101                         setUserSetting( 'editor', 'tinymce' );
102                         this.mode = 'html';
103
104                         P.className = 'active';
105                         H.className = '';
106                         edCloseAllTags(); // :-(
107                         qt.style.display = 'none';
108
109                         ta.style.color = '#FFF';
110                         ta.value = this.wpautop(ta.value);
111
112                         try {
113                                 if ( ed )
114                                         ed.show();
115                                 else
116                                         tinyMCE.execCommand("mceAddControl", false, id);
117                         } catch(e) {}
118
119                         ta.style.color = '#000';
120                 } else {
121                         setUserSetting( 'editor', 'html' );
122                         ta.style.color = '#000';
123                         this.mode = 'tinymce';
124                         H.className = 'active';
125                         P.className = '';
126
127                         if ( ed && !ed.isHidden() ) {
128                                 ta.style.height = ed.getContentAreaContainer().offsetHeight + 24 + 'px';
129                                 ed.hide();
130                         }
131
132                         qt.style.display = 'block';
133                 }
134                 return false;
135         },
136
137         _wp_Autop : function(pee) {
138                 var blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6]|fieldset|legend|hr';
139
140                 if ( pee.indexOf('<object') != -1 ) {
141                         pee = pee.replace(/<object[\s\S]+?<\/object>/g, function(a){
142                                 return a.replace(/[\r\n]+/g, '');
143                         });
144                 }
145
146                 pee = pee.replace(/<[^<>]+>/g, function(a){
147                         return a.replace(/[\r\n]+/g, ' ');
148                 });
149
150                 // Protect pre|script tags
151                 if ( pee.indexOf('<pre') != -1 || pee.indexOf('<script') != -1 ) {
152                         pee = pee.replace(/<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function(a) {
153                                 return a.replace(/(\r\n|\n)/g, '<wp_temp_br>');
154                         });
155                 }
156
157                 pee = pee + '\n\n';
158                 pee = pee.replace(/<br \/>\s*<br \/>/gi, '\n\n');
159                 pee = pee.replace(new RegExp('(<(?:'+blocklist+')(?: [^>]*)?>)', 'gi'), '\n$1');
160                 pee = pee.replace(new RegExp('(</(?:'+blocklist+')>)', 'gi'), '$1\n\n');
161                 pee = pee.replace(/<hr( [^>]*)?>/gi, '<hr$1>\n\n'); // hr is self closing block element
162                 pee = pee.replace(/\r\n|\r/g, '\n');
163                 pee = pee.replace(/\n\s*\n+/g, '\n\n');
164                 pee = pee.replace(/([\s\S]+?)\n\n/g, '<p>$1</p>\n');
165                 pee = pee.replace(/<p>\s*?<\/p>/gi, '');
166                 pee = pee.replace(new RegExp('<p>\\s*(</?(?:'+blocklist+')(?: [^>]*)?>)\\s*</p>', 'gi'), "$1");
167                 pee = pee.replace(/<p>(<li.+?)<\/p>/gi, '$1');
168                 pee = pee.replace(/<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
169                 pee = pee.replace(/<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
170                 pee = pee.replace(new RegExp('<p>\\s*(</?(?:'+blocklist+')(?: [^>]*)?>)', 'gi'), "$1");
171                 pee = pee.replace(new RegExp('(</?(?:'+blocklist+')(?: [^>]*)?>)\\s*</p>', 'gi'), "$1");
172                 pee = pee.replace(/\s*\n/gi, '<br />\n');
173                 pee = pee.replace(new RegExp('(</?(?:'+blocklist+')[^>]*>)\\s*<br />', 'gi'), "$1");
174                 pee = pee.replace(/<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1');
175                 pee = pee.replace(/(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]');
176
177                 pee = pee.replace(/(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function(a, b, c) {
178                         if ( c.match(/<p( [^>]*)?>/) )
179                                 return a;
180
181                         return b + '<p>' + c + '</p>';
182                 });
183
184                 // put back the line breaks in pre|script
185                 pee = pee.replace(/<wp_temp_br>/g, '\n');
186
187                 return pee;
188         },
189
190         pre_wpautop : function(content) {
191                 var t = this, o = { o: t, data: content, unfiltered: content };
192
193                 jQuery('body').trigger('beforePreWpautop', [o]);
194                 o.data = t._wp_Nop(o.data);
195                 jQuery('body').trigger('afterPreWpautop', [o]);
196                 return o.data;
197         },
198
199         wpautop : function(pee) {
200                 var t = this, o = { o: t, data: pee, unfiltered: pee };
201
202                 jQuery('body').trigger('beforeWpautop', [o]);
203                 o.data = t._wp_Autop(o.data);
204                 jQuery('body').trigger('afterWpautop', [o]);
205                 return o.data;
206         }
207 };