當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript sanitize-html類代碼示例

本文整理匯總了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;未經允許,請勿轉載。