Detalhes do pacote

@taiga-ui/dompurify

taiga-family7.1kApache-2.04.1.11

Inclusive Angular API for DOMPurify

angular, ng, dompurify, sanitizer

readme (leia-me)

NgDompurify

npm bundle size npm version code style: @tinkoff/linters

This library implements DOMPurify as Angular Sanitizer or Pipe. It delegates sanitizing to DOMPurify and supports the same configuration. See DOMPurify.

Read more about Sanitization in Angular and how ng-dompurify works in this article.

Install

npm install @taiga-ui/dompurify

If you do not have dompurify in your package, install also:

npm install dompurify
npm install --save-dev @types/dompurify

How to use

Either use pipe to sanitize your content when binding to [innerHTML] or use NgDompurifySanitizer service manually.

import {NgDompurifyModule} from '@taiga-ui/dompurify';

@NgModule({
  imports: [NgDompurifyModule],
})
export class MyModule {}

As a pipe:

<div [innerHtml]="value | dompurify"></div>

As a service:

import {SecurityContext} from '@angular/core';
import {NgDompurifySanitizer} from '@taiga-ui/dompurify';

@Component({})
export class MyComponent {
  constructor(private readonly dompurifySanitizer: NgDompurifySanitizer) {}

  purify(value: string): string {
    return this.dompurifySanitizer.sanitize(SecurityContext.HTML, value);
  }
}

You can also substitute Angular Sanitizer with DOMPurify so it is automatically used all the time:

import {NgModule, Sanitizer} from '@angular/core';
import {NgDompurifySanitizer} from '@taiga-ui/dompurify';
// ...

@NgModule({
  // ...
  providers: [
    {
      provide: Sanitizer,
      useClass: NgDompurifySanitizer,
    },
  ],
  // ...
})
export class AppModule {}

Configuring

Config for NgDompurifySanitizer or NgDompurifyDomSanitizer can be provided using token DOMPURIFY_CONFIG. NgDompurifyPipe supports passing DOMPurify config as an argument to override config from DI.

import {NgModule, Sanitizer} from '@angular/core';
import {NgDompurifySanitizer, DOMPURIFY_CONFIG} from '@taiga-ui/dompurify';
// ...

@NgModule({
  // ...
  providers: [
    {
      provide: Sanitizer,
      useClass: NgDompurifySanitizer,
    },
    {
      provide: DOMPURIFY_CONFIG,
      useValue: {FORBID_ATTR: ['id']},
    },
  ],
  // ...
})
export class AppModule {}

CSS sanitization

DOMPurify does not support sanitizing CSS. Angular starting version 10 dropped CSS sanitation as something that presents no threat in supported browsers. You can still provide a handler to sanitize CSS rules values upon binding if you want to:

import {NgModule, Sanitizer} from '@angular/core';
import {NgDompurifySanitizer, SANITIZE_STYLE} from '@taiga-ui/dompurify';

@NgModule({
  // ...
  providers: [
    {
      provide: Sanitizer,
      useClass: NgDompurifySanitizer,
    },
    {
      provide: SANITIZE_STYLE,
      useValue: yourImplementation, // <---
    },
  ],
  // ...
})
export class AppModule {}

Hooks

DOMPurify supports various hooks. You can provide them using DOMPURIFY_HOOKS token:

import {NgModule, Sanitizer} from '@angular/core';
import {NgDompurifySanitizer, DOMPURIFY_HOOKS, SANITIZE_STYLE} from '@taiga-ui/dompurify';

@NgModule({
  // ...
  providers: [
    {
      provide: Sanitizer,
      useClass: NgDompurifySanitizer,
    },
    {
      provide: SANITIZE_STYLE,
      useValue: yourImplementation,
    },
    {
      provide: DOMPURIFY_HOOKS,
      useValue: [
        {
          name: 'beforeSanitizeAttributes',
          hook: (node: Element) => {
            node.removeAttribute('id');
          },
        },
      ],
    },
  ],
  // ...
})
export class AppModule {}

Maintained

@taiga-ui/dompurify is a part of Taiga UI libraries family which is backed and used by a large enterprise. This means you can rely on timely support and continuous development.

License

🆓 Feel free to use our library in your commercial and private applications

All @taiga-ui/dompurify packages are covered by Apache 2.0

Read more about this license here

Demo

You can see live demo here: https://stackblitz.com/github/taiga-family/ng-dompurify/tree/master/projects/demo

changelog (log de mudanças)

4.1.10 (2024-11-21)

🐞 Bug Fixes

Changelog

All notable changes to this project will be documented in this file. See conventional commits guidelines.

4.1.9 (2024-09-27)

4.1.8 (2024-09-20)

🐞 Bug Fixes

4.1.7 (2024-09-10)

4.1.6 (2024-09-05)

4.1.5 (2024-08-28)

4.1.4 (2024-08-23)

4.1.3 (2024-08-22)

4.1.2 (2024-07-13)

4.1.1 (2024-07-05)

4.1.0 (2024-06-12)

4.0.1 (2024-06-12)

🚀 Features

  • css: support sanitizing CSS through provided handler (be0d3a6)
  • css: support sanitizing CSS through provided handler (1321a4f)
  • ngdompurifysanitizer: make service a single point of entrance to be able to attach hooks to DOMPurify in its constructor later (78ccfe9)
  • add angular 16 (#249) (1d986b5)
  • sanitizer: Remove DOM implementation as unnecessary and update … (#56) (d50cbdd)
  • ssr: support server side environment and update to DOMPurify 2+ (#30) (65ea43d)

🐞 Bug Fixes

  • service: implements now Angular Sanitizer insted of extending it to prevent problems in Ivy projects (2e7f7a7)
  • dependencies: make compatible with Angular 7 (7b3963a)
  • dependencies: set dependencies to be backwards compatible with Angular 6-7 (906b61d)
  • types: preserve ReadonlyArray in compiled code to support older TypeScript versions (387e87b)
  • types: preserve ReadonlyArray in compiled code to support older TypeScript versions (180e0dd)
  • ssr: fix type error in SSR environment (#41) (14299c5)
  • svg: fix SVG style vulnerability (#36) (87edb38)