最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to keep line breaks in CKEditor WYSIWYG editor - Stack Overflow

programmeradmin3浏览0评论

I have an HTML code as follows

<div class="editable" ...>
  <div class="code">
    This is an example.
    This is a new line
  </div>
</div>

In CSS, code has "word-wrap: pre" attribute, such that the text in the inner DIV will show two lines. I use CKEditor with DIV replacement method to edit it. However, it becomes

  <div class="code">
    This is an example.This is a new line
  </div>

The text inside the HTML tag will become one line long, beginning and trailing spaces and new line are stripped. So in CKEditor, although I have specified the config.contentsCss, it still shows one line because CKEditor has merge those two lines into one (I checked this in Chrome "Inspect Element" in CKEditor's iframe editor). Therefore, I see the source code or saved HTML, two lines format is not preserved because they are only one line.

I've googled and tried the CKEditor HTML writer or addRules to restrict the indent format and new line in begin/close tags, however, those seems work on HTML tags, not the document text. Is there any other methods to preserve line breaks of text?

I have an HTML code as follows

<div class="editable" ...>
  <div class="code">
    This is an example.
    This is a new line
  </div>
</div>

In CSS, code has "word-wrap: pre" attribute, such that the text in the inner DIV will show two lines. I use CKEditor with DIV replacement method to edit it. However, it becomes

  <div class="code">
    This is an example.This is a new line
  </div>

The text inside the HTML tag will become one line long, beginning and trailing spaces and new line are stripped. So in CKEditor, although I have specified the config.contentsCss, it still shows one line because CKEditor has merge those two lines into one (I checked this in Chrome "Inspect Element" in CKEditor's iframe editor). Therefore, I see the source code or saved HTML, two lines format is not preserved because they are only one line.

I've googled and tried the CKEditor HTML writer or addRules to restrict the indent format and new line in begin/close tags, however, those seems work on HTML tags, not the document text. Is there any other methods to preserve line breaks of text?

Share Improve this question asked Jul 8, 2013 at 13:38 jclinjclin 2,5592 gold badges22 silver badges27 bronze badges 6
  • You need the code formatted inside CKEditor? – aldux Commented Jul 10, 2013 at 20:34
  • NO, I don't want the code formatted, such that it keeps all whitespace s and line breaks before/after CKeditor. – jclin Commented Jul 10, 2013 at 23:27
  • Then you'll have to deal with line breaks (\n) and <br>'s – aldux Commented Jul 11, 2013 at 16:39
  • I don't have to deal with line breaks manually. Actually, if CKeditor think it can strip all begin and tail white-spaces (^[ \n\r\t]*|[ \n\r\t]*$), it means those does not affect the layout of line breaks viewed in the browser. But why this affect the result as I described in the problem because I have a CSS style of using word-wrap: pre, and CKeditor does not know that. It delete those white-spaces and make the viewing strange. And I find the solution, however, there is no config option to control that. It is acceptable with a very small hack. – jclin Commented Jul 18, 2013 at 0:52
  • 1 @jclin could you share the solution by answering your own question? – Edmund Sulzanok Commented Mar 13, 2014 at 19:24
 |  Show 1 more comment

6 Answers 6

Reset to default 7

I found it.

// preserve newlines in source
config.protectedSource.push( /\n/g ); 

http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-protectedSource

Use the <pre> HTML tag. Like this:

<div class="editable" ...>
  <div class="code"><pre>
    This is an example in a "pre".
    This is a new line
  </pre></div>
</div>

<div class="editable" ...>
      <div class="code">
        This is an example NOT in a "pre".
        Therefore this is NOT a new line
      </div>
    </div>

Or you can put a <br/> tag in between your lines. Its the ssame as hitting enter.

$(document).on('paste', 'textarea', function (e) {
    $(e.target).keyup(function (e) {
        var inputText = $(e.target).val();
        $(e.target).val(inputText.replace(/\n/g, '<br />'))
                .unbind('keyup');
    });
});

In my particular case, it was an extra tag, univis, that I needed to give similar semantics (i.e., leave indentation and inebreaks alone), and what we ended up doing was:

    CKEDITOR.dtd.$block.univis=1;
    CKEDITOR.dtd.$cdata.univis=1;
    CKEDITOR.dtd.univis=CKEDITOR.dtd.script;

But that looks like it might or might not be extensible to classes.

I got some Craft sites running and I don't want to paste the config file everywhere. For everyone else still having the problem: Just use redactor. Install and replace the field type. Correct everything once and you're done.

You can also simply use javascript replace function

.replace( /\n/g, '</p><p>' )
发布评论

评论列表(0)

  1. 暂无评论