source: trunk/plugins/PreserveScripts/preserve-scripts.js @ 916

Last change on this file since 916 was 916, checked in by ray, 11 years ago

#1098 New plugin PreserveScripts? for enhanced editing of pages containing PHP and/or JavaScript?

File size: 1.8 KB
Line 
1/*------------------------------------------*\
2PreserveScripts for Xinha
3____________________
4Replace blocks of of PHP or JavaScript with icons in the editor, this way making it possible to edit sourcecode containing PHP,
5and preventing Javascript from being accidentally deleted because it's normally invisible
6
7\*------------------------------------------*/
8
9function PreserveScripts(editor) {
10        this.editor = editor;
11}
12
13PreserveScripts._pluginInfo = {
14  name          : "PreserveScripts",
15  version       : "1.0",
16  developer     : "Raimund Meyer",
17  developer_url : "http://x-webservice.net",
18  c_owner       : "Raimund Meyer",
19  sponsor       : "",
20  sponsor_url   : "",
21  license       : "LGPL"
22}
23Xinha.Config.prototype.PreserveScripts =
24{
25        'preservePHP' : true,
26        'preserveJS' : true
27}
28PreserveScripts.prototype.inwardHtml = function(html)
29{
30        var s = this;
31        var c = s.editor.config.PreserveScripts;
32        this.storage = {}; //empty the cache
33        var i = 1;
34        html = html.replace(/\n?<\?php(\s|[^\s])*?\?>\n?/ig,
35                function(m)
36                {
37                        if ( c.preservePHP ) // if config set to false wipe out php completely, otherwise ugly fragments may remain
38                        {
39                                s.storage['PreserveScripts_'+i] = m;
40                                var r = '<img title="PHP" id="PreserveScripts_'+i+'" src="'+_editor_url+'plugins/PreserveScripts/php.png" />';
41                                i++;
42                                return r;
43                        }
44                        else
45                        {
46                                return '';
47                        }
48                });
49        if ( c.preserveJS )
50        {
51                html = html.replace(/\n?<script(\s|[^\s])*?<\/script>\n?/g,
52                        function(m)
53                        {
54                                s.storage['PreserveScripts_'+i] = m;
55                                var r = '<img title="JavaScript" id="PreserveScripts_'+i+'" src="' + _editor_url + 'plugins/PreserveScripts/js.png" />';
56                                i++;
57                                return r;
58                        });     
59        }
60        return html;
61}
62
63PreserveScripts.prototype.outwardHtml = function(html)
64{
65        var s = this;
66        html = html.replace(/<img[^>]*id="(PreserveScripts_\d+)"[^>]*>/g,function(m0,m1){return s.storage[m1];});
67        return html;
68}
Note: See TracBrowser for help on using the repository browser.