Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/spec/typeahead-container.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ describe('Component: TypeaheadContainer', () => {
beforeEach(() => {
component.query = 'fo';
component.matches = [
new TypeaheadMatch({ id: 0, name: 'foo' }, 'foo'),
new TypeaheadMatch({ id: 1, name: 'food' }, 'food')
new TypeaheadMatch({ id: 0, name: '&foo' }, '&foo'),
new TypeaheadMatch({ id: 1, name: '<food>' }, '<food>')
];
fixture.detectChanges();

Expand All @@ -78,6 +78,13 @@ describe('Component: TypeaheadContainer', () => {
expect(matches.length).toBe(2);
});

it('escapes the markup to prevent XSS', () => {
const ms = fixture.debugElement.queryAll(
By.css('.dropdown-menu li span')
);
expect(ms[1].nativeElement.innerHTML).toBe('&lt;<strong>fo</strong>od&gt;');
});

xit('should highlight query for match', () => {
// expect(matches[1].children[0].innerHTML).toBe('<strong>fo</strong>od');
const ms = fixture.debugElement.queryAll(
Expand Down
21 changes: 15 additions & 6 deletions src/typeahead/typeahead-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ export class TypeaheadContainerComponent {
this._active = value;
}

toSafeHTML(input: string): string {
const doc = new DOMParser().parseFromString('', 'text/html');
doc.body.appendChild(doc.createTextNode(input));

return doc.body.innerHTML;
}

highlight(match: TypeaheadMatch, query: string[] | string): string {
let itemStr: string = match.value;
let itemStrHelper: string = (this.parent && this.parent.typeaheadLatinize
Expand All @@ -151,11 +158,12 @@ export class TypeaheadContainerComponent {
tokenLen = query[i].length;
if (startIdx >= 0 && tokenLen > 0) {
itemStr =
`${itemStr.substring(0, startIdx)}<strong>${itemStr.substring(startIdx, startIdx + tokenLen)}</strong>` +
`${itemStr.substring(startIdx + tokenLen)}`;
`${this.toSafeHTML(itemStr.substring(0, startIdx))}` +
`<strong>${this.toSafeHTML(itemStr.substring(startIdx, startIdx + tokenLen))}</strong>` +
`${this.toSafeHTML(itemStr.substring(startIdx + tokenLen))}`;
itemStrHelper =
`${itemStrHelper.substring(0, startIdx)} ${' '.repeat(tokenLen)} ` +
`${itemStrHelper.substring(startIdx + tokenLen)}`;
`${this.toSafeHTML(itemStrHelper.substring(0, startIdx))} ${' '.repeat(tokenLen)} ` +
`${this.toSafeHTML(itemStrHelper.substring(startIdx + tokenLen))}`;
}
}
} else if (query) {
Expand All @@ -164,8 +172,9 @@ export class TypeaheadContainerComponent {
tokenLen = query.length;
if (startIdx >= 0 && tokenLen > 0) {
itemStr =
`${itemStr.substring(0, startIdx)}<strong>${itemStr.substring(startIdx, startIdx + tokenLen)}</strong>` +
`${itemStr.substring(startIdx + tokenLen)}`;
`${this.toSafeHTML(itemStr.substring(0, startIdx))}` +
`<strong>${this.toSafeHTML(itemStr.substring(startIdx, startIdx + tokenLen))}</strong>` +
`${this.toSafeHTML(itemStr.substring(startIdx + tokenLen))}`;
}
}

Expand Down