Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not Highlighting HTML tags #65

Merged
Merged
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
10 changes: 10 additions & 0 deletions packages/pipes/src/balHighlight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ describe('balHighlight', () => {
`<a href="localhost:4200/mybaloise-api/api/documents/v1/12345678-1234-4321-1234-123456789012-12345678?filename=Vertrag" target="_blank"><span class="bal-highlight">Vertrag</span></a>`,
)
})
test('should return the same text without highlighting when searching <', () => {
expect(balHighlight(`<b style="font-weight: 700;">Vertrag</b>`, '<')).toBe(
`<b style="font-weight: 700;">Vertrag</b>`,
)
})
test('should return the same text without highlighting when searching html props', () => {
expect(balHighlight(`<b style="font-weight: 700;">Vertrag</b>`, 'font-weight')).toBe(
`<b style="font-weight: 700;">Vertrag</b>`,
)
})
})
4 changes: 2 additions & 2 deletions packages/pipes/src/balHighlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
export function balHighlight(value: string, search: string, cssClass = 'bal-highlight'): string {
if (search && value) {
const hrefTag = '(href="[^>]+")|('
const hrefTag = '(<(.*?)>)|('
let pattern = hrefTag.concat(search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')).concat(')')
pattern = pattern
.split(' ')
Expand All @@ -17,7 +17,7 @@ export function balHighlight(value: string, search: string, cssClass = 'bal-high
.join('|')
const regex = new RegExp(pattern, 'gi')
return value.replace(regex, match => {
return match.includes('href') ? match : `<span class="${cssClass}">${match}</span>`
return match.includes('<') && match.includes('>') ? match : `<span class="${cssClass}">${match}</span>`
})
} else {
return value
Expand Down