Skip to content

Instantly share code, notes, and snippets.

@muthuspark
Created November 23, 2016 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muthuspark/60dba6afba5f6cefd8a8bc9417179028 to your computer and use it in GitHub Desktop.
Save muthuspark/60dba6afba5f6cefd8a8bc9417179028 to your computer and use it in GitHub Desktop.
Content Editable using only DIV directive
function ContentEditable() {
String.prototype.sanitizeHTML = function(white, black) {
if (!white) white = "b|i|p|br|div"; //allowed tags
if (!black) black = "script|object|embed"; //complete remove tags
var e = new RegExp("(<(" + black + ")[^>]*>.*</\\2>|(?!<[/]?(" + white + ")(\\s[^<]*>|[/]>|>))<[^<>]*>|(?!<[^<>\\s]+)\\s[^</>]+(?=[/>]))", "gi");
return this.replace(e, "");
}
return {
restrict: "A",
require: "ngModel",
link: function(scope, element, attrs, ngModel) {
function read() {
// var tmp = document.createElement("DIV");
// tmp.innerHTML = element.html().sanitizeHTML();
// var toprint = tmp.textContent || tmp.innerText || "";
ngModel.$setViewValue(element.html().sanitizeHTML());
}
ngModel.$render = function() {
element.html(ngModel.$viewValue || "");
};
element.bind("blur keyup change", function() {
scope.$apply(read);
});
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment