Skip to content

Commit

Permalink
fix(Regex): escape all characters to prevent exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuntoo committed Apr 18, 2016
1 parent 5c96c2d commit 3103352
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 3 additions & 0 deletions components/select/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function escapeRegexp(queryToEscape:string) {
return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
}
6 changes: 2 additions & 4 deletions components/select/select-pipes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Pipe} from 'angular2/core';
import {escapeRegexp} from './common';

@Pipe({
name: 'hightlight'
Expand All @@ -18,7 +19,7 @@ export class HightlightPipe {
// Replace tags with token
let tmpValue = value.replace( tagRE, '$!$');
// Replace search words
value = tmpValue.replace(new RegExp(this.escapeRegexp(query), 'gi'), '<strong>$&</strong>');
value = tmpValue.replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>');
// Reinsert HTML
for (let i = 0; value.indexOf('$!$') > -1; i++) {
value = value.replace('$!$', tagList[i]);
Expand All @@ -27,9 +28,6 @@ export class HightlightPipe {
return value;
}

private escapeRegexp(queryToEscape:string) {
return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
}
}

export function stripTags(input:string) {
Expand Down
5 changes: 3 additions & 2 deletions components/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
stripTags
} from './select-pipes';
import {IOptionsBehavior} from './select-interfaces';
import {escapeRegexp} from './common';

let optionsTemplate = `
<ul *ngIf="optionsOpened && options && options.length > 0 && !itemObjects[0].hasChildren()"
Expand Down Expand Up @@ -262,7 +263,7 @@ export class Select {
return;
}

if (srcElement.contains(context.element.nativeElement)
if (srcElement.contains(context.element.nativeElement)
&& e.srcElement && e.srcElement.className &&
e.srcElement.className.indexOf('ui-select') >= 0) {
if (e.target.nodeName !== 'INPUT') {
Expand Down Expand Up @@ -388,7 +389,7 @@ export class Select {

if (e.srcElement) {
this.inputValue = e.srcElement.value;
this.behavior.filter(new RegExp(this.inputValue, 'ig'));
this.behavior.filter(new RegExp(escapeRegexp(this.inputValue), 'ig'));
this.doEvent('typed', this.inputValue);
}
}
Expand Down

0 comments on commit 3103352

Please sign in to comment.