Reputation: 2757
I am trying to use ckeditor with cakephp and I've written a helper. The problem is when I enter some line breaks or add checkboxes (or other html elements) with CKeditor, the editor crashes the next time I edit the content. Firefox returns the following error:
SyntaxError: unterminated string literal
and highlights }).setData("<p>test</p>
from the section below:
<script type="text/javascript">CKEDITOR.replace('data[Template][body]',{
toolbar: '',
toolbarGroups: '',
width: 950,
height: 500
}).setData("<p>test</p>
<p> </p>
<p>test</p>");</script>
Here is the code in the cake helper:
$code = "CKEDITOR.replace('{$id}',{
{$options['toolbar']},
{$options['toolbarGroups']},
width: {$options['width']},
height: {$options['height']}
}).setData('" . trim($value). "');";
return $this->Javascript->codeBlock($code);
Any help is greatly appreciated.
Upvotes: 0
Views: 2186
Reputation: 2475
This is because you have linebreaks inside JavaScript string. You should output line breaks to HTML as "\n" so your HTML output would be like:
<script type="text/javascript">CKEDITOR.replace('data[Template][body]',{
toolbar: '',
toolbarGroups: '',
width: 950,
height: 500
}).setData("<p>test</p>\n<p> </p>\n<p>test</p>");</script>
So inside your helper:
$out .= "}).setData('" . str_replace("\n", '\n', $value). "');";
I've used single quotes so it will print out \n instead of line break;
Or you can use: str_replace("\n", "\\n", $value)
Upvotes: 2