forked from hinesboy/mavonEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add sanitizer for filtering HTML tags (hinesboy#744)
* fix: Add sanitizer for filtering HTML tags * fix: Do not share `markdown-it` instances * chore: fix lint Co-authored-by: wangsongc <[email protected]>
- Loading branch information
1 parent
8a2eb2a
commit b9489a3
Showing
7 changed files
with
107 additions
and
147 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
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
import { FilterXSS } from 'xss'; | ||
|
||
let xssHandler; | ||
|
||
function mavoneditor_sanitizer(state) { | ||
if (!xssHandler) { | ||
return; | ||
} | ||
sanitizer(state.tokens, ['inline', 'html_block']); | ||
} | ||
|
||
function sanitizer(tokens, types) { | ||
let originContent, children; | ||
for (let i = 0; i < tokens.length; i++) { | ||
if (types.indexOf(tokens[i].type) !== -1) { | ||
originContent = tokens[i].content; | ||
children = tokens[i].children; | ||
tokens[i].content = xssHandler.process(originContent); | ||
if (children && children.length && originContent !== tokens[i].content) { | ||
sanitizer(children, ['html_inline']); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default function (md, xssOptions) { | ||
if (md.options.html) { | ||
xssHandler = new FilterXSS(xssOptions); | ||
md.core.ruler.push('mavoneditor_sanitizer', mavoneditor_sanitizer); | ||
} | ||
} |
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