deb 8 perl path for make dev target
[freeside.git] / httemplate / elements / ckeditor / plugins / blockprotect / plugin.js
1 /**
2  * The "blockprotect" plugin.  Adapted from the "placeholder" plugin.
3  */
4
5 (function() {
6         var delim_o = '{';
7         var delim_c = '}';
8
9         var create_block = function(content, inside) {
10           if (inside) return content;
11           // fix nbsp's
12           content = content.replace(/ /gi, ' ');
13           // escape the content
14           var el = document.createElement('SPAN');
15           // IE8 compat
16           if( typeof(el.textContent) != 'undefined' ) {
17             el.textContent = content;
18           } else if( typeof(el.innerText) != 'undefined' ) {
19             el.innerText = content;
20           }
21           el.setAttribute('class', 'cke_blockprotect');
22           return el.outerHTML;
23         };
24         var block_writeHtml = function( element ) {
25           // to unescape the element contents, write it out as HTML,
26           // stick that into a SPAN element, and then extract the text
27           // content of that.
28           var inner_writer = new CKEDITOR.htmlParser.basicWriter;
29           element.writeChildrenHtml(inner_writer);
30
31           var el = document.createElement('SPAN');
32           el.innerHTML = inner_writer.getHtml();
33           if( typeof(el.textContent) != 'undefined' ) {
34             return el.textContent;
35           } else if( typeof(el.innerText) != 'undefined' ) {
36             return el.innerText;
37           }
38         };
39         var to_protected_html = function(data) {
40           var depth = 0;
41           var chunk = '';
42           var out = '';
43           var in_tag = false;
44           var p = 0; // position in the string
45           while( 1 ) {
46             // find the next delimiter of either kind
47             var i = data.indexOf(delim_o, p);
48             var j = data.indexOf(delim_c, p);
49             if (i == -1 && j == -1) {
50               // then there are no more delimiters
51               break;
52             } else if ((i < j || j == -1) && i != -1) {
53               // next delimiter is an open
54               // push everything from current position to 
55               // the delimiter
56               if ( i > p ) chunk += data.substr(p, i - p);
57               p = i + 1;
58               if ( depth == 0 ) {
59                 // we're in document text. find whether an HTML tag starts, 
60                 // or ends, before the next delimiter, so that we know whether
61                 // to output the next block in a SPAN or just as escaped text
62                 for(var q = 0; q < chunk.length; q++ ) {
63                   if (chunk[q] == '<') in_tag = true;
64                   if (chunk[q] == '>') in_tag = false;
65                 }
66
67                 // then output the chunk, and go to the start of the 
68                 // protected block
69                 out += chunk;
70                 chunk = '';
71               }
72               chunk += delim_o;
73               depth++;
74             } else if ((j < i || i == -1) && j != -1) {
75               // next delimiter is a close
76               if ( j > p ) chunk += data.substr(p, j - p);
77               p = j + 1;
78               depth--;
79               chunk += delim_c;
80               if ( depth == 0 ) {
81                 // end of a protected block
82                 out += create_block(chunk, in_tag);
83                 chunk = '';
84               } else if ( depth < 0 ) {
85                 depth = 0;
86               }
87             } else {
88               // can't happen
89             }
90           }
91           // append any text after the last delimiter
92           if ( depth ) {
93             out += create_block(data.substr(p), in_tag);
94           } else {
95             out += data.substr(p);
96           }
97           return out;
98         };
99
100         CKEDITOR.plugins.add( 'blockprotect', {
101                 afterInit: function( editor ) {
102                         CKEDITOR.addCss( '.cke_blockprotect' +
103                         '{' +
104                                         'background-color: #ffff88;' +
105                                         ( CKEDITOR.env.gecko ? 'cursor: default;' : '' ) +
106                                 '}'
107                         );
108                       
109                         // keep these from getting stripped out 
110                         editor.filter.allow('span(cke_blockprotect)',
111                                             'blockprotect', true);
112
113                         // add filter at the front of toHtml
114                         editor.on( 'toHtml',
115                           function( evt ) {
116                             evt.data.dataValue =
117                               to_protected_html(evt.data.dataValue);
118                             return evt;
119                           },
120                           this, null, 0
121                         );
122
123                         editor.dataProcessor.htmlFilter.addRules({
124                           elements: {
125                             span: function( element ) {
126                               if ( element.className = 'cke_blockprotect' ) {
127                                 // defeat HTML escaping
128                                 var content = block_writeHtml(element);
129                                 element.writeHtml = function(writer, filter) {
130                                   writer.text(content);
131                                 }
132                               }
133                             } // span function
134                           } // elements
135                         });
136                 }
137         }); // plugins.add
138 }) ();
139