Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
a.inkin committed Aug 29, 2019
1 parent 8e834f2 commit 3f99451
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 30 deletions.
4 changes: 2 additions & 2 deletions projects/ng-dompurify/src/lib/ng-dompurify.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {inject, Inject, Injectable, Sanitizer, SecurityContext} from '@angular/core';
import {sanitize, addHook, HookName} from 'dompurify';
import {Inject, Injectable, Sanitizer, SecurityContext} from '@angular/core';
import {sanitize, addHook, removeAllHooks} from 'dompurify';
import {SANITIZE_STYLE} from './tokens/sanitize-style';
import {DOMPURIFY_HOOKS} from './tokens/dompurify-hooks';
import {DOMPURIFY_CONFIG} from './tokens/dompurify-config';
Expand Down
19 changes: 15 additions & 4 deletions projects/ng-dompurify/src/lib/test/ng-dompurify-dom.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {NgDompurifyModule} from '../ng-dompurify.module';
import {sanitizeStyle} from './test-samples/sanitizeStyle';
import {SANITIZE_STYLE} from '../tokens/sanitize-style';
import {cleanUrl, dirtyUrl} from './test-samples/url';
import {removeAllHooks} from 'dompurify';

describe('NgDompurifyPipe', () => {
@Component({
Expand All @@ -18,14 +19,15 @@ describe('NgDompurifyPipe', () => {
})
class TestComponent {
content = '';
context = SecurityContext.HTML;
config = {};
context?: SecurityContext = SecurityContext.HTML;
config? = {};

@ViewChild('element', { static: false })
readonly element!: ElementRef<HTMLElement>;

get html(): boolean {
return this.context === SecurityContext.HTML
return this.context === undefined
|| this.context === SecurityContext.HTML
|| this.context === SecurityContext.SCRIPT
|| this.context === SecurityContext.NONE;
}
Expand Down Expand Up @@ -63,7 +65,7 @@ describe('NgDompurifyPipe', () => {
});

afterEach(() => {
TestBed.resetTestingModule();
removeAllHooks();
});

it('sanitizes HTML', () => {
Expand All @@ -73,6 +75,15 @@ describe('NgDompurifyPipe', () => {
expect(testComponent.element.nativeElement.innerHTML).toBe(cleanHtml);
});

it('sanitizes HTML by default', () => {
testComponent.content = dirtyHtml;
testComponent.context = undefined;
testComponent.config = undefined;
fixture.detectChanges();

expect(testComponent.element.nativeElement.innerHTML).toBe(cleanHtml);
});

it('sanitizes HTML with config', () => {
testComponent.content = dirtyHtml;
testComponent.config = {FORBID_TAGS: ['br']};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@ import {TestBed} from '@angular/core/testing';
import {sanitizeStyle} from './test-samples/sanitizeStyle';
import {SANITIZE_STYLE} from '../tokens/sanitize-style';
import {cleanStyle, dirtyStyle} from './test-samples/style';
import {removeAllHooks} from 'dompurify';

describe('NgDompurifyDomSanitizer', () => {
let service: NgDompurifyDomSanitizer;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
NgDompurifyDomSanitizer,
{
provide: SANITIZE_STYLE,
useValue: sanitizeStyle,
},
NgDompurifyDomSanitizer,
],
});

service = TestBed.get(NgDompurifyDomSanitizer);
});

afterEach(() => {
TestBed.resetTestingModule();
removeAllHooks();
});

it('should be created', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {DOMPURIFY_HOOKS} from '../tokens/dompurify-hooks';
import {SANITIZE_STYLE} from '../tokens/sanitize-style';
import {sanitizeStyle} from './test-samples/sanitizeStyle';
import {cleanStyleTag, dirtyStyleTag} from './test-samples/style';
import {removeAllHooks} from 'dompurify';

describe('NgDompurifySanitizer', () => {
const hooks: ReadonlyArray<NgDompurifyHook> = [{
Expand All @@ -23,23 +24,23 @@ describe('NgDompurifySanitizer', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
NgDompurifySanitizer,
{
provide: SANITIZE_STYLE,
useValue: sanitizeStyle,
},
{
provide: DOMPURIFY_HOOKS,
useValue: hooks,
}
},
NgDompurifySanitizer,
],
});

service = TestBed.get(NgDompurifySanitizer);
});

afterEach(() => {
TestBed.resetTestingModule();
removeAllHooks();
});

it('should be created', () => {
Expand Down Expand Up @@ -97,7 +98,7 @@ describe('NgDompurifySanitizer default DI', () => {
});

afterEach(() => {
TestBed.resetTestingModule();
removeAllHooks();
});

it('sanitizes styles into nothing by default', () => {
Expand Down
2 changes: 1 addition & 1 deletion projects/ng-dompurify/src/lib/test/test-samples/style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const dirtyStyle = 'expression(evil)';
export const cleanStyle = '';
export const dirtyStyleTag = '<br><style>.red {color: red; background: expression(evil)}</style>';
export const dirtyStyleTag = '<br><style>@import "malicious.css"; .red {color: red; background: expression(evil)}</style>';
export const cleanStyleTag = '<br><style>.red{\ncolor:red;\n}</style>';
18 changes: 7 additions & 11 deletions projects/ng-dompurify/src/lib/utils/addCSSRules.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import {SanitizeStyle} from '../types/sanitize-style';
import {validateStyles} from './validateStyles';
import {isSelectorRule} from './isSelectorRule';

/**
* Take CSS rules and analyze them, create string wrapper to
* apply them to the DOM later on. Note that only selector rules
* are supported right now
*
* @private
*/
export function addCSSRules(cssRules: CSSRuleList, sanitizeStyle: SanitizeStyle): ReadonlyArray<string> {
const output: string[] = [];

for (let index = cssRules.length - 1; index >= 0; index--) {
const rule = cssRules[index];

// check for rules with selector
if (rule.type === 1 && (rule instanceof CSSStyleRule) && rule.selectorText) {
output.push(rule.selectorText + '{');

if (rule.style) {
output.push(...validateStyles(rule.style, sanitizeStyle));
}

output.push('}');
if (isSelectorRule(rule)) {
output.push(
rule.selectorText + '{',
...validateStyles(rule.style, sanitizeStyle),
'}'
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {validateStyles} from './validateStyles';

/**
* afterSanitizeAttributes factory to sanitize CSS rules from inline styles through custom function
*
* @private
*/
export function createAfterSanitizeAttributes(sanitizeStyle: SanitizeStyle): DompurifyHook {
return node => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {addCSSRules} from './addCSSRules';

/**
* uponSanitizeElementHook factory to sanitize CSS rules from HTMLStyleElement through custom function
*
* @private
*/
export function createUponSanitizeElementHook(sanitizeStyle: SanitizeStyle): DompurifyHook {
return node => {
Expand Down
8 changes: 8 additions & 0 deletions projects/ng-dompurify/src/lib/utils/isSelectorRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Checks CSSRule for basic CSSStyleRule selector rule
*/
export function isSelectorRule(rule: CSSRule): rule is CSSStyleRule {
return rule.type === CSSRule.STYLE_RULE
&& rule instanceof CSSStyleRule
&& !!rule.selectorText;
}
2 changes: 0 additions & 2 deletions projects/ng-dompurify/src/lib/utils/validateStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {SanitizeStyle} from '../types/sanitize-style';
/**
* Take CSS property-value pairs and validate them through provided method,
* then add the styles to an array of property-value pairs
*
* @private
*/
export function validateStyles(styles: CSSStyleDeclaration, sanitizeStyle: SanitizeStyle): ReadonlyArray<string> {
const output: string[] = [];
Expand Down

0 comments on commit 3f99451

Please sign in to comment.