(
tag: string,
prop: string,
value: any,
expectedSanitizedValue: any,
bypassFn: Function,
isAttribute: boolean = true,
throws: boolean = false,
)
| 1494 | return value; |
| 1495 | } |
| 1496 | function verify( |
| 1497 | tag: string, |
| 1498 | prop: string, |
| 1499 | value: any, |
| 1500 | expectedSanitizedValue: any, |
| 1501 | bypassFn: Function, |
| 1502 | isAttribute: boolean = true, |
| 1503 | throws: boolean = false, |
| 1504 | ) { |
| 1505 | it(`should sanitize <${tag} ${prop}> ${isAttribute ? 'properties' : 'attributes'} (value=${value})`, () => { |
| 1506 | @Directive({ |
| 1507 | selector: '[unsafeUrlHostBindingDir]', |
| 1508 | host: { |
| 1509 | [`[${isAttribute ? 'attr.' : ''}${prop}]`]: 'value', |
| 1510 | }, |
| 1511 | }) |
| 1512 | class UnsafeDir { |
| 1513 | value: any = value; |
| 1514 | } |
| 1515 | |
| 1516 | @Component({ |
| 1517 | template: `<${tag} unsafeUrlHostBindingDir></${tag}>`, |
| 1518 | |
| 1519 | imports: [UnsafeDir], |
| 1520 | changeDetection: ChangeDetectionStrategy.Eager, |
| 1521 | }) |
| 1522 | class App { |
| 1523 | @ViewChild(UnsafeDir) unsafeDir!: UnsafeDir; |
| 1524 | } |
| 1525 | |
| 1526 | const fixture = TestBed.createComponent(App); |
| 1527 | fixture.detectChanges(); |
| 1528 | const el = fixture.nativeElement.querySelector(tag)!; |
| 1529 | const current = () => (isAttribute ? el.getAttribute(prop) : (el as any)[prop]); |
| 1530 | |
| 1531 | fixture.componentInstance.unsafeDir.value = value; |
| 1532 | fixture.detectChanges(); |
| 1533 | expect(current()).toEqual(expectedSanitizedValue); |
| 1534 | |
| 1535 | fixture.componentInstance.unsafeDir.value = bypassFn(value); |
| 1536 | if (throws) { |
| 1537 | expect(() => fixture.detectChanges()).toThrowError(/Required a safe URL, got a \w+/); |
| 1538 | } else { |
| 1539 | fixture.detectChanges(); |
| 1540 | expect(current()).toEqual(bypassFn == identity ? expectedSanitizedValue : value); |
| 1541 | } |
| 1542 | }); |
| 1543 | } |
| 1544 | |
| 1545 | verify( |
| 1546 | 'a', |
no test coverage detected