Skip to content

Commit

Permalink
feat(module:theme): add url pipe (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Nov 5, 2018
1 parent 145e4a9 commit 62724a0
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/theme/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export { DatePipe } from './src/pipes/date/date.pipe';
export { CNCurrencyPipe } from './src/pipes/currency/cn-currency.pipe';
export { KeysPipe } from './src/pipes/keys/keys.pipe';
export { YNPipe } from './src/pipes/yn/yn.pipe';
export { HTMLPipe } from './src/pipes/safe/html.pipe';
export { URLPipe } from './src/pipes/safe/url.pipe';
export { AlainThemeConfig } from './src/theme.config';
export { AlainThemeModule } from './src/theme.module';
export { VERSION } from './src/version';
7 changes: 0 additions & 7 deletions packages/theme/src/pipes/html/index.en-US.md

This file was deleted.

7 changes: 0 additions & 7 deletions packages/theme/src/pipes/html/index.zh-CN.md

This file was deleted.

File renamed without changes.
21 changes: 21 additions & 0 deletions packages/theme/src/pipes/safe/index.en-US.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
order: 1
title: safe-XSS
type: Pipe
---

## html

Simplify the use of `bypassSecurityTrustHtml`.

```html
<div [innerHTML]="content | html"></div>
```

## url

Simplify the use of `bypassSecurityTrustUrl`.

```html
<a [href]="file_url | url" target="_blank"></a>
```
21 changes: 21 additions & 0 deletions packages/theme/src/pipes/safe/index.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
order: 1
title: safe-XSS
type: Pipe
---

## html

简化 `bypassSecurityTrustHtml` 的使用。

```html
<div [innerHTML]="content | html"></div>
```

## url

简化 `bypassSecurityTrustUrl` 的使用。

```html
<a [href]="file_url | url" target="_blank"></a>
```
38 changes: 38 additions & 0 deletions packages/theme/src/pipes/safe/url.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { AlainThemeModule } from '../../theme.module';

describe('Pipe: url', () => {
let fixture: ComponentFixture<TestComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [AlainThemeModule.forRoot()],
declarations: [TestComponent],
});
fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
});

[
{ value: '', result: `` },
{ value: 'https://ng-alain.com', result: `https://ng-alain.com` },
].forEach((item: any) => {
it(`${item.value.toString()} muse be ${item.result}`, () => {
fixture.componentInstance.value = item.value;
fixture.detectChanges();
const el = fixture.debugElement.query(By.css('#result'))
.nativeElement as HTMLElement;
expect(el.attributes.getNamedItem('href').textContent).toBe(item.result);
});
});
});

@Component({
template: `<a id="result" [href]="value | url"></a>`,
})
class TestComponent {
value = '';
}
11 changes: 11 additions & 0 deletions packages/theme/src/pipes/safe/url.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PipeTransform, Pipe } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({ name: 'url' })
export class URLPipe implements PipeTransform {
constructor(private dom: DomSanitizer) {}

transform(url: string): string {
return url ? this.dom.bypassSecurityTrustUrl(url) as any : '';
}
}
5 changes: 3 additions & 2 deletions packages/theme/src/theme.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import { CNCurrencyPipe } from './pipes/currency/cn-currency.pipe';
import { KeysPipe } from './pipes/keys/keys.pipe';
import { YNPipe } from './pipes/yn/yn.pipe';
import { I18nPipe } from './services/i18n/i18n.pipe';
import { HTMLPipe } from './pipes/html/html.pipe';
const PIPES = [DatePipe, CNCurrencyPipe, KeysPipe, YNPipe, I18nPipe, HTMLPipe];
import { HTMLPipe } from './pipes/safe/html.pipe';
import { URLPipe } from './pipes/safe/url.pipe';
const PIPES = [DatePipe, CNCurrencyPipe, KeysPipe, YNPipe, I18nPipe, HTMLPipe, URLPipe];

// #endregion

Expand Down

0 comments on commit 62724a0

Please sign in to comment.