How can I refuse to enter blank paragraphs in CKEDITOR?

I posted a related note in How to clear Django + CKeditor breaks? on how to remove infringing empty paragraphs from Python / Django.

However, I would like to ask more directly whether CKeditor gives you the option to discard the added blank paragraphs or to delete all blank paragraphs before posting.

Example HTML source with one programmatically added empty paragraph (indents are highlighted with one tab, not spaces):

<p> This little bunny is <em>shy</em>, but also very affectionate.</p> <p> &nbsp;</p> <p> Would you like to meet her?</p> 

I get one paragraph added per save.

How can I prevent, exclude, etc. add blank paragraphs except at the initiative of the user?

Thanks,

+5
source share
1 answer

The problem is that you delete the opening and closing closing tags before saving them in your database, which leaves the inconsistent html tags and therefore invalid html (... whenever you have more than one p block). In this way,

 <p>Something</p> <p>Something else</p> 

becomes:

 Something</p> // invalid html - closing tag with no corresponding opening tag <p>Something else // invalid html - opening tag with no corresponding closing tag 

CKEditor is actually trying to fix the wrong html for you when loading back to the editor.

If you do not need paragraph tags, I would suggest using the configuration option config.autoParagraph = false; and / or config.enterMode = CKEDITOR.ENTER_BR; (CKEditor will use <br /> instead of having the user enter a new line) only depending on your requirements.

Another possible solution would be to change your function before saving to search and replace all occurrences of '<p>' with '' and '</p>' with '<br />' , before permanently deleting any trailing '<br />' .

Steps to duplicate the problem:

a. You can go to http://sdk.ckeditor.com/samples/accessibilitychecker.html and click the Source button and paste the following:

 <p>Something</p> <p>Something else</p> 

C. Click Source and it will display this html correctly.

C. Press Source again to return to the original mode, and only 2 p will show

E. Remove the opening paragraphs and closing paragraph tags:

 Something</p> // invalid html - closing tag with no corresponding opening tag <p>Something else // invalid html - opening tag with no corresponding closing tag 

E Double-click Source to display the output, and then to see the adjusted source:

 <p>Something</p> <p>&nbsp;</p> <p>Something else</p> 
+2
source

Source: https://habr.com/ru/post/1265581/


All Articles