-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stripes 953v2 - Export sanitize logic for usage at the module level. (#…
…79) * Only use DOMPurify's output if it changed something * export sanitize functionality for the module level so that sanitization can happen prior to the form state loop * actually add sanitizer.js * rename export to SANITIZE_CONFIG, document module-level sanitization * docs tweaks * document return type * Update CHANGELOG.md
- Loading branch information
Showing
6 changed files
with
56 additions
and
12 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 |
---|---|---|
@@ -1 +1 @@ | ||
export { TemplateEditor, PreviewModal, TokensSection, tokensReducer } from './src'; | ||
export { TemplateEditor, PreviewModal, TokensSection, tokensReducer, sanitize, SANITIZE_CONFIG } from './src'; |
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,16 @@ | ||
import DOMPurify from 'dompurify'; | ||
|
||
export const SANITIZE_CONFIG = { ADD_TAGS: ['Barcode'], ADD_ATTR: ['target', 'rel'] }; | ||
|
||
export const sanitize = (value, config = SANITIZE_CONFIG) => { | ||
// since DOMPurify has a known issue of reversing the order of attributes in perfectly admissible HTML | ||
// we check to see if the value was affected - and if not, we just return the unaffected value. | ||
let resultValue = DOMPurify.sanitize(value, config); | ||
if (value !== resultValue) { | ||
const removed = DOMPurify.removed.map((item) => item.attribute?.name || item.element?.outerHTML); | ||
if (removed && removed.length === 0) { | ||
resultValue = value; | ||
} | ||
} | ||
return resultValue; | ||
}; |