Skip to content

Commit

Permalink
* Fix bug: Caption to image doesn't work with Wordpress 3.4 (http://…
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Kesicki committed Sep 19, 2012
1 parent 9377ed1 commit 5daeb54
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 35 deletions.
23 changes: 10 additions & 13 deletions includes/ckeditor.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,23 +353,20 @@ function getTinyMCEObject()
{
if (command == "mceInsertContent")
{
pattern = /(\[caption.+\])/ig;
if (pattern.test(text))
{
text = text.replace(/>/g, '>');
text = text.replace(/&lt;/g, '<');
pattern = /(<[\w'"=\s]+>([\S\s]+)<\/\S+>)/ig;
text= text.replace(pattern, function(match, cont)
//test if image has capiton and make necessary text format
pattern = /\[caption(.*)\]<.*>(.*)\[\/caption\]/i;
if (pattern.test(text)) {
replace_match = pattern.exec(text);
text = text.replace(/<img (.*) \/>/g, function( match, cont )
{
cont = cont.replace(/<[\w'"=\s]+>([\S\s]+)<\/\S+>/ig, function(match, cont){
return cont;
cont = cont.replace(/class="(.*)"/g, function( match, cont ){
tmp = 'class="' + cont + ' wp-caption"';
return tmp;
});
return cont;
tmp = '<img ' + cont + ' data-cke-caption=\'' + replace_match[1] + '\' data-cke-caption-text=\'' + replace_match[2] + '\' />' ;
return tmp;
});
text = text.replace(/<br\/>|<br>|<br \>|<br \/ >|<br\/ >/i,'');
text = text.replace(/"/i,'&quot;');
}

//setTimeout is required in IE8 when inserting Image gallery from an external modal dialog
if (typeof editorCKE == 'undefined') editorCKE = CKEDITOR.instances[ckeditorSettings.textarea_id];
setTimeout(function(){
Expand Down
2 changes: 1 addition & 1 deletion plugins/wpeditimage/editimage.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</label>
</th>
<td class="field">
<input type="text" id="img_cap" name="img_cap" value="" size="60" />
<textarea id="img_cap" name="img_cap" ></textarea>
</td>
</tr>

Expand Down
18 changes: 11 additions & 7 deletions plugins/wpeditimage/js/editimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,16 @@ wpImage = {
t.getImageData();
c = el.getAttribute('class');

pattern = /caption=("|'|&quot;)(.*)("|'|&quot;)/i;
match = pattern.exec(el.getAttribute('data-cke-caption'));
if (match && match[2])
/* pattern = /caption=("|'|&quot;)(.*)("|'|&quot;)/i;
match = pattern.exec(el.getAttribute('data-cke-caption'));*/

if (/*match && match[2]*/ el.getAttribute('data-cke-caption-text'))
{
tmp = match[2].replace(/&amp;/g,'&');
/*tmp = match[2].replace(/&amp;/g,'&');
tmp = tmp.replace(/&lt;/g,'<');
tmp = tmp.replace(/&gt;/g,'>');
f.img_cap.value = tmp;

f.img_cap.value = tmp;*/
f.img_cap.value = el.getAttribute('data-cke-caption-text');
}else
{
f.img_cap.value = '';
Expand Down Expand Up @@ -356,6 +357,7 @@ wpImage = {
//img_class = img_class.replace( /wp-caption/gi, '' );
el.removeClass('wp-caption');
el.removeAttribute('data-cke-caption');
el.removeAttribute('data-cke-caption-text');
window.parent.caption = '';
}
img_class = img_class.replace( /wp-caption/gi, '' );
Expand Down Expand Up @@ -438,14 +440,16 @@ wpImage = {
});
captionText = captionText.replace(/<br\/>|<br>|<br \>|<br \/ >|<br\/ >/i,'');
captionText = captionText.replace(/"/i,'&quot;');
el.setAttribute('data-cke-caption',' id="'+cap_id+'" align="'+t.align+'" width="'+f.width.value+'" caption="'+window.parent.CKEDITOR.tools.htmlEncode(captionText)+'"');
el.setAttribute('data-cke-caption',' id="'+cap_id+'" align="'+t.align+'" width="'+f.width.value+'"');
el.setAttribute('data-cke-caption-text',window.parent.CKEDITOR.tools.htmlEncode(captionText));
el.addClass('wp-caption');
el.addClass(t.align);
} else {
cap_id = '';
f.img_cap.value = '';
f.width.value = '';
el.removeAttribute('data-cke-caption');
el.removeAttribute('data-cke-caption-text');
el.removeClass('wp-caption');

if ( DIV) {
Expand Down
46 changes: 33 additions & 13 deletions plugins/wpgallery/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ if (!Array.prototype.indexOf)
}

var caption = '';
var image_attributes = [];
(function()
{
CKEDITOR.plugins.add( 'wpgallery',
Expand Down Expand Up @@ -259,10 +260,9 @@ var caption = '';
//clear caption from WYSIWYG mode and save its attributes in variable caption
text = text.replace(/\[caption(.*)\]/ig,function(match, cont)
{
caption = cont;
return '';
});
text = text.replace(/\[\/caption\]/ig,function(match, cont)
text = text.replace(/(.*)\[\/caption\]/ig,function(match, cont)
{
return '';
});
Expand All @@ -274,15 +274,15 @@ var caption = '';
{
//for image with caption add special attribute
var pattern = /wp-image-([0-9]+)/i;
if(element.attributes && pattern.test(element.attributes['class']))
if(element.attributes && pattern.test(element.attributes['class']) && image_attributes.length > 0)
{
var match = pattern.exec(element.attributes['class']);
pattern = new RegExp('id="attachment_'+match[1]+'"',"i");

if (match && match[1] && caption !== '' && pattern.test(caption) !== false)
{
//element.attributes['data-cke-caption'] =window.parent.CKEDITOR.tools.htmlEncode(caption);
pattern = new RegExp('wp-caption',"i");
obj = get_attribute_object(image_attributes, 'wp-image-' + match[1]);
if (obj) {
caption = obj['data-cke-caption'];
caption_text = obj['data-cke-caption-text'];
element.next.value = caption_text + '[/caption]';
pattern = new RegExp('wp-caption',"i");
if (!pattern.exec(element.attributes['class']))
{
element.attributes['class'] = element.attributes['class'] + ' wp-caption';
Expand All @@ -298,9 +298,9 @@ var caption = '';
element.attributes['class'] = element.attributes['class'] + ' '+match[1];
}
}
element.attributes['data-cke-caption'] = caption;
element.attributes['data-cke-caption'] = caption;
element.attributes['data-cke-caption-text'] = caption_text;
}

}
}
}
Expand Down Expand Up @@ -328,7 +328,19 @@ var caption = '';
//array of allowed attributes
var allowed_attributes = ['src', 'alt', 'title', 'width', 'height', 'class', 'style'];
text = element.attributes['data-cke-caption'];

pattern = /wp-image-[0-9]+/;
var match = pattern.exec(element.attributes['class']);
image_id = match.shift();

var attributes = element.attributes;

//build object with necessary attributes
obj = new Object();
obj.name = image_id;
obj.data = attributes;
image_attributes.push(obj);

var startElement = '<img ';
for (var i in attributes)
{
Expand All @@ -341,7 +353,7 @@ var caption = '';
var pattern = /amp;/ig;
if (!pattern.test(text))
text = CKEDITOR.tools.htmlEncode(text);
text = '[caption'+text+']'+html+'[/caption]';
text = '[caption '+text+']'+html+ element.attributes['data-cke-caption-text'] + '[/caption]';
delete element.name;

return new CKEDITOR.htmlParser.text(text);
Expand Down Expand Up @@ -427,4 +439,12 @@ var caption = '';
return null;
}
};
})();
})();

function get_attribute_object(container, name) {
for (var i in container) {
if (container[i].name == name)
return container[i].data;
}
return false;
}
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ Features:

== Changelog ==

= 3.6.4 DEV - 19.07.2012 =
= 3.6.4 DEV - 19.09.2012 =

* Update CKEditor to 3.6.4 version
* Fix bug: Caption to image doesn't work with Wordpress 3.4 (http://wordpress.org/support/topic/incompatible-with-wordpress-34)
* New feature: Link to existing content (http://wordpress.org/support/topic/plugin-ckeditor-for-wordpress-link-to-existing-content)

= V3.6.3 - 21.06.2012 =
Expand Down

0 comments on commit 5daeb54

Please sign in to comment.