-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refactorVersionHistory' of https://github.com/G-Ambatte…
…/homebrewery into refactorVersionHistory
- Loading branch information
Showing
7 changed files
with
819 additions
and
318 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Derived from the vue-html-secure package, customized for Homebrewery | ||
|
||
let doc = null; | ||
let div = null; | ||
|
||
function safeHTML(htmlString) { | ||
// If the Document interface doesn't exist, exit | ||
if(typeof document == 'undefined') 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.localName.indexOf('type') == 0 && test.value.match(/submit/i);}, | ||
(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.remove(); | ||
return; | ||
} | ||
// Check remaining elements for blacklisted attributes | ||
for (const attribute of element.attributes){ | ||
if(blacklistAttrs.some((test)=>{return test(attribute);})) { | ||
element.removeAttribute(attribute.localName); | ||
break; | ||
}; | ||
}; | ||
}); | ||
|
||
return div.innerHTML; | ||
}; | ||
|
||
module.exports.safeHTML = safeHTML; |
Oops, something went wrong.