2 * The "blockprotect" plugin. Adapted from the "placeholder" plugin.
9 var create_block = function(content, inside) {
10 if (inside) return content;
12 content = content.replace(/ /gi, ' ');
14 var el = document.createElement('SPAN');
16 if( typeof(el.textContent) != 'undefined' ) {
17 el.textContent = content;
18 } else if( typeof(el.innerText) != 'undefined' ) {
19 el.innerText = content;
21 el.setAttribute('class', 'cke_blockprotect');
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
28 var inner_writer = new CKEDITOR.htmlParser.basicWriter;
29 element.writeChildrenHtml(inner_writer);
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' ) {
39 var to_protected_html = function(data) {
44 var p = 0; // position in the string
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
52 } else if ((i < j || j == -1) && i != -1) {
53 // next delimiter is an open
54 // push everything from current position to
56 if ( i > p ) chunk += data.substr(p, i - p);
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;
67 // then output the chunk, and go to the start of the
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);
81 // end of a protected block
82 out += create_block(chunk, in_tag);
84 } else if ( depth < 0 ) {
91 // append any text after the last delimiter
93 out += create_block(data.substr(p), in_tag);
95 out += data.substr(p);
100 CKEDITOR.plugins.add( 'blockprotect', {
101 afterInit: function( editor ) {
102 CKEDITOR.addCss( '.cke_blockprotect' +
104 'background-color: #ffff88;' +
105 ( CKEDITOR.env.gecko ? 'cursor: default;' : '' ) +
109 // keep these from getting stripped out
110 editor.filter.allow('span(cke_blockprotect)',
111 'blockprotect', true);
113 // add filter at the front of toHtml
117 to_protected_html(evt.data.dataValue);
123 editor.dataProcessor.htmlFilter.addRules({
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);
137 editor.on( 'afterInsertHtml', function( evt ) {
138 // if this leaves an open SPAN, move the cursor
140 var range = editor.getSelection().getRanges()[0];
141 var ec = range.endContainer;
142 if ( ec.hasClass('cke_blockprotect') ) {
143 range.setEndAfter(ec);
145 editor.getSelection().selectRanges([ range ]);