Created
August 14, 2014 18:25
-
-
Save jacoor/bce42a3cbd968dca5a03 to your computer and use it in GitHub Desktop.
clearing clipboard contents from everything except <a href="*">*</a> on paste
This file contains hidden or 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
elm.bind('paste', function(e) { | |
//console.log(e); | |
e.preventDefault(); | |
var text = (e.originalEvent || e).clipboardData.getData("text/html"); | |
if (!text){ | |
console.log('no html'); | |
text = (e.originalEvent || e).clipboardData.getData("text/plain"); | |
} | |
var temp = $('<div />'); | |
$(temp).html(text); | |
$(temp).find('*').each(function(index, element){ | |
var text = element.innerText; | |
var newElement; | |
if (element.nodeName.toLowerCase() === 'a') { | |
newElement = document.createElement('a'); | |
newElement.href = element.href; | |
newElement.innerText = text; | |
} else { | |
newElement = document.createTextNode(text); | |
} | |
$(element).replaceWith(newElement); | |
}); | |
document.execCommand("insertHTML", false, temp.html()); | |
$scope.pasted = true; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment