mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-04 01:42:42 +00:00
Remove vue-html-secure package
This commit is contained in:
44
client/homebrew/brewRenderer/safeHTML.js
Normal file
44
client/homebrew/brewRenderer/safeHTML.js
Normal file
@@ -0,0 +1,44 @@
|
||||
let doc = null;
|
||||
let div = null;
|
||||
|
||||
function safeHTML(htmlString) {
|
||||
// If the Document interface doesn't exist, exit
|
||||
if(!document) return null;
|
||||
// If the test document and div don't exist, create them
|
||||
if(!doc) doc = document.implementation.createHTMLDocument('');
|
||||
if(!div) div = doc.createElement('div');
|
||||
|
||||
// Set the test div contents to the evaluation string
|
||||
div.innerHTML = htmlString;
|
||||
// Grab all nodes from the test div
|
||||
const elements = div.querySelectorAll('*');
|
||||
|
||||
// Blacklisted tags
|
||||
const blacklistTags = ['script', 'noscript', 'noembed'];
|
||||
// Tests to remove attributes
|
||||
const blacklistAttrs = [
|
||||
(test)=>{return test.localName.indexOf('on') == 0;},
|
||||
(test)=>{return test.value.replace(/[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205f\u3000]/g, '').toLowerCase().trim().indexOf('javascript:') == 0;}
|
||||
];
|
||||
|
||||
|
||||
elements.forEach((element)=>{
|
||||
// Check each element for blacklisted type
|
||||
if(blacklistTags.includes(element?.localName?.toLowerCase())) {
|
||||
element.parentNode.removeChild(element);
|
||||
return;
|
||||
}
|
||||
// Check remaining elements for blacklisted attributes
|
||||
if(element.hasAttributes()){
|
||||
for (const attribute of element.attributes){
|
||||
let result = false;
|
||||
blacklistAttrs.forEach((test)=>{result ||= test(attribute);});
|
||||
if(result) element.removeAttribute(attribute.localName);
|
||||
};
|
||||
};
|
||||
});
|
||||
|
||||
return div.innerHTML;
|
||||
};
|
||||
|
||||
module.exports.safeHTML = safeHTML;
|
||||
Reference in New Issue
Block a user