• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript sanitize-html类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中sanitize-html的典型用法代码示例。如果您正苦于以下问题:TypeScript sanitize-html类的具体用法?TypeScript sanitize-html怎么用?TypeScript sanitize-html使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了sanitize-html类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: function

import * as sanitize from 'sanitize-html';

let options: sanitize.IOptions = {
  allowedTags: sanitize.defaults.allowedTags.concat('h1', 'h2', 'img'),
  allowedAttributes: {
    'a': sanitize.defaults.allowedAttributes['a'].concat('rel'),
    'img': ['src', 'height', 'width', 'alt']
  },
	transformTags: { 
    'a': sanitize.simpleTransform('a', { 'rel': 'nofollow' }),
    'img': (tagName: string, attribs: sanitize.Attributes) => {
      let img = { tagName, attribs };
      img.attribs['alt'] = 'transformed' ;
      return img;
    }
  },
  exclusiveFilter: function(frame: sanitize.IFrame) {
    return frame.tag === 'a' && !frame.text.trim();
  },
  allowedSchemesByTag: {
    'a': ['http', 'https']
  },
  allowProtocolRelative: false
};

let unsafe = '<div><script>alert("hello");</script></div>';

let safe = sanitize(unsafe, options);
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:28,代码来源:sanitize-html-tests.ts


示例2: validate

	/**
	 * Checks the value stored to see if its correct in its current form
	 */
    public validate(): Promise<boolean | Error> {
        const transformedValue = this.value;
        const toRemove: number[] = [];
        for ( let i = 0, l = transformedValue.length; i < l; i++ ) {
            transformedValue[ i ] = sanitizeHtml( transformedValue[ i ].trim(), { allowedTags: [] } );

            if ( transformedValue[ i ].trim() === '' )
                toRemove.push( i );
        }

        // Remove any '' cells
        for ( let i = toRemove.length - 1; i >= 0; i-- )
            transformedValue.splice( toRemove[ i ], 1 );

        const maxCharacters = this.maxCharacters;
        const minCharacters = this.minCharacters;


        if ( transformedValue.length < this.minItems )
            return Promise.reject<Error>( new Error( `You must select at least ${this.minItems} item${( this.minItems === 1 ? '' : 's' )} for ${this.name}` ) );
        if ( transformedValue.length > this.maxItems )
            return Promise.reject<Error>( new Error( `You have selected too many items for ${this.name}, please only use up to ${this.maxItems}` ) );

        for ( let i = 0, l = transformedValue.length; i < l; i++ ) {
            transformedValue[ i ] = transformedValue[ i ].trim();
            if ( transformedValue[ i ].length > maxCharacters )
                return Promise.reject<Error>( new Error( `The character length of '${transformedValue[ i ]}' in ${this.name} is too long, please keep it below ${maxCharacters}` ) );
            else if ( transformedValue[ i ].length < minCharacters )
                return Promise.reject<Error>( new Error( `The character length of '${transformedValue[ i ]}' in ${this.name} is too short, please keep it above ${minCharacters}` ) );
        }

        return Promise.resolve( true );
    }
开发者ID:Webinate,项目名称:modepress,代码行数:36,代码来源:schema-text-array.ts


示例3: strictSanitize

export function strictSanitize(value: string) {
    const config = {
        allowedAttributes: {},
        allowedTags: [] as string[],
        textFilter(text: string) {
            return text.replace(/&quot;/g, '"');
        },
    };

    return sanitizeHtml(value, config);
}
开发者ID:lovett,项目名称:notifier,代码行数:11,代码来源:sanitize.ts


示例4: tolerantSanitize

export function tolerantSanitize(value: string) {
    const config = {
        allowedAttributes: {
            a: ['href'],
        },
        allowedSchemes: ['http', 'https', 'mailto'],
        allowedTags: ['b', 'i', 'em', 'strong', 'a', 'p'],
        textFilter(text: string) {
            return text.replace(/&quot;/g, '"');
        },
    };

    return sanitizeHtml(value, config);
}
开发者ID:lovett,项目名称:notifier,代码行数:14,代码来源:sanitize.ts


示例5: sanitize

export function sanitize(
  html: string,
  opts: IOptions,
  overrideOpts: (opts: sanitizeHTML.IOptions) => sanitizeHTML.IOptions,
): string {
  let sanitizeHTMLOpts = defaultSanitizeHTMLOptions as sanitizeHTML.IOptions;

  // Apply ting options
  opts = opts || {};
  sanitizeHTMLOpts.exclusiveFilter = (frame: sanitizeHTML.IFrame): boolean => {
    if (frame.attribs.id) {
      // id attribute is not allowed unless opts.idFilter returns true
      return opts.idFilter ? !opts.idFilter(frame.attribs.id as string) : true;
    }
    return false;
  };

  if (overrideOpts) {
    sanitizeHTMLOpts = overrideOpts(sanitizeHTMLOpts);
  }

  return sanitizeHTML(html, sanitizeHTMLOpts);
}
开发者ID:mgenware,项目名称:ting,代码行数:23,代码来源:main.ts


示例6: sanitize_

export const sanitize = (arg: string) => {
  return sanitize_(arg, { allowedTags });
};
开发者ID:CrossLead,项目名称:crosslead-adapters,代码行数:3,代码来源:util.ts



注:本文中的sanitize-html类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript scuid类代码示例发布时间:2022-05-28
下一篇:
TypeScript run-sequence类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap