I have a problem with contenteditable
line breaks on SAFARI/CHROME. When I press \"return\" on a contentEditable
This technique appears to avoid the Chrome bug that does show BRs the first time (with the code you mentionned you need to push two times the Enter key).
It's not a perfect hack but it works: it adds a whitespace after your BR so it show properly.
However, you will see that adding only a whitespace " " will NOT change anything, it works with other letters. The browser will not show it, probably because it is like a blank space inside an html page, it simply does not mean anything. To get rid of that bug I created a div with jQuery that includes a
tag, and I take the text()
property to put it in the textnode otherwise it doesn't work.
$editables = $('[contenteditable=true]');
$editables.filter("p,span").on('keypress',function(e){
if(e.keyCode==13){ //enter && shift
e.preventDefault(); //Prevent default browser behavior
if (window.getSelection) {
var selection = window.getSelection(),
range = selection.getRangeAt(0),
br = document.createElement("br"),
textNode = document.createTextNode("\u00a0"); //Passing " " directly will not end up being shown correctly
range.deleteContents();//required or not?
range.insertNode(br);
range.collapse(false);
range.insertNode(textNode);
range.selectNodeContents(textNode);
selection.removeAllRanges();
selection.addRange(range);
return false;
}
}
});
Listening to keystrokes seems like a lot of work. Would something as simple as this be feasible:
var html = $('.content').html().replace(/<div>/gi,'<br>').replace(/<\/div>/gi,'');
JSFiddle demo here.
Also, it looks like sanitize.js allows a custom configuration. Have you tried adding div
as an allowed element?
Allowing HTML/CSS is dangerous, so ensure you are being extra cautious.
EDIT: Removed server side comments. Sanitising occurs at time of usage- if the client wants to bypass this locally it can be done so at their own risk. Thanks Vincent McNabb for pointing this out.
Webkits won’t render a single br
if it’s the last (non-empty) child of a container. For Safari, the native keystroke to insert a line break is Ctrl + Return. If you look closely, you notice that Safari actually inserts two br
s while only rendering one; however, it will insert a single br
if there is some trailing text, or will get rid of double br
s once you enter some (if there were doubles).
Apparently, it seems a solution for Webkits is to insert double br
s as they themselves do, and let them handle the rest.
You can simply use following command
document.execCommand('insertHTML',false,'<br>');
This command will prevent default behavior and add a tag you wish. That's all, 1 line of code
And even easier approach.
CSS ONLY.
To you contenteditable element apply display:inline-block
rule
and this will add brs only
You can use:
document.execCommand("defaultParagraphSeparator", false, "br");
The complete reference is here
see this Fiddle
Or this post
How to parse editable DIV's text with browser compatibility