-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlUtilities.js
30 lines (26 loc) · 1.09 KB
/
htmlUtilities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//thanks robert k from https://stackoverflow.com/questions/5796718/html-entity-decode
//Decodes HTML encoded strings
//Usage: decodedString = decodeEntities(string)
var decodeEntities = (function() {
// this prevents any overhead from creating the object each time
var element = document.createElement('div');
function decodeHTMLEntities (str) {
if(str && typeof str === 'string') {
// strip script/html tags
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
element.innerHTML = str;
str = element.textContent;
element.textContent = '';
}
return str;
}
return decodeHTMLEntities;
})();
//thanks david hedlund https://stackoverflow.com/questions/27422951/javascript-how-to-paste-as-plain-text-in-multiple-fields-or-elements
//Makes it so that pasting only pastes plain text
var plainTextPaste = function(e) {
e.preventDefault();
var text = e.clipboardData.getData("text/plain");
document.execCommand("insertHTML", false, text);
}