Created
November 23, 2016 05:15
-
-
Save muthuspark/60dba6afba5f6cefd8a8bc9417179028 to your computer and use it in GitHub Desktop.
Content Editable using only DIV directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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