ckeditor - 不在textarea中设置html数据

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>&nbsp;</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.

This is because you have linebreaks inside JavaScript string. You should output line breaks to HTML as " " 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>
<p>&nbsp;</p>
<p>test</p>");</script>

So inside your helper:

$out .= "}).setData('" .  str_replace("
", '
', $value). "');";

I've used single quotes so it will print out instead of line break;

Or you can use: str_replace(" ", "\ ", $value)