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