0

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.

4

1 回答 1

2

这是因为 JavaScript 字符串中有换行符。您应该将换行符输出为 HTML 为“\n”,以便您的 HTML 输出如下所示:

<script type="text/javascript">CKEDITOR.replace('data[Template][body]',{
        toolbar: '',
        toolbarGroups: '',
        width: 950,
        height: 500
}).setData("<p>test</p>\n<p>&nbsp;</p>\n<p>test</p>");</script>

所以在你的助手里面:

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

我使用了单引号,所以它会打印出 \n 而不是换行符;

或者您可以使用:str_replace("\n", "\\n", $value)

于 2013-09-25T13:11:28.113 回答