Skip to content

Commit

Permalink
add support for onTagRemoved
Browse files Browse the repository at this point in the history
  • Loading branch information
blazkablatnik authored and robikosir committed Nov 8, 2023
1 parent 372b929 commit 6f72b13
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,26 @@ function onIgnoreTagStripAll() {
return "";
}

function onTagRemoved() {
// do nothing by default
}

/**
* remove tag body
* specify a `tags` list, if the tag is not in the `tags` list then process by the specify function (optional)
*
* @param {array} tags
* @param {function} next
*/
function StripTagBody(tags, next) {
function StripTagBody(tags, next, onTagRemoved) {
if (typeof next !== "function") {
next = function () { };
}

if (typeof onTagRemoved !== "function") {
onTagRemoved = function () { };
}

var isRemoveAllTag = !Array.isArray(tags);
function isRemoveTag(tag) {
if (isRemoveAllTag) return true;
Expand All @@ -359,6 +367,7 @@ function StripTagBody(tags, next) {
return {
onIgnoreTag: function (tag, html, options) {
if (isRemoveTag(tag)) {
onTagRemoved(tag)
if (options.isValidEmpty) {
// Empty tags have no content to remove so just remove them
posStart = false;
Expand Down Expand Up @@ -444,6 +453,7 @@ exports.whiteList = getDefaultWhiteList();
exports.getDefaultWhiteList = getDefaultWhiteList;
exports.onTag = onTag;
exports.onIgnoreTag = onIgnoreTag;
exports.onTagRemoved = onTagRemoved;
exports.onTagAttr = onTagAttr;
exports.onIgnoreTagAttr = onIgnoreTagAttr;
exports.safeAttrValue = safeAttrValue;
Expand Down
5 changes: 4 additions & 1 deletion lib/xss.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function FilterXSS(options) {
options.onTag = options.onTag || DEFAULT.onTag;
options.onTagAttr = options.onTagAttr || DEFAULT.onTagAttr;
options.onIgnoreTag = options.onIgnoreTag || DEFAULT.onIgnoreTag;
options.onTagRemoved = options.onTagRemoved || DEFAULT.onTagRemoved;
options.onIgnoreTagAttr = options.onIgnoreTagAttr || DEFAULT.onIgnoreTagAttr;
options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
options.escapeHtml = options.escapeHtml || DEFAULT.escapeHtml;
Expand Down Expand Up @@ -133,6 +134,7 @@ FilterXSS.prototype.process = function (html) {
var whiteList = options.whiteList;
var onTag = options.onTag;
var onIgnoreTag = options.onIgnoreTag;
var onTagRemoved = options.onTagRemoved;
var onTagAttr = options.onTagAttr;
var onIgnoreTagAttr = options.onIgnoreTagAttr;
var safeAttrValue = options.safeAttrValue;
Expand All @@ -154,7 +156,8 @@ FilterXSS.prototype.process = function (html) {
if (options.stripIgnoreTagBody) {
stripIgnoreTagBody = DEFAULT.StripTagBody(
options.stripIgnoreTagBody,
onIgnoreTag
onIgnoreTag,
onTagRemoved
);
onIgnoreTag = stripIgnoreTagBody.onIgnoreTag;
}
Expand Down
11 changes: 10 additions & 1 deletion typings/xss.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare module "xss" {
onTag?: OnTagHandler;
onTagAttr?: OnTagAttrHandler;
onIgnoreTag?: OnTagHandler;
onTagRemoved?: OnTagRemovedHandler;
onIgnoreTagAttr?: OnTagAttrHandler;
safeAttrValue?: SafeAttrValueHandler;
escapeHtml?: EscapeHandler;
Expand Down Expand Up @@ -95,6 +96,10 @@ declare module "xss" {
video?: string[];
}

type OnTagRemovedHandler = (
tag: string
) => void;

type OnTagHandler = (
tag: string,
html: string,
Expand Down Expand Up @@ -133,6 +138,8 @@ declare module "xss" {

export type OnTagHandler = XSS.OnTagHandler;

export type OnTagRemovedHandler = XSS.OnTagRemovedHandler;

export type OnTagAttrHandler = XSS.OnTagAttrHandler;

export type SafeAttrValueHandler = XSS.SafeAttrValueHandler;
Expand All @@ -143,7 +150,8 @@ declare module "xss" {

export function StripTagBody(
tags: string[],
next: () => void
next: () => void,
onTagRemoved: () => void
): {
onIgnoreTag(
tag: string,
Expand Down Expand Up @@ -181,6 +189,7 @@ declare module "xss" {
export function getDefaultWhiteList(): IWhiteList;
export const onTag: OnTagHandler;
export const onIgnoreTag: OnTagHandler;
export const onTagRemoved: OnTagRemovedHandler;
export const onTagAttr: OnTagAttrHandler;
export const onIgnoreTagAttr: OnTagAttrHandler;
export const safeAttrValue: SafeAttrValueHandler;
Expand Down

0 comments on commit 6f72b13

Please sign in to comment.