I'm trying to turn some dumb quotes into curly quotes on the fly in a contenteditable
div (as seen here), using the following:
$("#editor").on("keyup", ".content", function () {
$(".content").html(function() {
var start = this.selectionStart, end = this.selectionEnd;
return $(this).html()
.replace(/'\b/g, "\u2018") // opening single
.replace(/\b'/g, "\u2019") // closing single
.replace(/"\b/g, "\u201c") // opening double
.replace(/\b"/g, "\u201d") // closing double
.replace(/--/g, "\u2014") // em-dash
.replace(/\b\u2018\b/g, "'"); // handle conjunctions
this.setSelectionRange(start, end);
});
// other stuff happens here...
});
The return
bit works fine on its own, but moves the caret position back to the start of the dive after every keystroke, which is obviously not desirable. But trying to keep the caret position (using some code seen here) throws Unreachable 'this' after 'return'
in JSHint and so doesn't actually do anything in the browser. Can someone point me in the right direction here, please?